Project

General

Profile

1
/**
2
 *        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
 *
8
 *     Version: '$Id: BasicNode.java 135 2000-06-08 00:16:35Z jones $'
9
 */
10

    
11
package edu.ucsb.nceas.metacat;
12

    
13
import java.io.IOException;
14
import java.util.Hashtable;
15
import java.util.Enumeration;
16
import java.util.Vector;
17

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

    
21
    private long	node_id;
22
    private String	tagname;
23
    private long	parent_id;
24
    private Hashtable	attributes;
25
    private int         childNum;
26
    private int         nodeIndex;
27
    private String      nodeType;
28
    private Vector	children;
29

    
30
    /** Construct a Basic Node */
31
    public BasicNode () {
32
      children = new Vector();
33
      attributes = new Hashtable(); 
34
      this.childNum = 0;
35
    }
36

    
37
    /** 
38
     * Construct a Basic Node 
39
     *
40
     * @param tagname the name of the node
41
     */
42
    public BasicNode (String tagname) {
43
      this();
44
      this.tagname = tagname;
45
    }
46

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

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

    
89
    /** Get the id of this node */
90
    public long getNodeID() 
91
    { 
92
      return node_id; 
93
    }
94

    
95
    /** Set the id of this node */
96
    public void setNodeID(long node_id) 
97
    { 
98
      this.node_id = node_id; 
99
    }
100

    
101
    /** Get the parent id of this node */
102
    public long getParentID() 
103
    { 
104
      return parent_id; 
105
    }
106

    
107
    /** Set the parent id of this node */
108
    public void setParentID(long parent_id) 
109
    { 
110
      this.parent_id = parent_id; 
111
    }
112

    
113
    /** Get the name of this node */
114
    public String getTagName() 
115
    { 
116
      return tagname; 
117
    }
118

    
119
    /** Set the name of this node */
120
    public void setTagName(String tagname) 
121
    { 
122
      this.tagname = tagname; 
123
    }
124

    
125
    /** Get the attributes as a string */
126
    public String getAttributes() {
127
      StringBuffer buf = new StringBuffer();
128
      String attName = null;
129
      String attValue = null;
130

    
131
      Enumeration attList = attributes.keys();
132
      while (attList.hasMoreElements()) {
133
        attName = (String)attList.nextElement();
134
        attValue = (String)attributes.get(attName);
135
        buf.append(" ").append(attName).append("=\"");
136
        buf.append(attValue).append("\"");        
137
      }
138
      return buf.toString();      
139
    }
140

    
141
    /** Add a new attribute to this node, or set its value */
142
    public void setAttribute(String attName, String attValue) {
143
      if (attName != null) {
144
        // Enter the attribute in the hash table
145
        attributes.put(attName, attValue);
146
 
147
      } else {
148
        System.err.println("Attribute name must not be null!");
149
      }
150
    }
151

    
152
    /** Get an attribute value by name */
153
    public String getAttribute(String attName) {
154
      return (String)attributes.get(attName);
155
    }
156

    
157
    /** Get nodeIndex of the node */
158
    public int getNodeIndex() {
159
      return this.nodeIndex;
160
    }
161

    
162
    /** Set the node index of this node */
163
    public void setNodeIndex(int nodeIndex) { 
164
      this.nodeIndex = nodeIndex; 
165
    }
166

    
167
    /** Get the type of this node */
168
    public String getNodeType() { 
169
      return nodeType; 
170
    }
171

    
172
    /** Set the type of this node */
173
    public void setNodeType(String type) { 
174
      this.nodeType = type; 
175
    }
176

    
177
    /** Add a child node to this node */
178
    public void addChildNode(BasicNode child) { 
179
      this.children.add(child);
180
    }
181

    
182
    /** Get the an enumeration of the children of this node */
183
    public Enumeration getChildren() { 
184
      return children.elements(); 
185
    }
186

    
187
    /** increase childNum when new child for the node is created */
188
    public int incChildNum() {
189
      return ++this.childNum;    
190
    }    
191

    
192
}
(1-1/18)