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: tao $'
11
 *     '$Date: 2003-03-19 15:27:58 -0800 (Wed, 19 Mar 2003) $'
12
 * '$Revision: 1497 $'
13
 *
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
 */
28

    
29
package edu.ucsb.nceas.metacat;
30

    
31
import java.sql.*;
32
import java.io.IOException;
33
import java.util.Enumeration;
34
import java.util.Iterator;
35
import java.util.TreeSet;
36
import java.util.Vector;
37

    
38
/**
39
 * A Class that represents an XML element and its contents,
40
 * and can build itself from a database connection
41
 */
42
public class ElementNode extends BasicNode {
43

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

    
53
    MetaCatUtil util = new MetaCatUtil();
54

    
55
    // 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 + ")", 50);
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 + ")", 50);
76

    
77
          if ((currentNode.nodetype).equals("ELEMENT")) {
78
            util.debugMessage("Creating child node: " + currentNode.nodeid, 50);
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

    
102
        } else {
103
          util.debugMessage("  Discarding child: " + currentNode.nodeid +
104
                          " (" + currentNode.parentnodeid +
105
                          ", " + currentNode.nodeindex +
106
                          ", " + currentNode.nodetype + ")", 50);
107
        }
108
      }
109
    }
110
  }
111

    
112
  /** 
113
   * String representation for display purposes (recursively descends through
114
   * children to create an XML subtree)
115
   */
116
  public String toString () {
117

    
118
    StringBuffer value = new StringBuffer();
119
    String nodetype = getNodeType();
120

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

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

    
136
    if (nodetype.equals("ELEMENT")) {
137
      value.append("</");
138
      value.append(getTagName());
139
      value.append('>');
140
    }
141

    
142
    return value.toString();
143
  }
144
}
(33-33/61)