KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > config > XMLConfiguration


1 /*
2  * Created on Oct 26, 2004
3  */

4 package com.openedit.config;
5
6 import java.io.IOException JavaDoc;
7 import java.io.Reader JavaDoc;
8 import java.io.StringWriter JavaDoc;
9 import java.util.ArrayList JavaDoc;
10 import java.util.HashMap JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Set JavaDoc;
14
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17 import org.dom4j.Attribute;
18 import org.dom4j.Document;
19 import org.dom4j.DocumentHelper;
20 import org.dom4j.Element;
21 import org.dom4j.io.OutputFormat;
22 import org.dom4j.io.XMLWriter;
23
24 import com.openedit.OpenEditRuntimeException;
25 import com.openedit.util.XmlUtil;
26
27 /**
28  * @author cburkey
29  *
30  */

31 public class XMLConfiguration implements Configuration
32 {
33     private static final Log log = LogFactory.getLog(XMLConfiguration.class);
34     protected HashMap JavaDoc fieldAttributes;
35     protected String JavaDoc fieldText;
36     protected String JavaDoc fieldName;
37     protected List JavaDoc fieldChildren;
38     protected Configuration fieldParent;
39     
40     /**
41      *
42      */

43     public XMLConfiguration()
44     {
45     }
46
47     public XMLConfiguration(String JavaDoc inName)
48     {
49         setName(inName);
50     }
51     
52     /**
53      * @param inRootElement
54      */

55     public void populate(Element inRootElement)
56     {
57         setName(inRootElement.getName());
58         setValue(inRootElement.getText());
59         
60         for (Iterator JavaDoc iter = inRootElement.attributeIterator(); iter.hasNext();)
61         {
62             Attribute attrib = (Attribute) iter.next();
63             addAttribute(attrib.getName(),attrib.getValue());
64         }
65         
66         for (Iterator JavaDoc iter = inRootElement.elementIterator(); iter.hasNext();)
67         {
68             Element element = (Element) iter.next();
69             
70             addChild(new XMLConfiguration(element));
71         }
72
73     }
74
75     /**
76      * @param inConfiguration
77      */

78     public Configuration addChild(Configuration inConfiguration)
79     {
80         if ( fieldChildren == null)
81         {
82             fieldChildren = new ArrayList JavaDoc();
83         }
84         fieldChildren.add( inConfiguration);
85         inConfiguration.setParent(this);
86         return inConfiguration;
87     }
88
89     /**
90      * @param inName
91      * @param inValue
92      */

93     private void addAttribute(String JavaDoc inName, String JavaDoc inValue)
94     {
95         if ( fieldAttributes == null)
96         {
97             fieldAttributes = new HashMap JavaDoc();
98         }
99         fieldAttributes.put( inName, inValue);
100     }
101     
102     /**
103      * @param inChild
104      */

105     public XMLConfiguration(Element inChild)
106     {
107         populate(inChild);
108     }
109
110     public XMLConfiguration(Reader JavaDoc inRead)
111     {
112         readXML( inRead);
113     }
114
115     /* (non-javadoc)
116      * @see com.anthonyeden.lib.config.Configuration#getChildren(java.lang.String)
117      */

118     public List JavaDoc getChildren(String JavaDoc inString)
119     {
120         //this is annoying. We are going to create fake dom4j nodes
121
List JavaDoc hits = new ArrayList JavaDoc(realChildren().size());
122         for (Iterator JavaDoc iter = realChildren().iterator(); iter.hasNext();)
123         {
124             XMLConfiguration element = (XMLConfiguration) iter.next();
125             if ( inString.equals( element.getName()) )
126             {
127                 hits.add( element);
128             }
129         }
130         
131         
132         return hits;
133     }
134
135
136     /* (non-javadoc)
137      * @see com.anthonyeden.lib.config.Configuration#getAttribute(java.lang.String)
138      */

139     public String JavaDoc getAttribute(String JavaDoc inString)
140     {
141         return (String JavaDoc)getAttributes().get( inString);
142     }
143     public void setAttribute(String JavaDoc inKey, String JavaDoc inValue)
144     {
145         getAttributes().put(inKey,inValue);
146     }
147     /* (non-javadoc)
148      * @see com.anthonyeden.lib.config.Configuration#getValue()
149      */

150     public String JavaDoc getValue()
151     {
152         return fieldText;
153     }
154     public void setValue( String JavaDoc inValue)
155     {
156         fieldText = inValue;
157         if ( fieldText != null)
158         {
159             fieldText = fieldText.trim();
160             if ( fieldText.length() == 0)
161             {
162                 fieldText = null;
163             }
164         }
165     }
166
167     /* (non-javadoc)
168      * @see com.anthonyeden.lib.config.Configuration#getChild(java.lang.String)
169      */

170     public Configuration getChild(String JavaDoc inString)
171     {
172         for (Iterator JavaDoc iter = realChildren().iterator(); iter.hasNext();)
173         {
174             XMLConfiguration config = (XMLConfiguration) iter.next();
175             if ( inString.equals( config.getName() ) )
176             {
177                 return config;
178             }
179         }
180         return null;
181     }
182     protected List JavaDoc realChildren()
183     {
184         if (fieldChildren == null)
185         {
186             fieldChildren = new ArrayList JavaDoc();
187             
188         }
189
190         return fieldChildren;
191     }
192
193     /* (non-javadoc)
194      * @see com.anthonyeden.lib.config.Configuration#getChildren()
195      */

196     public List JavaDoc getChildren()
197     {
198
199         return realChildren();
200         
201     }
202
203     /* (non-javadoc)
204      * @see com.anthonyeden.lib.config.Configuration#getChildValue(java.lang.String)
205      */

206     public String JavaDoc getChildValue(String JavaDoc inString)
207     {
208         Configuration config = getChild(inString);
209         if ( config != null)
210         {
211             return config.getValue();
212         }
213         return null;
214     }
215
216     /**
217      * @return
218      */

219     public List JavaDoc getAttributeNames()
220     {
221         Set JavaDoc attribs = getAttributes().keySet();
222         List JavaDoc names = new ArrayList JavaDoc(attribs.size());
223         
224         for (Iterator JavaDoc iter = attribs.iterator(); iter.hasNext();)
225         {
226             String JavaDoc a = (String JavaDoc) iter.next();
227             names.add( a );
228         }
229         return names;
230     }
231
232
233     public HashMap JavaDoc getAttributes()
234     {
235         if (fieldAttributes == null)
236         {
237             fieldAttributes = new HashMap JavaDoc(2);
238         }
239         return fieldAttributes;
240     }
241     public void setAttributes(HashMap JavaDoc inAttributes)
242     {
243         fieldAttributes = inAttributes;
244     }
245     public String JavaDoc getName()
246     {
247         return fieldName;
248     }
249     public void setName(String JavaDoc inName)
250     {
251         fieldName = inName;
252     }
253
254     /**
255      * @param inString
256      * @return
257      */

258     public Configuration addChild(String JavaDoc inString)
259     {
260         XMLConfiguration config = new XMLConfiguration(inString);
261         addChild(config);
262         return config;
263     }
264
265     /**
266      * @param inString
267      * @return
268      */

269     public Iterator JavaDoc getChildIterator(String JavaDoc inString)
270     {
271         return getChildren(inString).iterator();
272     }
273     /**
274      * @param inElement
275      */

276     public void removeChild(Configuration inElement)
277     {
278         getChildren().remove(inElement);
279     }
280
281     public Configuration getParent()
282     {
283         return fieldParent;
284     }
285     public void setParent(Configuration inParent)
286     {
287         fieldParent = inParent;
288     }
289     public String JavaDoc toString()
290     {
291         return toXml("UTF-8");
292     }
293     /**
294      * Returns this configuration as an XML document with the specified
295      * encoding.
296      *
297      * @param inEncoding The encoding
298      *
299      * @return An XML document
300      */

301     public String JavaDoc toXml( String JavaDoc inEncoding )
302     {
303         Document doc = DocumentHelper.createDocument();
304         Element root = doc.addElement(getName());
305         appendXml(this,root);
306         StringWriter JavaDoc text = new StringWriter JavaDoc();
307         OutputFormat format = OutputFormat.createPrettyPrint();
308         format.setEncoding(inEncoding);
309         XMLWriter out = new XMLWriter(text, format);
310         try
311         {
312             out.write(doc);
313         }
314         catch (IOException JavaDoc ex)
315         {
316             throw new OpenEditRuntimeException(ex);
317         }
318         return text.toString();
319     }
320     public Document asXml()
321     {
322         Document doc = DocumentHelper.createDocument();
323         Element root = doc.addElement(getName());
324         appendXml(this,root);
325         return doc;
326     }
327
328     
329     /**
330      * @param inConfiguration
331      * @param inRoot
332      */

333     public void appendXml(XMLConfiguration inConfiguration, Element inElement)
334     {
335         for (Iterator JavaDoc iter = inConfiguration.getAttributeNames().iterator(); iter.hasNext();)
336         {
337             String JavaDoc id = (String JavaDoc) iter.next();
338             inElement.addAttribute(id,inConfiguration.getAttribute(id));
339         }
340         for (Iterator JavaDoc iter = inConfiguration.getChildren().iterator(); iter.hasNext();)
341         {
342             XMLConfiguration child = (XMLConfiguration) iter.next();
343             appendXml(child,inElement.addElement(child.getName()));
344         }
345         if ( inConfiguration.getValue() != null && inConfiguration.getValue().trim().length() > 0)
346         {
347             inElement.setText(inConfiguration.getValue());
348         }
349     }
350     public boolean hasChildren()
351     {
352         return fieldChildren != null && getChildren().size() > 0;
353     }
354
355     /**
356      * @param inString
357      * @return
358      */

359     public boolean hasChild(String JavaDoc inString)
360     {
361         return getChild(inString) != null;
362     }
363     /**
364      * Method should be avoided due to slow performance without shared SaxReader
365      * @param inReader
366      */

367     public void readXML(Reader JavaDoc inReader)
368     {
369         XmlUtil util = new XmlUtil();
370         Element root = util.getXml(inReader, "UTF-8");
371         populate(root);
372     }
373 }
374
Popular Tags