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 XMLCollection extends XMLBaseForCollectionAndComplex { 14 15 public XMLCollection (XMLComplexElement parent, boolean isRequired) { 16 super(parent, isRequired); 17 } 18 19 public XMLCollection (XMLComplexChoice parent, boolean isRequired) { 20 super(parent, isRequired); 21 } 22 23 public void initCaches () { 24 super.initCaches(); 25 if (elements.size()>0 && elements.get(0) instanceof XMLCollectionElement) { 26 elementMap.clear(); 27 Iterator it=elements.iterator(); 28 while (it.hasNext()) { 29 XMLCollectionElement el=(XMLCollectionElement)it.next(); 30 elementMap.put(el.getId(),el); 31 } 32 } 33 cachesInitialized=true; 34 } 35 36 public void clearCaches () { 37 super.clearCaches(); 38 elementMap.clear(); 39 } 40 41 42 public void add (XMLElement el) { 43 if (isReadOnly) { 44 throw new RuntimeException ("Can't set the value of read only element!"); 45 } 46 elements.add(el); 47 } 48 49 50 public boolean add (int no,XMLElement el) { 51 if (isReadOnly) { 52 throw new RuntimeException ("Can't set the value of read only element!"); 53 } 54 if (no<0 || no>size()) return false; 55 elements.add(no,el); 56 return true; 57 } 58 59 60 public int remove (XMLElement el) { 61 return super.remove(el); 62 } 63 64 65 protected XMLElement remove (int no) { 66 return super.remove(no); 67 } 68 69 72 public boolean isEmpty () { 73 return size()==0; 74 } 75 76 80 public XMLCollectionElement getCollectionElement (String id) { 81 if (isReadOnly && cachesInitialized) { 82 return (XMLCollectionElement)elementMap.get(id); 83 } else { 84 Iterator it=elements.iterator(); 85 while (it.hasNext()) { 86 XMLCollectionElement ce=(XMLCollectionElement)it.next(); 87 if (ce.getId().equals(id)) { 88 return ce; 89 } 90 } 91 return null; 92 } 93 } 94 95 98 public boolean containsElement (String id) { 99 return getCollectionElement(id)!=null; 100 } 101 102 103 public void clear () { 104 elements.clear(); 105 elementMap.clear(); 106 } 107 108 112 public abstract XMLElement generateNewElement(); 113 114 public Object clone () { 115 XMLCollection d=(XMLCollection)super.clone(); 116 d.elements=new ArrayList (); 117 d.elementMap=new SequencedHashMap(); 118 d.cachesInitialized=false; 119 Iterator it=this.elements.iterator(); 120 while (it.hasNext()) { 121 Object obj=it.next(); 122 XMLElement el=(XMLElement)obj; 123 XMLElement cl=(XMLElement)el.clone(); 124 cl.setParent(d); 125 d.elements.add(cl); 126 } 127 return d; 128 } 129 130 } 131 | Popular Tags |