001: package org.enhydra.shark.xpdl;
002:
003: import java.util.ArrayList;
004: import java.util.Iterator;
005:
006: import org.enhydra.shark.utilities.SequencedHashMap;
007:
008: /**
009: * Base class for implementing XMLComplexElement and XMLCollection classes.
010: *
011: * @author Sasa Bojanic
012: */
013: public abstract class XMLBaseForCollectionAndComplex extends XMLElement {
014:
015: protected SequencedHashMap elementMap;
016: protected ArrayList elements;
017:
018: protected transient boolean cachesInitialized = false;
019:
020: public XMLBaseForCollectionAndComplex(XMLElement parent,
021: boolean isRequired) {
022: super (parent, isRequired);
023: elements = new ArrayList();
024: elementMap = new SequencedHashMap();
025: }
026:
027: public XMLBaseForCollectionAndComplex(XMLElement parent,
028: String name, boolean isRequired) {
029: super (parent, name, isRequired);
030: elements = new ArrayList();
031: elementMap = new SequencedHashMap();
032: }
033:
034: public void setValue(String v) {
035: throw new RuntimeException(
036: "Can't set value for this type of element!");
037: }
038:
039: /**
040: * Sets this element, and all contained elements to be read only or not.
041: */
042: public void setReadOnly(boolean ro) {
043: super .setReadOnly(ro);
044: Iterator it = elements.iterator();
045: while (it.hasNext()) {
046: XMLElement el = (XMLElement) it.next();
047: el.setReadOnly(ro);
048: }
049: if (!ro) {
050: clearCaches();
051: }
052: }
053:
054: public void setNotifyMainListeners(boolean notify) {
055: super .setNotifyMainListeners(notify);
056: Iterator it = elements.iterator();
057: while (it.hasNext()) {
058: XMLElement el = (XMLElement) it.next();
059: el.setNotifyMainListeners(notify);
060: }
061: }
062:
063: public void setNotifyListeners(boolean notify) {
064: super .setNotifyListeners(notify);
065: Iterator it = elements.iterator();
066: while (it.hasNext()) {
067: XMLElement el = (XMLElement) it.next();
068: el.setNotifyListeners(notify);
069: }
070: }
071:
072: /**
073: * Initializes caches in read-only mode. If mode is not read-only,
074: * throws RuntimeException.
075: */
076: public void initCaches() {
077: if (!isReadOnly) {
078: throw new RuntimeException(
079: toName()
080: + ": Caches can be initialized only in read-only mode!");
081: }
082: clearCaches();
083: Iterator it = elements.iterator();
084: while (it.hasNext()) {
085: XMLElement el = (XMLElement) it.next();
086: if (el instanceof XMLBaseForCollectionAndComplex) {
087: ((XMLBaseForCollectionAndComplex) el).initCaches();
088: } else if (el instanceof XMLComplexChoice) {
089: ((XMLComplexChoice) el).initCaches();
090: }
091: }
092: cachesInitialized = true;
093: }
094:
095: public void clearCaches() {
096: Iterator it = elements.iterator();
097: while (it.hasNext()) {
098: XMLElement el = (XMLElement) it.next();
099: if (el instanceof XMLBaseForCollectionAndComplex) {
100: ((XMLBaseForCollectionAndComplex) el).clearCaches();
101: } else if (el instanceof XMLComplexChoice) {
102: ((XMLComplexChoice) el).clearCaches();
103: }
104: }
105: cachesInitialized = false;
106: }
107:
108: /** Adds new element. */
109: protected abstract void add(XMLElement el);
110:
111: /** Adds new element to a certain position */
112: protected abstract boolean add(int no, XMLElement el);
113:
114: /** Returns true if there is such element in collection. */
115: public boolean contains(XMLElement el) {
116: return elements.contains(el);
117: }
118:
119: /** Gets the element from specified location. */
120: public XMLElement get(int no) {
121: if (no < 0 || no >= size())
122: throw new RuntimeException(
123: "There is no element at position " + no + "!");
124:
125: return (XMLElement) elements.get(no);
126: }
127:
128: /** Returns the number of elements. */
129: public int size() {
130: return elements.size();
131: }
132:
133: /** Returns the copy of the list all elements within collection. */
134: public ArrayList toElements() {
135: return new ArrayList(elements);
136: }
137:
138: /** Returns the copy of the map of all elements within collection. */
139: public SequencedHashMap toElementMap() {
140: return new SequencedHashMap(elementMap);
141: }
142:
143: public boolean equals(Object e) {
144: boolean equals = super .equals(e);
145: if (equals) {
146: XMLBaseForCollectionAndComplex el = (XMLBaseForCollectionAndComplex) e;
147: equals = (this .elements.equals(el.elements));
148: // System.out.println("The subelements equal - "+equals);
149: //return (this.elements.equals(el.elements));
150: }
151: return equals;
152: }
153:
154: }
|