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
    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 126 jones
      this.childNum = 0;
54 23 jones
    }
55
56 135 jones
    /**
57
     * Construct a Basic Node
58 31 jones
     *
59 134 jones
     * @param tagname the name of the node
60 135 jones
     */
61
    public BasicNode (String tagname) {
62
      this();
63
      this.tagname = tagname;
64
    }
65
66
    /**
67
     * Construct a Basic Node
68
     *
69
     * @param tagname the name of the node
70 134 jones
     * @param parent_id the id number of the parent node
71 135 jones
     * @param nodeIndex - index of node among siblings in parent node
72 134 jones
     *                    Every node initializes childNum to 0 when
73 122 jones
     *                    created and has interface incChildNum
74
     *                    when new child is created
75 31 jones
     */
76 129 jones
    public BasicNode (String tagname, long parent_id, int nodeIndex) {
77 23 jones
      this();
78 21 jones
      this.tagname = tagname;
79
      this.parent_id = parent_id;
80 72 bojilova
      this.nodeIndex = nodeIndex;
81 21 jones
    }
82
83 135 jones
    /** Construct a Basic Node
84 31 jones
     *
85 133 jones
     * @param node_id the id number of the node
86
     * @param tagname the name of the node
87
     * @param parent_id the id number of the parent node
88 31 jones
     */
89 133 jones
    public BasicNode (long node_id, String tagname, long parent_id,
90 122 jones
                         int nodeIndex) {
91 72 bojilova
      this(tagname,parent_id,nodeIndex);
92 133 jones
      this.node_id = node_id;
93 21 jones
    }
94
95 134 jones
    /** convert the node to a string representation for display */
96 129 jones
/*  MAKE THIS AN ABSTRACT METHOD??????
97 21 jones
    public String toString ()
98
    {
99 24 jones
	StringBuffer value = new StringBuffer();
100
	value.append('<');
101
	value.append(getTagName());
102
	value.append(getAttributes().toString());
103
	value.append('>');
104
	return value.toString();
105 21 jones
    }
106 126 jones
*/
107 21 jones
108 134 jones
    /** Get the id of this node */
109 133 jones
    public long getNodeID()
110 23 jones
    {
111 133 jones
      return node_id;
112 23 jones
    }
113 21 jones
114 134 jones
    /** Set the id of this node */
115 133 jones
    public void setNodeID(long node_id)
116 23 jones
    {
117 133 jones
      this.node_id = node_id;
118 23 jones
    }
119
120 134 jones
    /** Get the parent id of this node */
121 23 jones
    public long getParentID()
122
    {
123
      return parent_id;
124
    }
125
126 134 jones
    /** Set the parent id of this node */
127 23 jones
    public void setParentID(long parent_id)
128
    {
129
      this.parent_id = parent_id;
130
    }
131
132 149 bojilova
    /** Get the root node id of this node */
133
    public long getRootNodeID()
134
    {
135
      return rootnode_id;
136
    }
137
138
    /** Set the root node id of this node */
139
    public void setRootNodeID(long rootnode_id)
140
    {
141
      this.rootnode_id = rootnode_id;
142
    }
143
144
    /** Get the doc id of this node */
145 162 bojilova
    public String getDocID()
146 149 bojilova
    {
147
      return doc_id;
148
    }
149
150
    /** Set the doc id of this node */
151 162 bojilova
    public void setDocID(String doc_id)
152 149 bojilova
    {
153
      this.doc_id = doc_id;
154
    }
155
156 134 jones
    /** Get the name of this node */
157 23 jones
    public String getTagName()
158
    {
159
      return tagname;
160
    }
161 21 jones
162 134 jones
    /** Set the name of this node */
163 23 jones
    public void setTagName(String tagname)
164
    {
165
      this.tagname = tagname;
166
    }
167
168 21 jones
    /** Get the attributes as a string */
169
    public String getAttributes() {
170
      StringBuffer buf = new StringBuffer();
171
      String attName = null;
172
      String attValue = null;
173
174
      Enumeration attList = attributes.keys();
175
      while (attList.hasMoreElements()) {
176
        attName = (String)attList.nextElement();
177
        attValue = (String)attributes.get(attName);
178 122 jones
        buf.append(" ").append(attName).append("=\"");
179
        buf.append(attValue).append("\"");
180 21 jones
      }
181
      return buf.toString();
182
    }
183
184 134 jones
    /** Add a new attribute to this node, or set its value */
185 21 jones
    public void setAttribute(String attName, String attValue) {
186
      if (attName != null) {
187
        // Enter the attribute in the hash table
188
        attributes.put(attName, attValue);
189
190
      } else {
191
        System.err.println("Attribute name must not be null!");
192
      }
193
    }
194
195
    /** Get an attribute value by name */
196
    public String getAttribute(String attName) {
197
      return (String)attributes.get(attName);
198
    }
199
200 134 jones
    /** Get nodeIndex of the node */
201 72 bojilova
    public int getNodeIndex() {
202
      return this.nodeIndex;
203
    }
204
205 126 jones
    /** Set the node index of this node */
206
    public void setNodeIndex(int nodeIndex) {
207
      this.nodeIndex = nodeIndex;
208
    }
209 122 jones
210
    /** Get the type of this node */
211 126 jones
    public String getNodeType() {
212 122 jones
      return nodeType;
213
    }
214
215
    /** Set the type of this node */
216 126 jones
    public void setNodeType(String type) {
217 122 jones
      this.nodeType = type;
218
    }
219 126 jones
220 127 jones
    /** Add a child node to this node */
221 129 jones
    public void addChildNode(BasicNode child) {
222 127 jones
      this.children.add(child);
223
    }
224
225
    /** Get the an enumeration of the children of this node */
226
    public Enumeration getChildren() {
227
      return children.elements();
228
    }
229
230 134 jones
    /** increase childNum when new child for the node is created */
231 126 jones
    public int incChildNum() {
232
      return ++this.childNum;
233
    }
234
235 21 jones
}