Project

General

Profile

1
/**
2
 *        Name: BasicNode.java
3
 *     Purpose: A Class that represents an XML element 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 133 2000-06-07 22:40:05Z 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 element 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 Element */
31
    public BasicNode () {
32
      children = new Vector();
33
      attributes = new Hashtable(); 
34
      this.childNum = 0;
35
    }
36

    
37
    /** Construct a Basic Element 
38
     *
39
     * @param tagname the name of the element
40
     * @param parent_id the id number of the parent element
41
     * @param nodeIndex - order of node among siblings in parent node
42
     *                    Every element initializes childNum to 0 when 
43
     *                    created and has interface incChildNum
44
     *                    when new child is created
45
     */
46
    public BasicNode (String tagname, long parent_id, int nodeIndex) {
47
      this();
48
      this.tagname = tagname;
49
      this.parent_id = parent_id;
50
      this.nodeIndex = nodeIndex;
51
    }
52
    
53
    /** Construct a Basic Element 
54
     *
55
     * @param node_id the id number of the node
56
     * @param tagname the name of the node
57
     * @param parent_id the id number of the parent node
58
     */
59
    public BasicNode (long node_id, String tagname, long parent_id, 
60
                         int nodeIndex) {
61
      this(tagname,parent_id,nodeIndex);
62
      this.node_id = node_id;
63
    }
64

    
65
    /** convert the element to a string representation for display */
66
/*  MAKE THIS AN ABSTRACT METHOD??????
67
    public String toString ()
68
    {
69
	StringBuffer value = new StringBuffer();
70
	value.append('<');
71
	value.append(getTagName());
72
	value.append(getAttributes().toString());
73
	value.append('>');
74
	return value.toString();
75
    }
76
*/
77

    
78
    /** Get the id of this element */
79
    public long getNodeID() 
80
    { 
81
      return node_id; 
82
    }
83

    
84
    /** Set the id of this element */
85
    public void setNodeID(long node_id) 
86
    { 
87
      this.node_id = node_id; 
88
    }
89

    
90
    /** Get the parent id of this element */
91
    public long getParentID() 
92
    { 
93
      return parent_id; 
94
    }
95

    
96
    /** Set the parent id of this element */
97
    public void setParentID(long parent_id) 
98
    { 
99
      this.parent_id = parent_id; 
100
    }
101

    
102
    /** Get the name of this element */
103
    public String getTagName() 
104
    { 
105
      return tagname; 
106
    }
107

    
108
    /** Set the tagname of this element */
109
    public void setTagName(String tagname) 
110
    { 
111
      this.tagname = tagname; 
112
    }
113

    
114
    /** Get the attributes as a string */
115
    public String getAttributes() {
116
      StringBuffer buf = new StringBuffer();
117
      String attName = null;
118
      String attValue = null;
119

    
120
      Enumeration attList = attributes.keys();
121
      while (attList.hasMoreElements()) {
122
        attName = (String)attList.nextElement();
123
        attValue = (String)attributes.get(attName);
124
        buf.append(" ").append(attName).append("=\"");
125
        buf.append(attValue).append("\"");        
126
      }
127
      return buf.toString();      
128
    }
129

    
130
    /** Add a new attribute to this element, or set its value */
131
    public void setAttribute(String attName, String attValue) {
132
      if (attName != null) {
133
        // Enter the attribute in the hash table
134
        attributes.put(attName, attValue);
135
 
136
      } else {
137
        System.err.println("Attribute name must not be null!");
138
      }
139
    }
140

    
141
    /** Get an attribute value by name */
142
    public String getAttribute(String attName) {
143
      return (String)attributes.get(attName);
144
    }
145

    
146
    /** Get nodeIndex of the node element */
147
    public int getNodeIndex() {
148
      return this.nodeIndex;
149
    }
150

    
151
    /** Set the node index of this node */
152
    public void setNodeIndex(int nodeIndex) { 
153
      this.nodeIndex = nodeIndex; 
154
    }
155

    
156
    /** Get the type of this node */
157
    public String getNodeType() { 
158
      return nodeType; 
159
    }
160

    
161
    /** Set the type of this node */
162
    public void setNodeType(String type) { 
163
      this.nodeType = type; 
164
    }
165

    
166
    /** Add a child node to this node */
167
    public void addChildNode(BasicNode child) { 
168
      this.children.add(child);
169
    }
170

    
171
    /** Get the an enumeration of the children of this node */
172
    public Enumeration getChildren() { 
173
      return children.elements(); 
174
    }
175

    
176
    /** increase childNum when new child for the element is created */
177
    public int incChildNum() {
178
      return ++this.childNum;    
179
    }    
180

    
181
}
(1-1/19)