Project

General

Profile

1 22 jones
/**
2 203 jones
 *  '$RCSfile$'
3
 *    Purpose: A Class that represents an XML element and its contents,
4
 *             and can build itself from a database connection
5
 *  Copyright: 2000 Regents of the University of California and the
6
 *             National Center for Ecological Analysis and Synthesis
7
 *    Authors: Matt Jones
8 349 jones
 *    Release: @release@
9 22 jones
 *
10 203 jones
 *   '$Author$'
11
 *     '$Date$'
12
 * '$Revision$'
13 22 jones
 */
14
15 51 jones
package edu.ucsb.nceas.metacat;
16 22 jones
17
import java.sql.*;
18
import java.io.IOException;
19 388 jones
import java.util.Enumeration;
20
import java.util.Iterator;
21
import java.util.TreeSet;
22 23 jones
import java.util.Vector;
23 22 jones
24 31 jones
/**
25
 * A Class that represents an XML element and its contents,
26
 * and can build itself from a database connection
27
 */
28 130 jones
public class ElementNode extends BasicNode {
29 22 jones
30 388 jones
  /**
31
   * Construct a new ElementNode instance, and recursively create its children
32
   *
33
   * @param nodeRecordList the nodedata to use to initialize, which is a
34
   *        TreeSet of NodeRecord objects
35
   * @param nodeid the identifier for the node to be created
36
   */
37
  public ElementNode (TreeSet nodeRecordList, long nodeid) {
38 22 jones
39 388 jones
    MetaCatUtil util = new MetaCatUtil();
40 24 jones
41 388 jones
    // Step through all of the node records we were given
42
    Iterator it = nodeRecordList.iterator();
43
    while (it.hasNext()) {
44
      NodeRecord currentNode = (NodeRecord)it.next();
45
      if (currentNode.nodeid == nodeid) {
46
        util.debugMessage("Got Node ID: " + currentNode.nodeid +
47
                          " (" + currentNode.parentnodeid +
48
                          ", " + currentNode.nodeindex +
49
                          ", " + currentNode.nodetype + ")");
50
        // Process the current node
51
        setNodeType(currentNode.nodetype);
52
        setNodeID(currentNode.nodeid);
53
        setParentID(currentNode.parentnodeid);
54
        setTagName(currentNode.nodename);
55
      } else {
56
        // Process the children nodes
57
        if (currentNode.parentnodeid == getNodeID()) {
58
          util.debugMessage("  Processing child: " + currentNode.nodeid +
59
                          " (" + currentNode.parentnodeid +
60
                          ", " + currentNode.nodeindex +
61
                          ", " + currentNode.nodetype + ")");
62 22 jones
63 388 jones
          if ((currentNode.nodetype).equals("ELEMENT")) {
64
            util.debugMessage("Creating child node: " + currentNode.nodeid);
65
            ElementNode child = new ElementNode(nodeRecordList,
66
                                                currentNode.nodeid);
67
            addChildNode(child);
68
          } else if (currentNode.nodetype.equals("ATTRIBUTE")) {
69
            setAttribute(currentNode.nodename,currentNode.nodedata);
70
          } else if (currentNode.nodetype.equals("TEXT")) {
71
            TextNode child = new TextNode(currentNode.nodeid,
72
                                          currentNode.parentnodeid,
73
                                          currentNode.nodedata);
74
            addChildNode(child);
75
          } else if (currentNode.nodetype.equals("COMMENT")) {
76
            CommentNode child = new CommentNode(currentNode.nodeid,
77
                                                currentNode.parentnodeid,
78
                                                currentNode.nodedata);
79
            addChildNode(child);
80
          } else if (currentNode.nodetype.equals("PI")) {
81
            PINode child = new PINode(currentNode.nodeid,
82
                                      currentNode.parentnodeid,
83
                                      currentNode.nodename,
84
                                      currentNode.nodedata);
85
            addChildNode(child);
86
          }
87 22 jones
88 388 jones
        } else {
89
          util.debugMessage("  Discarding child: " + currentNode.nodeid +
90
                          " (" + currentNode.parentnodeid +
91
                          ", " + currentNode.nodeindex +
92
                          ", " + currentNode.nodetype + ")");
93 23 jones
        }
94
      }
95
    }
96 388 jones
  }
97 24 jones
98 388 jones
  /**
99
   * String representation for display purposes (recursively descends through
100
   * children to create an XML subtree)
101
   */
102
  public String toString () {
103 24 jones
104 388 jones
    StringBuffer value = new StringBuffer();
105
    String nodetype = getNodeType();
106 122 jones
107 388 jones
    if (nodetype.equals("ELEMENT")) {
108
      value.append('<');
109
      value.append(getTagName());
110
      value.append(getAttributes().toString());
111
      value.append('>');
112
    }
113 24 jones
114 388 jones
    // Process children recursively here
115
    BasicNode child = null;
116
    Enumeration e = getChildren();
117
    while (e.hasMoreElements()) {
118
      child = (BasicNode)e.nextElement();
119
      value.append(child);
120
    }
121 24 jones
122 388 jones
    if (nodetype.equals("ELEMENT")) {
123
      value.append("</");
124
      value.append(getTagName());
125
      value.append('>');
126 24 jones
    }
127
128 388 jones
    return value.toString();
129
  }
130 22 jones
}