1 package org.enhydra.shark.xpdl.elements; 2 3 import java.util.ArrayList ; 4 import java.util.Iterator ; 5 6 import org.enhydra.shark.utilities.SequencedHashMap; 7 import org.enhydra.shark.xpdl.XMLCollection; 8 import org.enhydra.shark.xpdl.XMLComplexElement; 9 import org.enhydra.shark.xpdl.XMLElement; 10 import org.enhydra.shark.xpdl.XMLUtil; 11 12 17 public class ExtendedAttributes extends XMLCollection { 18 19 public transient SequencedHashMap eaMap; 20 21 protected String extAttribsString; 22 23 public ExtendedAttributes (XMLComplexElement parent) { 24 super(parent, false); 25 } 26 27 public XMLElement generateNewElement() { 28 return new ExtendedAttribute(this); 29 } 30 31 public ExtendedAttribute getFirstExtendedAttributeForName (String name) { 32 ExtendedAttribute ea=null; 33 ArrayList l=getElementsForName(name); 34 if (l!=null && l.size()>0) { 35 ea=(ExtendedAttribute)l.get(0); 36 } 37 return ea; 38 } 39 40 public void initCaches () { 41 super.initCaches(); 42 Iterator it=elements.iterator(); 43 while (it.hasNext()) { 44 ExtendedAttribute ea=(ExtendedAttribute)it.next(); 45 String eaName=ea.getName(); 46 ArrayList l=(ArrayList )eaMap.get(eaName); 47 if (l==null) { 48 l=new ArrayList (); 49 } 50 l.add(ea); 51 eaMap.put(eaName,l); 52 } 53 if (parent instanceof Application) { 54 getExtendedAttributesString(); 55 } 56 } 57 58 public void initExtAttribString () { 59 if (!isReadOnly) { 60 throw new RuntimeException ("This method can be used only in read-only mode!"); 61 } 62 if (this.size()>0) { 63 try { 64 extAttribsString=XMLUtil.stringifyExtendedAttributes(this); 65 } catch (Throwable thr) { 66 throw new RuntimeException ("Can't stringify extended attributes!"); 67 } 68 } else { 69 extAttribsString=""; 70 } 71 } 72 73 public void clearExtAttribString () { 74 extAttribsString=null; 75 } 76 77 public String getExtendedAttributesString () { 78 if (!isReadOnly) { 79 throw new RuntimeException ("This method can be used only in read-only mode!"); 80 } 81 if (extAttribsString==null) { 82 initExtAttribString(); 83 } 84 return extAttribsString; 85 } 86 87 public void clearCaches () { 88 eaMap=new SequencedHashMap(); 89 super.clearCaches(); 90 } 91 92 public void clear () { 93 if (eaMap!=null) { 94 eaMap.clear(); 95 } 96 super.clear(); 97 } 98 99 102 public boolean containsElement (String name) { 103 if (isReadOnly && cachesInitialized) { 104 return eaMap.containsKey(name); 105 } else { 106 Iterator it=elements.iterator(); 107 while (it.hasNext()) { 108 ExtendedAttribute ea=(ExtendedAttribute)it.next(); 109 if (ea.getName().equals(name)) { 110 return true; 111 } 112 } 113 return false; 114 } 115 } 116 117 120 public boolean containsValue (String val) { 121 Iterator it=elements.iterator(); 122 while (it.hasNext()) { 123 ExtendedAttribute ea=(ExtendedAttribute)it.next(); 124 if (ea.getVValue().equals(val)) { 125 return true; 126 } 127 } 128 return false; 129 } 130 131 134 public ArrayList getElementsForName (String name) { 135 if (isReadOnly && cachesInitialized) { 136 return (ArrayList )eaMap.get(name); 137 } else { 138 ArrayList l=new ArrayList (); 139 Iterator it=elements.iterator(); 140 while (it.hasNext()) { 141 ExtendedAttribute ea=(ExtendedAttribute)it.next(); 142 if (ea.getName().equals(name)) { 143 l.add(ea); 144 } 145 } 146 return l; 147 } 148 } 149 150 public Object clone () { 151 ExtendedAttributes d=(ExtendedAttributes)super.clone(); 152 d.extAttribsString=this.extAttribsString; 153 154 return d; 155 } 156 157 } 158 | Popular Tags |