KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > jawe > xml > XMLElement


1 /* XMLElement.java
2  *
3  * Authors:
4  * Stefanovic Nenad chupo@iis.ns.ac.yu
5  * Bojanic Sasa sasaboy@neobee.net
6  * Puskas Vladimir vpuskas@eunet.yu
7  * Pilipovic Goran zboniek@uns.ac.yu
8  *
9  */

10
11
12 package org.enhydra.jawe.xml;
13
14 import org.enhydra.jawe.xml.panels.*;
15 import org.enhydra.jawe.xml.ToNameMutableTreeNode;
16
17 import javax.swing.JPanel JavaDoc;
18 import javax.swing.JComponent JavaDoc;
19 import javax.swing.tree.*;
20 import javax.swing.text.*;
21
22 import java.io.*;
23 import java.util.*;
24 import org.w3c.dom.*;
25
26 /**
27 * XMLElement class instance represents program equivalence to
28 * the element defined in some XML XML. It contains main
29 * characteristics common for all XML elements, and is intended to
30 * be uses as the base class for classes that will represent concrete
31 * XML element such as "simple" element, "complex" element,
32 * "collection" of elements, "choice" of elements, "attribute" ...
33 * <p>When classes that extends this class are created, they
34 * can call this class constructor without arguments, in which
35 * case the member of this class that corresponds to the XML element
36 * name is filled with the name of extended class, or with the
37 * argument that represents the name of element, where such name
38 * directly sets the previously mentioned member (This is the case
39 * when creating 'attribute' or 'choice' XML elements). The label to
40 * be written in a dialog for editing value of element, is set
41 * to the language specific value, which has to be defined in
42 * the property file under the key: "<b>ElementName</b>Key".
43 * <p> This class contains method #getPanel, which
44 * should be overriden by classes that extends this class, and
45 * which is used to show the panel with the field that represent
46 * a value of XML element (In the case of complex elements, the
47 * panel will show fields of all elements that complex element
48 * is made of). This panel is shown in a dialog (see {@link
49 * XMLElementDialog}) that allows user to edit this field, and
50 * when user presses OK, the new value is put in the value member
51 * of this class.
52 * <p>When program decides to write an XML file according to
53 * it's XMLElement derived class members, it calls #toXML
54 * method of those classes, which is defined by this class, but
55 * has no implementation. This method has to write the proper
56 * element opening and closing tag (defined by element name), and the
57 * previously defined value of element to put inside tags.
58 * <p>When there is a need of reading an XML file, the values that
59 * corresponds to particular element (determined by element name
60 * and it's position within other elements) are put into this
61 * element value member by calling #fromXML method, and passing it
62 * a tag to process.
63 * <p> This class also defines methods that are performing a check
64 * for validity, emptiness or mandatory status of the element, and
65 * are also supposed to be redefined by classes that extends it.
66 * <p><p>NOTE: Although this class is not declared abstract, it is
67 * uselles without redefining it's methods.
68 */

