Changeset 26

Show
Ignore:
Timestamp:
07/26/05 10:38:35 (3 years ago)
Author:
schst
Message:

Allow attributes to be set as required (implements request #1)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/net/schst/XJConf/AttributeDefinition.java

    r25 r26  
    1313 * - Default value for non-existent attributes 
    1414 * - Setter method to set the attribute 
     15 * - Whether the attribute is required, or not 
    1516 *  
    1617 * @author Stephan Schmidt <stephan.schmidt@schlund.de> 
     
    1819public class AttributeDefinition implements Definition{ 
    1920     
     21    /** 
     22     * Name of the attribute 
     23     */ 
    2024    private String name = null; 
     25     
     26    /** 
     27     * Type of the attribute 
     28     */ 
    2129    private String type = null; 
     30     
     31    /** 
     32     * Name of the setter method 
     33     */ 
    2234    private String setter = null; 
     35     
     36    /** 
     37     * Default value 
     38     */ 
    2339    private String defaultValue = null; 
    2440     
     41    /** 
     42     * Whether the attribute is required 
     43     */ 
     44    private boolean required = false; 
     45     
    2546    /** 
    2647     * create a new attribute definition for a String attribute 
     
    6485    } 
    6586     
     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 
    66100    /** 
    67101     * Set the setter method 
     
    137171     * @throws ValueConversionException 
    138172     */ 
    139     public Object convertValue(Tag tag, ClassLoader loader) throws ValueConversionException { 
     173    public Object convertValue(Tag tag, ClassLoader loader) 
     174        throws ValueConversionException { 
    140175        String value; 
    141176 
     
    145180            value = this.getDefault(); 
    146181        } 
     182 
    147183        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! 
    148190            return null; 
    149191        } 
     192         
    150193        Object instance = null; 
    151194        try { 
    152195            Class cl = Class.forName(this.type, true,loader); 
    153196            Class paramTypes[] = {String.class}; 
    154              
     197 
    155198            Constructor co = cl.getConstructor(paramTypes); 
    156199            String params[] = {value}; 
  • trunk/src/net/schst/XJConf/DefinitionParser.java

    r21 r26  
    8383            TagDefinition def = new TagDefinition(atts.getValue("name"), atts.getValue("type")); 
    8484 
     85            // key attribute 
    8586            String keyAtt = atts.getValue("keyAttribute"); 
    8687            if (keyAtt != null) { 
     
    9293                } 
    9394            } 
     95 
     96            // setter 
    9497            String setter = atts.getValue("setter"); 
    9598            if (setter != null) { 
     
    134137            try { 
    135138                AttributeDefinition attDef = new AttributeDefinition(atts.getValue("name"), atts.getValue("type")); 
     139                 
     140                // setter method 
    136141                String setter = atts.getValue("setter"); 
    137142                if (setter != null) { 
    138143                    attDef.setSetterMethod(setter); 
    139144                } 
     145                 
     146                // default value 
    140147                String defaultValue = atts.getValue("default"); 
    141148                if (defaultValue != null) { 
    142149                    attDef.setDefault(defaultValue); 
    143150                } 
     151                 
     152                // required 
     153                String requiredAtt = atts.getValue("required"); 
     154                if (requiredAtt != null && requiredAtt.equals("true")) { 
     155                    attDef.setRequired(true); 
     156                } 
    144157                def.addChildDefinition(attDef); 
     158 
    145159            } catch (Exception e) { 
    146160                throw new XJConfException("Could not process attribute", e); 
  • trunk/src/net/schst/XJConf/Examples/Color.java

    r1 r26  
    2323        this.name = name; 
    2424    } 
    25      
     25 
    2626    public void setRed(Integer val) { 
    2727        this.red = val;