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