001: /*
002: * Created on Oct 26, 2004
003: */
004: package com.openedit.config;
005:
006: import java.io.IOException;
007: import java.io.Reader;
008: import java.io.StringWriter;
009: import java.util.ArrayList;
010: import java.util.HashMap;
011: import java.util.Iterator;
012: import java.util.List;
013: import java.util.Set;
014:
015: import org.apache.commons.logging.Log;
016: import org.apache.commons.logging.LogFactory;
017: import org.dom4j.Attribute;
018: import org.dom4j.Document;
019: import org.dom4j.DocumentHelper;
020: import org.dom4j.Element;
021: import org.dom4j.io.OutputFormat;
022: import org.dom4j.io.XMLWriter;
023:
024: import com.openedit.OpenEditRuntimeException;
025: import com.openedit.util.XmlUtil;
026:
027: /**
028: * @author cburkey
029: *
030: */
031: public class XMLConfiguration implements Configuration {
032: private static final Log log = LogFactory
033: .getLog(XMLConfiguration.class);
034: protected HashMap fieldAttributes;
035: protected String fieldText;
036: protected String fieldName;
037: protected List fieldChildren;
038: protected Configuration fieldParent;
039:
040: /**
041: *
042: */
043: public XMLConfiguration() {
044: }
045:
046: public XMLConfiguration(String inName) {
047: setName(inName);
048: }
049:
050: /**
051: * @param inRootElement
052: */
053: public void populate(Element inRootElement) {
054: setName(inRootElement.getName());
055: setValue(inRootElement.getText());
056:
057: for (Iterator iter = inRootElement.attributeIterator(); iter
058: .hasNext();) {
059: Attribute attrib = (Attribute) iter.next();
060: addAttribute(attrib.getName(), attrib.getValue());
061: }
062:
063: for (Iterator iter = inRootElement.elementIterator(); iter
064: .hasNext();) {
065: Element element = (Element) iter.next();
066:
067: addChild(new XMLConfiguration(element));
068: }
069:
070: }
071:
072: /**
073: * @param inConfiguration
074: */
075: public Configuration addChild(Configuration inConfiguration) {
076: if (fieldChildren == null) {
077: fieldChildren = new ArrayList();
078: }
079: fieldChildren.add(inConfiguration);
080: inConfiguration.setParent(this );
081: return inConfiguration;
082: }
083:
084: /**
085: * @param inName
086: * @param inValue
087: */
088: private void addAttribute(String inName, String inValue) {
089: if (fieldAttributes == null) {
090: fieldAttributes = new HashMap();
091: }
092: fieldAttributes.put(inName, inValue);
093: }
094:
095: /**
096: * @param inChild
097: */
098: public XMLConfiguration(Element inChild) {
099: populate(inChild);
100: }
101:
102: public XMLConfiguration(Reader inRead) {
103: readXML(inRead);
104: }
105:
106: /* (non-javadoc)
107: * @see com.anthonyeden.lib.config.Configuration#getChildren(java.lang.String)
108: */
109: public List getChildren(String inString) {
110: //this is annoying. We are going to create fake dom4j nodes
111: List hits = new ArrayList(realChildren().size());
112: for (Iterator iter = realChildren().iterator(); iter.hasNext();) {
113: XMLConfiguration element = (XMLConfiguration) iter.next();
114: if (inString.equals(element.getName())) {
115: hits.add(element);
116: }
117: }
118:
119: return hits;
120: }
121:
122: /* (non-javadoc)
123: * @see com.anthonyeden.lib.config.Configuration#getAttribute(java.lang.String)
124: */
125: public String getAttribute(String inString) {
126: return (String) getAttributes().get(inString);
127: }
128:
129: public void setAttribute(String inKey, String inValue) {
130: getAttributes().put(inKey, inValue);
131: }
132:
133: /* (non-javadoc)
134: * @see com.anthonyeden.lib.config.Configuration#getValue()
135: */
136: public String getValue() {
137: return fieldText;
138: }
139:
140: public void setValue(String inValue) {
141: fieldText = inValue;
142: if (fieldText != null) {
143: fieldText = fieldText.trim();
144: if (fieldText.length() == 0) {
145: fieldText = null;
146: }
147: }
148: }
149:
150: /* (non-javadoc)
151: * @see com.anthonyeden.lib.config.Configuration#getChild(java.lang.String)
152: */
153: public Configuration getChild(String inString) {
154: for (Iterator iter = realChildren().iterator(); iter.hasNext();) {
155: XMLConfiguration config = (XMLConfiguration) iter.next();
156: if (inString.equals(config.getName())) {
157: return config;
158: }
159: }
160: return null;
161: }
162:
163: protected List realChildren() {
164: if (fieldChildren == null) {
165: fieldChildren = new ArrayList();
166:
167: }
168:
169: return fieldChildren;
170: }
171:
172: /* (non-javadoc)
173: * @see com.anthonyeden.lib.config.Configuration#getChildren()
174: */
175: public List getChildren() {
176:
177: return realChildren();
178:
179: }
180:
181: /* (non-javadoc)
182: * @see com.anthonyeden.lib.config.Configuration#getChildValue(java.lang.String)
183: */
184: public String getChildValue(String inString) {
185: Configuration config = getChild(inString);
186: if (config != null) {
187: return config.getValue();
188: }
189: return null;
190: }
191:
192: /**
193: * @return
194: */
195: public List getAttributeNames() {
196: Set attribs = getAttributes().keySet();
197: List names = new ArrayList(attribs.size());
198:
199: for (Iterator iter = attribs.iterator(); iter.hasNext();) {
200: String a = (String) iter.next();
201: names.add(a);
202: }
203: return names;
204: }
205:
206: public HashMap getAttributes() {
207: if (fieldAttributes == null) {
208: fieldAttributes = new HashMap(2);
209: }
210: return fieldAttributes;
211: }
212:
213: public void setAttributes(HashMap inAttributes) {
214: fieldAttributes = inAttributes;
215: }
216:
217: public String getName() {
218: return fieldName;
219: }
220:
221: public void setName(String inName) {
222: fieldName = inName;
223: }
224:
225: /**
226: * @param inString
227: * @return
228: */
229: public Configuration addChild(String inString) {
230: XMLConfiguration config = new XMLConfiguration(inString);
231: addChild(config);
232: return config;
233: }
234:
235: /**
236: * @param inString
237: * @return
238: */
239: public Iterator getChildIterator(String inString) {
240: return getChildren(inString).iterator();
241: }
242:
243: /**
244: * @param inElement
245: */
246: public void removeChild(Configuration inElement) {
247: getChildren().remove(inElement);
248: }
249:
250: public Configuration getParent() {
251: return fieldParent;
252: }
253:
254: public void setParent(Configuration inParent) {
255: fieldParent = inParent;
256: }
257:
258: public String toString() {
259: return toXml("UTF-8");
260: }
261:
262: /**
263: * Returns this configuration as an XML document with the specified
264: * encoding.
265: *
266: * @param inEncoding The encoding
267: *
268: * @return An XML document
269: */
270: public String toXml(String inEncoding) {
271: Document doc = DocumentHelper.createDocument();
272: Element root = doc.addElement(getName());
273: appendXml(this , root);
274: StringWriter text = new StringWriter();
275: OutputFormat format = OutputFormat.createPrettyPrint();
276: format.setEncoding(inEncoding);
277: XMLWriter out = new XMLWriter(text, format);
278: try {
279: out.write(doc);
280: } catch (IOException ex) {
281: throw new OpenEditRuntimeException(ex);
282: }
283: return text.toString();
284: }
285:
286: public Document asXml() {
287: Document doc = DocumentHelper.createDocument();
288: Element root = doc.addElement(getName());
289: appendXml(this , root);
290: return doc;
291: }
292:
293: /**
294: * @param inConfiguration
295: * @param inRoot
296: */
297: public void appendXml(XMLConfiguration inConfiguration,
298: Element inElement) {
299: for (Iterator iter = inConfiguration.getAttributeNames()
300: .iterator(); iter.hasNext();) {
301: String id = (String) iter.next();
302: inElement
303: .addAttribute(id, inConfiguration.getAttribute(id));
304: }
305: for (Iterator iter = inConfiguration.getChildren().iterator(); iter
306: .hasNext();) {
307: XMLConfiguration child = (XMLConfiguration) iter.next();
308: appendXml(child, inElement.addElement(child.getName()));
309: }
310: if (inConfiguration.getValue() != null
311: && inConfiguration.getValue().trim().length() > 0) {
312: inElement.setText(inConfiguration.getValue());
313: }
314: }
315:
316: public boolean hasChildren() {
317: return fieldChildren != null && getChildren().size() > 0;
318: }
319:
320: /**
321: * @param inString
322: * @return
323: */
324: public boolean hasChild(String inString) {
325: return getChild(inString) != null;
326: }
327:
328: /**
329: * Method should be avoided due to slow performance without shared SaxReader
330: * @param inReader
331: */
332: public void readXML(Reader inReader) {
333: XmlUtil util = new XmlUtil();
334: Element root = util.getXml(inReader, "UTF-8");
335: populate(root);
336: }
337: }
|