1 19 20 package com.sslexplorer.boot; 21 22 import java.io.IOException ; 23 import java.net.URL ; 24 import java.util.Enumeration ; 25 import java.util.Iterator ; 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 name, boolean supportsReplacementsVariablesInValues) throws IOException , JDOMException { 39 super(name, supportsReplacementsVariablesInValues); 40 loadPropertyCategoryDefinitionsFromResources(); 41 loadPropertyDefinitionsFromResources(); 42 } 43 44 45 void loadPropertyCategoryDefinitionsFromResources() throws IOException , JDOMException { 46 SAXBuilder build = new SAXBuilder(); 47 for(Enumeration <URL > e = getClass().getClassLoader().getResources( 48 "META-INF/" + getName() + "-categories.xml"); e.hasMoreElements(); ) { 49 URL 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 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 , JDOMException { 68 SAXBuilder build = new SAXBuilder(); 69 for(Enumeration <URL > e = getClass().getClassLoader().getResources( 70 "META-INF/" + getName() + "-definitions.xml"); e.hasMoreElements(); ) { 71 URL 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 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 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 |