KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > xpdl > elements > ExtendedAttributes


1 package org.enhydra.shark.xpdl.elements;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Iterator JavaDoc;
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 /**
13  * Represents coresponding element from XPDL schema.
14  *
15  * @author Sasa Bojanic
16  */

17 public class ExtendedAttributes extends XMLCollection {
18
19    public transient SequencedHashMap eaMap;
20    
21    protected String JavaDoc 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 JavaDoc name) {
32       ExtendedAttribute ea=null;
33       ArrayList JavaDoc 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 JavaDoc it=elements.iterator();
43       while (it.hasNext()) {
44          ExtendedAttribute ea=(ExtendedAttribute)it.next();
45          String JavaDoc eaName=ea.getName();
46          ArrayList JavaDoc l=(ArrayList JavaDoc)eaMap.get(eaName);
47          if (l==null) {
48             l=new ArrayList JavaDoc();
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 JavaDoc("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 JavaDoc thr) {
66             throw new RuntimeException JavaDoc("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 JavaDoc getExtendedAttributesString () {
78       if (!isReadOnly) {
79          throw new RuntimeException JavaDoc("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    /**
100     * Returns true if there is at least one ExtendedAttribute with such name.
101     */

102    public boolean containsElement (String JavaDoc name) {
103       if (isReadOnly && cachesInitialized) {
104          return eaMap.containsKey(name);
105       } else {
106          Iterator JavaDoc 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    /**
118     * Returns true if there is at least one ExtendedAttribute with such value.
119     */

120    public boolean containsValue (String JavaDoc val) {
121       Iterator JavaDoc 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    /**
132     * Returns all elements with specified name.
133     */

134    public ArrayList JavaDoc getElementsForName (String JavaDoc name) {
135       if (isReadOnly && cachesInitialized) {
136          return (ArrayList JavaDoc)eaMap.get(name);
137       } else {
138          ArrayList JavaDoc l=new ArrayList JavaDoc();
139          Iterator JavaDoc 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 JavaDoc clone () {
151       ExtendedAttributes d=(ExtendedAttributes)super.clone();
152       d.extAttribsString=this.extAttribsString;
153       
154       return d;
155    }
156    
157 }
158
Popular Tags