Project

General

Profile

1 21 jones
/**
2 168 jones
 *      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 21 jones
 *
8 168 jones
 *   Version: '$Id$'
9 21 jones
 */
10
11 74 jones
package edu.ucsb.nceas.metacat;
12 21 jones
13
import java.io.IOException;
14
import java.util.Hashtable;
15
import java.util.Enumeration;
16 126 jones
import java.util.Vector;
17 21 jones
18 134 jones
/** A Class that represents an XML node and its contents */
19 129 jones
public class BasicNode {
20 21 jones
21 133 jones
    private long	node_id;
22
    private String	tagname;
23
    private long	parent_id;
24 149 bojilova
    private long    rootnode_id;
25 162 bojilova
    private String  doc_id;
26 72 bojilova
    private Hashtable	attributes;
27
    private int         childNum;
28
    private int         nodeIndex;
29 122 jones
    private String      nodeType;
30 162 bojilova
    private Vector  	children;
31 21 jones
32 135 jones
    /** Construct a Basic Node */
33 129 jones
    public BasicNode () {
34 126 jones
      children = new Vector();
35 23 jones
      attributes = new Hashtable();
36 126 jones
      this.childNum = 0;
37 23 jones
    }
38
39 135 jones
    /**
40
     * Construct a Basic Node
41 31 jones
     *
42 134 jones
     * @param tagname the name of the node
43 135 jones
     */
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 134 jones
     * @param parent_id the id number of the parent node
54 135 jones
     * @param nodeIndex - index of node among siblings in parent node
55 134 jones
     *                    Every node initializes childNum to 0 when
56 122 jones
     *                    created and has interface incChildNum
57
     *                    when new child is created
58 31 jones
     */
59 129 jones
    public BasicNode (String tagname, long parent_id, int nodeIndex) {
60 23 jones
      this();
61 21 jones
      this.tagname = tagname;
62
      this.parent_id = parent_id;
63 72 bojilova
      this.nodeIndex = nodeIndex;
64 21 jones
    }
65
66 135 jones
    /** Construct a Basic Node
67 31 jones
     *
68 133 jones
     * @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 31 jones
     */
72 133 jones
    public BasicNode (long node_id, String tagname, long parent_id,
73 122 jones
                         int nodeIndex) {
74 72 bojilova
      this(tagname,parent_id,nodeIndex);
75 133 jones
      this.node_id = node_id;
76 21 jones
    }
77
78 134 jones
    /** convert the node to a string representation for display */
79 129 jones
/*  MAKE THIS AN ABSTRACT METHOD??????
80 21 jones
    public String toString ()
81
    {
82 24 jones
	StringBuffer value = new StringBuffer();
83
	value.append('<');
84
	value.append(getTagName());
85
	value.append(getAttributes().toString());
86
	value.append('>');
87
	return value.toString();
88 21 jones
    }
89 126 jones
*/
90 21 jones
91 134 jones
    /** Get the id of this node */
92 133 jones
    public long getNodeID()
93 23 jones
    {
94 133 jones
      return node_id;
95 23 jones
    }
96 21 jones
97 134 jones
    /** Set the id of this node */
98 133 jones
    public void setNodeID(long node_id)
99 23 jones
    {
100 133 jones
      this.node_id = node_id;
101 23 jones
    }
102
103 134 jones
    /** Get the parent id of this node */
104 23 jones
    public long getParentID()
105
    {
106
      return parent_id;
107
    }
108
109 134 jones
    /** Set the parent id of this node */
110 23 jones
    public void setParentID(long parent_id)
111
    {
112
      this.parent_id = parent_id;
113
    }
114
115 149 bojilova
    /** 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 162 bojilova
    public String getDocID()
129 149 bojilova
    {
130
      return doc_id;
131
    }
132
133
    /** Set the doc id of this node */
134 162 bojilova
    public void setDocID(String doc_id)
135 149 bojilova
    {
136
      this.doc_id = doc_id;
137
    }
138
139 134 jones
    /** Get the name of this node */
140 23 jones
    public String getTagName()
141
    {
142
      return tagname;
143
    }
144 21 jones
145 134 jones
    /** Set the name of this node */
146 23 jones
    public void setTagName(String tagname)
147
    {
148
      this.tagname = tagname;
149
    }
150
151 21 jones
    /** 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 122 jones
        buf.append(" ").append(attName).append("=\"");
162
        buf.append(attValue).append("\"");
163 21 jones
      }
164
      return buf.toString();
165
    }
166
167 134 jones
    /** Add a new attribute to this node, or set its value */
168 21 jones
    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 134 jones
    /** Get nodeIndex of the node */
184 72 bojilova
    public int getNodeIndex() {
185
      return this.nodeIndex;
186
    }
187
188 126 jones
    /** Set the node index of this node */
189
    public void setNodeIndex(int nodeIndex) {
190
      this.nodeIndex = nodeIndex;
191
    }
192 122 jones
193
    /** Get the type of this node */
194 126 jones
    public String getNodeType() {
195 122 jones
      return nodeType;
196
    }
197
198
    /** Set the type of this node */
199 126 jones
    public void setNodeType(String type) {
200 122 jones
      this.nodeType = type;
201
    }
202 126 jones
203 127 jones
    /** Add a child node to this node */
204 129 jones
    public void addChildNode(BasicNode child) {
205 127 jones
      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 134 jones
    /** increase childNum when new child for the node is created */
214 126 jones
    public int incChildNum() {
215
      return ++this.childNum;
216
    }
217
218 21 jones
}