1 package org.enhydra.shark.xpdl; 2 3 import java.util.ArrayList ; 4 import java.util.Iterator ; 5 6 11 public abstract class XMLComplexChoice extends XMLElement { 12 13 protected ArrayList choices; 14 15 protected XMLElement choosen; 16 17 protected transient boolean cachesInitialized=false; 18 19 public XMLComplexChoice(XMLComplexElement parent, String name, boolean isRequired) { 20 super(parent, name, isRequired); 21 fillChoices(); 22 } 23 24 public void setValue(String v) { 25 throw new RuntimeException ("Can't set value for this type of element!"); 26 } 27 28 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 43 public void initCaches () { 44 if (!isReadOnly) { 45 throw new RuntimeException ("Caches can be initialized only in read-only mode!"); 46 } 47 clearCaches(); 48 Iterator 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 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 82 public ArrayList 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 ("Can't set the value of read only element!"); 93 } 94 if (!choices.contains(ch)) { 95 throw new RuntimeException ("Incorrect value! The possible values are: " + choices); 96 } 97 choosen = ch; 98 } 99 100 protected abstract void fillChoices (); 101 102 public Object clone() { 103 XMLComplexChoice d = (XMLComplexChoice) super.clone(); 104 105 d.choices = new ArrayList (); 106 d.choosen = null; 107 d.cachesInitialized=false; 108 Iterator 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 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 |