Project

General

Profile

1 21 jones
/**
2 203 jones
 *  '$RCSfile$'
3
 *    Purpose: A Class that represents an XML node and its contents
4
 *  Copyright: 2000 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Matt Jones
7 349 jones
 *    Release: @release@
8 21 jones
 *
9 203 jones
 *   '$Author$'
10
 *     '$Date$'
11
 * '$Revision$'
12 669 jones
 *
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 21 jones
 */
27
28 74 jones
package edu.ucsb.nceas.metacat;
29 21 jones
30
import java.io.IOException;
31
import java.util.Hashtable;
32
import java.util.Enumeration;
33 126 jones
import java.util.Vector;
34 21 jones
35 134 jones
/** A Class that represents an XML node and its contents */
36 129 jones
public class BasicNode {
37 21 jones
38 133 jones
    private long	node_id;
39
    private String	tagname;
40
    private long	parent_id;
41 149 bojilova
    private long    rootnode_id;
42 162 bojilova
    private String  doc_id;
43 72 bojilova
    private Hashtable	attributes;
44 821 bojilova
    private Hashtable	namespace;
45 72 bojilova
    private int         childNum;
46
    private int         nodeIndex;
47 122 jones
    private String      nodeType;
48 162 bojilova
    private Vector  	children;
49 21 jones
50 135 jones
    /** Construct a Basic Node */
51 129 jones
    public BasicNode () {
52 126 jones
      children = new Vector();
53 23 jones
      attributes = new Hashtable();
54 821 bojilova
      namespace = new Hashtable();
55 126 jones
      this.childNum = 0;
56 23 jones
    }
57
58 135 jones
    /**
59
     * Construct a Basic Node
60 31 jones
     *
61 134 jones
     * @param tagname the name of the node
62 135 jones
     */
63
    public BasicNode (String tagname) {
64
      this();
65
      this.tagname = tagname;
66
    }
67
68
    /**
69
     * Construct a Basic Node
70
     *
71
     * @param tagname the name of the node
72 134 jones
     * @param parent_id the id number of the parent node
73 135 jones
     * @param nodeIndex - index of node among siblings in parent node
74 134 jones
     *                    Every node initializes childNum to 0 when
75 122 jones
     *                    created and has interface incChildNum
76
     *                    when new child is created
77 31 jones
     */
78 129 jones
    public BasicNode (String tagname, long parent_id, int nodeIndex) {
79 23 jones
      this();
80 21 jones
      this.tagname = tagname;
81
      this.parent_id = parent_id;
82 72 bojilova
      this.nodeIndex = nodeIndex;
83 21 jones
    }
84
85 135 jones
    /** Construct a Basic Node
86 31 jones
     *
87 133 jones
     * @param node_id the id number of the node
88
     * @param tagname the name of the node
89
     * @param parent_id the id number of the parent node
90 31 jones
     */
91 133 jones
    public BasicNode (long node_id, String tagname, long parent_id,
92 122 jones
                         int nodeIndex) {
93 72 bojilova
      this(tagname,parent_id,nodeIndex);
94 133 jones
      this.node_id = node_id;
95 21 jones
    }
96
97 134 jones
    /** convert the node to a string representation for display */
98 129 jones
/*  MAKE THIS AN ABSTRACT METHOD??????
99 21 jones
    public String toString ()
100
    {
101 24 jones
	StringBuffer value = new StringBuffer();
102
	value.append('<');
103
	value.append(getTagName());
104
	value.append(getAttributes().toString());
105
	value.append('>');
106
	return value.toString();
107 21 jones
    }
108 126 jones
*/
109 21 jones
110 134 jones
    /** Get the id of this node */
111 133 jones
    public long getNodeID()
112 23 jones
    {
113 133 jones
      return node_id;
114 23 jones
    }
115 21 jones
116 134 jones
    /** Set the id of this node */
117 133 jones
    public void setNodeID(long node_id)
118 23 jones
    {
119 133 jones
      this.node_id = node_id;
120 23 jones
    }
121
122 134 jones
    /** Get the parent id of this node */
123 23 jones
    public long getParentID()
124
    {
125
      return parent_id;
126
    }
127
128 134 jones
    /** Set the parent id of this node */
129 23 jones
    public void setParentID(long parent_id)
130
    {
131
      this.parent_id = parent_id;
132
    }
133
134 149 bojilova
    /** Get the root node id of this node */
135
    public long getRootNodeID()
136
    {
137
      return rootnode_id;
138
    }
139
140
    /** Set the root node id of this node */
141
    public void setRootNodeID(long rootnode_id)
142
    {
143
      this.rootnode_id = rootnode_id;
144
    }
145
146
    /** Get the doc id of this node */
147 162 bojilova
    public String getDocID()
148 149 bojilova
    {
149
      return doc_id;
150
    }
151
152
    /** Set the doc id of this node */
153 162 bojilova
    public void setDocID(String doc_id)
154 149 bojilova
    {
155
      this.doc_id = doc_id;
156
    }
157
158 134 jones
    /** Get the name of this node */
159 23 jones
    public String getTagName()
160
    {
161
      return tagname;
162
    }
163 21 jones
164 134 jones
    /** Set the name of this node */
165 23 jones
    public void setTagName(String tagname)
166
    {
167
      this.tagname = tagname;
168
    }
169
170 21 jones
    /** Get the attributes as a string */
171
    public String getAttributes() {
172
      StringBuffer buf = new StringBuffer();
173
      String attName = null;
174
      String attValue = null;
175
176
      Enumeration attList = attributes.keys();
177
      while (attList.hasMoreElements()) {
178
        attName = (String)attList.nextElement();
179
        attValue = (String)attributes.get(attName);
180 122 jones
        buf.append(" ").append(attName).append("=\"");
181
        buf.append(attValue).append("\"");
182 21 jones
      }
183
      return buf.toString();
184
    }
185
186 134 jones
    /** Add a new attribute to this node, or set its value */
187 21 jones
    public void setAttribute(String attName, String attValue) {
188
      if (attName != null) {
189
        // Enter the attribute in the hash table
190
        attributes.put(attName, attValue);
191
192
      } else {
193
        System.err.println("Attribute name must not be null!");
194
      }
195
    }
196
197
    /** Get an attribute value by name */
198
    public String getAttribute(String attName) {
199
      return (String)attributes.get(attName);
200
    }
201
202 821 bojilova
    /** Add a namespace to this node */
203
    public void setNamespace(String prefix, String uri) {
204
      if (prefix != null) {
205
        // Enter the namespace in the hash table
206
        namespace.put(prefix, uri);
207
      } else {
208
        System.err.println("Namespace prefix must not be null!");
209
      }
210
    }
211
212
    /** Get an uri of the namespace prefix */
213
    public String getNamespace(String prefix) {
214
      return (String)namespace.get(prefix);
215
    }
216
217 134 jones
    /** Get nodeIndex of the node */
218 72 bojilova
    public int getNodeIndex() {
219
      return this.nodeIndex;
220
    }
221
222 126 jones
    /** Set the node index of this node */
223
    public void setNodeIndex(int nodeIndex) {
224
      this.nodeIndex = nodeIndex;
225
    }
226 122 jones
227
    /** Get the type of this node */
228 126 jones
    public String getNodeType() {
229 122 jones
      return nodeType;
230
    }
231
232
    /** Set the type of this node */
233 126 jones
    public void setNodeType(String type) {
234 122 jones
      this.nodeType = type;
235
    }
236 126 jones
237 127 jones
    /** Add a child node to this node */
238 129 jones
    public void addChildNode(BasicNode child) {
239 127 jones
      this.children.add(child);
240
    }
241
242
    /** Get the an enumeration of the children of this node */
243
    public Enumeration getChildren() {
244
      return children.elements();
245
    }
246
247 134 jones
    /** increase childNum when new child for the node is created */
248 126 jones
    public int incChildNum() {
249
      return ++this.childNum;
250
    }
251
252 21 jones
}