001: package org.enhydra.shark.xpdl.elements;
002:
003: import java.util.ArrayList;
004: import java.util.Iterator;
005:
006: import org.enhydra.shark.utilities.SequencedHashMap;
007: import org.enhydra.shark.xpdl.XMLCollection;
008: import org.enhydra.shark.xpdl.XMLComplexElement;
009: import org.enhydra.shark.xpdl.XMLElement;
010: import org.enhydra.shark.xpdl.XMLUtil;
011:
012: /**
013: * Represents coresponding element from XPDL schema.
014: *
015: * @author Sasa Bojanic
016: */
017: public class ExtendedAttributes extends XMLCollection {
018:
019: public transient SequencedHashMap eaMap;
020:
021: protected String extAttribsString;
022:
023: public ExtendedAttributes(XMLComplexElement parent) {
024: super (parent, false);
025: }
026:
027: public void makeAs(XMLElement el) {
028: super .makeAs(el);
029: this .extAttribsString = ((ExtendedAttributes) el).extAttribsString;
030: }
031:
032: public XMLElement generateNewElement() {
033: return new ExtendedAttribute(this );
034: }
035:
036: public ExtendedAttribute getFirstExtendedAttributeForName(
037: String name) {
038: ExtendedAttribute ea = null;
039: ArrayList l = getElementsForName(name);
040: if (l != null && l.size() > 0) {
041: ea = (ExtendedAttribute) l.get(0);
042: }
043: return ea;
044: }
045:
046: public void initCaches() {
047: super .initCaches();
048: Iterator it = elements.iterator();
049: while (it.hasNext()) {
050: ExtendedAttribute ea = (ExtendedAttribute) it.next();
051: String eaName = ea.getName();
052: ArrayList l = (ArrayList) eaMap.get(eaName);
053: if (l == null) {
054: l = new ArrayList();
055: }
056: l.add(ea);
057: eaMap.put(eaName, l);
058: }
059: if (parent instanceof Application) {
060: getExtendedAttributesString();
061: }
062: }
063:
064: public void initExtAttribString() {
065: if (!isReadOnly) {
066: throw new RuntimeException(
067: "This method can be used only in read-only mode!");
068: }
069: if (this .size() > 0) {
070: try {
071: extAttribsString = XMLUtil
072: .stringifyExtendedAttributes(this );
073: } catch (Throwable thr) {
074: throw new RuntimeException(
075: "Can't stringify extended attributes!");
076: }
077: } else {
078: extAttribsString = "";
079: }
080: }
081:
082: public void clearExtAttribString() {
083: extAttribsString = null;
084: }
085:
086: public String getExtendedAttributesString() {
087: if (!isReadOnly) {
088: throw new RuntimeException(
089: "This method can be used only in read-only mode!");
090: }
091: if (extAttribsString == null) {
092: initExtAttribString();
093: }
094: return extAttribsString;
095: }
096:
097: public void clearCaches() {
098: eaMap = new SequencedHashMap();
099: super .clearCaches();
100: }
101:
102: public void clear() {
103: if (eaMap != null) {
104: eaMap.clear();
105: }
106: super .clear();
107: }
108:
109: /**
110: * Returns true if there is at least one ExtendedAttribute with such name.
111: */
112: public boolean containsElement(String name) {
113: if (isReadOnly && cachesInitialized) {
114: return eaMap.containsKey(name);
115: }
116:
117: Iterator it = elements.iterator();
118: while (it.hasNext()) {
119: ExtendedAttribute ea = (ExtendedAttribute) it.next();
120: if (ea.getName().equals(name)) {
121: return true;
122: }
123: }
124: return false;
125: }
126:
127: /**
128: * Returns true if there is at least one ExtendedAttribute with such value.
129: */
130: public boolean containsValue(String val) {
131: Iterator it = elements.iterator();
132: while (it.hasNext()) {
133: ExtendedAttribute ea = (ExtendedAttribute) it.next();
134: if (ea.getVValue().equals(val)) {
135: return true;
136: }
137: }
138: return false;
139: }
140:
141: /**
142: * Returns all elements with specified name.
143: */
144: public ArrayList getElementsForName(String name) {
145: if (isReadOnly && cachesInitialized) {
146: return (ArrayList) eaMap.get(name);
147: }
148:
149: ArrayList l = new ArrayList();
150: Iterator it = elements.iterator();
151: while (it.hasNext()) {
152: ExtendedAttribute ea = (ExtendedAttribute) it.next();
153: if (ea.getName().equals(name)) {
154: l.add(ea);
155: }
156: }
157: return l;
158: }
159:
160: public Object clone() {
161: ExtendedAttributes d = (ExtendedAttributes) super.clone();
162: d.extAttribsString = this.extAttribsString;
163:
164: return d;
165: }
166:
167: }
|