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