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