Project

General

Profile

1 21 jones
/**
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$'
9
 */
10
11
//package project;
12
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
    private Hashtable		attributes;
25 21 jones
26 31 jones
    /** Construct a Basic Element */
27 23 jones
    public BasicElement () {
28
      content = new StringBuffer();
29
      attributes = new Hashtable();
30
    }
31
32 31 jones
    /** Construct a Basic Element
33
     *
34
     * @param tagname the name of the element
35
     * @param parent_id the id number of the parent element
36
     */
37 21 jones
    public BasicElement (String tagname, long parent_id) {
38 23 jones
      this();
39 21 jones
      this.tagname = tagname;
40
      this.parent_id = parent_id;
41
    }
42
43 31 jones
    /** Construct a Basic Element
44
     *
45
     * @param element_id the id number of the element
46
     * @param tagname the name of the element
47
     * @param parent_id the id number of the parent element
48
     */
49 21 jones
    public BasicElement (long element_id, String tagname, long parent_id) {
50
      this(tagname,parent_id);
51
      this.element_id = element_id;
52
    }
53
54 31 jones
    /** convert the element to a string representation for display */
55 21 jones
    public String toString ()
56
    {
57 24 jones
	StringBuffer value = new StringBuffer();
58
	value.append('<');
59
	value.append(getTagName());
60
	value.append(getAttributes().toString());
61
	value.append('>');
62
        // Process children recursively here?
63
        // Or do it in ReaderElement using a stack so we don;t have the
64
        // whole thing in memory at once?
65
	//value.append("</");
66
	//value.append(getTagName());
67
	//value.append('>');
68
	return value.toString();
69 21 jones
    }
70
71
    /** Get the id of this element */
72 23 jones
    public long getElementID()
73
    {
74
      return element_id;
75
    }
76 21 jones
77 23 jones
    /** Set the id of this element */
78
    public void setElementID(long element_id)
79
    {
80
      this.element_id = element_id;
81
    }
82
83
    /** Get the parent id of this element */
84
    public long getParentID()
85
    {
86
      return parent_id;
87
    }
88
89
    /** Set the parent id of this element */
90
    public void setParentID(long parent_id)
91
    {
92
      this.parent_id = parent_id;
93
    }
94
95 21 jones
    /** Get the name of this element */
96 23 jones
    public String getTagName()
97
    {
98
      return tagname;
99
    }
100 21 jones
101 23 jones
    /** Set the tagname of this element */
102
    public void setTagName(String tagname)
103
    {
104
      this.tagname = tagname;
105
    }
106
107 21 jones
    /** Get the attributes as a string */
108
    public String getAttributes() {
109
      StringBuffer buf = new StringBuffer();
110
      String attName = null;
111
      String attValue = null;
112
113
      Enumeration attList = attributes.keys();
114
      while (attList.hasMoreElements()) {
115
        attName = (String)attList.nextElement();
116
        attValue = (String)attributes.get(attName);
117 24 jones
        buf.append(" ").append(attName).append("=\"").append(attValue).append("\"");
118 21 jones
      }
119
      return buf.toString();
120
    }
121
122
    /** Add a new attribute to this element, or set its value */
123
    public void setAttribute(String attName, String attValue) {
124
      if (attName != null) {
125
        // Enter the attribute in the hash table
126
        attributes.put(attName, attValue);
127
128
      } else {
129
        System.err.println("Attribute name must not be null!");
130
      }
131
    }
132
133
    /** Get an attribute value by name */
134
    public String getAttribute(String attName) {
135
      return (String)attributes.get(attName);
136
    }
137
138
    /** Append to the content of the element */
139
    public void appendContent(char[] cbuf, int start, int len) {
140
      this.content.append( cbuf, start, len );
141
    }
142
143
    /** Append to the content of the element */
144
    public void appendContent(String new_content) {
145
      this.content.append( new_content );
146
    }
147
148
    /** Get the content of the element */
149
    public String getContent() {
150
      return this.content.toString();
151
    }
152
}