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
 *
9
 *   '$Author: daigle $'
10
 *     '$Date: 2008-07-06 21:25:34 -0700 (Sun, 06 Jul 2008) $'
11
 * '$Revision: 4080 $'
12
 *
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
 */
27

    
28
package edu.ucsb.nceas.metacat;
29

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

    
37
import org.apache.log4j.Logger;
38

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

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

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

    
56
    // Step through all of the node records we were given
57
    Iterator it = nodeRecordList.iterator();
58
    while (it.hasNext()) {
59
      NodeRecord currentNode = (NodeRecord)it.next();
60
      if (currentNode.getNodeId() == nodeid) {
61
        logMetacat.info("Got Node ID: " + currentNode.getNodeId() +
62
                          " (" + currentNode.getParentNodeId() +
63
                          ", " + currentNode.getNodeIndex() + 
64
                          ", " + currentNode.getNodeType() + ")");
65
        // Process the current node
66
        setNodeType(currentNode.getNodeType());
67
        setNodeID(currentNode.getNodeId());
68
        setParentID(currentNode.getParentNodeId());
69
        setTagName(currentNode.getNodeName());
70
      } else {
71
        // Process the children nodes
72
        if (currentNode.getParentNodeId() == getNodeID()) {
73
        	logMetacat.info("  Processing child: " + currentNode.getNodeId() +
74
                          " (" + currentNode.getParentNodeId() +
75
                          ", " + currentNode.getNodeIndex() + 
76
                          ", " + currentNode.getNodeType() + ")");
77

    
78
          if ((currentNode.getNodeType()).equals("ELEMENT")) {
79
        	logMetacat.info("Creating child node: " + currentNode.getNodeId());
80
            ElementNode child = new ElementNode(nodeRecordList,
81
                                                currentNode.getNodeId());
82
            addChildNode(child);
83
          } else if (currentNode.getNodeType().equals("ATTRIBUTE")) {
84
            setAttribute(currentNode.getNodeName(),currentNode.getNodeData());
85
          } else if (currentNode.getNodeType().equals("TEXT")) {
86
            TextNode child = new TextNode(currentNode.getNodeId(),
87
                                          currentNode.getParentNodeId(),
88
                                          currentNode.getNodeData());
89
            addChildNode(child);
90
          } else if (currentNode.getNodeType().equals("COMMENT")) {
91
            CommentNode child = new CommentNode(currentNode.getNodeId(),
92
                                                currentNode.getParentNodeId(),
93
                                                currentNode.getNodeData());
94
            addChildNode(child);
95
          } else if (currentNode.getNodeType().equals("PI")) {
96
            PINode child = new PINode(currentNode.getNodeId(),
97
                                      currentNode.getParentNodeId(),
98
                                      currentNode.getNodeName(),
99
                                      currentNode.getNodeData());
100
            addChildNode(child);
101
          }
102

    
103
        } else {
104
        	logMetacat.info("  Discarding child: " + currentNode.getNodeId() +
105
                          " (" + currentNode.getParentNodeId() +
106
                          ", " + currentNode.getNodeIndex() +
107
                          ", " + currentNode.getNodeType() + ")");
108
        }
109
      }
110
    }
111
  }
112

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

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

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

    
129
    // Process children recursively here
130
    BasicNode child = null;
131
    Enumeration e = getChildren();
132
    while (e.hasMoreElements()) {
133
      child = (BasicNode)e.nextElement(); 
134
      value.append(child);
135
    }
136

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

    
143
    return value.toString();
144
  }
145
}
(30-30/63)