001: /*
002: * Copyright 2001-2007 Hippo (www.hippo.nl)
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package nl.hippo.cms.editor.flow;
017:
018: import java.util.*;
019: import org.w3c.dom.Element;
020: import org.w3c.dom.Document;
021: import org.w3c.dom.Text;
022:
023: public abstract class AbstractFlowContextComponent {
024: protected Configuration editorConfiguration;
025: protected HashMap m_values;
026: protected HashMap m_components;
027: protected boolean dirtyFlag = true;
028: protected FlowHelper helper;
029: public static String COMPONENT_SEPARATOR = ".";
030:
031: private AbstractFlowContextComponent() {
032: m_components = new HashMap();
033: m_values = new HashMap();
034: }
035:
036: public AbstractFlowContextComponent(Configuration conf,
037: FlowHelper fh) {
038: this ();
039: this .editorConfiguration = conf;
040: this .helper = fh;
041: }
042:
043: protected AbstractFlowContextComponent getComponent(String key) {
044:
045: if (m_components != null && m_components.containsKey(key))
046: return (AbstractFlowContextComponent) m_components.get(key);
047:
048: return null;
049: }
050:
051: protected void addComponent(AbstractFlowContextComponent afcc) {
052: m_components.put(afcc.getComponentKey(), afcc);
053: }
054:
055: protected void addValue(String key, String value) {
056: m_values.put(key, value);
057: }
058:
059: public void setDirtyFlag(boolean flag) {
060: dirtyFlag = flag;
061: Iterator it = m_components.keySet().iterator();
062: while (it.hasNext())
063: ((AbstractFlowContextComponent) m_components.get(it.next()))
064: .setDirtyFlag(flag);
065: }
066:
067: public boolean isDirty() {
068: if (dirtyFlag)
069: return true;
070:
071: Iterator it = m_components.keySet().iterator();
072: while (it.hasNext())
073: if (((AbstractFlowContextComponent) m_components.get(it
074: .next())).isDirty())
075: return true;
076: return false;
077: }
078:
079: protected void update() {
080: Iterator it = m_components.keySet().iterator();
081: while (it.hasNext())
082: ((AbstractFlowContextComponent) m_components.get(it.next()))
083: .update();
084: setDirtyFlag(false);
085: }
086:
087: public void checkForUpdate() {
088:
089: if (isDirty())
090: update();
091: setDirtyFlag(false);
092: }
093:
094: public String getValue(String key) {
095: if (key.indexOf(COMPONENT_SEPARATOR) >= 0
096: && key.indexOf(COMPONENT_SEPARATOR) < key.length()) {
097: String componentKey = key.substring(0, key
098: .indexOf(COMPONENT_SEPARATOR));
099: if (m_components.containsKey(componentKey)) {
100: AbstractFlowContextComponent afcc = ((AbstractFlowContextComponent) m_components
101: .get(componentKey));
102: return afcc.getValue(key.substring(key
103: .indexOf(COMPONENT_SEPARATOR) + 1));
104: }
105: }
106:
107: // component not found, check if value exists with the specified key
108: checkForUpdate();
109: if (m_values.get(key) != null)
110: return (String) m_values.get(key);
111:
112: return "";
113: }
114:
115: public ArrayList valuesToXML(Document d) {
116: ArrayList valueElements = new ArrayList();
117:
118: Iterator it = m_values.keySet().iterator();
119: Element el;
120: String key;
121: Text valueText;
122: while (it.hasNext()) {
123: key = (String) it.next();
124: if ((key).length() > 0) {
125: el = d.createElement(key);
126: valueText = d
127: .createTextNode((String) m_values.get(key));
128: el.appendChild(valueText);
129: valueElements.add(el);
130: }
131: }
132:
133: return valueElements;
134: }
135:
136: public ArrayList componentsToXML(Document d) {
137: ArrayList componentElements = new ArrayList();
138: Iterator it = m_components.keySet().iterator();
139: while (it.hasNext())
140: componentElements
141: .add(((AbstractFlowContextComponent) m_components
142: .get(it.next())).toXML(d));
143: return componentElements;
144: }
145:
146: public Element toXML(Document d) {
147: checkForUpdate();
148: Element el = d.createElement(getComponentKey());
149:
150: ArrayList valueElements = valuesToXML(d);
151: ArrayList componentElements = componentsToXML(d);
152:
153: Iterator valueIterator = valueElements.iterator();
154: while (valueIterator.hasNext())
155: el.appendChild((Element) valueIterator.next());
156:
157: Iterator componentIterator = componentElements.iterator();
158: while (componentIterator.hasNext())
159: el.appendChild((Element) componentIterator.next());
160:
161: return el;
162: }
163:
164: public HashMap getComponents() {
165: return m_components;
166: }
167:
168: public HashMap getValues() {
169: checkForUpdate();
170: return m_values;
171: }
172:
173: public abstract String getComponentKey();
174: }
|