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 2663 sgarg
import org.apache.log4j.Logger;
39
40 31 jones
/**
41
 * A Class that represents an XML element and its contents,
42
 * and can build itself from a database connection
43
 */
44 130 jones
public class ElementNode extends BasicNode {
45 22 jones
46 2663 sgarg
  private static Logger logMetacat = Logger.getLogger(ElementNode.class);
47
48 388 jones
  /**
49
   * Construct a new ElementNode instance, and recursively create its children
50
   *
51
   * @param nodeRecordList the nodedata to use to initialize, which is a
52
   *        TreeSet of NodeRecord objects
53
   * @param nodeid the identifier for the node to be created
54
   */
55
  public ElementNode (TreeSet nodeRecordList, long nodeid) {
56 22 jones
57 388 jones
    MetaCatUtil util = new MetaCatUtil();
58 24 jones
59 388 jones
    // Step through all of the node records we were given
60
    Iterator it = nodeRecordList.iterator();
61
    while (it.hasNext()) {
62
      NodeRecord currentNode = (NodeRecord)it.next();
63
      if (currentNode.nodeid == nodeid) {
64 2663 sgarg
        logMetacat.info("Got Node ID: " + currentNode.nodeid +
65 388 jones
                          " (" + currentNode.parentnodeid +
66
                          ", " + currentNode.nodeindex +
67 2663 sgarg
                          ", " + currentNode.nodetype + ")");
68 388 jones
        // Process the current node
69
        setNodeType(currentNode.nodetype);
70
        setNodeID(currentNode.nodeid);
71
        setParentID(currentNode.parentnodeid);
72
        setTagName(currentNode.nodename);
73
      } else {
74
        // Process the children nodes
75
        if (currentNode.parentnodeid == getNodeID()) {
76 2663 sgarg
        	logMetacat.info("  Processing child: " + currentNode.nodeid +
77 388 jones
                          " (" + currentNode.parentnodeid +
78
                          ", " + currentNode.nodeindex +
79 2663 sgarg
                          ", " + currentNode.nodetype + ")");
80 22 jones
81 388 jones
          if ((currentNode.nodetype).equals("ELEMENT")) {
82 2663 sgarg
        	logMetacat.info("Creating child node: " + currentNode.nodeid);
83 388 jones
            ElementNode child = new ElementNode(nodeRecordList,
84
                                                currentNode.nodeid);
85
            addChildNode(child);
86
          } else if (currentNode.nodetype.equals("ATTRIBUTE")) {
87
            setAttribute(currentNode.nodename,currentNode.nodedata);
88
          } else if (currentNode.nodetype.equals("TEXT")) {
89
            TextNode child = new TextNode(currentNode.nodeid,
90
                                          currentNode.parentnodeid,
91
                                          currentNode.nodedata);
92
            addChildNode(child);
93
          } else if (currentNode.nodetype.equals("COMMENT")) {
94
            CommentNode child = new CommentNode(currentNode.nodeid,
95
                                                currentNode.parentnodeid,
96
                                                currentNode.nodedata);
97
            addChildNode(child);
98
          } else if (currentNode.nodetype.equals("PI")) {
99
            PINode child = new PINode(currentNode.nodeid,
100
                                      currentNode.parentnodeid,
101
                                      currentNode.nodename,
102
                                      currentNode.nodedata);
103
            addChildNode(child);
104
          }
105 22 jones
106 388 jones
        } else {
107 2663 sgarg
        	logMetacat.info("  Discarding child: " + currentNode.nodeid +
108 388 jones
                          " (" + currentNode.parentnodeid +
109
                          ", " + currentNode.nodeindex +
110 2663 sgarg
                          ", " + currentNode.nodetype + ")");
111 23 jones
        }
112
      }
113
    }
114 388 jones
  }
115 24 jones
116 388 jones
  /**
117
   * String representation for display purposes (recursively descends through
118
   * children to create an XML subtree)
119
   */
120
  public String toString () {
121 24 jones
122 388 jones
    StringBuffer value = new StringBuffer();
123
    String nodetype = getNodeType();
124 122 jones
125 388 jones
    if (nodetype.equals("ELEMENT")) {
126
      value.append('<');
127
      value.append(getTagName());
128
      value.append(getAttributes().toString());
129
      value.append('>');
130
    }
131 24 jones
132 388 jones
    // Process children recursively here
133
    BasicNode child = null;
134
    Enumeration e = getChildren();
135
    while (e.hasMoreElements()) {
136
      child = (BasicNode)e.nextElement();
137
      value.append(child);
138
    }
139 24 jones
140 388 jones
    if (nodetype.equals("ELEMENT")) {
141
      value.append("</");
142
      value.append(getTagName());
143
      value.append('>');
144 24 jones
    }
145
146 388 jones
    return value.toString();
147
  }
148 22 jones
}