Changeset 26
- Timestamp:
- 07/26/05 10:38:35 (3 years ago)
- Files:
-
- trunk/src/net/schst/XJConf/AttributeDefinition.java (modified) (5 diffs)
- trunk/src/net/schst/XJConf/DefinitionParser.java (modified) (3 diffs)
- trunk/src/net/schst/XJConf/Examples/Color.java (modified) (1 diff)
- trunk/src/net/schst/XJConf/Examples/TestAttributesRequired.java (added)
- trunk/src/net/schst/XJConf/MissingAttributeException.java (added)
- trunk/xml/defines-attributes-required.xml (added)
- trunk/xml/test-attributes-required.xml (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/net/schst/XJConf/AttributeDefinition.java
r25 r26 13 13 * - Default value for non-existent attributes 14 14 * - Setter method to set the attribute 15 * - Whether the attribute is required, or not 15 16 * 16 17 * @author Stephan Schmidt <stephan.schmidt@schlund.de> … … 18 19 public class AttributeDefinition implements Definition{ 19 20 21 /** 22 * Name of the attribute 23 */ 20 24 private String name = null; 25 26 /** 27 * Type of the attribute 28 */ 21 29 private String type = null; 30 31 /** 32 * Name of the setter method 33 */ 22 34 private String setter = null; 35 36 /** 37 * Default value 38 */ 23 39 private String defaultValue = null; 24 40 41 /** 42 * Whether the attribute is required 43 */ 44 private boolean required = false; 45 25 46 /** 26 47 * create a new attribute definition for a String attribute … … 64 85 } 65 86 87 /** 88 * @return Returns the required. 89 */ 90 public boolean isRequired() { 91 return this.required; 92 } 93 /** 94 * @param required The required to set. 95 */ 96 public void setRequired(boolean required) { 97 this.required = required; 98 } 99 66 100 /** 67 101 * Set the setter method … … 137 171 * @throws ValueConversionException 138 172 */ 139 public Object convertValue(Tag tag, ClassLoader loader) throws ValueConversionException { 173 public Object convertValue(Tag tag, ClassLoader loader) 174 throws ValueConversionException { 140 175 String value; 141 176 … … 145 180 value = this.getDefault(); 146 181 } 182 147 183 if (value == null) { 184 if (this.isRequired()) { 185 throw new MissingAttributeException("The attribute '" + this.name + "' is required for the tag '" + tag.getName() + "'."); 186 } 187 188 // it's no use to create an instance of a class passing null 189 // to the constructor. This will at least fail with Integers! 148 190 return null; 149 191 } 192 150 193 Object instance = null; 151 194 try { 152 195 Class cl = Class.forName(this.type, true,loader); 153 196 Class paramTypes[] = {String.class}; 154 197 155 198 Constructor co = cl.getConstructor(paramTypes); 156 199 String params[] = {value}; trunk/src/net/schst/XJConf/DefinitionParser.java
r21 r26 83 83 TagDefinition def = new TagDefinition(atts.getValue("name"), atts.getValue("type")); 84 84 85 // key attribute 85 86 String keyAtt = atts.getValue("keyAttribute"); 86 87 if (keyAtt != null) { … … 92 93 } 93 94 } 95 96 // setter 94 97 String setter = atts.getValue("setter"); 95 98 if (setter != null) { … … 134 137 try { 135 138 AttributeDefinition attDef = new AttributeDefinition(atts.getValue("name"), atts.getValue("type")); 139 140 // setter method 136 141 String setter = atts.getValue("setter"); 137 142 if (setter != null) { 138 143 attDef.setSetterMethod(setter); 139 144 } 145 146 // default value 140 147 String defaultValue = atts.getValue("default"); 141 148 if (defaultValue != null) { 142 149 attDef.setDefault(defaultValue); 143 150 } 151 152 // required 153 String requiredAtt = atts.getValue("required"); 154 if (requiredAtt != null && requiredAtt.equals("true")) { 155 attDef.setRequired(true); 156 } 144 157 def.addChildDefinition(attDef); 158 145 159 } catch (Exception e) { 146 160 throw new XJConfException("Could not process attribute", e); trunk/src/net/schst/XJConf/Examples/Color.java
r1 r26 23 23 this.name = name; 24 24 } 25 25 26 26 public void setRed(Integer val) { 27 27 this.red = val;
