KickJava   Java API By Example, From Geeks To Geeks.

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


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 collection of elements from XML schema.
10  *
11  * @author Sasa Bojanic
12  */

13 public abstract class XMLCollection extends XMLBaseForCollectionAndComplex {
14
15    public XMLCollection (XMLComplexElement parent, boolean isRequired) {
16       super(parent, isRequired);
17    }
18
19    public XMLCollection (XMLComplexChoice parent, boolean isRequired) {
20       super(parent, isRequired);
21    }
22
23    public void initCaches () {
24       super.initCaches();
25       if (elements.size()>0 && elements.get(0) instanceof XMLCollectionElement) {
26          elementMap.clear();
27          Iterator JavaDoc it=elements.iterator();
28          while (it.hasNext()) {
29             XMLCollectionElement el=(XMLCollectionElement)it.next();
30             elementMap.put(el.getId(),el);
31          }
32       }
33       cachesInitialized=true;
34    }
35    
36    public void clearCaches () {
37       super.clearCaches();
38       elementMap.clear();
39    }
40
41    /** NOTE: Method signature changed to public. */
42    public void add (XMLElement el) {
43       if (isReadOnly) {
44          throw new RuntimeException JavaDoc("Can't set the value of read only element!");
45       }
46       elements.add(el);
47    }
48
49    /** NOTE: Method signature changed to public. */
50    public boolean add (int no,XMLElement el) {
51       if (isReadOnly) {
52          throw new RuntimeException JavaDoc("Can't set the value of read only element!");
53       }
54       if (no<0 || no>size()) return false;
55       elements.add(no,el);
56       return true;
57    }
58
59    /** NOTE: Method signature changed to public. */
60    public int remove (XMLElement el) {
61       return super.remove(el);
62    }
63
64    /** NOTE: Method signature changed to public. */
65    protected XMLElement remove (int no) {
66       return super.remove(no);
67    }
68    
69    /**
70     * Returns <tt>true</tt> if there are no elements within collection.
71     */

72    public boolean isEmpty () {
73       return size()==0;
74    }
75    
76    /**
77     * Returns the element specified by Id attribute.
78     * Use only if this is collection of XMLCollectionElements.
79     */

80    public XMLCollectionElement getCollectionElement (String JavaDoc id) {
81       if (isReadOnly && cachesInitialized) {
82          return (XMLCollectionElement)elementMap.get(id);
83       } else {
84          Iterator JavaDoc it=elements.iterator();
85          while (it.hasNext()) {
86             XMLCollectionElement ce=(XMLCollectionElement)it.next();
87             if (ce.getId().equals(id)) {
88                return ce;
89             }
90          }
91          return null;
92       }
93    }
94
95    /**
96     * Returns true if element with given Id exists in collection.
97     */

98    public boolean containsElement (String JavaDoc id) {
99       return getCollectionElement(id)!=null;
100    }
101
102    /** Clears the collection. */
103    public void clear () {
104       elements.clear();
105       elementMap.clear();
106    }
107    
108    /**
109     * Generates the new element that made collection. Derived classes
110     * has to implement this method to create it's collection element.
111     */

112    public abstract XMLElement generateNewElement();
113       
114    public Object JavaDoc clone () {
115       XMLCollection d=(XMLCollection)super.clone();
116       d.elements=new ArrayList JavaDoc();
117       d.elementMap=new SequencedHashMap();
118       d.cachesInitialized=false;
119       Iterator JavaDoc it=this.elements.iterator();
120       while (it.hasNext()) {
121          Object JavaDoc obj=it.next();
122          XMLElement el=(XMLElement)obj;
123          XMLElement cl=(XMLElement)el.clone();
124          cl.setParent(d);
125          d.elements.add(cl);
126       }
127       return d;
128    }
129    
130 }
131
Popular Tags