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
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 23 jones
    public BasicElement () {
26
      content = new StringBuffer();
27
      attributes = new Hashtable();
28
    }
29
30 21 jones
    public BasicElement (String tagname, long parent_id) {
31 23 jones
      this();
32 21 jones
      this.tagname = tagname;
33
      this.parent_id = parent_id;
34
    }
35
36
    public BasicElement (long element_id, String tagname, long parent_id) {
37
      this(tagname,parent_id);
38
      this.element_id = element_id;
39
    }
40
41
    // used by JTree to display this node
42
    public String toString ()
43
    {
44
	StringBuffer	value = new StringBuffer ();
45
	value.append ('<');
46
	value.append (getTagName ());
47
	value.append (getAttributes ().toString ());
48
	value.append ('>');
49
	return value.toString ();
50
    }
51
52
    /** Get the id of this element */
53 23 jones
    public long getElementID()
54
    {
55
      return element_id;
56
    }
57 21 jones
58 23 jones
    /** Set the id of this element */
59
    public void setElementID(long element_id)
60
    {
61
      this.element_id = element_id;
62
    }
63
64
    /** Get the parent id of this element */
65
    public long getParentID()
66
    {
67
      return parent_id;
68
    }
69
70
    /** Set the parent id of this element */
71
    public void setParentID(long parent_id)
72
    {
73
      this.parent_id = parent_id;
74
    }
75
76 21 jones
    /** Get the name of this element */
77 23 jones
    public String getTagName()
78
    {
79
      return tagname;
80
    }
81 21 jones
82 23 jones
    /** Set the tagname of this element */
83
    public void setTagName(String tagname)
84
    {
85
      this.tagname = tagname;
86
    }
87
88 21 jones
    /** Get the attributes as a string */
89
    public String getAttributes() {
90
      StringBuffer buf = new StringBuffer();
91
      String attName = null;
92
      String attValue = null;
93
94
      Enumeration attList = attributes.keys();
95
      while (attList.hasMoreElements()) {
96
        attName = (String)attList.nextElement();
97
        attValue = (String)attributes.get(attName);
98
        buf.append(" ").append(attName).append("=").append(attValue);
99
      }
100
      return buf.toString();
101
    }
102
103
    /** Add a new attribute to this element, or set its value */
104
    public void setAttribute(String attName, String attValue) {
105
      if (attName != null) {
106
        // Enter the attribute in the hash table
107
        attributes.put(attName, attValue);
108
109
      } else {
110
        System.err.println("Attribute name must not be null!");
111
      }
112
    }
113
114
    /** Get an attribute value by name */
115
    public String getAttribute(String attName) {
116
      return (String)attributes.get(attName);
117
    }
118
119
    /** Append to the content of the element */
120
    public void appendContent(char[] cbuf, int start, int len) {
121
      this.content.append( cbuf, start, len );
122
    }
123
124
    /** Append to the content of the element */
125
    public void appendContent(String new_content) {
126
      this.content.append( new_content );
127
    }
128
129
    /** Get the content of the element */
130
    public String getContent() {
131
      return this.content.toString();
132
    }
133
134
}