Changeset 16
- Timestamp:
- 07/22/05 16:43:43 (3 years ago)
- Files:
-
- trunk/src/net/schst/XJConf/AttributeDefinition.java (modified) (1 diff)
- trunk/src/net/schst/XJConf/ConstructorDefinition.java (added)
- trunk/src/net/schst/XJConf/Definition.java (modified) (1 diff)
- trunk/src/net/schst/XJConf/DefinitionParser.java (modified) (3 diffs)
- trunk/src/net/schst/XJConf/Examples/ConstructorColor.java (added)
- trunk/src/net/schst/XJConf/Examples/TestConstructor.java (added)
- trunk/src/net/schst/XJConf/TagDefinition.java (modified) (3 diffs)
- trunk/xml/defines-constructor.xml (added)
- trunk/xml/test-constructor.xml (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/net/schst/XJConf/AttributeDefinition.java
r13 r16 140 140 return instance; 141 141 } 142 143 public void addAttribute(AttributeDefinition att) throws Exception { 144 } 142 145 } trunk/src/net/schst/XJConf/Definition.java
r13 r16 11 11 public Object convertValue(Object value, ClassLoader loader) throws ValueConversionException; 12 12 public String getSetterMethod(); 13 public void addAttribute(AttributeDefinition att) throws Exception; 13 14 } trunk/src/net/schst/XJConf/DefinitionParser.java
r7 r16 92 92 } 93 93 94 // define the constructor 95 if (qName.equals("constructor")) { 96 ConstructorDefinition def = new ConstructorDefinition(); 97 this.defStack.push(def); 98 } 99 94 100 // define an attribute 95 101 if (qName.equals("attribute")) { 96 102 // get the current tag 97 TagDefinition def = (TagDefinition)this.defStack.pop();103 Definition def = (Definition)this.defStack.pop(); 98 104 try { 99 105 AttributeDefinition attDef = new AttributeDefinition(atts.getValue("name"), atts.getValue("type")); … … 106 112 attDef.setDefault(defaultValue); 107 113 } 108 109 114 def.addAttribute(attDef); 110 115 } catch (Throwable t) { … … 128 133 this.currentNamespace = "__default"; 129 134 } 135 136 // set the constructor 137 if (qName.equals("constructor")) { 138 ConstructorDefinition constructorDef = (ConstructorDefinition)this.defStack.pop(); 139 TagDefinition tagDef = (TagDefinition)this.defStack.peek(); 140 tagDef.setConstructor(constructorDef); 141 } 130 142 131 143 if (qName.equals("tag")) { trunk/src/net/schst/XJConf/TagDefinition.java
r13 r16 17 17 private String setter = null; 18 18 private String nameAttribute = null; 19 19 private ConstructorDefinition constructor = null; 20 21 public void setConstructor(ConstructorDefinition constructor) { 22 this.constructor = constructor; 23 } 24 20 25 /** 21 26 * Constructor for simple types … … 146 151 147 152 Class cl; 148 Object instance ;153 Object instance = null; 149 154 150 155 // get the data … … 162 167 } 163 168 164 // constructor with one parameter 165 try { 166 // get the constructor 167 Class paramTypes[] = {data.getClass()}; 168 Constructor co = cl.getConstructor(paramTypes); 169 170 // call the constructor 171 String params[] = {data}; 172 instance = co.newInstance(params); 173 174 } catch (Throwable e) { 175 // get a new instance without constructor parameters 176 try { 177 instance = cl.newInstance(); 178 } catch (Exception t) { 179 throw new ValueConversionException("Could not create instance of " + this.type, t); 180 } 181 } 169 // A constructor has been defined 170 if (this.constructor != null) { 171 ArrayList conParams = this.constructor.getParams(); 172 AttributeDefinition att; 173 174 Class[] cParamTypes = new Class[conParams.size()]; 175 Object[] cParams = new Object[conParams.size()]; 176 for (int i = 0; i < conParams.size(); i++) { 177 att = (AttributeDefinition)conParams.get(i); 178 String attName = att.getName(); 179 String attVal = tag.getAttribute(attName); 180 Object val = att.convertValue(attVal,loader); 181 182 cParamTypes[i] = val.getClass(); 183 cParams[i] = val; 184 } 185 186 try { 187 Constructor co = cl.getConstructor(cParamTypes); 188 instance = co.newInstance(cParams); 189 } catch (Exception e){ 190 throw new ValueConversionException("Could not instantiate " + this.getType(), e); 191 } 192 } 193 194 if (instance == null) { 195 // constructor with one parameter 196 try { 197 // get the constructor 198 Class paramTypes[] = {data.getClass()}; 199 Constructor co = cl.getConstructor(paramTypes); 200 201 // call the constructor 202 String params[] = {data}; 203 instance = co.newInstance(params); 204 205 } catch (Throwable e) { 206 // get a new instance without constructor parameters 207 try { 208 instance = cl.newInstance(); 209 } catch (Exception t) { 210 throw new ValueConversionException("Could not create instance of " + this.type, t); 211 } 212 } 213 } 214 182 215 String methodName = null; 183 216
