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