KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Base class for implementing XMLComplexElement and XMLCollection classes.
10  *
11  * @author Sasa Bojanic
12  */

13 public abstract class XMLBaseForCollectionAndComplex extends XMLElement {
14
15    protected SequencedHashMap elementMap;
16    protected ArrayList JavaDoc elements;
17    
18    protected transient boolean cachesInitialized=false;
19    
20    public XMLBaseForCollectionAndComplex (XMLElement parent, boolean isRequired) {
21       super(parent, isRequired);
22       elements=new ArrayList JavaDoc();
23       elementMap=new SequencedHashMap();
24    }
25
26    public void setValue(String JavaDoc v) {
27       throw new RuntimeException JavaDoc("Can't set value for this type of element!");
28    }
29
30    /**
31     * Sets this element, and all contained elements to be read only or not.
32     */

33     public void setReadOnly (boolean ro) {
34        super.setReadOnly(ro);
35        Iterator JavaDoc it=elements.iterator();
36        while (it.hasNext()) {
37           XMLElement el=(XMLElement)it.next();
38           el.setReadOnly(ro);
39        }
40        if (!ro) {
41           clearCaches();
42        }
43     }
44    
45    /**
46     * Initializes caches in read-only mode. If mode is not read-only,
47     * throws RuntimeException.
48     */

49    public void initCaches () {
50       if (!isReadOnly) {
51          throw new RuntimeException JavaDoc("Caches can be initialized only in read-only mode!");
52       }
53       clearCaches();
54       Iterator JavaDoc it=elements.iterator();
55       while (it.hasNext()) {
56          XMLElement el=(XMLElement)it.next();
57          if (el instanceof XMLBaseForCollectionAndComplex) {
58             ((XMLBaseForCollectionAndComplex)el).initCaches();
59          } else if (el instanceof XMLComplexChoice) {
60             ((XMLComplexChoice)el).initCaches();
61          }
62       }
63       cachesInitialized=true;
64    }
65
66    public void clearCaches () {
67       Iterator JavaDoc it=elements.iterator();
68       while (it.hasNext()) {
69          XMLElement el=(XMLElement)it.next();
70          if (el instanceof XMLBaseForCollectionAndComplex) {
71             ((XMLBaseForCollectionAndComplex)el).clearCaches();
72          } else if (el instanceof XMLComplexChoice) {
73             ((XMLComplexChoice)el).clearCaches();
74          }
75       }
76       cachesInitialized=false;
77    }
78    
79    /** Adds new element. */
80    protected abstract void add (XMLElement el);
81
82    /** Adds new element to a certain position */
83    protected abstract boolean add (int no,XMLElement el);
84    
85    /** Removes specified element. */
86    protected int remove (XMLElement el) {
87       if (isReadOnly) {
88          throw new RuntimeException JavaDoc("Can't remove element from read only structure!");
89       }
90       int ind=elements.indexOf(el);
91       if (ind>=0) {
92          elements.remove(el);
93          elementMap.remove(el);
94       }
95       return ind;
96    }
97
98    /** Removes element at specified location. */
99    protected XMLElement remove (int no) {
100       if (isReadOnly) {
101          throw new RuntimeException JavaDoc("Can't remove element from read only structure!");
102       }
103       if (no<0 || no>=size()) return null;
104       XMLElement el=(XMLElement)elements.remove(no);
105       // here, we don't care about position
106
elementMap.remove(el);
107       return el;
108    }
109    
110    /** Returns true if there is such element in collection. */
111    public boolean contains (XMLElement el) {
112       return elements.contains(el);
113    }
114
115    /** Gets the element from specified location. */
116    public XMLElement get (int no) {
117       if (no<0 || no>=size()) return null;
118       return (XMLElement)elements.get(no);
119    }
120
121    /** Returns the number of elements. */
122    public int size () {
123       return elements.size();
124    }
125
126    /** Returns the copy of the list all elements within collection. */
127    public ArrayList JavaDoc toElements() {
128       return new ArrayList JavaDoc(elements);
129    }
130    
131    /** Returns the copy of the map of all elements within collection. */
132    public SequencedHashMap toElementMap () {
133       return new SequencedHashMap(elementMap);
134    }
135
136    public boolean equals (Object JavaDoc e) {
137       boolean equals=super.equals(e);
138       if (equals) {
139          XMLBaseForCollectionAndComplex el=(XMLBaseForCollectionAndComplex)e;
140          return (this.elements.equals(el.elements));
141       }
142       return false;
143    }
144    
145 }
146
Popular Tags