KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > boot > AbstractXMLDefinedPropertyClass


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.boot;
21
22 import java.io.IOException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.jdom.Element;
30 import org.jdom.JDOMException;
31 import org.jdom.input.SAXBuilder;
32
33
34 public abstract class AbstractXMLDefinedPropertyClass extends AbstractPropertyClass {
35     
36     final static Log log = LogFactory.getLog(AbstractXMLDefinedPropertyClass.class);
37
38     public AbstractXMLDefinedPropertyClass(String JavaDoc name, boolean supportsReplacementsVariablesInValues) throws IOException JavaDoc, JDOMException {
39         super(name, supportsReplacementsVariablesInValues);
40         loadPropertyCategoryDefinitionsFromResources();
41         loadPropertyDefinitionsFromResources();
42     }
43
44     
45     void loadPropertyCategoryDefinitionsFromResources() throws IOException JavaDoc, JDOMException {
46         SAXBuilder build = new SAXBuilder();
47         for(Enumeration JavaDoc<URL JavaDoc> e = getClass().getClassLoader().getResources(
48             "META-INF/" + getName() + "-categories.xml"); e.hasMoreElements(); ) {
49             URL JavaDoc u = e.nextElement();
50             log.info("Loading categories for class " + getName() + " from " + u);
51             Element root = build.build(u).getRootElement();
52             if(!root.getName().equals("categories")) {
53                 throw new JDOMException("Root element in " + u + " should be <categories>");
54             }
55             for(Iterator JavaDoc i = root.getChildren().iterator(); i.hasNext(); ) {
56                 Element c = (Element)i.next();
57                 if(c.getName().equals("category")) {
58                     addCategories(c, null);
59                 }
60                 else {
61                     throw new JDOMException("Expect root element of <categories> with child elements of <category>. Got <" + c.getName() + ">.");
62                 }
63             }
64         }
65     }
66
67     void loadPropertyDefinitionsFromResources() throws IOException JavaDoc, JDOMException {
68         SAXBuilder build = new SAXBuilder();
69         for(Enumeration JavaDoc<URL JavaDoc> e = getClass().getClassLoader().getResources(
70             "META-INF/" + getName() + "-definitions.xml"); e.hasMoreElements(); ) {
71             URL JavaDoc u = e.nextElement();
72             log.info("Loading property definitions for class " + getName() + " from " + u);
73             Element root = build.build(u).getRootElement();
74             if(!root.getName().equals("definitions")) {
75                 throw new JDOMException("Root element in " + u + " should be <definitions>");
76             }
77             for(Iterator JavaDoc i = root.getChildren().iterator(); i.hasNext(); ) {
78                 Element c = (Element)i.next();
79                 if(c.getName().equals("definition")) {
80                     registerPropertyDefinition(createDefinition(c));
81                 }
82                 else {
83                     throw new JDOMException("Expect root element of <definitions> with child elements of <definition>. Got <" + c.getName() + ">.");
84                 }
85             }
86         }
87     }
88
89     void addCategories(Element el, PropertyDefinitionCategory parent) throws JDOMException {
90         PropertyDefinitionCategory cat = new DefaultPropertyDefinitionCategory(el.getAttribute("id").getIntValue(), el
91                         .getAttributeValue("bundle"), el.getAttributeValue("image"));
92         addPropertyDefinitionCategory(parent == null ? -1 : parent.getId(), cat);
93         for (Iterator JavaDoc i = el.getChildren().iterator(); i.hasNext();) {
94             addCategories((Element) i.next(), cat);
95         }
96     }
97     
98     protected PropertyDefinition createDefinition(Element element) throws JDOMException {
99         return new XMLPropertyDefinition(element);
100     }
101
102 }
103
Popular Tags