Project

General

Profile

1
/**
2
 *        Name: BasicElement.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: BasicElement.java 74 2000-05-05 00:37:58Z 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

    
17
/** A Class that represents an XML element and its contents */
18
public class BasicElement {
19

    
20
    private long		element_id;
21
    private String		tagname;
22
    private StringBuffer	content;
23
    private long		parent_id;
24
    private Hashtable	attributes;
25
    private int         childNum;
26
    private int         nodeIndex;
27

    
28
    /** Construct a Basic Element */
29
    public BasicElement () {
30
      content = new StringBuffer();
31
      attributes = new Hashtable(); 
32
    }
33

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

    
61
    /** convert the element to a string representation for display */
62
    public String toString ()
63
    {
64
	StringBuffer value = new StringBuffer();
65
	value.append('<');
66
	value.append(getTagName());
67
	value.append(getAttributes().toString());
68
	value.append('>');
69
        // Process children recursively here?
70
        // Or do it in ReaderElement using a stack so we don;t have the 
71
        // whole thing in memory at once?
72
	//value.append("</");
73
	//value.append(getTagName());
74
	//value.append('>');
75
	return value.toString();
76
    }
77

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

    
84
    /** Set the id of this element */
85
    public void setElementID(long element_id) 
86
    { 
87
      this.element_id = element_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("=\"").append(attValue).append("\"");        
125
      }
126
      return buf.toString();      
127
    }
128

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

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

    
145
    /** Append to the content of the element */
146
    public void appendContent(char[] cbuf, int start, int len) {
147
      this.content.append( cbuf, start, len );
148
    }
149

    
150
    /** Append to the content of the element */
151
    public void appendContent(String new_content) {
152
      this.content.append( new_content );
153
    }
154

    
155
    /** Get the content of the element */
156
    public String getContent() {
157
      return this.content.toString();
158
    }
159

    
160
    /** Get nodeIndex of the element */
161
    public int getNodeIndex() {
162
      return this.nodeIndex;
163
    }
164

    
165
    /** increase childNum when new child for the element is created */
166
    public int incChildNum() {
167
      return ++this.childNum;    
168
    }    
169
}
(1-1/30)