Project

General

Profile

1
/**
2
 *  '$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
 *
8
 *   '$Author: jones $'
9
 *     '$Date: 2000-06-26 03:35:05 -0700 (Mon, 26 Jun 2000) $'
10
 * '$Revision: 203 $'
11
 */
12

    
13
package edu.ucsb.nceas.metacat;
14

    
15
import java.io.IOException;
16
import java.util.Hashtable;
17
import java.util.Enumeration;
18
import java.util.Vector;
19

    
20
/** A Class that represents an XML node and its contents */
21
public class BasicNode {
22

    
23
    private long	node_id;
24
    private String	tagname;
25
    private long	parent_id;
26
    private long    rootnode_id;
27
    private String  doc_id;
28
    private Hashtable	attributes;
29
    private int         childNum;
30
    private int         nodeIndex;
31
    private String      nodeType;
32
    private Vector  	children;
33

    
34
    /** Construct a Basic Node */
35
    public BasicNode () {
36
      children = new Vector();
37
      attributes = new Hashtable(); 
38
      this.childNum = 0;
39
    }
40

    
41
    /** 
42
     * Construct a Basic Node 
43
     *
44
     * @param tagname the name of the node
45
     */
46
    public BasicNode (String tagname) {
47
      this();
48
      this.tagname = tagname;
49
    }
50

    
51
    /** 
52
     * Construct a Basic Node 
53
     *
54
     * @param tagname the name of the node
55
     * @param parent_id the id number of the parent node
56
     * @param nodeIndex - index of node among siblings in parent node
57
     *                    Every node initializes childNum to 0 when 
58
     *                    created and has interface incChildNum
59
     *                    when new child is created
60
     */
61
    public BasicNode (String tagname, long parent_id, int nodeIndex) {
62
      this();
63
      this.tagname = tagname;
64
      this.parent_id = parent_id;
65
      this.nodeIndex = nodeIndex;
66
    }
67
    
68
    /** Construct a Basic Node 
69
     *
70
     * @param node_id the id number of the node
71
     * @param tagname the name of the node
72
     * @param parent_id the id number of the parent node
73
     */
74
    public BasicNode (long node_id, String tagname, long parent_id, 
75
                         int nodeIndex) {
76
      this(tagname,parent_id,nodeIndex);
77
      this.node_id = node_id;
78
    }
79

    
80
    /** convert the node to a string representation for display */
81
/*  MAKE THIS AN ABSTRACT METHOD??????
82
    public String toString ()
83
    {
84
	StringBuffer value = new StringBuffer();
85
	value.append('<');
86
	value.append(getTagName());
87
	value.append(getAttributes().toString());
88
	value.append('>');
89
	return value.toString();
90
    }
91
*/
92

    
93
    /** Get the id of this node */
94
    public long getNodeID() 
95
    { 
96
      return node_id; 
97
    }
98

    
99
    /** Set the id of this node */
100
    public void setNodeID(long node_id) 
101
    { 
102
      this.node_id = node_id; 
103
    }
104

    
105
    /** Get the parent id of this node */
106
    public long getParentID() 
107
    { 
108
      return parent_id; 
109
    }
110

    
111
    /** Set the parent id of this node */
112
    public void setParentID(long parent_id) 
113
    { 
114
      this.parent_id = parent_id; 
115
    }
116

    
117
    /** Get the root node id of this node */
118
    public long getRootNodeID() 
119
    { 
120
      return rootnode_id; 
121
    }
122

    
123
    /** Set the root node id of this node */
124
    public void setRootNodeID(long rootnode_id) 
125
    { 
126
      this.rootnode_id = rootnode_id; 
127
    }
128

    
129
    /** Get the doc id of this node */
130
    public String getDocID() 
131
    { 
132
      return doc_id; 
133
    }
134

    
135
    /** Set the doc id of this node */
136
    public void setDocID(String doc_id) 
137
    { 
138
      this.doc_id = doc_id; 
139
    }
140

    
141
    /** Get the name of this node */
142
    public String getTagName() 
143
    { 
144
      return tagname; 
145
    }
146

    
147
    /** Set the name of this node */
148
    public void setTagName(String tagname) 
149
    { 
150
      this.tagname = tagname; 
151
    }
152

    
153
    /** Get the attributes as a string */
154
    public String getAttributes() {
155
      StringBuffer buf = new StringBuffer();
156
      String attName = null;
157
      String attValue = null;
158

    
159
      Enumeration attList = attributes.keys();
160
      while (attList.hasMoreElements()) {
161
        attName = (String)attList.nextElement();
162
        attValue = (String)attributes.get(attName);
163
        buf.append(" ").append(attName).append("=\"");
164
        buf.append(attValue).append("\"");        
165
      }
166
      return buf.toString();      
167
    }
168

    
169
    /** Add a new attribute to this node, or set its value */
170
    public void setAttribute(String attName, String attValue) {
171
      if (attName != null) {
172
        // Enter the attribute in the hash table
173
        attributes.put(attName, attValue);
174
 
175
      } else {
176
        System.err.println("Attribute name must not be null!");
177
      }
178
    }
179

    
180
    /** Get an attribute value by name */
181
    public String getAttribute(String attName) {
182
      return (String)attributes.get(attName);
183
    }
184

    
185
    /** Get nodeIndex of the node */
186
    public int getNodeIndex() {
187
      return this.nodeIndex;
188
    }
189

    
190
    /** Set the node index of this node */
191
    public void setNodeIndex(int nodeIndex) { 
192
      this.nodeIndex = nodeIndex; 
193
    }
194

    
195
    /** Get the type of this node */
196
    public String getNodeType() { 
197
      return nodeType; 
198
    }
199

    
200
    /** Set the type of this node */
201
    public void setNodeType(String type) { 
202
      this.nodeType = type; 
203
    }
204

    
205
    /** Add a child node to this node */
206
    public void addChildNode(BasicNode child) { 
207
      this.children.add(child);
208
    }
209

    
210
    /** Get the an enumeration of the children of this node */
211
    public Enumeration getChildren() { 
212
      return children.elements(); 
213
    }
214

    
215
    /** increase childNum when new child for the node is created */
216
    public int incChildNum() {
217
      return ++this.childNum;    
218
    }    
219

    
220
}
221

    
222
/**
223
 * '$Log$
224
 * 'Revision 1.19.2.2  2000/06/25 23:38:16  jones
225
 * 'Added RCSfile keyword
226
 * '
227
 * 'Revision 1.19.2.1  2000/06/25 23:34:17  jones
228
 * 'Changed documentation formatting, added log entries at bottom of source files
229
 * ''
230
 */
(3-3/19)