Project

General

Profile

1 21 jones
/**
2
 *        Name: BasicElement.java
3
 *     Purpose: A Class that represents an XML element and its contents
4 35 jones
 *   Copyright: 2000 Regents of the University of California and the
5
 *              National Center for Ecological Analysis and Synthesis
6 21 jones
 *     Authors: Matt Jones
7
 *
8
 *     Version: '$Id$'
9
 */
10
11 74 jones
package edu.ucsb.nceas.metacat;
12 21 jones
13
import java.io.IOException;
14
import java.util.Hashtable;
15
import java.util.Enumeration;
16
17 31 jones
/** A Class that represents an XML element and its contents */
18 21 jones
public class BasicElement {
19
20 29 jones
    private long		element_id;
21
    private String		tagname;
22
    private StringBuffer	content;
23
    private long		parent_id;
24 72 bojilova
    private Hashtable	attributes;
25
    private int         childNum;
26
    private int         nodeIndex;
27 21 jones
28 31 jones
    /** Construct a Basic Element */
29 23 jones
    public BasicElement () {
30
      content = new StringBuffer();
31
      attributes = new Hashtable();
32
    }
33
34 31 jones
    /** 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 72 bojilova
     * @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 31 jones
     */
42 72 bojilova
    public BasicElement (String tagname, long parent_id, int nodeIndex) {
43 23 jones
      this();
44 21 jones
      this.tagname = tagname;
45
      this.parent_id = parent_id;
46 72 bojilova
      this.nodeIndex = nodeIndex;
47
      this.childNum = 0;
48 21 jones
    }
49
50 31 jones
    /** 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 72 bojilova
    public BasicElement (long element_id, String tagname, long parent_id, int nodeIndex) {
57
      this(tagname,parent_id,nodeIndex);
58 21 jones
      this.element_id = element_id;
59
    }
60
61 31 jones
    /** convert the element to a string representation for display */
62 21 jones
    public String toString ()
63
    {
64 24 jones
	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 21 jones
    }
77
78
    /** Get the id of this element */
79 23 jones
    public long getElementID()
80
    {
81
      return element_id;
82
    }
83 21 jones
84 23 jones
    /** 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 21 jones
    /** Get the name of this element */
103 23 jones
    public String getTagName()
104
    {
105
      return tagname;
106
    }
107 21 jones
108 23 jones
    /** Set the tagname of this element */
109
    public void setTagName(String tagname)
110
    {
111
      this.tagname = tagname;
112
    }
113
114 21 jones
    /** 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 24 jones
        buf.append(" ").append(attName).append("=\"").append(attValue).append("\"");
125 21 jones
      }
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 72 bojilova
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 21 jones
}