Changeset 30
- Timestamp:
- 07/26/05 13:27:58 (3 years ago)
- Files:
-
- trunk (modified) (1 prop)
- trunk/src/net/schst/XJConf/AttributeDefinition.java (modified) (5 diffs)
- trunk/src/net/schst/XJConf/CDataDefinition.java (modified) (5 diffs)
- trunk/src/net/schst/XJConf/DefinitionParser.java (modified) (2 diffs)
- trunk/src/net/schst/XJConf/Examples/ColorPrimitives.java (added)
- trunk/src/net/schst/XJConf/Examples/TestPrimitives.java (added)
- trunk/src/net/schst/XJConf/ObjectValueConverter.java (added)
- trunk/src/net/schst/XJConf/PrimitiveValueConverter.java (added)
- trunk/src/net/schst/XJConf/TagDefinition.java (modified) (7 diffs)
- trunk/src/net/schst/XJConf/ValueConverter.java (added)
- trunk/xml/defines-primitives.xml (added)
- trunk/xml/test-primitives.xml (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk
- Property svn:ignore changed from
build
dist
to
build
dist
.settings
- Property svn:ignore changed from
trunk/src/net/schst/XJConf/AttributeDefinition.java
r26 r30 1 1 package net.schst.XJConf; 2 3 import java.lang.reflect.Constructor;4 2 5 3 /** … … 44 42 private boolean required = false; 45 43 44 /** 45 * Converter used to convert the attribute 46 */ 47 private ValueConverter vConverter; 48 46 49 /** 47 50 * create a new attribute definition for a String attribute … … 63 66 this.name = name; 64 67 this.type = type; 68 69 if (this.type.indexOf(".") == -1) { 70 this.vConverter = new PrimitiveValueConverter(this.type); 71 } else { 72 this.vConverter = new ObjectValueConverter(this.type); 73 } 65 74 } 66 75 … … 142 151 public Class getValueType(Tag tag) { 143 152 try { 144 return Class.forName(this.type);145 } catch ( ClassNotFoundException e) {153 return this.vConverter.getType(); 154 } catch (Exception e) { 146 155 throw new RuntimeException("Could not return type."); 147 156 } … … 185 194 throw new MissingAttributeException("The attribute '" + this.name + "' is required for the tag '" + tag.getName() + "'."); 186 195 } 187 188 196 // it's no use to create an instance of a class passing null 189 197 // to the constructor. This will at least fail with Integers! 190 198 return null; 191 199 } 200 Class paramTypes[] = {String.class}; 201 String params[] = {value}; 192 202 193 Object instance = null; 194 try { 195 Class cl = Class.forName(this.type, true,loader); 196 Class paramTypes[] = {String.class}; 197 198 Constructor co = cl.getConstructor(paramTypes); 199 String params[] = {value}; 200 instance = co.newInstance(params); 201 } catch (Exception t) { 202 throw new ValueConversionException("Could not create attribute " + this.name + " of type " + this.type, t); 203 } 203 Object instance = this.vConverter.convertValue(params, paramTypes, loader); 204 204 return instance; 205 205 } trunk/src/net/schst/XJConf/CDataDefinition.java
r25 r30 1 1 package net.schst.XJConf; 2 3 import java.lang.reflect.Constructor;4 2 5 3 /** … … 24 22 25 23 /** 24 * Converter used to convert the character data 25 */ 26 private ValueConverter vConverter; 27 28 /** 26 29 * create a new CDataDefinition for a String 27 30 */ 28 31 public CDataDefinition() { 29 32 this.type = "java.lang.String"; 33 this.vConverter = new ObjectValueConverter(this.type); 30 34 } 31 35 … … 37 41 public CDataDefinition(String type) { 38 42 this.type = type; 43 44 if (this.type.indexOf(".") == -1) { 45 this.vConverter = new PrimitiveValueConverter(this.type); 46 } else { 47 this.vConverter = new ObjectValueConverter(this.type); 48 } 39 49 } 40 50 … … 65 75 throws ValueConversionException { 66 76 67 String value = tag.getData(); 68 Object instance = null; 69 Constructor co = null; 70 try { 71 Class cl = Class.forName(this.type, true,loader); 72 Class paramTypes[] = {String.class}; 73 74 co = cl.getConstructor(paramTypes); 75 String params[] = {value}; 76 instance = co.newInstance(params); 77 } catch (Exception t) { 78 throw new ValueConversionException("Could not convert character data to type " + this.type, t); 79 } 77 Object params[] = {tag.getData()}; 78 Class paramTypes[] = {String.class}; 79 80 Object instance = this.vConverter.convertValue(params, paramTypes, loader); 80 81 return instance; 81 82 } … … 95 96 public Class getValueType(Tag tag) { 96 97 try { 97 return Class.forName(this.type);98 } catch ( ClassNotFoundException e) {98 return this.vConverter.getType(); 99 } catch (Exception e) { 99 100 throw new RuntimeException("Could not return type."); 100 101 } trunk/src/net/schst/XJConf/DefinitionParser.java
r27 r30 71 71 */ 72 72 public void startElement(String namespaceURI, String sName, String qName, Attributes atts) 73 throws SAXException {73 throws SAXException { 74 74 75 75 // define a tag … … 78 78 return; 79 79 } 80 80 81 81 // define a tag 82 82 if (qName.equals("tag")) { 83 TagDefinition def = new TagDefinition(atts.getValue("name"), atts.getValue("type")); 83 84 TagDefinition def; 85 String type = atts.getValue("type"); 86 if (type != null) { 87 def = new TagDefinition(atts.getValue("name"), type); 88 } else { 89 def = new TagDefinition(atts.getValue("name"), atts.getValue("primitive")); 90 } 84 91 85 92 // key attribute trunk/src/net/schst/XJConf/TagDefinition.java
r29 r30 1 1 package net.schst.XJConf; 2 2 3 import java.lang.reflect.Constructor;4 3 import java.lang.reflect.Method; 5 4 import java.util.ArrayList; … … 21 20 private ConstructorDefinition constructor = null; 22 21 22 private ValueConverter vConverter = null; 23 23 24 // TODO: Eventually call the setter method for the cdata 24 25 private CDataDefinition cdata = null; … … 26 27 /** 27 28 * Constructor for simple types 28 *29 29 * @param type 30 30 */ 31 31 public TagDefinition(String name, String type) { 32 this.name = name;32 this.name = name; 33 33 this.tagName = name; 34 this.type = type; 34 this.type = type; 35 if (this.type.indexOf(".") == -1) { 36 this.vConverter = new PrimitiveValueConverter(type); 37 } else { 38 this.vConverter = new ObjectValueConverter(type); 39 } 35 40 } 36 41 … … 133 138 public Class getValueType(Tag tag) { 134 139 try { 135 return Class.forName(this.type);136 } catch ( ClassNotFoundException e) {140 return this.vConverter.getType(); 141 } catch (Exception e) { 137 142 throw new RuntimeException("Could not return type."); 138 143 } … … 175 180 throws ValueConversionException { 176 181 177 Class instanceClass;178 Object instance = null;179 180 182 // get the data 181 183 String data = tag.getData(); 182 184 if (data == null) { 183 185 data = ""; 184 }185 186 // try to get the class object187 try {188 instanceClass = Class.forName(this.type,true,loader);189 } catch (Exception e) {190 throw new ValueConversionException("Class " + this.type + " does not exist", e);191 186 } 192 187 … … 214 209 cParamTypes[i] = paramDef.getValueType(tag); 215 210 } 216 217 // try to create a new instance 218 try { 219 Constructor co = instanceClass.getConstructor(cParamTypes); 220 instance = co.newInstance(cParams); 221 } catch (Exception e){ 222 try { 223 // no matching constructor has been found 224 // try to instantiate the class without using 225 // a constructor 226 instance = instanceClass.newInstance(); 227 } catch (Exception e2) { 228 throw new ValueConversionException("Could not create instance of " + this.type, e2); 229 } 230 } 211 Object instance = this.vConverter.convertValue(cParams, cParamTypes, loader); 231 212 232 213 // add attributes and child elements … … 266 247 methodName = att.getSetterMethod(); 267 248 Class meParamTypes[] = {att.getValueType(tag)}; 268 269 249 Method me = cl.getMethod(methodName, meParamTypes); 270 250 Object meParams[] = {val}; 271 251 272 252 me.invoke(instance, meParams); 273 253 } catch (Exception e) {
