001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * 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. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018: /*
019: * RuntimeConfigDefsParser.java
020: *
021: * Created on June 4, 2005, 1:57 PM
022: */
023:
024: package org.apache.roller.config.runtime;
025:
026: import java.io.IOException;
027: import java.io.InputStream;
028: import java.util.Iterator;
029: import java.util.List;
030: import org.jdom.Document;
031: import org.jdom.Element;
032: import org.jdom.JDOMException;
033: import org.jdom.input.SAXBuilder;
034:
035: /**
036: * The parser for the rollerRuntimeConfigDefs.xml file.
037: * This class uses jdom to unmarshall the xml into a series of java objects.
038: *
039: * @author Allen Gilliland
040: */
041: public class RuntimeConfigDefsParser {
042:
043: /** Creates a new instance of RuntimeConfigDefsParser */
044: public RuntimeConfigDefsParser() {
045: }
046:
047: /**
048: * Unmarshall the given input stream into our defined
049: * set of Java objects.
050: **/
051: public RuntimeConfigDefs unmarshall(InputStream instream)
052: throws IOException, JDOMException {
053:
054: if (instream == null)
055: throw new IOException("InputStream is null!");
056:
057: RuntimeConfigDefs configs = new RuntimeConfigDefs();
058:
059: SAXBuilder builder = new SAXBuilder();
060: Document doc = builder.build(instream);
061:
062: Element root = doc.getRootElement();
063: List configdefs = root.getChildren("config-def");
064: Iterator iter = configdefs.iterator();
065: while (iter.hasNext()) {
066: Element e = (Element) iter.next();
067: configs.addConfigDef(this .elementToConfigDef(e));
068: }
069:
070: return configs;
071: }
072:
073: private ConfigDef elementToConfigDef(Element element) {
074:
075: ConfigDef configdef = new ConfigDef();
076:
077: configdef.setName(element.getAttributeValue("name"));
078:
079: List displaygroups = element.getChildren("display-group");
080: Iterator iter = displaygroups.iterator();
081: while (iter.hasNext()) {
082: Element e = (Element) iter.next();
083: configdef.addDisplayGroup(this .elementToDisplayGroup(e));
084: }
085:
086: return configdef;
087: }
088:
089: private DisplayGroup elementToDisplayGroup(Element element) {
090:
091: DisplayGroup displaygroup = new DisplayGroup();
092:
093: displaygroup.setName(element.getAttributeValue("name"));
094: displaygroup.setKey(element.getAttributeValue("key"));
095:
096: List displaygroups = element.getChildren("property-def");
097: Iterator iter = displaygroups.iterator();
098: while (iter.hasNext()) {
099: Element e = (Element) iter.next();
100: displaygroup.addPropertyDef(this .elementToPropertyDef(e));
101: }
102:
103: return displaygroup;
104: }
105:
106: private PropertyDef elementToPropertyDef(Element element) {
107:
108: PropertyDef prop = new PropertyDef();
109:
110: prop.setName(element.getAttributeValue("name"));
111: prop.setKey(element.getAttributeValue("key"));
112: prop.setType(element.getChildText("type"));
113: prop.setDefaultValue(element.getChildText("default-value"));
114:
115: // optional elements
116: if (element.getChild("rows") != null)
117: prop.setRows(element.getChildText("rows"));
118:
119: if (element.getChild("cols") != null)
120: prop.setCols(element.getChildText("cols"));
121:
122: return prop;
123: }
124:
125: }
|