Changeset 64

Show
Ignore:
Timestamp:
08/05/06 17:29:33 (2 years ago)
Author:
schst
Message:

Added possibility to use a factory method for creating objects. This feature is still under development and subject to change

Files:

Legend:

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

    r61 r64  
    2323public class DefinitionParser extends DefaultHandler { 
    2424     
    25     /** 
     25    private static final String TAG_FACTORY_METHOD = "factoryMethod"; 
     26 
     27    /** 
    2628     * Stack for currently open definitions 
    2729     */ 
     
    174176            this.defStack.push(def); 
    175177        } 
     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 
    176186 
    177187        // define the character data 
     
    266276        } 
    267277 
     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         
    268290        // set the cdata handling 
    269291        if (qName.equals("cdata")) { 
  • trunk/src/main/java/net/schst/XJConf/TagDefinition.java

    r61 r64  
    2323    private String nameAttribute = null; 
    2424    private ConstructorDefinition constructor = null; 
    25  
    26     private ValueConverter vConverter = null; 
     25    private FactoryMethodDefinition factoryMethod = null; 
     26 
     27    private ValueConverter valueConverter; 
    2728     
    2829    // TODO: Eventually call the setter method for the cdata 
     
    5960    public void setType(String type) { 
    6061        this.type    = type; 
    61         if (this.type.indexOf(".") == -1) { 
    62             this.vConverter = new PrimitiveValueConverter(type); 
    63         } else { 
    64             this.vConverter = new ObjectValueConverter(type); 
    65         } 
    6662    } 
    6763     
     
    7571     */ 
    7672    public void addChildDefinition(Definition def) throws Exception { 
     73                 
    7774        if (def instanceof AttributeDefinition) { 
    7875            this.addAttribute((AttributeDefinition)def); 
    7976            return; 
    8077        } 
     78        if (def instanceof FactoryMethodDefinition) { 
     79            this.factoryMethod = (FactoryMethodDefinition)def; 
     80            return; 
     81        } 
    8182        if (def instanceof ConstructorDefinition) { 
    8283            this.constructor = (ConstructorDefinition)def; 
     
    173174    public Class getValueType(Tag tag, ClassLoader loader) { 
    174175        try { 
    175             return this.vConverter.getType(loader); 
     176            return this.getValueConverter().getType(loader); 
    176177        } catch (Exception e) { 
    177178            throw new RuntimeException("Could not return type."); 
     
    203204        return "set" + this.name.substring(0,1).toUpperCase() + this.name.substring(1); 
    204205    } 
    205      
     206 
    206207    /** 
    207208     * Convert the value of the tag. 
     
    232233        } 
    233234         
    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        } 
    235241        Definition paramDef; 
    236242         
     
    244250            cParamTypes[i] = paramDef.getValueType(tag, loader); 
    245251        } 
    246         Object instance = this.vConverter.convertValue(cParams, cParamTypes, loader); 
     252        Object instance = this.getValueConverter().convertValue(cParams, cParamTypes, loader); 
    247253         
    248254        // add attributes and child elements 
     
    434440        return copy; 
    435441    } 
     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    } 
    436463}