KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.enhydra.shark.xpdl;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Iterator JavaDoc;
5
6 import org.enhydra.shark.utilities.SequencedHashMap;
7
8 /**
9  * Class that represents complex element from XML schema.
10  *
11  * @author Sasa Bojanic
12  */

13 public abstract class XMLComplexElement extends XMLBaseForCollectionAndComplex {
14
15    public XMLComplexElement(XMLElement parent, boolean isRequired) {
16       super(parent, isRequired);
17       fillStructure();
18    }
19
20    protected void add (XMLElement el) {
21       elements.add(el);
22       elementMap.put(el.toName(),el);
23    }
24
25    protected boolean add (int no,XMLElement el) {
26       if (no<0 || no>size()) return false;
27       elements.add(no,el);
28       // here, we don't care about position
29
elementMap.put(el.toName(),el);
30       return true;
31    }
32    
33    
34    /**
35     * It is empty if its value is not set, and if
36     * all elements in the structure are empty.
37     */

38    public boolean isEmpty () {
39       boolean isEmpty = true;
40       Iterator JavaDoc it = elements.iterator();
41       while ( it.hasNext() ) {
42          XMLElement el = ( XMLElement ) it.next();
43          isEmpty = isEmpty && el.isEmpty();
44       }
45       isEmpty=isEmpty && value.trim().length()==0;
46       return isEmpty;
47    }
48
49    /**
50     * Returns the collection of XML elements this element is made of.
51     */

52    public ArrayList JavaDoc getXMLElements () {
53       ArrayList JavaDoc els=new ArrayList JavaDoc();
54       Iterator JavaDoc it=elements.iterator();
55       while (it.hasNext()) {
56          Object JavaDoc el=it.next();
57          if (!(el instanceof XMLAttribute)) {
58             els.add(el);
59          }
60       }
61       return els;
62    }
63
64    /**
65     * Returns the collection of XML attributes this element is made of.
66     */

67    public ArrayList JavaDoc getXMLAttributes () {
68       ArrayList JavaDoc attribs=new ArrayList JavaDoc();
69       Iterator JavaDoc it=elements.iterator();
70       while (it.hasNext()) {
71          Object JavaDoc el=it.next();
72          if (el instanceof XMLAttribute) {
73             attribs.add(el);
74          }
75       }
76       return attribs;
77    }
78
79
80    /**
81     * Sets the element from structure with specified name to
82     * the specified value.
83     */

84    public void set(String JavaDoc name,String JavaDoc value) {
85       if (isReadOnly) {
86          throw new RuntimeException JavaDoc("Can't change element from read only structure!");
87       }
88       XMLElement el = get(name);
89       if (el!=null) {
90          el.setValue(value);
91       } else {
92          throw new RuntimeException JavaDoc("No such element!");
93       }
94    }
95
96    /**
97     * Sets the element that is placed at specified location within structure
98     * to the specified value.
99     */

100    public boolean set(int no,String JavaDoc value) {
101       if (no<0 || no>=size()) return false;
102       if (isReadOnly) {
103          throw new RuntimeException JavaDoc("Can't change element from read only structure!");
104       }
105       XMLElement el=(XMLElement)get(no);
106       el.setValue(value);
107       return true;
108    }
109
110    /** Gets the element with specified name from stucture. */
111    public XMLElement get (String JavaDoc name) {
112       return (XMLElement)elementMap.get(name);
113    }
114
115    /** Returns true if there is an element with such element in structure. */
116    public boolean containsName (String JavaDoc name) {
117       return elementMap.containsKey(name);
118    }
119    
120    /**
121     * The classes that are derived from this class has to give its
122     * definition for this method. It is used to insert all members
123     * of those classes that are derived from XMLElement.
124     * <p>NOTE: The order of inserted elements is relevant for XML to be
125     * valid (members of classes derived from this class must be
126     * inserted into first mentioned list in the same order that
127     * they are within a corresponding tag for those classes
128     * within WfMC XML).
129     */

130    protected abstract void fillStructure();
131
132    public Object JavaDoc clone() {
133       XMLComplexElement d = (XMLComplexElement)super.clone();
134       d.elements = new ArrayList JavaDoc();
135       d.elementMap = new SequencedHashMap();
136       d.cachesInitialized=false;
137       Iterator JavaDoc it=elements.iterator();
138       while (it.hasNext()) {
139          XMLElement el=(XMLElement)it.next();
140          XMLElement cl=(XMLElement)el.clone();
141          cl.setParent(d);
142          d.elements.add(cl);
143          d.elementMap.put(cl.toName(),cl);
144       }
145       return d;
146    }
147    
148 }
149
Popular Tags