69 public class XMLElement implements Serializable, Cloneable JavaDoc {
70
71    /**
72    * Equivalent for XML element name. Used when writting
73    * instance of this class to XML file.
74    */

75    protected String JavaDoc name;
76    /**
77    * Language specific name of XML element that is shown in
78    * editing dialog.
79    */

80    protected String JavaDoc labelName;
81    /**
82    * Supposed to contain the value for XML element.
83    * This is true for simple elements and attributes,
84    * more complex elements uses it as they need.
85    */

86    protected Object JavaDoc value;
87    /**
88    * Indicates if an element is required - corresponds
89    * to the same XML element definition.
90    */

91    protected boolean isRequired=false;
92    /** Used in dialogs to forbid editing of an element value. */
93    protected boolean isReadOnly=false;
94
95    /** Flag that say is node is collapsed */
96    protected boolean isCollapsed = true;
97
98 //private static int genEl=0;
99
/**
100    * Creates a new instance of element: sets the <code>name</code> member
101    * to the name of inner class that extends this element, and the
102    * <code>labelName</code> member to the value read from property file
103    * under the key of previously set <code>name</code> member.
104    */

105    public XMLElement () {
106       name=getClass().getName();
107       int lastDot=name.lastIndexOf(".");
108       if (lastDot>=0) {
109          name=name.substring(lastDot+1,name.length());
110       }
111
112       labelName=XMLUtil.getLanguageDependentString(name+"Key");
113       value=new String JavaDoc();
114 //System.out.println("Generated element no. "+(++genEl));
115
}
116
117    /**
118    * Creates a new instance of element: sets the <code>name</code> member
119    * to the specified one, and the <code>labelName</code> member to the
120    * value read from property file under the key of previously set
121    * <code>name</code> member.
122    *
123    * @param name The name of XML element that this class represents.
124    */

125    public XMLElement (String JavaDoc name) {
126       this.name=name;
127       this.labelName=XMLUtil.getLanguageDependentString(name+"Key");
128       value=new String JavaDoc();
129 //System.out.println("Generated element no. "+(++genEl));
130
}
131
132    /**
133    * Sets the 'read only' property of element to the specified one.
134    * This enables/disables editing of the element within dialog.
135    * Some derived classes (especially class that represents complex
136    * element) need to redefine this method.
137    *
138    * @param ro <tt>true</tt> if element will be 'read only',
139    * <tt>false</tt> otherwise.
140    */

141    public void setReadOnly (boolean ro) {
142       isReadOnly=ro;
143    }
144
145    /**
146    * Returns the 'read only' status of element.
147    * This indicates if the editing of element within dialog
148    * is enabled or disabled.
149    *
150    * @return <tt>true</tt> if element is 'read only',
151    * <tt>false</tt> otherwise.
152    */

153    public boolean isReadOnly () {
154       return isReadOnly;
155    }
156
157    /**
158    * Sets the 'required' property of the element.
159    * If element is 'required', the dialog for editing
160    * it's value can't be closed until the value is defined.
161    *
162    * @param r <tt>true</tt> if element will be 'required',
163    * <tt>false</tt> otherwise.
164    */

165    public void setRequired (boolean r) {
166       isRequired=r;
167    }
168
169    /**
170    * Returns the 'required' property of the element.
171    * If element is 'required', the dialog for editing
172    * it's value can't be closed until the value is defined.
173    *
174    * @return <tt>true</tt> if element is 'required',
175    * <tt>false</tt> otherwise.
176    */
public boolean isRequired () {
177       return isRequired;
178    }
179
180    /**
181    * Indicates if element is empty. Some derived classes
182    * (especially class that represents complex element) need to
183    * redefine this method.
184    *
185    * @return <tt>true</tt> if user haven't defined the element
186    * value within dialog, <tt>false</tt> otherwise.
187    */

188    public boolean isEmpty () {
189       return !(value!=null && value.toString().trim().length()>0);
190    }
191
192    /**
193    * Indicates if element is valid in XML XML sense.
194    * This implementation claims that element is always valid. The derived
195    * classes has to implement it's own definition of validity.
196    *
197    * @return <tt>true</tt> if element is valid, <tt>false</tt> otherwise.
198    */

199    public boolean isValid () {
200       return true;
201    }
202
203    /**
204    * Indicates if dialog data enter for element is valid.
205    * This implementation claims that element is always valid. The derived
206    * classes has to implement it's own definition of validity.
207    *
208    * @return <tt>true</tt> if dialog data for the element are valid,
209    * <tt>false</tt> otherwise.
210    */

211    public boolean isValidEnter (XMLPanel p) {
212       return true;
213    }
214
215    /**
216    * Supposed to be overriden by derived classes to implement
217    * element specific writting to an XML file.
218    *
219    * @param parent parent node
220    * @return The text for a corresponding WfMC XML element tag.
221    */

222    public void toXML(Node parent) throws DOMException {
223       // external parser
224
if (parent!=null) {
225          if (null != value) {
226             Node node = (parent.getOwnerDocument()).createElement(name);
227             node.setNodeValue(toString());
228             parent.appendChild(node);
229          }
230       }
231    }
232
233    /**
234    * Supposed to be overriden by derived classes to implement
235    * element specific reading from an XML file. This implementation
236    * satisfies only "simple" elements.
237    *
238    * @param node root node
239    */

240    public void fromXML(Node node) {
241       if (node!=null) {
242 //System.out.println("Node is "+node.getNodeName()+", value is="+node.getNodeValue());
243
Object JavaDoc newVal;
244          if (node.hasChildNodes()) {
245             newVal=node.getChildNodes().item(0).getNodeValue();
246          // should never happen
247
} else {
248             newVal=node.getNodeValue();
249          }
250          if (newVal!=null) {
251             value=newVal;
252          }
253       }
254    }
255
256    /**
257    * Supposed to be used (and overriden) by class that
258    * represent XML choice of complex elements.
259    *
260    * @param name The element name.
261    * @param node root node
262    */

263    public void fromXML(String JavaDoc name,Node node) {
264       try {
265          this.name=name;
266          this.labelName=XMLUtil.getLanguageDependentString(name+"Key");
267       } catch (Exception JavaDoc ex) {}
268       fromXML(node);
269    }
270
271    /**
272    * Returns the panel for editing the element. This implementation
273    * returns an empty basic panel, the derived classes should implement
274    * this method to suite their needs.
275    *
276    * @return A panel to put into dialog for editing element.
277    */

278    public XMLPanel getPanel () {
279       return new XMLPanel();
280    }
281
282    public void setLabelName (String JavaDoc lName) {
283       labelName=lName;
284    }
285
286    /**
287    * Sets the element value (in the sense of XML element).
288    *
289    * @param v The value to set.
290    */

291    public void setValue(Object JavaDoc v) {
292       value=v;
293    }
294
295    /**
296    * Gets the element value. The value represents an XML
297    * tag content for this element.
298    *
299    * @return The value of this element.
300    */

301    public Object JavaDoc toValue() {
302       return value;
303    }
304
305    /**
306    * Returns the text for element 'name' presentation within
307    * a dialog for editing element. This text is language
308    * specific and is set reading a property file.
309    *
310    * @return The language specific presentation of element 'name'.
311    */

312    public String JavaDoc toLabel () {
313       return labelName;
314    }
315
316    /**
317    * Returns the name of element.
318    *
319    * @return The 'name' attribute of element.
320    */

321    public String JavaDoc toName () {
322       return name;
323    }
324
325    /**
326    * Returns the text representation of element's 'value'.
327    * This method only calls <code>toString</code> method
328    * of it's value member, if value is <code>null</code>,
329    * returns labelName member.
330    * @return The value's string representation.
331    */

332    public String JavaDoc toString () {
333       if (value!=null) {
334          return value.toString().trim();
335       } else if (labelName!=null) {
336          return labelName;
337       } else {
338          return "";
339       }
340    }
341
342    /**
343    * Used to create exact copy of instance of this class.
344    * The newly created instance will have the same name,
345    * label name and value as the copied instance. Also it
346    * will have the same 'read only' and 'required' properties
347    * as the copied one.
348    *
349    * @return The newly created instance of XMLElement class.
350    */

351    public Object JavaDoc clone () {
352       XMLElement d=null;
353       try {
354          //System.out.println("Cloning XMLELement "+name);
355
d=(XMLElement)super.clone();
356          d.name=new String JavaDoc(this.name);
357          if (this.labelName!=null) {
358             d.labelName=new String JavaDoc(this.labelName);
359          } else {
360             d.labelName=null;
361          }
362          if (value instanceof XMLElement) {
363             d.value=((XMLElement)value).clone();
364          } else {
365             if (this.value!=null) {
366                d.value=new String JavaDoc(this.value.toString());
367             } else {
368                this.value=null;
369             }
370          }
371          d.isRequired=this.isRequired;
372          d.isReadOnly=this.isReadOnly;
373       } catch (CloneNotSupportedException JavaDoc e) {
374          // Won't happen because we implement Cloneable
375
throw new Error JavaDoc(e.toString());
376       }
377       return d;
378    }
379
380    public void refreshLabelName() {
381       labelName=XMLUtil.getLanguageDependentString(name+"Key");
382    }
383
384    /**
385     * Make tree node for this element and all subelements.
386     * @return tree node for this element.
387     */

388    public DefaultMutableTreeNode getNode() {
389       return new ToNameMutableTreeNode(this);
390    }
391
392    protected String JavaDoc OFFSET = " ";
393    protected String JavaDoc NEWLINE = "\n";
394    protected AttributeSet atts = new SimpleAttributeSet();
395
396    /**
397     * This method will call getPrintDescription(String indent) with parameter OFFSET.
398     * This method is called on root node from which description will be maked.
399     * @return Description of this element and all subelements.
400     */

401    public String JavaDoc getPrintDescription(StyledDocument doc) {
402       return this.getPrintDescription(OFFSET, doc);
403    }
404
405    /**
406     * Description of this element and all subelements.
407     * @return return print description of element.
408     */

409    public String JavaDoc getPrintDescription(String JavaDoc indent, StyledDocument doc) {
410       String JavaDoc retVal = "";
411       try {
412          StringBuffer JavaDoc all = new StringBuffer JavaDoc();
413          all.append( toName() + " : " + value.toString().replaceAll( "\n", " " ) );
414          retVal = all.toString();
415
416          AttributeSet atts = new SimpleAttributeSet();
417          doc.insertString( doc.getLength(), retVal, atts );
418
419       }catch(Exception JavaDoc e) {
420          e.printStackTrace();
421       }
422
423       return retVal;
424    }
425
426    public void setCollapsed(boolean isCollapsed) {
427       this.isCollapsed = isCollapsed;
428    }
429
430    public boolean isCollapsed() {
431       return this.isCollapsed;
432    }
433
434
435 }
436
437 /* End of XMLElement.java */
438
Popular Tags