KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */

18 /*
19  * RuntimeConfigDefsParser.java
20  *
21  * Created on June 4, 2005, 1:57 PM
22  */

23
24 package org.apache.roller.config.runtime;
25
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import org.jdom.Document;
31 import org.jdom.Element;
32 import org.jdom.JDOMException;
33 import org.jdom.input.SAXBuilder;
34
35
36 /**
37  * The parser for the rollerRuntimeConfigDefs.xml file.
38  * This class uses jdom to unmarshall the xml into a series of java objects.
39  *
40  * @author Allen Gilliland
41  */

42 public class RuntimeConfigDefsParser {
43     
44     /** Creates a new instance of RuntimeConfigDefsParser */
45     public RuntimeConfigDefsParser() {}
46     
47     
48     /**
49      * Unmarshall the given input stream into our defined
50      * set of Java objects.
51      **/

52     public RuntimeConfigDefs unmarshall(InputStream JavaDoc instream)
53         throws IOException JavaDoc, JDOMException {
54         
55         if(instream == null)
56             throw new IOException JavaDoc("InputStream is null!");
57         
58         RuntimeConfigDefs configs = new RuntimeConfigDefs();
59         
60         SAXBuilder builder = new SAXBuilder();
61         Document doc = builder.build(instream);
62         
63         Element root = doc.getRootElement();
64         List JavaDoc configdefs = root.getChildren("config-def");
65         Iterator JavaDoc iter = configdefs.iterator();
66         while (iter.hasNext()) {
67             Element e = (Element) iter.next();
68             configs.addConfigDef(this.elementToConfigDef(e));
69         }
70         
71         return configs;
72     }
73     
74     
75     private ConfigDef elementToConfigDef(Element element) {
76         
77         ConfigDef configdef = new ConfigDef();
78         
79         configdef.setName(element.getAttributeValue("name"));
80         
81         List JavaDoc displaygroups = element.getChildren("display-group");
82         Iterator JavaDoc iter = displaygroups.iterator();
83         while (iter.hasNext())
84         {
85             Element e = (Element) iter.next();
86             configdef.addDisplayGroup(this.elementToDisplayGroup(e));
87         }
88         
89         return configdef;
90     }
91     
92     
93     private DisplayGroup elementToDisplayGroup(Element element) {
94         
95         DisplayGroup displaygroup = new DisplayGroup();
96         
97         displaygroup.setName(element.getAttributeValue("name"));
98         displaygroup.setKey(element.getAttributeValue("key"));
99         
100         List JavaDoc displaygroups = element.getChildren("property-def");
101         Iterator JavaDoc iter = displaygroups.iterator();
102         while (iter.hasNext())
103         {
104             Element e = (Element) iter.next();
105             displaygroup.addPropertyDef(this.elementToPropertyDef(e));
106         }
107         
108         return displaygroup;
109     }
110     
111     
112     private PropertyDef elementToPropertyDef(Element element) {
113         
114         PropertyDef prop = new PropertyDef();
115         
116         prop.setName(element.getAttributeValue("name"));
117         prop.setKey(element.getAttributeValue("key"));
118         prop.setType(element.getChildText("type"));
119         prop.setDefaultValue(element.getChildText("default-value"));
120         
121         // optional elements
122
if(element.getChild("rows") != null)
123             prop.setRows(element.getChildText("rows"));
124         
125         if(element.getChild("cols") != null)
126             prop.setCols(element.getChildText("cols"));
127         
128         return prop;
129     }
130     
131 }
132
Popular Tags