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 669 jones
 *
14
 * This program is free software; you can redistribute it and/or modify
15
 * it under the terms of the GNU General Public License as published by
16
 * the Free Software Foundation; either version 2 of the License, or
17
 * (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU General Public License
25
 * along with this program; if not, write to the Free Software
26
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27 22 jones
 */
28
29 51 jones
package edu.ucsb.nceas.metacat;
30 22 jones
31
import java.sql.*;
32
import java.io.IOException;
33 388 jones
import java.util.Enumeration;
34
import java.util.Iterator;
35
import java.util.TreeSet;
36 23 jones
import java.util.Vector;
37 22 jones
38 31 jones
/**
39
 * A Class that represents an XML element and its contents,
40
 * and can build itself from a database connection
41
 */
42 130 jones
public class ElementNode extends BasicNode {
43 22 jones
44 388 jones
  /**
45
   * Construct a new ElementNode instance, and recursively create its children
46
   *
47
   * @param nodeRecordList the nodedata to use to initialize, which is a
48
   *        TreeSet of NodeRecord objects
49
   * @param nodeid the identifier for the node to be created
50
   */
51
  public ElementNode (TreeSet nodeRecordList, long nodeid) {
52 22 jones
53 388 jones
    MetaCatUtil util = new MetaCatUtil();
54 24 jones
55 388 jones
    // Step through all of the node records we were given
56
    Iterator it = nodeRecordList.iterator();
57
    while (it.hasNext()) {
58
      NodeRecord currentNode = (NodeRecord)it.next();
59
      if (currentNode.nodeid == nodeid) {
60
        util.debugMessage("Got Node ID: " + currentNode.nodeid +
61
                          " (" + currentNode.parentnodeid +
62
                          ", " + currentNode.nodeindex +
63
                          ", " + currentNode.nodetype + ")");
64
        // Process the current node
65
        setNodeType(currentNode.nodetype);
66
        setNodeID(currentNode.nodeid);
67
        setParentID(currentNode.parentnodeid);
68
        setTagName(currentNode.nodename);
69
      } else {
70
        // Process the children nodes
71
        if (currentNode.parentnodeid == getNodeID()) {
72
          util.debugMessage("  Processing child: " + currentNode.nodeid +
73
                          " (" + currentNode.parentnodeid +
74
                          ", " + currentNode.nodeindex +
75
                          ", " + currentNode.nodetype + ")");
76 22 jones
77 388 jones
          if ((currentNode.nodetype).equals("ELEMENT")) {
78
            util.debugMessage("Creating child node: " + currentNode.nodeid);
79
            ElementNode child = new ElementNode(nodeRecordList,
80
                                                currentNode.nodeid);
81
            addChildNode(child);
82
          } else if (currentNode.nodetype.equals("ATTRIBUTE")) {
83
            setAttribute(currentNode.nodename,currentNode.nodedata);
84
          } else if (currentNode.nodetype.equals("TEXT")) {
85
            TextNode child = new TextNode(currentNode.nodeid,
86
                                          currentNode.parentnodeid,
87
                                          currentNode.nodedata);
88
            addChildNode(child);
89
          } else if (currentNode.nodetype.equals("COMMENT")) {
90
            CommentNode child = new CommentNode(currentNode.nodeid,
91
                                                currentNode.parentnodeid,
92
                                                currentNode.nodedata);
93
            addChildNode(child);
94
          } else if (currentNode.nodetype.equals("PI")) {
95
            PINode child = new PINode(currentNode.nodeid,
96
                                      currentNode.parentnodeid,
97
                                      currentNode.nodename,
98
                                      currentNode.nodedata);
99
            addChildNode(child);
100
          }
101 22 jones
102 388 jones
        } else {
103
          util.debugMessage("  Discarding child: " + currentNode.nodeid +
104
                          " (" + currentNode.parentnodeid +
105
                          ", " + currentNode.nodeindex +
106
                          ", " + currentNode.nodetype + ")");
107 23 jones
        }
108
      }
109
    }
110 388 jones
  }
111 24 jones
112 388 jones
  /**
113
   * String representation for display purposes (recursively descends through
114
   * children to create an XML subtree)
115
   */
116
  public String toString () {
117 24 jones
118 388 jones
    StringBuffer value = new StringBuffer();
119
    String nodetype = getNodeType();
120 122 jones
121 388 jones
    if (nodetype.equals("ELEMENT")) {
122
      value.append('<');
123
      value.append(getTagName());
124
      value.append(getAttributes().toString());
125
      value.append('>');
126
    }
127 24 jones
128 388 jones
    // Process children recursively here
129
    BasicNode child = null;
130
    Enumeration e = getChildren();
131
    while (e.hasMoreElements()) {
132
      child = (BasicNode)e.nextElement();
133
      value.append(child);
134
    }
135 24 jones
136 388 jones
    if (nodetype.equals("ELEMENT")) {
137
      value.append("</");
138
      value.append(getTagName());
139
      value.append('>');
140 24 jones
    }
141
142 388 jones
    return value.toString();
143
  }
144 22 jones
}