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: sgarg $'
11
 *     '$Date: 2005-10-10 11:06:55 -0700 (Mon, 10 Oct 2005) $'
12
 * '$Revision: 2663 $'
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
import org.apache.log4j.Logger;
39

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

    
46
  private static Logger logMetacat = Logger.getLogger(ElementNode.class);
47

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

    
57
    MetaCatUtil util = new MetaCatUtil();
58

    
59
    // 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
        logMetacat.info("Got Node ID: " + currentNode.nodeid +
65
                          " (" + currentNode.parentnodeid +
66
                          ", " + currentNode.nodeindex + 
67
                          ", " + currentNode.nodetype + ")");
68
        // 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
        	logMetacat.info("  Processing child: " + currentNode.nodeid +
77
                          " (" + currentNode.parentnodeid +
78
                          ", " + currentNode.nodeindex + 
79
                          ", " + currentNode.nodetype + ")");
80

    
81
          if ((currentNode.nodetype).equals("ELEMENT")) {
82
        	logMetacat.info("Creating child node: " + currentNode.nodeid);
83
            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

    
106
        } else {
107
        	logMetacat.info("  Discarding child: " + currentNode.nodeid +
108
                          " (" + currentNode.parentnodeid +
109
                          ", " + currentNode.nodeindex +
110
                          ", " + currentNode.nodetype + ")");
111
        }
112
      }
113
    }
114
  }
115

    
116
  /** 
117
   * String representation for display purposes (recursively descends through
118
   * children to create an XML subtree)
119
   */
120
  public String toString () {
121

    
122
    StringBuffer value = new StringBuffer();
123
    String nodetype = getNodeType();
124

    
125
    if (nodetype.equals("ELEMENT")) {
126
      value.append('<');
127
      value.append(getTagName());
128
      value.append(getAttributes().toString());
129
      value.append('>');
130
    } 
131

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

    
140
    if (nodetype.equals("ELEMENT")) {
141
      value.append("</");
142
      value.append(getTagName());
143
      value.append('>');
144
    }
145

    
146
    return value.toString();
147
  }
148
}
(33-33/63)