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
    MetaCatUtil util = new MetaCatUtil();
57 24 jones
58 388 jones
    // Step through all of the node records we were given
59
    Iterator it = nodeRecordList.iterator();
60
    while (it.hasNext()) {
61
      NodeRecord currentNode = (NodeRecord)it.next();
62 2763 sgarg
      if (currentNode.getNodeId() == nodeid) {
63
        logMetacat.info("Got Node ID: " + currentNode.getNodeId() +
64
                          " (" + currentNode.getParentNodeId() +
65
                          ", " + currentNode.getNodeIndex() +
66
                          ", " + currentNode.getNodeType() + ")");
67 388 jones
        // Process the current node
68 2763 sgarg
        setNodeType(currentNode.getNodeType());
69
        setNodeID(currentNode.getNodeId());
70
        setParentID(currentNode.getParentNodeId());
71
        setTagName(currentNode.getNodeName());
72 388 jones
      } else {
73
        // Process the children nodes
74 2763 sgarg
        if (currentNode.getParentNodeId() == getNodeID()) {
75
        	logMetacat.info("  Processing child: " + currentNode.getNodeId() +
76
                          " (" + currentNode.getParentNodeId() +
77
                          ", " + currentNode.getNodeIndex() +
78
                          ", " + currentNode.getNodeType() + ")");
79 22 jones
80 2763 sgarg
          if ((currentNode.getNodeType()).equals("ELEMENT")) {
81
        	logMetacat.info("Creating child node: " + currentNode.getNodeId());
82 388 jones
            ElementNode child = new ElementNode(nodeRecordList,
83 2763 sgarg
                                                currentNode.getNodeId());
84 388 jones
            addChildNode(child);
85 2763 sgarg
          } else if (currentNode.getNodeType().equals("ATTRIBUTE")) {
86
            setAttribute(currentNode.getNodeName(),currentNode.getNodeData());
87
          } else if (currentNode.getNodeType().equals("TEXT")) {
88
            TextNode child = new TextNode(currentNode.getNodeId(),
89
                                          currentNode.getParentNodeId(),
90
                                          currentNode.getNodeData());
91 388 jones
            addChildNode(child);
92 2763 sgarg
          } else if (currentNode.getNodeType().equals("COMMENT")) {
93
            CommentNode child = new CommentNode(currentNode.getNodeId(),
94
                                                currentNode.getParentNodeId(),
95
                                                currentNode.getNodeData());
96 388 jones
            addChildNode(child);
97 2763 sgarg
          } else if (currentNode.getNodeType().equals("PI")) {
98
            PINode child = new PINode(currentNode.getNodeId(),
99
                                      currentNode.getParentNodeId(),
100
                                      currentNode.getNodeName(),
101
                                      currentNode.getNodeData());
102 388 jones
            addChildNode(child);
103
          }
104 22 jones
105 388 jones
        } else {
106 2763 sgarg
        	logMetacat.info("  Discarding child: " + currentNode.getNodeId() +
107
                          " (" + currentNode.getParentNodeId() +
108
                          ", " + currentNode.getNodeIndex() +
109
                          ", " + currentNode.getNodeType() + ")");
110 23 jones
        }
111
      }
112
    }
113 388 jones
  }
114 24 jones
115 388 jones
  /**
116
   * String representation for display purposes (recursively descends through
117
   * children to create an XML subtree)
118
   */
119
  public String toString () {
120 24 jones
121 388 jones
    StringBuffer value = new StringBuffer();
122
    String nodetype = getNodeType();
123 122 jones
124 388 jones
    if (nodetype.equals("ELEMENT")) {
125
      value.append('<');
126
      value.append(getTagName());
127
      value.append(getAttributes().toString());
128
      value.append('>');
129
    }
130 24 jones
131 388 jones
    // Process children recursively here
132
    BasicNode child = null;
133
    Enumeration e = getChildren();
134
    while (e.hasMoreElements()) {
135
      child = (BasicNode)e.nextElement();
136
      value.append(child);
137
    }
138 24 jones
139 388 jones
    if (nodetype.equals("ELEMENT")) {
140
      value.append("</");
141
      value.append(getTagName());
142
      value.append('>');
143 24 jones
    }
144
145 388 jones
    return value.toString();
146
  }
147 22 jones
}