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 XMLComplexElement extends XMLBaseForCollectionAndComplex { 14 15 public XMLComplexElement(XMLElement parent, boolean isRequired) { 16 super(parent, isRequired); 17 fillStructure(); 18 } 19 20 protected void add (XMLElement el) { 21 elements.add(el); 22 elementMap.put(el.toName(),el); 23 } 24 25 protected boolean add (int no,XMLElement el) { 26 if (no<0 || no>size()) return false; 27 elements.add(no,el); 28 elementMap.put(el.toName(),el); 30 return true; 31 } 32 33 34 38 public boolean isEmpty () { 39 boolean isEmpty = true; 40 Iterator it = elements.iterator(); 41 while ( it.hasNext() ) { 42 XMLElement el = ( XMLElement ) it.next(); 43 isEmpty = isEmpty && el.isEmpty(); 44 } 45 isEmpty=isEmpty && value.trim().length()==0; 46 return isEmpty; 47 } 48 49 52 public ArrayList getXMLElements () { 53 ArrayList els=new ArrayList (); 54 Iterator it=elements.iterator(); 55 while (it.hasNext()) { 56 Object el=it.next(); 57 if (!(el instanceof XMLAttribute)) { 58 els.add(el); 59 } 60 } 61 return els; 62 } 63 64 67 public ArrayList getXMLAttributes () { 68 ArrayList attribs=new ArrayList (); 69 Iterator it=elements.iterator(); 70 while (it.hasNext()) { 71 Object el=it.next(); 72 if (el instanceof XMLAttribute) { 73 attribs.add(el); 74 } 75 } 76 return attribs; 77 } 78 79 80 84 public void set(String name,String value) { 85 if (isReadOnly) { 86 throw new RuntimeException ("Can't change element from read only structure!"); 87 } 88 XMLElement el = get(name); 89 if (el!=null) { 90 el.setValue(value); 91 } else { 92 throw new RuntimeException ("No such element!"); 93 } 94 } 95 96 100 public boolean set(int no,String value) { 101 if (no<0 || no>=size()) return false; 102 if (isReadOnly) { 103 throw new RuntimeException ("Can't change element from read only structure!"); 104 } 105 XMLElement el=(XMLElement)get(no); 106 el.setValue(value); 107 return true; 108 } 109 110 111 public XMLElement get (String name) { 112 return (XMLElement)elementMap.get(name); 113 } 114 115 116 public boolean containsName (String name) { 117 return elementMap.containsKey(name); 118 } 119 120 130 protected abstract void fillStructure(); 131 132 public Object clone() { 133 XMLComplexElement d = (XMLComplexElement)super.clone(); 134 d.elements = new ArrayList (); 135 d.elementMap = new SequencedHashMap(); 136 d.cachesInitialized=false; 137 Iterator it=elements.iterator(); 138 while (it.hasNext()) { 139 XMLElement el=(XMLElement)it.next(); 140 XMLElement cl=(XMLElement)el.clone(); 141 cl.setParent(d); 142 d.elements.add(cl); 143 d.elementMap.put(cl.toName(),cl); 144 } 145 return d; 146 } 147 148 } 149 | Popular Tags |