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
 *    Release: @release@
8
 *
9
 *   '$Author: bojilova $'
10
 *     '$Date: 2001-08-15 13:38:35 -0700 (Wed, 15 Aug 2001) $'
11
 * '$Revision: 821 $'
12
 *
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
 */
27

    
28
package edu.ucsb.nceas.metacat;
29

    
30
import java.io.IOException;
31
import java.util.Hashtable;
32
import java.util.Enumeration;
33
import java.util.Vector;
34

    
35
/** A Class that represents an XML node and its contents */
36
public class BasicNode {
37

    
38
    private long	node_id;
39
    private String	tagname;
40
    private long	parent_id;
41
    private long    rootnode_id;
42
    private String  doc_id;
43
    private Hashtable	attributes;
44
    private Hashtable	namespace;
45
    private int         childNum;
46
    private int         nodeIndex;
47
    private String      nodeType;
48
    private Vector  	children;
49

    
50
    /** Construct a Basic Node */
51
    public BasicNode () {
52
      children = new Vector();
53
      attributes = new Hashtable(); 
54
      namespace = new Hashtable(); 
55
      this.childNum = 0;
56
    }
57

    
58
    /** 
59
     * Construct a Basic Node 
60
     *
61
     * @param tagname the name of the node
62
     */
63
    public BasicNode (String tagname) {
64
      this();
65
      this.tagname = tagname;
66
    }
67

    
68
    /** 
69
     * Construct a Basic Node 
70
     *
71
     * @param tagname the name of the node
72
     * @param parent_id the id number of the parent node
73
     * @param nodeIndex - index of node among siblings in parent node
74
     *                    Every node initializes childNum to 0 when 
75
     *                    created and has interface incChildNum
76
     *                    when new child is created
77
     */
78
    public BasicNode (String tagname, long parent_id, int nodeIndex) {
79
      this();
80
      this.tagname = tagname;
81
      this.parent_id = parent_id;
82
      this.nodeIndex = nodeIndex;
83
    }
84
    
85
    /** Construct a Basic Node 
86
     *
87
     * @param node_id the id number of the node
88
     * @param tagname the name of the node
89
     * @param parent_id the id number of the parent node
90
     */
91
    public BasicNode (long node_id, String tagname, long parent_id, 
92
                         int nodeIndex) {
93
      this(tagname,parent_id,nodeIndex);
94
      this.node_id = node_id;
95
    }
96

    
97
    /** convert the node to a string representation for display */
98
/*  MAKE THIS AN ABSTRACT METHOD??????
99
    public String toString ()
100
    {
101
	StringBuffer value = new StringBuffer();
102
	value.append('<');
103
	value.append(getTagName());
104
	value.append(getAttributes().toString());
105
	value.append('>');
106
	return value.toString();
107
    }
108
*/
109

    
110
    /** Get the id of this node */
111
    public long getNodeID() 
112
    { 
113
      return node_id; 
114
    }
115

    
116
    /** Set the id of this node */
117
    public void setNodeID(long node_id) 
118
    { 
119
      this.node_id = node_id; 
120
    }
121

    
122
    /** Get the parent id of this node */
123
    public long getParentID() 
124
    { 
125
      return parent_id; 
126
    }
127

    
128
    /** Set the parent id of this node */
129
    public void setParentID(long parent_id) 
130
    { 
131
      this.parent_id = parent_id; 
132
    }
133

    
134
    /** Get the root node id of this node */
135
    public long getRootNodeID() 
136
    { 
137
      return rootnode_id; 
138
    }
139

    
140
    /** Set the root node id of this node */
141
    public void setRootNodeID(long rootnode_id) 
142
    { 
143
      this.rootnode_id = rootnode_id; 
144
    }
145

    
146
    /** Get the doc id of this node */
147
    public String getDocID() 
148
    { 
149
      return doc_id; 
150
    }
151

    
152
    /** Set the doc id of this node */
153
    public void setDocID(String doc_id) 
154
    { 
155
      this.doc_id = doc_id; 
156
    }
157

    
158
    /** Get the name of this node */
159
    public String getTagName() 
160
    { 
161
      return tagname; 
162
    }
163

    
164
    /** Set the name of this node */
165
    public void setTagName(String tagname) 
166
    { 
167
      this.tagname = tagname; 
168
    }
169

    
170
    /** Get the attributes as a string */
171
    public String getAttributes() {
172
      StringBuffer buf = new StringBuffer();
173
      String attName = null;
174
      String attValue = null;
175

    
176
      Enumeration attList = attributes.keys();
177
      while (attList.hasMoreElements()) {
178
        attName = (String)attList.nextElement();
179
        attValue = (String)attributes.get(attName);
180
        buf.append(" ").append(attName).append("=\"");
181
        buf.append(attValue).append("\"");        
182
      }
183
      return buf.toString();      
184
    }
185

    
186
    /** Add a new attribute to this node, or set its value */
187
    public void setAttribute(String attName, String attValue) {
188
      if (attName != null) {
189
        // Enter the attribute in the hash table
190
        attributes.put(attName, attValue);
191
 
192
      } else {
193
        System.err.println("Attribute name must not be null!");
194
      }
195
    }
196

    
197
    /** Get an attribute value by name */
198
    public String getAttribute(String attName) {
199
      return (String)attributes.get(attName);
200
    }
201

    
202
    /** Add a namespace to this node */
203
    public void setNamespace(String prefix, String uri) {
204
      if (prefix != null) {
205
        // Enter the namespace in the hash table
206
        namespace.put(prefix, uri);
207
      } else {
208
        System.err.println("Namespace prefix must not be null!");
209
      }
210
    }
211

    
212
    /** Get an uri of the namespace prefix */
213
    public String getNamespace(String prefix) {
214
      return (String)namespace.get(prefix);
215
    }
216

    
217
    /** Get nodeIndex of the node */
218
    public int getNodeIndex() {
219
      return this.nodeIndex;
220
    }
221

    
222
    /** Set the node index of this node */
223
    public void setNodeIndex(int nodeIndex) { 
224
      this.nodeIndex = nodeIndex; 
225
    }
226

    
227
    /** Get the type of this node */
228
    public String getNodeType() { 
229
      return nodeType; 
230
    }
231

    
232
    /** Set the type of this node */
233
    public void setNodeType(String type) { 
234
      this.nodeType = type; 
235
    }
236

    
237
    /** Add a child node to this node */
238
    public void addChildNode(BasicNode child) { 
239
      this.children.add(child);
240
    }
241

    
242
    /** Get the an enumeration of the children of this node */
243
    public Enumeration getChildren() { 
244
      return children.elements(); 
245
    }
246

    
247
    /** increase childNum when new child for the node is created */
248
    public int incChildNum() {
249
      return ++this.childNum;    
250
    }    
251

    
252
}
(14-14/58)