KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > config > runtime > RuntimeConfigDefsParser


1 /*
2  * RuntimeConfigDefsParser.java
3  *
4  * Created on June 4, 2005, 1:57 PM
5  */

6
7 package org.roller.config.runtime;
8
9 import java.io.IOException JavaDoc;
10 import java.io.InputStream JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
13 import org.jdom.Document;
14 import org.jdom.Element;
15 import org.jdom.JDOMException;
16 import org.jdom.input.SAXBuilder;
17
18
19 /**
20  * The parser for the rollerRuntimeConfigDefs.xml file.
21  * This class uses jdom to unmarshall the xml into a series of java objects.
22  *
23  * @author Allen Gilliland
24  */

25 public class RuntimeConfigDefsParser {
26     
27     /** Creates a new instance of RuntimeConfigDefsParser */
28     public RuntimeConfigDefsParser() {}
29     
30     
31     /**
32      * Unmarshall the given input stream into our defined
33      * set of Java objects.
34      **/

35     public RuntimeConfigDefs unmarshall(InputStream JavaDoc instream)
36         throws IOException JavaDoc, JDOMException {
37         
38         RuntimeConfigDefs configs = new RuntimeConfigDefs();
39         
40         SAXBuilder builder = new SAXBuilder();
41         Document doc = builder.build(instream);
42         
43         Element root = doc.getRootElement();
44         List JavaDoc configdefs = root.getChildren("config-def");
45         Iterator JavaDoc iter = configdefs.iterator();
46         while (iter.hasNext()) {
47             Element e = (Element) iter.next();
48             configs.addConfigDef(this.elementToConfigDef(e));
49         }
50         
51         return configs;
52     }
53     
54     
55     private ConfigDef elementToConfigDef(Element element) {
56         
57         ConfigDef configdef = new ConfigDef();
58         
59         configdef.setName(element.getAttributeValue("name"));
60         
61         List JavaDoc displaygroups = element.getChildren("display-group");
62         Iterator JavaDoc iter = displaygroups.iterator();
63         while (iter.hasNext())
64         {
65             Element e = (Element) iter.next();
66             configdef.addDisplayGroup(this.elementToDisplayGroup(e));
67         }
68         
69         return configdef;
70     }
71     
72     
73     private DisplayGroup elementToDisplayGroup(Element element) {
74         
75         DisplayGroup displaygroup = new DisplayGroup();
76         
77         displaygroup.setName(element.getAttributeValue("name"));
78         displaygroup.setKey(element.getAttributeValue("key"));
79         
80         List JavaDoc displaygroups = element.getChildren("property-def");
81         Iterator JavaDoc iter = displaygroups.iterator();
82         while (iter.hasNext())
83         {
84             Element e = (Element) iter.next();
85             displaygroup.addPropertyDef(this.elementToPropertyDef(e));
86         }
87         
88         return displaygroup;
89     }
90     
91     
92     private PropertyDef elementToPropertyDef(Element element) {
93         
94         PropertyDef prop = new PropertyDef();
95         
96         prop.setName(element.getAttributeValue("name"));
97         prop.setKey(element.getAttributeValue("key"));
98         prop.setType(element.getChildText("type"));
99         prop.setDefaultValue(element.getChildText("default-value"));
100         
101         // optional elements
102
if(element.getChild("rows") != null)
103             prop.setRows(element.getChildText("rows"));
104         
105         if(element.getChild("cols") != null)
106             prop.setCols(element.getChildText("cols"));
107         
108         return prop;
109     }
110     
111 }
112
Popular Tags