1
|
/**
|
2
|
* Name: BasicElement.java
|
3
|
* Purpose: A Class that represents an XML element and its contents
|
4
|
* Institution: National Center for Ecological Analysis and Synthesis
|
5
|
* Copyright: 2000
|
6
|
* Authors: Matt Jones
|
7
|
*
|
8
|
* Version: '$Id: BasicElement.java 21 2000-04-11 20:26:43Z jones $'
|
9
|
*/
|
10
|
|
11
|
//package project;
|
12
|
|
13
|
import java.io.IOException;
|
14
|
import java.util.Hashtable;
|
15
|
import java.util.Enumeration;
|
16
|
|
17
|
public class BasicElement {
|
18
|
|
19
|
protected long element_id;
|
20
|
protected String tagname;
|
21
|
protected StringBuffer content;
|
22
|
protected long parent_id;
|
23
|
protected Hashtable attributes;
|
24
|
|
25
|
public BasicElement (String tagname, long parent_id) {
|
26
|
this.tagname = tagname;
|
27
|
this.parent_id = parent_id;
|
28
|
content = new StringBuffer();
|
29
|
attributes = new Hashtable();
|
30
|
}
|
31
|
|
32
|
public BasicElement (long element_id, String tagname, long parent_id) {
|
33
|
this(tagname,parent_id);
|
34
|
this.element_id = element_id;
|
35
|
}
|
36
|
|
37
|
// used by JTree to display this node
|
38
|
public String toString ()
|
39
|
{
|
40
|
StringBuffer value = new StringBuffer ();
|
41
|
value.append ('<');
|
42
|
value.append (getTagName ());
|
43
|
value.append (getAttributes ().toString ());
|
44
|
value.append ('>');
|
45
|
return value.toString ();
|
46
|
}
|
47
|
|
48
|
/** Get the id of this element */
|
49
|
public long getElementID() { return element_id; }
|
50
|
|
51
|
/** Get the name of this element */
|
52
|
public String getTagName() { return tagname; }
|
53
|
|
54
|
/** Get the attributes as a string */
|
55
|
public String getAttributes() {
|
56
|
StringBuffer buf = new StringBuffer();
|
57
|
String attName = null;
|
58
|
String attValue = null;
|
59
|
|
60
|
Enumeration attList = attributes.keys();
|
61
|
while (attList.hasMoreElements()) {
|
62
|
attName = (String)attList.nextElement();
|
63
|
attValue = (String)attributes.get(attName);
|
64
|
buf.append(" ").append(attName).append("=").append(attValue);
|
65
|
}
|
66
|
return buf.toString();
|
67
|
}
|
68
|
|
69
|
/** Add a new attribute to this element, or set its value */
|
70
|
public void setAttribute(String attName, String attValue) {
|
71
|
if (attName != null) {
|
72
|
// Enter the attribute in the hash table
|
73
|
attributes.put(attName, attValue);
|
74
|
|
75
|
} else {
|
76
|
System.err.println("Attribute name must not be null!");
|
77
|
}
|
78
|
}
|
79
|
|
80
|
/** Get an attribute value by name */
|
81
|
public String getAttribute(String attName) {
|
82
|
return (String)attributes.get(attName);
|
83
|
}
|
84
|
|
85
|
/** Append to the content of the element */
|
86
|
public void appendContent(char[] cbuf, int start, int len) {
|
87
|
this.content.append( cbuf, start, len );
|
88
|
}
|
89
|
|
90
|
/** Append to the content of the element */
|
91
|
public void appendContent(String new_content) {
|
92
|
this.content.append( new_content );
|
93
|
}
|
94
|
|
95
|
/** Get the content of the element */
|
96
|
public String getContent() {
|
97
|
return this.content.toString();
|
98
|
}
|
99
|
|
100
|
}
|