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: jones $'
10
 *     '$Date: 2006-11-10 10:25:38 -0800 (Fri, 10 Nov 2006) $'
11
 * '$Revision: 3077 $'
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
    MetaCatUtil util = new MetaCatUtil();
57

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

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

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

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

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

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

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

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

    
145
    return value.toString();
146
  }
147
}
(33-33/66)