Changeset 21
- Timestamp:
- 07/25/05 17:34:56 (3 years ago)
- Files:
-
- trunk/src/net/schst/XJConf/AttributeDefinition.java (modified) (3 diffs)
- trunk/src/net/schst/XJConf/CDataDefinition.java (added)
- trunk/src/net/schst/XJConf/ChildDefinition.java (added)
- trunk/src/net/schst/XJConf/ConstructorDefinition.java (modified) (1 diff)
- trunk/src/net/schst/XJConf/Definition.java (modified) (1 diff)
- trunk/src/net/schst/XJConf/DefinitionParser.java (modified) (6 diffs)
- trunk/src/net/schst/XJConf/Examples/TestCDataSetter.java (added)
- trunk/src/net/schst/XJConf/Examples/TestConstructor.java (modified) (1 diff)
- trunk/src/net/schst/XJConf/Tag.java (modified) (4 diffs)
- trunk/src/net/schst/XJConf/TagDefinition.java (modified) (7 diffs)
- trunk/src/net/schst/XJConf/XmlReader.java (modified) (4 diffs)
- trunk/xml/defines-constructor.xml (modified) (1 diff)
- trunk/xml/defines-set-cdata.xml (added)
- trunk/xml/test-constructor.xml (modified) (1 diff)
- trunk/xml/test-set-cdata.xml (added)
- trunk/xml/test.xml (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/net/schst/XJConf/AttributeDefinition.java
r16 r21 20 20 private String name = null; 21 21 private String type = null; 22 private String definition = null;23 22 private String setter = null; 24 23 private String defaultValue = null; … … 125 124 * @throws ValueConversionException 126 125 */ 127 public Object convertValue(Object val, ClassLoader loader) throws ValueConversionException { 128 String value = (String)val; 126 public Object convertValue(Tag tag, ClassLoader loader) throws ValueConversionException { 127 String value; 128 129 if (tag.hasAttribute(this.getName())) { 130 value = tag.getAttribute(name); 131 } else { 132 value = this.getDefault(); 133 } 129 134 Object instance = null; 130 135 try { … … 141 146 } 142 147 143 public void addAttribute(AttributeDefinition att) throws Exception { 148 /** 149 * Add a child definition 150 * 151 * Attributes cannot have any children. 152 */ 153 public void addChildDefinition(Definition def) throws Exception { 144 154 } 145 155 } trunk/src/net/schst/XJConf/ConstructorDefinition.java
r16 r21 4 4 5 5 /** 6 * Definition for the constructor of a class 7 * 6 8 * @author Stephan Schmidt <stephan.schmidt@schlund.de> 7 9 */ 8 10 public class ConstructorDefinition implements Definition { 9 11 12 /** 13 * Parameters of the constructor 14 */ 10 15 private ArrayList params = new ArrayList(); 11 12 public void addAttribute(AttributeDefinition param) { 13 this.params.add(param); 16 17 /** 18 * Add a new child definition (equals a parameter of the constructor) 19 */ 20 public void addChildDefinition(Definition def) throws Exception { 21 this.params.add(def); 14 22 } 15 23 24 /** 25 * Get the parameters of the constructor 26 * @return 27 */ 16 28 public ArrayList getParams() { 17 29 return this.params; 18 30 } 19 31 20 public Object convertValue(Object value, ClassLoader loader) 32 /** 33 * Convert the constructor. 34 * 35 * This does not do anything! 36 */ 37 public Object convertValue(Tag tag, ClassLoader loader) 21 38 throws ValueConversionException { 22 39 return null; 23 40 } 41 42 /** 43 * Get the name under which it will be stored 44 */ 24 45 public String getName() { 25 46 return "__constructor"; 26 47 } 48 49 /** 50 * Get the setter method 51 */ 27 52 public String getSetterMethod() { 28 53 return null; 29 54 } 30 31 public String getType() {32 return null;33 }34 55 } trunk/src/net/schst/XJConf/Definition.java
r16 r21 7 7 */ 8 8 public interface Definition { 9 /** 10 * Get the name under which the information 11 * will be stored. 12 * 13 * @return name of the value 14 */ 9 15 public String getName(); 10 public String getType(); 11 public Object convertValue(Object value, ClassLoader loader) throws ValueConversionException; 16 17 /** 18 * Get the converted value. 19 * 20 * XJConf will pass the complete tag to this method 21 * 22 * @param value 23 * @param loader 24 * @return 25 * @throws ValueConversionException 26 */ 27 public Object convertValue(Tag tag, ClassLoader loader) throws ValueConversionException; 28 29 /** 30 * Get the name of the setter method 31 * 32 * @return 33 */ 12 34 public String getSetterMethod(); 13 public void addAttribute(AttributeDefinition att) throws Exception; 35 36 /** 37 * Add a child definition of any type 38 * 39 * @param def 40 */ 41 public void addChildDefinition(Definition def) throws Exception; 14 42 } trunk/src/net/schst/XJConf/DefinitionParser.java
r16 r21 21 21 public class DefinitionParser extends DefaultHandler { 22 22 23 /** 24 * Stack for currently open definitions 25 */ 23 26 private Stack defStack = new Stack(); 24 27 28 /** 29 * The current namespace 30 */ 25 31 private String currentNamespace = "__default"; 26 32 33 /** 34 * All extracted namespace definitions 35 */ 27 36 private NamespaceDefinitions defs = new NamespaceDefinitions(); 28 37 … … 67 76 if (qName.equals("namespace")) { 68 77 this.currentNamespace = atts.getValue("uri"); 69 70 78 return; 71 79 } … … 95 103 if (qName.equals("constructor")) { 96 104 ConstructorDefinition def = new ConstructorDefinition(); 105 this.defStack.push(def); 106 } 107 108 // define the character data 109 if (qName.equals("cdata")) { 110 CDataDefinition def; 111 String type = atts.getValue("type"); 112 if (type == null) { 113 def = new CDataDefinition(); 114 } else { 115 def = new CDataDefinition(type); 116 } 117 String setter = atts.getValue("setter"); 118 if (setter != null) { 119 def.setSetterMethod(setter); 120 } 121 this.defStack.push(def); 122 } 123 124 // define a child tag data 125 if (qName.equals("child")) { 126 ChildDefinition def = new ChildDefinition(atts.getValue("name")); 97 127 this.defStack.push(def); 98 128 } … … 112 142 attDef.setDefault(defaultValue); 113 143 } 114 def.add Attribute(attDef);115 } catch ( Throwable t) {116 System.out.println("Jetzt ist was schief gegangen!");144 def.addChildDefinition(attDef); 145 } catch (Exception e) { 146 throw new XJConfException("Could not process attribute", e); 117 147 } 118 148 this.defStack.push(def); … … 128 158 */ 129 159 public void endElement(String namespaceURI, String sName, String qName) 130 throws SAXException {160 throws SAXException { 131 161 132 162 if (qName.equals("namespace")) { … … 138 168 ConstructorDefinition constructorDef = (ConstructorDefinition)this.defStack.pop(); 139 169 TagDefinition tagDef = (TagDefinition)this.defStack.peek(); 140 tagDef.setConstructor(constructorDef); 141 } 142 170 try { 171 tagDef.addChildDefinition(constructorDef); 172 } catch (Exception e) { 173 throw new RuntimeException(e.getMessage(), e); 174 } 175 } 176 177 // set the cdata handling 178 if (qName.equals("cdata")) { 179 CDataDefinition cdataDef = (CDataDefinition)this.defStack.pop(); 180 Definition parentDef = (Definition)this.defStack.peek(); 181 try { 182 parentDef.addChildDefinition(cdataDef); 183 } catch (Exception e) { 184 throw new RuntimeException(e.getMessage(), e); 185 } 186 } 187 188 // set the child handling 189 if (qName.equals("child")) { 190 ChildDefinition childDef = (ChildDefinition)this.defStack.pop(); 191 Definition parentDef = (Definition)this.defStack.peek(); 192 try { 193 parentDef.addChildDefinition(childDef); 194 } catch (Exception e) { 195 throw new RuntimeException(e.getMessage(), e); 196 } 197 } 198 143 199 if (qName.equals("tag")) { 144 145 200 TagDefinition def = (TagDefinition)this.defStack.pop(); 146 201 trunk/src/net/schst/XJConf/Examples/TestConstructor.java
r16 r21 26 26 ConstructorColor color = (ConstructorColor)conf.getConfigValue("color"); 27 27 System.out.println(color); 28 color = (ConstructorColor)conf.getConfigValue("color2"); 29 System.out.println(color); 30 color = (ConstructorColor)conf.getConfigValue("color3"); 31 System.out.println(color); 28 32 } 29 33 } trunk/src/net/schst/XJConf/Tag.java
r13 r21 3 3 import java.util.ArrayList; 4 4 import java.util.HashMap; 5 import java.util.Iterator; 5 6 6 7 import org.xml.sax.Attributes; … … 35 36 private ArrayList children = new ArrayList(); 36 37 38 /** 39 * Tag definition used for this tag 40 */ 37 41 private TagDefinition def = null; 38 42 … … 81 85 } 82 86 87 public boolean hasAttribute(String name) { 88 return this.atts.containsKey(name); 89 } 90 83 91 /** 84 92 * get an attribute … … 100 108 } 101 109 110 public Tag getChild(String name) { 111 for (Iterator iter = this.children.iterator(); iter.hasNext();) { 112 Tag child = (Tag) iter.next(); 113 if (child.getName().equals(name)) { 114 return child; 115 } 116 } 117 return null; 118 } 119 102 120 /** 103 121 * Get the name of the tag trunk/src/net/schst/XJConf/TagDefinition.java
r17 r21 11 11 * Defintion of an XML tag 12 12 * 13 * @author sschmidt13 * @author Stephan Schmidt <stephan.schmidt@schlund.de> 14 14 */ 15 15 public class TagDefinition implements Definition { … … 22 22 private ConstructorDefinition constructor = null; 23 23 24 public void setConstructor(ConstructorDefinition constructor) { 25 this.constructor = constructor; 26 } 27 24 // TODO: Eventually call the setter method for the cdata 25 private CDataDefinition cdata = null; 26 28 27 /** 29 28 * Constructor for simple types … … 36 35 this.type = type; 37 36 } 38 37 38 /** 39 * Add a new child definition 40 * 41 * Possible definitions are: 42 * - AttributeDefinition 43 * - ConstrcutorDefinition 44 * - CDataDefinition 45 */ 46 public void addChildDefinition(Definition def) throws Exception { 47 if (def instanceof AttributeDefinition) { 48 this.addAttribute((AttributeDefinition)def); 49 return; 50 } 51 if (def instanceof ConstructorDefinition) { 52 this.constructor = (ConstructorDefinition)def; 53 return; 54 } 55 if (def instanceof CDataDefinition) { 56 this.cdata = (CDataDefinition)def; 57 return; 58 } 59 } 60 39 61 /** 40 62 * Add an attribute to the tag … … 150 172 * @throws ValueConversionException 151 173 */ 152 public Object convertValue(Object v, ClassLoader loader) throws ValueConversionException { 153 Tag tag = (Tag)v; 154 174 public Object convertValue(Tag tag, ClassLoader loader) throws ValueConversionException { 155 175 Class cl; 156 176 Object instance = null; … … 169 189 throw new ValueConversionException("Class " + this.type + " does not exist", e); 170 190 } 171 172 // A constructor has been defined 173 if (this.constructor != null) { 174 ArrayList conParams = this.constructor.getParams(); 175 AttributeDefinition att; 176 177 Class[] cParamTypes = new Class[conParams.size()]; 178 Object[] cParams = new Object[conParams.size()]; 179 for (int i = 0; i < conParams.size(); i++) { 180 att = (AttributeDefinition)conParams.get(i); 181 String attName = att.getName(); 182 String attVal = tag.getAttribute(attName); 183 Object val = att.convertValue(attVal,loader); 184 185 cParamTypes[i] = val.getClass(); 186 cParams[i] = val; 191 192 // no constructor definition has been set, 193 // create a new one 194 if (this.constructor == null) { 195 this.constructor = new ConstructorDefinition(); 196 try { 197 this.constructor.addChildDefinition(new CDataDefinition()); 198 } catch (Exception e) { 199 throw new ValueConversionException("Could not create constructor object", e); 187 200 } 188 201 } 202 203 ArrayList conParams = this.constructor.getParams(); 204 Definition paramDef; 205 206 Class[] cParamTypes = new Class[conParams.size()]; 207 Object[] cParams = new Object[conParams.size()]; 208 209 // get all values and their types 210 for (int i = 0; i < conParams.size(); i++) { 211 paramDef = (Definition)conParams.get(i); 212 Object val = paramDef.convertValue(tag,loader); 213 cParamTypes[i] = val.getClass(); 214 cParams[i] = val; 215 } 216 217 // try to create a new instance 218 try { 219 Constructor co = cl.getConstructor(cParamTypes); 220 instance = co.newInstance(cParams); 221 } catch (Exception e){ 189 222 try { 190 Constructor co = cl.getConstructor(cParamTypes); 191 instance = co.newInstance(cParams); 192 } catch (Exception e){ 193 throw new ValueConversionException("Could not instantiate " + this.getType(), e); 223 instance = cl.newInstance(); 224 } catch (Exception t) { 225 throw new ValueConversionException("Could not create instance of " + this.type, t); 194 226 } 195 227 } 196 228 197 if (instance == null) { 198 // constructor with one parameter 199 try { 200 // get the constructor 201 Class paramTypes[] = {data.getClass()}; 202 Constructor co = cl.getConstructor(paramTypes); 203 204 // call the constructor 205 String params[] = {data}; 206 instance = co.newInstance(params); 207 208 } catch (Throwable e) { 209 // get a new instance without constructor parameters 210 try { 211 instance = cl.newInstance(); 212 } catch (Exception t) { 213 throw new ValueConversionException("Could not create instance of " + this.type, t); 214 } 215 } 216 } 217 229 // set all attributes 218 230 String methodName = null; 219 220 // set all attributes221 231 for (int i = 0; i < this.atts.size(); i++) { 222 232 223 233 // get the attribute definition 224 234 AttributeDefinition att = (AttributeDefinition)this.atts.get(i); 225 String attName = att.getName(); 226 String attVal = tag.getAttribute(attName); 227 228 // attribute is not set 229 if (attVal == null) { 230 231 // check for default 232 if (att.getDefault() == null) { 233 continue; 234 } 235 attVal = att.getDefault(); 236 } 237 Object val = att.convertValue(attVal,loader); 235 Object val = att.convertValue(tag,loader); 238 236 239 237 try { … … 245 243 246 244 me.invoke(instance, meParams); 247 248 245 } catch (Exception t) { 249 throw new ValueConversionException("Could not set attribute " + att Name+ " of " + this.type, t);246 throw new ValueConversionException("Could not set attribute " + att.getName() + " of " + this.type, t); 250 247 } 251 248 } … … 261 258 262 259 methodName = child.getDefinition().getSetterMethod(); 260 261 // if the method name is set to ignore, the child will 262 // be ignored 263 if (methodName != null && methodName.equals("__ignore")) { 264 continue; 265 } 263 266 Object childValue = child.getDefinition().convertValue(child,loader); 264 267 Object childParams[] = {childValue}; trunk/src/net/schst/XJConf/XmlReader.java
r15 r21 183 183 throw new InternalError("Could not configure the parser correctly."); 184 184 } catch (SAXException e) { 185 e.printStackTrace(); 186 System.exit(0); 185 187 throw new XJConfException(e.getMessage()); 186 188 } catch (IOException e) { … … 218 220 */ 219 221 public void startElement(String namespaceURI, String sName, String qName, Attributes atts) 220 throws SAXException {222 throws SAXException { 221 223 222 224 if (this.myNamespace.equals(namespaceURI) && this.depth > 0) { 223 225 return; 224 226 } 225 226 227 this.depth++; 227 228 … … 266 267 return; 267 268 } 268 269 269 this.depth--; 270 270 … … 281 281 // get the last tag from the stack 282 282 Tag tag = (Tag)this.tagStack.pop(); 283 283 284 284 285 if (this.extensions.containsKey(namespaceURI)) { 285 286 ((Extension)(this.extensions.get(namespaceURI))).endElement(this, tag); 286 287 } else { 287 288 288 if (this.depth == 1) { 289 289 this.config.put(tag.getKey(), tag.getConvertedValue(this.loader)); trunk/xml/defines-constructor.xml
r16 r21 7 7 </constructor> 8 8 </tag> 9 10 <tag name="color2" type="net.schst.XJConf.Examples.ConstructorColor"> 11 <constructor> 12 <attribute name="red" type="java.lang.Integer"/> 13 <attribute name="green" type="java.lang.Integer"/> 14 <cdata type="java.lang.Integer"/> 15 </constructor> 16 </tag> 17 18 <tag name="color3" type="net.schst.XJConf.Examples.ConstructorColor"> 19 <constructor> 20 <child name="red"/> 21 <child name="green"/> 22 <child name="blue"/> 23 </constructor> 24 </tag> 25 26 <tag name="red" type="java.lang.Integer" setter="__ignore"/> 27 <tag name="green" type="java.lang.Integer" setter="__ignore"/> 28 <tag name="blue" type="java.lang.Integer" setter="__ignore"/> 9 29 </defines> trunk/xml/test-constructor.xml
r16 r21 1 1 <configuration> 2 2 <color red="100" green="25" blue="10"/> 3 4 <!-- The cdata is the third parameter in the constructor --> 5 <color2 red="100" green="25">23</color2> 6 7 <!-- Use child tags for the constructor --> 8 <color3> 9 <red>111</red> 10 <green>222</green> 11 <blue>333</blue> 12 </color3> 3 13 </configuration> trunk/xml/test.xml
r1 r21 17 17 <color red="0" green="20" blue="80" title="A dark blue">dark-blue</color> 18 18 </complex2> 19 20 19 </configuration>
