1 package org.enhydra.shark.xpdl; 2 3 import java.util.ArrayList ; 4 import java.util.Iterator ; 5 6 import org.enhydra.shark.utilities.SequencedHashMap; 7 8 13 public abstract class XMLBaseForCollectionAndComplex extends XMLElement { 14 15 protected SequencedHashMap elementMap; 16 protected ArrayList elements; 17 18 protected transient boolean cachesInitialized=false; 19 20 public XMLBaseForCollectionAndComplex (XMLElement parent, boolean isRequired) { 21 super(parent, isRequired); 22 elements=new ArrayList (); 23 elementMap=new SequencedHashMap(); 24 } 25 26 public void setValue(String v) { 27 throw new RuntimeException ("Can't set value for this type of element!"); 28 } 29 30 33 public void setReadOnly (boolean ro) { 34 super.setReadOnly(ro); 35 Iterator 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 49 public void initCaches () { 50 if (!isReadOnly) { 51 throw new RuntimeException ("Caches can be initialized only in read-only mode!"); 52 } 53 clearCaches(); 54 Iterator 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 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 80 protected abstract void add (XMLElement el); 81 82 83 protected abstract boolean add (int no,XMLElement el); 84 85 86 protected int remove (XMLElement el) { 87 if (isReadOnly) { 88 throw new RuntimeException ("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 99 protected XMLElement remove (int no) { 100 if (isReadOnly) { 101 throw new RuntimeException ("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 elementMap.remove(el); 107 return el; 108 } 109 110 111 public boolean contains (XMLElement el) { 112 return elements.contains(el); 113 } 114 115 116 public XMLElement get (int no) { 117 if (no<0 || no>=size()) return null; 118 return (XMLElement)elements.get(no); 119 } 120 121 122 public int size () { 123 return elements.size(); 124 } 125 126 127 public ArrayList toElements() { 128 return new ArrayList (elements); 129 } 130 131 132 public SequencedHashMap toElementMap () { 133 return new SequencedHashMap(elementMap); 134 } 135 136 public boolean equals (Object 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 |