Project

General

Profile

1
/**
2
 *        Name: BasicElement.java
3
 *     Purpose: A Class that represents an XML element and its contents
4
 *   Copyright: 2000 Regents of the University of California and the
5
 *              National Center for Ecological Analysis and Synthesis
6
 *     Authors: Matt Jones
7
 *
8
 *     Version: '$Id: BasicElement.java 35 2000-04-13 16:48:37Z jones $'
9
 */
10

    
11
//package project;
12

    
13
import java.io.IOException;
14
import java.util.Hashtable;
15
import java.util.Enumeration;
16

    
17
/** A Class that represents an XML element and its contents */
18
public class BasicElement {
19

    
20
    private long		element_id;
21
    private String		tagname;
22
    private StringBuffer	content;
23
    private long		parent_id;
24
    private Hashtable		attributes;
25

    
26
    /** Construct a Basic Element */
27
    public BasicElement () {
28
      content = new StringBuffer();
29
      attributes = new Hashtable(); 
30
    }
31

    
32
    /** 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
    public BasicElement (String tagname, long parent_id) {
38
      this();
39
      this.tagname = tagname;
40
      this.parent_id = parent_id;
41
    }
42
    
43
    /** 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
    public BasicElement (long element_id, String tagname, long parent_id) {
50
      this(tagname,parent_id);
51
      this.element_id = element_id;
52
    }
53

    
54
    /** convert the element to a string representation for display */
55
    public String toString ()
56
    {
57
	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
    }
70

    
71
    /** Get the id of this element */
72
    public long getElementID() 
73
    { 
74
      return element_id; 
75
    }
76

    
77
    /** 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
    /** Get the name of this element */
96
    public String getTagName() 
97
    { 
98
      return tagname; 
99
    }
100

    
101
    /** Set the tagname of this element */
102
    public void setTagName(String tagname) 
103
    { 
104
      this.tagname = tagname; 
105
    }
106

    
107
    /** 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
        buf.append(" ").append(attName).append("=\"").append(attValue).append("\"");        
118
      }
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
}
(1-1/13)