Project

General

Profile

1
/**
2
 *  '$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
 *    Release: @release@
9
 *
10
 *   '$Author: jones $'
11
 *     '$Date: 2000-08-21 14:52:48 -0700 (Mon, 21 Aug 2000) $'
12
 * '$Revision: 388 $'
13
 */
14

    
15
package edu.ucsb.nceas.metacat;
16

    
17
import java.sql.*;
18
import java.io.IOException;
19
import java.util.Enumeration;
20
import java.util.Iterator;
21
import java.util.TreeSet;
22
import java.util.Vector;
23

    
24
/**
25
 * A Class that represents an XML element and its contents,
26
 * and can build itself from a database connection
27
 */
28
public class ElementNode extends BasicNode {
29

    
30
  /** 
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

    
39
    MetaCatUtil util = new MetaCatUtil();
40

    
41
    // 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

    
63
          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

    
88
        } else {
89
          util.debugMessage("  Discarding child: " + currentNode.nodeid +
90
                          " (" + currentNode.parentnodeid +
91
                          ", " + currentNode.nodeindex +
92
                          ", " + currentNode.nodetype + ")");
93
        }
94
      }
95
    }
96
  }
97

    
98
  /** 
99
   * String representation for display purposes (recursively descends through
100
   * children to create an XML subtree)
101
   */
102
  public String toString () {
103

    
104
    StringBuffer value = new StringBuffer();
105
    String nodetype = getNodeType();
106

    
107
    if (nodetype.equals("ELEMENT")) {
108
      value.append('<');
109
      value.append(getTagName());
110
      value.append(getAttributes().toString());
111
      value.append('>');
112
    } 
113

    
114
    // 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

    
122
    if (nodetype.equals("ELEMENT")) {
123
      value.append("</");
124
      value.append(getTagName());
125
      value.append('>');
126
    }
127

    
128
    return value.toString();
129
  }
130
}
(18-18/29)