KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* XMLComplexElement.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.tree.*;
18 import javax.swing.text.*;
19 import java.util.*;
20 import org.w3c.dom.*;
21
22 /**
23  * Represents the complex XML element. For e.g., the following
24  * definition within XML schema:
25  * <pre>
26  * &lt;xsd:element name="DataField"&gt;
27  * &lt;xsd:complexType&gt;
28  * &lt;xsd:sequence&gt;
29  * &lt;xsd:element ref="DataType"/&gt;
30  * &lt;xsd:element ref="InitialValue" minOccurs="0"/&gt;
31  * &lt;xsd:element ref="Length" minOccurs="0"/&gt;
32  * &lt;xsd:element ref="Description" minOccurs="0"/&gt;
33  * &lt;xsd:element ref="ExtendedAttributes" minOccurs="0"/&gt;
34  * &lt;/xsd:sequence&gt;
35  * &lt;xsd:attribute name="Id" type="xsd:NMTOKEN" use="required"/&gt;
36  * &lt;xsd:attribute name="Name" type="xsd:string"/&gt;
37  * &lt;xsd:attribute name="IsArray" default="FALSE"&gt;
38  * &lt;xsd:simpleType&gt;
39  * &lt;xsd:restriction base="xsd:NMTOKEN"&gt;
40  * &lt;xsd:enumeration value="TRUE"/&gt;
41  * &lt;xsd:enumeration value="FALSE"/&gt;
42  * &lt;/xsd:restriction&gt;
43  * &lt;/xsd:simpleType&gt;
44  * &lt;/xsd:attribute&gt;
45  * &lt;/xsd:complexType&gt;
46  * &lt;/xsd:element&gt;
47  * </pre>
48  * will be presented as the object of the class derived from this class,
49  * and the name of the class will be DataField. The DataField object will
50  * have the other objects which are instances of XMLElement class that
51  * will be placed within it's collection {@link #complexStructure}.
52  * These elements, in this particular case, will be the instances of
53  * {@link XMLAttribute} class (that will be also placed into another
54  * collection - {@link #attributes}) which will represent the 'Id', 'Name'
55  * and 'IsArray' XML attribute, the instances of classes <b>InitialValue</b>,
56  * <b>Length</b>, <b>Description</b> which are all derived from {@link
57  * XMLSimpleElement} class and represents corresponding XML simple elements,
58  * and the instances of classes <b>DataType</b> and <b>ExtendedAttributes</b>,
59  * which are also derived from <code>XMLComplexElement</code> class.
60  * <p> The panels of elements that instance of <code>XMLComplexElement</code>
61  * class contains in it's {@link #complexStructure} collection, will be placed
62  * on it's panel, which allows it's editing.
63  * <p> When writting or reading XML document, the {@link #complexStructure}
64  * will be used to write/read all required elements.
65  * <p> The methods for setting or getting the value of each element by it's name,
66  * or by it's position within {@link #complexStructure} collectioin is provided.
67  * <p>NOTE: Although this class is not declared abstract, it is
68  * uselles without redefining it's method {@link #fillStructure}.
69  * The classes derived from this class corresponds to the
70  * complex XML element.
71  */

