KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > xpdl > XMLElement


1 package org.enhydra.shark.xpdl;
2
3 import java.io.Serializable JavaDoc;
4
5 /**
6  * Base class for representing elements from XML schema.
7  *
8  * @author Sasa Bojanic
9  */

10 public abstract class XMLElement implements Serializable JavaDoc, Cloneable JavaDoc {
11
12    /**
13     * Equivalent for XML element name. Used when writting instance of this class to XML file.
14     */

15    private String JavaDoc name;
16
17    /**
18     * Indicates if element is required - corresponds to the same XML element definition.
19     */

20    private boolean isRequired = false;
21
22    /**
23     * Supposed to contain the value for XML element. This is true for simple elements and
24     * attributes, more complex elements uses it as they need.
25     */

26    protected String JavaDoc value;
27
28    /**
29     * Indicates if an element is read only.
30     */

31    protected boolean isReadOnly = false;
32
33    /**
34     * Reference to parent object in DOM tree.
35     */

36    protected XMLElement parent;
37
38    /**
39     * Creates a new instance of element: sets <code>name</code> to name of concrete class
40     * implementation of this abstract class, and <code>parent</code> and <code>isRequired</code>
41     * properties to the specified ones.
42     * <p>
43     * It also sets the value of this element to an empty String.
44     */

45    public XMLElement(XMLElement parent, boolean isRequired) {
46       this.parent = parent;
47       this.isRequired = isRequired;
48       this.name = getClass().getName();
49       this.name = XMLUtil.getShortClassName(name);
50       this.value = new String JavaDoc();
51    }
52
53    /**
54     * Creates a new instance of element: sets <code>name</code>,
55     * <code>parent</code> <code>isRequired</code> properties to specified ones.
56     * <p>
57     * It also sets the value of this element to an empty String.
58     */

59    public XMLElement(XMLElement parent, String JavaDoc name, boolean isRequired) {
60       this.parent = parent;
61       this.name = name;
62       this.isRequired = isRequired;
63       this.value = new String JavaDoc();
64    }
65
66    /**
67     * Sets 'read only' property of element to specified value. This enables/disables editing of the
68     * element value for the simple elements and attributes, or changes to attributes and elements of
69     * complex objects and collections.
70     * <p>
71     * If element is read only, and one wants to change its property, the RuntimeException will be
72     * thrown.
73     */

74    public void setReadOnly(boolean ro) {
75       this.isReadOnly = ro;
76    }
77
78    /**
79     * Returns the 'read only' status of element.
80     * <p>
81     * If element is read only, and one wants to change its property, the RuntimeException will be
82     * thrown.
83     */

84    public boolean isReadOnly() {
85       return isReadOnly;
86    }
87
88    /**
89     * Returns if the element is required or not, which is defined by XPDL schema. If element is
90     * required, its value must be defined (In the case of complex elements, all the required
91     * subelements must be defined). Otherwise, the whole Package won't be valid by the XPDL schema.
92     */

93    public boolean isRequired() {
94       return isRequired;
95    }
96
97    /**
98     * Indicates if element is empty.
99     */

100    public boolean isEmpty() {
101       return !(value != null && value.trim().length() > 0);
102    }
103
104    /**
105     * Sets the element value. If it is simple element or an non-choice attribute, this sets the
106     * actual value of the element. If it is choice attribute, it sets the choosen value. Only some
107     * complex elements (Condition, SchemaType, and ExtendedAttribute) allows you to use this method,
108     * while others will throw RuntimeException.
109     */

110    public void setValue(String JavaDoc v) {
111       if (isReadOnly) {
112          throw new RuntimeException JavaDoc("Can't set the value of read only element!");
113       }
114       this.value = v;
115    }
116
117    /**
118     * Returns the element value.
119     */

120    public String JavaDoc toValue() {
121       return value;
122    }
123
124    /**
125     * Returns the name of element.
126     */

127    public String JavaDoc toName() {
128       return name;
129    }
130
131    /** Gets the parent element in DOM tree. */
132    public XMLElement getParent() {
133       return parent;
134    }
135
136    /**
137     * Sets the parent element in DOM tree.
138     * <p>
139     * This method is used when collection, complex element or complex choice is cloned, to set new
140     * parent element of the cloned sub-elements.
141     */

142    public void setParent(XMLElement el) {
143       this.parent = el;
144    }
145
146    /**
147     * Used to create exact copy of the element.
148     */

149    public Object JavaDoc clone() {
150       XMLElement d = null;
151       try {
152          //System.out.println("Cloning XMLELement "+name);
153
d = (XMLElement) super.clone();
154          d.name = new String JavaDoc(this.name);
155          d.value = new String JavaDoc(this.value);
156          d.isRequired = this.isRequired;
157          d.isReadOnly = this.isReadOnly;
158          d.parent = this.parent;
159       } catch (CloneNotSupportedException JavaDoc ex) {
160          // Won't happen because we implement Cloneable
161
throw new Error JavaDoc(ex.toString());
162       }
163       return d;
164    }
165
166    public boolean equals(Object JavaDoc e) {
167       if (this == e)
168          return true;
169 //System.out.println("Checking eq for el "+e+" with el "+this);
170
if (e != null && e instanceof XMLElement && e.getClass().equals(this.getClass())) {
171 //System.out.println("Els are the same class");
172
XMLElement el = (XMLElement) e;
173          // TODO: do we need to check isReadOnly for equality?
174
return (this.name.equals(el.name) &&
175                   this.value.equals(el.value) &&
176                   this.isRequired == el.isRequired);
177          //&& (this.parent == null ? el.parent == null : this.parent.equals(el.parent)));
178
}/* else {
179          System.out.println("Els are not the same class: el="+e+", this="+this);
180
181       }*/

182       return false;
183    }
184
185 }
186
187
Popular Tags