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: 2006-11-10 10:25:38 -0800 (Fri, 10 Nov 2006) $'
10
 * '$Revision: 3077 $'
11
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
 */
26

    
27
package edu.ucsb.nceas.metacat;
28

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
251
}
(12-12/67)