72 public class XMLComplexElement
73     extends XMLElement {
74
75    /**
76     * The list of all elements (attributes, and other elements) that
77     * complex element is consisted of.
78     */

79    protected java.util.List JavaDoc complexStructure = new ArrayList();
80    /** The list of attributes that complex element contains. */
81    protected java.util.List JavaDoc attributes = new ArrayList();
82
83    public XMLComplexElement() {
84       super();
85    }
86
87    /**
88     * Returns the collection of elements this element is made of.
89     */

90    public Collection toComplexType() {
91       return complexStructure;
92    }
93
94    /**
95     * Returns the collection of strings that represents elements
96     * that this element is made of.
97     */

98    public Collection toComplexTypeValues() {
99       java.util.List JavaDoc l = new ArrayList();
100       Iterator it = complexStructure.iterator();
101       while ( it.hasNext() ) {
102          XMLElement el = ( XMLElement ) it.next();
103          if ( el instanceof XMLAttribute ) {
104             l.add( el.toString() );
105          }
106          else {
107             l.add( el.toValue() );
108          }
109       }
110       return l;
111    }
112
113    /**
114     * Sets the element and all elements it is made of to be
115     * read only or not.
116     */

117    public void setReadOnly( boolean ro ) {
118       super.setReadOnly( ro );
119       Iterator it = complexStructure.iterator();
120       while ( it.hasNext() ) {
121          XMLElement el = ( XMLElement ) it.next();
122          el.setReadOnly( ro );
123       }
124    }
125
126    public boolean isEmpty() {
127       boolean isEmpty = true;
128       Iterator it = complexStructure.iterator();
129       while ( it.hasNext() ) {
130          XMLElement el = ( XMLElement ) it.next();
131          isEmpty = isEmpty && el.isEmpty();
132       }
133       return isEmpty;
134    }
135
136    public boolean isValid() {
137       boolean isValid = true;
138       Iterator it = complexStructure.iterator();
139       while ( it.hasNext() ) {
140          XMLElement el = ( XMLElement ) it.next();
141          isValid = isValid && el.isValid();
142       }
143       return isValid;
144    }
145
146    public void toXML( Node parent ) throws DOMException {
147       if ( isEmpty() && !isRequired() )
148          return;
149       if ( parent != null ) {
150          Node node = ( parent.getOwnerDocument() ).createElement( name );
151          for ( Iterator it = complexStructure.iterator(); it.hasNext(); ) {
152             XMLElement el = ( XMLElement ) it.next();
153             el.toXML( node );
154          }
155          parent.appendChild( node );
156       }
157    }
158
159    public void fromXML( Node node ) {
160       processAttributes( node );
161       processElements( node );
162    }
163
164    protected void processAttributes( Node node ) {
165       if ( node != null ) {
166          if ( node.hasAttributes() ) {
167             NamedNodeMap attribs = node.getAttributes();
168             for ( int i = 0; i < attribs.getLength(); ++i ) {
169                Node attrib = ( Node ) attribs.item( i );
170                try {
171 //System.out.println("Getting attrib "+attrib.getNodeName());
172
get( attrib.getNodeName() ).fromXML( attrib );
173                }
174                catch ( NullPointerException JavaDoc npe ) {
175                   /*if (attribute.getNodeValue().trim().length() > 0 ) {
176                      System.err.println("Processing attributes for "+ name
177                                      +" element having problems with "
178                                      + attribute.getNodeName()+" attribute\n"
179                                      + attribute.getNodeValue().trim());
180                                      }*/

181                }
182             }
183          }
184       }
185    }
186
187    protected void processElements( Node node ) {
188       if ( node != null ) {
189          if ( node.hasChildNodes() ) {
190             XMLUtil.parseElements( node, complexStructure );
191          }
192       }
193    }
194
195    public XMLPanel getPanel() {
196       XMLElement[] c = new XMLElement[complexStructure.size()];
197       complexStructure.toArray( c );
198       return new XMLGroupPanel( this, c, toLabel() );
199    }
200
201    /** Gets the element that is placed at specified no. within structure. */
202    public XMLElement get( int no ) {
203       try {
204          return ( XMLElement ) complexStructure.get( no );
205       }
206       catch ( Exception JavaDoc ex ) {
207          return null;
208       }
209    }
210
211    /**
212     * Sets the element that is placed at specified no. within structure
213     * to the specified value.
214     */

215    public void set( int no, Object JavaDoc value ) {
216       XMLElement el;
217       try {
218          el = get( no );
219       }
220       catch ( Exception JavaDoc ex ) {
221          el = null;
222       }
223       if ( el != null ) {
224          el.setValue( value );
225       }
226    }
227
228    /** Gets the element with specified name from stucture. */
229    public XMLElement get( String JavaDoc name ) {
230       Iterator it = complexStructure.iterator();
231       while ( it.hasNext() ) {
232          XMLElement el = ( XMLElement ) it.next();
233          if ( el.name.equals( name ) ) {
234             return el;
235          }
236       }
237       return null;
238    }
239
240    /**
241     * Sets the element with specified name from stucture to
242     * the specified value.
243     */

244    public void set( String JavaDoc name, Object JavaDoc value ) {
245       XMLElement el = get( name );
246       if ( el != null ) {
247          el.setValue( value );
248       }
249    }
250
251    public String JavaDoc toString() {
252       if ( labelName != null ) {
253          return labelName;
254       }
255       else {
256          return "";
257       }
258    }
259
260    /**
261     * The classes that are derived from this class has to give its
262     * definition for this method. It is used to insert all members
263     * of those classes that are derived from XMLElement
264     * class (or classes that are derived from that class) into
265     * one list, and the members that are derived from
266     * XMLAttribute class also to insert into other list.
267     * Many methods of this class uses those lists:
268     * <code>toXML</code> method uses them to properly write an element
269     * tag to the XML file,
270     * <code>fromXML</code> method uses them to properly read an element
271     * tag from the XML file,
272     * <code>getPanel</code> method uses them to properly display members
273     * of this class,
274     * <code>get</code> and <code>set</code> uses them to properly
275     * retrieve/set the elements from list, ... and so on
276     * <p>NOTE: The order of inserted elements is relevant for XML to be
277     * valid (members of classes derived from this class must be
278     * inserted into first mentioned list in the same order that
279     * they are within a corresponding tag for those classes
280     * within WfMC XML).
281     */

282    protected void fillStructure() {
283       return;
284    }
285
286    // prepeares cloning for extended classes
287
public Object JavaDoc clone() {
288       XMLComplexElement d = ( XMLComplexElement )super.clone();
289       d.complexStructure = new ArrayList();
290       d.attributes = new ArrayList();
291
292       return d;
293    }
294
295    public void refreshLabelName() {
296       super.refreshLabelName();
297       Iterator itCs = complexStructure.iterator();
298       while ( itCs.hasNext() ) {
299          XMLElement el = ( XMLElement ) itCs.next();
300          el.refreshLabelName();
301       }
302    }
303
304    /**
305     * DefaultMutableTreeNode with all subnodes of this element.
306     * @return return node with subelements.
307     */

308    public DefaultMutableTreeNode getNode() {
309       DefaultMutableTreeNode node = new ToNameMutableTreeNode( this );
310       for ( int i = 0; i < this.complexStructure.size(); i++ ) {
311          if ( !this.attributes.contains( this.complexStructure.get( i ) ) ) {
312             node.add(
313                 ( ( XMLElement )this.complexStructure.get( i ) ).getNode()
314                 );
315          }
316       }
317       return node;
318    }
319
320    /**
321     * Count child elements of this complex element.
322     * @return count of subelements.
323     */

324    public int getElementsCount() {
325       int elementsCount = 0;
326       for ( int i = 0; i < this.complexStructure.size(); i++ ) {
327          if ( !this.attributes.contains( this.complexStructure.get( i ) ) ) {
328             elementsCount++;
329          }
330       }
331       return elementsCount;
332    }
333
334    /**
335     * Return element child nodes, without attributes.
336     * @return list with subelements.
337     */

338    public List getChildElements() {
339       List list = new ArrayList();
340       for ( int i = 0; i < this.complexStructure.size(); i++ ) {
341          if ( !this.attributes.contains( this.complexStructure.get( i ) ) ) {
342             list.add( this.complexStructure.get( i ) );
343          }
344       }
345       return list;
346    }
347
348    /**
349     * Make print description of this element and all subelements and attributes.
350     * @return print description of element
351     */

352    public String JavaDoc getPrintDescription( String JavaDoc indent, StyledDocument doc ) {
353       String JavaDoc retVal = "";
354       try {
355          StringBuffer JavaDoc all = new StringBuffer JavaDoc();
356          //attributes
357
StringBuffer JavaDoc attributes = new StringBuffer JavaDoc();
358          int size = this.attributes.size();
359          for ( int i = 0; i < size; i++ ) {
360             XMLAttribute attr = ( ( XMLAttribute )this.attributes.get( i ) );
361             attributes.append( attr.toName() + " = " + attr.toString() );
362             if ( i != size - 1 ) {
363                attributes.append( ", " );
364             }
365          }
366
367          all.append( toName() + " : " + attributes.toString() );
368          doc.insertString( doc.getLength(), toName() + " : " + attributes.toString(), atts );
369
370          //only extended nodes should be displayed
371
if ( !this.isCollapsed() ) {
372             //subelements
373
String JavaDoc value = "";
374             for ( int i = 0; i < this.complexStructure.size(); i++ ) {
375                if ( !this.attributes.contains( this.complexStructure.get( i ) ) ) {
376                   value = ( ( XMLElement )this.complexStructure.get( i ) ).getPrintDescription(
377                       indent +
378                       OFFSET, doc );
379                   if ( value != null && !value.trim().equals( "" ) ) {
380                      all.append( NEWLINE );
381                      all.append( indent );
382                      all.append( value );
383                   }
384                }
385             }
386          }
387          retVal = all.toString();
388       }
389       catch ( Exception JavaDoc e ) {
390          e.printStackTrace();
391       }
392       return retVal;
393    }
394
395 }
396
Popular Tags