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: jones $'
10
 *     '$Date: 2001-01-18 11:52:00 -0800 (Thu, 18 Jan 2001) $'
11
 * '$Revision: 669 $'
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 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
      this.childNum = 0;
54
    }
55

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
200
    /** Get nodeIndex of the node */
201
    public int getNodeIndex() {
202
      return this.nodeIndex;
203
    }
204

    
205
    /** Set the node index of this node */
206
    public void setNodeIndex(int nodeIndex) { 
207
      this.nodeIndex = nodeIndex; 
208
    }
209

    
210
    /** Get the type of this node */
211
    public String getNodeType() { 
212
      return nodeType; 
213
    }
214

    
215
    /** Set the type of this node */
216
    public void setNodeType(String type) { 
217
      this.nodeType = type; 
218
    }
219

    
220
    /** Add a child node to this node */
221
    public void addChildNode(BasicNode child) { 
222
      this.children.add(child);
223
    }
224

    
225
    /** Get the an enumeration of the children of this node */
226
    public Enumeration getChildren() { 
227
      return children.elements(); 
228
    }
229

    
230
    /** increase childNum when new child for the node is created */
231
    public int incChildNum() {
232
      return ++this.childNum;    
233
    }    
234

    
235
}
(9-9/43)