KickJava   Java API By Example, From Geeks To Geeks.

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


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

11 public abstract class XMLComplexChoice extends XMLElement {
12
13    protected ArrayList JavaDoc choices;
14
15    protected XMLElement choosen;
16
17    protected transient boolean cachesInitialized=false;
18
19    public XMLComplexChoice(XMLComplexElement parent, String JavaDoc name, boolean isRequired) {
20       super(parent, name, isRequired);
21       fillChoices();
22    }
23
24    public void setValue(String JavaDoc v) {
25       throw new RuntimeException JavaDoc("Can't set value for this type of element!");
26    }
27
28    /**
29     * Overrides super-method to set this element and all of its
30     * choice elements read only value to the one specified.
31     */

32    public void setReadOnly (boolean ro) {
33       super.setReadOnly(ro);
34       for (int i = 0; i < choices.size(); i++) {
35          ((XMLElement) choices.get(i)).setReadOnly(ro);
36       }
37    }
38
39    /**
40     * Initializes caches in read-only mode. If mode is not read-only,
41     * throws RuntimeException.
42     */

43    public void initCaches () {
44       if (!isReadOnly) {
45          throw new RuntimeException JavaDoc("Caches can be initialized only in read-only mode!");
46       }
47       clearCaches();
48       Iterator JavaDoc it=choices.iterator();
49       while (it.hasNext()) {
50          XMLElement el=(XMLElement)it.next();
51          if (el instanceof XMLBaseForCollectionAndComplex) {
52             ((XMLBaseForCollectionAndComplex)el).initCaches();
53          } else if (el instanceof XMLComplexChoice) {
54             ((XMLComplexChoice)el).initCaches();
55          }
56       }
57       cachesInitialized=true;
58    }
59
60    public void clearCaches () {
61       Iterator JavaDoc it=choices.iterator();
62       while (it.hasNext()) {
63          XMLElement el=(XMLElement)it.next();
64          if (el instanceof XMLBaseForCollectionAndComplex) {
65             ((XMLBaseForCollectionAndComplex)el).clearCaches();
66          } else if (el instanceof XMLComplexChoice) {
67             ((XMLComplexChoice)el).clearCaches();
68          }
69       }
70       cachesInitialized=false;
71    }
72    
73    public boolean isEmpty() {
74       return (choosen instanceof XMLEmptyChoiceElement);
75    }
76
77    /**
78     * The possible choices - instances of XMLElement class.
79     *
80     * @return the possible choices for this element.
81     */

82    public ArrayList JavaDoc getChoices() {
83       return choices;
84    }
85
86    public XMLElement getChoosen() {
87       return choosen;
88    }
89
90    public void setChoosen(XMLElement ch) {
91       if (isReadOnly) {
92          throw new RuntimeException JavaDoc("Can't set the value of read only element!");
93       }
94       if (!choices.contains(ch)) {
95          throw new RuntimeException JavaDoc("Incorrect value! The possible values are: " + choices);
96       }
97       choosen = ch;
98    }
99
100    protected abstract void fillChoices ();
101
102    public Object JavaDoc clone() {
103       XMLComplexChoice d = (XMLComplexChoice) super.clone();
104
105       d.choices = new ArrayList JavaDoc();
106       d.choosen = null;
107       d.cachesInitialized=false;
108       Iterator JavaDoc it=choices.iterator();
109       while (it.hasNext()) {
110          XMLElement c=(XMLElement)it.next();
111          XMLElement cloned = (XMLElement) c.clone();
112          d.choices.add(cloned);
113          cloned.setParent(d);
114          if (d.choosen==null && this.choosen.equals(c)) {
115             d.choosen = cloned;
116          }
117       }
118       return d;
119    }
120
121    public boolean equals (Object JavaDoc e) {
122       boolean equals=super.equals(e);
123       if (equals) {
124          XMLComplexChoice el=(XMLComplexChoice)e;
125          return (this.choices.equals(el.choices) && this.choosen.equals(el.choosen));
126       }
127       return false;
128    }
129
130 }
131
Popular Tags