Changeset 16

Show
Ignore:
Timestamp:
07/22/05 16:43:43 (3 years ago)
Author:
schst
Message:

Allow definition of a constructor, this is very alpha and needs a rewrite, SUE AT YOUR OWN RISK!

Files:

Legend:

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

    r13 r16  
    140140        return instance; 
    141141    } 
     142 
     143    public void addAttribute(AttributeDefinition att) throws Exception { 
     144    } 
    142145} 
  • trunk/src/net/schst/XJConf/Definition.java

    r13 r16  
    1111    public Object convertValue(Object value, ClassLoader loader) throws ValueConversionException; 
    1212    public String getSetterMethod(); 
     13    public void addAttribute(AttributeDefinition att) throws Exception; 
    1314} 
  • trunk/src/net/schst/XJConf/DefinitionParser.java

    r7 r16  
    9292        } 
    9393         
     94        // define the constructor 
     95        if (qName.equals("constructor")) { 
     96            ConstructorDefinition def = new ConstructorDefinition(); 
     97            this.defStack.push(def); 
     98        } 
     99         
    94100        // define an attribute 
    95101        if (qName.equals("attribute")) { 
    96102            // get the current tag 
    97             TagDefinition def = (TagDefinition)this.defStack.pop(); 
     103            Definition def = (Definition)this.defStack.pop(); 
    98104            try { 
    99105                AttributeDefinition attDef = new AttributeDefinition(atts.getValue("name"), atts.getValue("type")); 
     
    106112                    attDef.setDefault(defaultValue); 
    107113                } 
    108  
    109114                def.addAttribute(attDef); 
    110115            } catch (Throwable t) { 
     
    128133            this.currentNamespace = "__default"; 
    129134        } 
     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        } 
    130142         
    131143        if (qName.equals("tag")) { 
  • trunk/src/net/schst/XJConf/TagDefinition.java

    r13 r16  
    1717    private String setter = null; 
    1818    private String nameAttribute = null; 
    19      
     19    private ConstructorDefinition constructor = null; 
     20     
     21    public void setConstructor(ConstructorDefinition constructor) { 
     22        this.constructor = constructor; 
     23    } 
     24     
    2025    /** 
    2126     * Constructor for simple types 
     
    146151         
    147152        Class cl; 
    148         Object instance
     153        Object instance = null
    149154         
    150155        // get the data 
     
    162167        } 
    163168         
    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 
    182215        String methodName = null; 
    183216