Changeset 64
- Timestamp:
- 08/05/06 17:29:33 (2 years ago)
- Files:
-
- trunk/src/main/java/net/schst/XJConf/DefinitionParser.java (modified) (3 diffs)
- trunk/src/main/java/net/schst/XJConf/FactoryMethodDefinition.java (added)
- trunk/src/main/java/net/schst/XJConf/FactoryMethodValueConverter.java (added)
- trunk/src/main/java/net/schst/XJConf/TagDefinition.java (modified) (8 diffs)
- trunk/src/test/java/net/schst/XJConf/tests/FactoryMethodTestCase.java (added)
- trunk/src/test/java/net/schst/XJConf/tests/helpers/FactoryMethodClass.java (added)
- trunk/src/test/resources/tests/defines/FactoryMethodTestCase.xml (added)
- trunk/src/test/resources/tests/xml/FactoryMethodTestCase-1.xml (added)
- trunk/src/test/resources/tests/xml/FactoryMethodTestCase-2.xml (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/java/net/schst/XJConf/DefinitionParser.java
r61 r64 23 23 public class DefinitionParser extends DefaultHandler { 24 24 25 /** 25 private static final String TAG_FACTORY_METHOD = "factoryMethod"; 26 27 /** 26 28 * Stack for currently open definitions 27 29 */ … … 174 176 this.defStack.push(def); 175 177 } 178 179 // define the constructor 180 if (qName.equals(TAG_FACTORY_METHOD)) { 181 // TODO: check, whether name has been specified 182 FactoryMethodDefinition def = new FactoryMethodDefinition(atts.getValue("name")); 183 this.defStack.push(def); 184 } 185 176 186 177 187 // define the character data … … 266 276 } 267 277 278 // set the factory method 279 if (qName.equals(TAG_FACTORY_METHOD)) { 280 FactoryMethodDefinition factoryDef = (FactoryMethodDefinition)this.defStack.pop(); 281 TagDefinition tagDef = (TagDefinition)this.defStack.peek(); 282 try { 283 tagDef.addChildDefinition(factoryDef); 284 } catch (Exception e) { 285 throw new InvalidTagDefinitionException("Could not register the factory method.", e); 286 } 287 } 288 289 268 290 // set the cdata handling 269 291 if (qName.equals("cdata")) { trunk/src/main/java/net/schst/XJConf/TagDefinition.java
r61 r64 23 23 private String nameAttribute = null; 24 24 private ConstructorDefinition constructor = null; 25 26 private ValueConverter vConverter = null; 25 private FactoryMethodDefinition factoryMethod = null; 26 27 private ValueConverter valueConverter; 27 28 28 29 // TODO: Eventually call the setter method for the cdata … … 59 60 public void setType(String type) { 60 61 this.type = type; 61 if (this.type.indexOf(".") == -1) {62 this.vConverter = new PrimitiveValueConverter(type);63 } else {64 this.vConverter = new ObjectValueConverter(type);65 }66 62 } 67 63 … … 75 71 */ 76 72 public void addChildDefinition(Definition def) throws Exception { 73 77 74 if (def instanceof AttributeDefinition) { 78 75 this.addAttribute((AttributeDefinition)def); 79 76 return; 80 77 } 78 if (def instanceof FactoryMethodDefinition) { 79 this.factoryMethod = (FactoryMethodDefinition)def; 80 return; 81 } 81 82 if (def instanceof ConstructorDefinition) { 82 83 this.constructor = (ConstructorDefinition)def; … … 173 174 public Class getValueType(Tag tag, ClassLoader loader) { 174 175 try { 175 return this. vConverter.getType(loader);176 return this.getValueConverter().getType(loader); 176 177 } catch (Exception e) { 177 178 throw new RuntimeException("Could not return type."); … … 203 204 return "set" + this.name.substring(0,1).toUpperCase() + this.name.substring(1); 204 205 } 205 206 206 207 /** 207 208 * Convert the value of the tag. … … 232 233 } 233 234 234 ArrayList conParams = this.constructor.getParams(); 235 ArrayList conParams; 236 if (this.factoryMethod != null) { 237 conParams = this.factoryMethod.getParams(); 238 } else { 239 conParams = this.constructor.getParams(); 240 } 235 241 Definition paramDef; 236 242 … … 244 250 cParamTypes[i] = paramDef.getValueType(tag, loader); 245 251 } 246 Object instance = this. vConverter.convertValue(cParams, cParamTypes, loader);252 Object instance = this.getValueConverter().convertValue(cParams, cParamTypes, loader); 247 253 248 254 // add attributes and child elements … … 434 440 return copy; 435 441 } 442 443 /** 444 * Get the value converter for this tag 445 * 446 * @return 447 */ 448 private ValueConverter getValueConverter() { 449 450 if (this.valueConverter == null) { 451 if (this.type.indexOf(".") == -1) { 452 this.valueConverter = new PrimitiveValueConverter(this.type); 453 } else { 454 if (this.factoryMethod != null) { 455 this.valueConverter = new FactoryMethodValueConverter(this.type, this.factoryMethod.getName()); 456 } else { 457 this.valueConverter = new ObjectValueConverter(this.type); 458 } 459 } 460 } 461 return this.valueConverter; 462 } 436 463 }
