Changeset 30

Show
Ignore:
Timestamp:
07/26/05 13:27:58 (3 years ago)
Author:
schst
Message:

Implemented request #6 support for primitive types

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk

    • Property svn:ignore changed from
      build
      dist
      to
      build
      dist
      .settings
  • trunk/src/net/schst/XJConf/AttributeDefinition.java

    r26 r30  
    11package net.schst.XJConf; 
    2  
    3 import java.lang.reflect.Constructor; 
    42 
    53/** 
     
    4442    private boolean required = false; 
    4543     
     44    /** 
     45     * Converter used to convert the attribute 
     46     */ 
     47    private ValueConverter vConverter; 
     48     
    4649    /** 
    4750     * create a new attribute definition for a String attribute 
     
    6366        this.name = name; 
    6467        this.type = type; 
     68         
     69        if (this.type.indexOf(".") == -1) { 
     70            this.vConverter = new PrimitiveValueConverter(this.type); 
     71        } else { 
     72            this.vConverter = new ObjectValueConverter(this.type); 
     73        } 
    6574    } 
    6675     
     
    142151    public Class getValueType(Tag tag) { 
    143152        try { 
    144             return Class.forName(this.type); 
    145         } catch (ClassNotFoundException e) { 
     153            return this.vConverter.getType(); 
     154        } catch (Exception e) { 
    146155            throw new RuntimeException("Could not return type."); 
    147156        } 
     
    185194                throw new MissingAttributeException("The attribute '" + this.name + "' is required for the tag '" + tag.getName() + "'."); 
    186195            } 
    187              
    188196            // it's no use to create an instance of a class passing null 
    189197            // to the constructor. This will at least fail with Integers! 
    190198            return null; 
    191199        } 
     200        Class paramTypes[] = {String.class}; 
     201        String params[] = {value}; 
    192202         
    193         Object instance = null; 
    194         try { 
    195             Class cl = Class.forName(this.type, true,loader); 
    196             Class paramTypes[] = {String.class}; 
    197  
    198             Constructor co = cl.getConstructor(paramTypes); 
    199             String params[] = {value}; 
    200             instance = co.newInstance(params); 
    201         } catch (Exception t) { 
    202             throw new ValueConversionException("Could not create attribute " + this.name + " of type " + this.type, t); 
    203         } 
     203        Object instance = this.vConverter.convertValue(params, paramTypes, loader); 
    204204        return instance; 
    205205    } 
  • trunk/src/net/schst/XJConf/CDataDefinition.java

    r25 r30  
    11package net.schst.XJConf; 
    2  
    3 import java.lang.reflect.Constructor; 
    42 
    53/** 
     
    2422 
    2523    /** 
     24     * Converter used to convert the character data 
     25     */ 
     26    private ValueConverter vConverter; 
     27     
     28    /** 
    2629     * create a new CDataDefinition for a String 
    2730     */ 
    2831    public CDataDefinition() { 
    2932        this.type = "java.lang.String"; 
     33        this.vConverter = new ObjectValueConverter(this.type); 
    3034    } 
    3135 
     
    3741    public CDataDefinition(String type) { 
    3842        this.type = type; 
     43 
     44        if (this.type.indexOf(".") == -1) { 
     45            this.vConverter = new PrimitiveValueConverter(this.type); 
     46        } else { 
     47            this.vConverter = new ObjectValueConverter(this.type); 
     48        } 
    3949    } 
    4050 
     
    6575            throws ValueConversionException { 
    6676 
    67         String value = tag.getData(); 
    68         Object instance = null; 
    69         Constructor co = null; 
    70         try { 
    71             Class cl = Class.forName(this.type, true,loader); 
    72             Class paramTypes[] = {String.class}; 
    73              
    74             co = cl.getConstructor(paramTypes); 
    75             String params[] = {value}; 
    76             instance = co.newInstance(params); 
    77         } catch (Exception t) { 
    78             throw new ValueConversionException("Could not convert character data to type " + this.type, t); 
    79         } 
     77        Object params[]    = {tag.getData()}; 
     78        Class paramTypes[] = {String.class}; 
     79         
     80        Object instance = this.vConverter.convertValue(params, paramTypes, loader); 
    8081        return instance; 
    8182    } 
     
    9596    public Class getValueType(Tag tag) { 
    9697        try { 
    97             return Class.forName(this.type); 
    98         } catch (ClassNotFoundException e) { 
     98            return this.vConverter.getType(); 
     99        } catch (Exception e) { 
    99100            throw new RuntimeException("Could not return type."); 
    100101        } 
  • trunk/src/net/schst/XJConf/DefinitionParser.java

    r27 r30  
    7171    */ 
    7272    public void startElement(String namespaceURI, String sName, String qName, Attributes atts) 
    73    throws SAXException { 
     73        throws SAXException { 
    7474 
    7575        // define a tag 
     
    7878            return; 
    7979        } 
    80          
     80 
    8181        // define a tag 
    8282        if (qName.equals("tag")) { 
    83             TagDefinition def = new TagDefinition(atts.getValue("name"), atts.getValue("type")); 
     83 
     84            TagDefinition def; 
     85            String type = atts.getValue("type"); 
     86            if (type != null) { 
     87                def = new TagDefinition(atts.getValue("name"), type); 
     88            } else { 
     89                def = new TagDefinition(atts.getValue("name"), atts.getValue("primitive")); 
     90            } 
    8491 
    8592            // key attribute 
  • trunk/src/net/schst/XJConf/TagDefinition.java

    r29 r30  
    11package net.schst.XJConf; 
    22 
    3 import java.lang.reflect.Constructor; 
    43import java.lang.reflect.Method; 
    54import java.util.ArrayList; 
     
    2120    private ConstructorDefinition constructor = null; 
    2221 
     22    private ValueConverter vConverter = null; 
     23     
    2324    // TODO: Eventually call the setter method for the cdata 
    2425    private CDataDefinition cdata = null; 
     
    2627    /** 
    2728     * Constructor for simple types 
    28      *  
    2929     * @param type 
    3030     */ 
    3131    public TagDefinition(String name, String type) { 
    32         this.name = name; 
     32        this.name    = name; 
    3333        this.tagName = name; 
    34         this.type = type; 
     34        this.type    = type; 
     35        if (this.type.indexOf(".") == -1) { 
     36            this.vConverter = new PrimitiveValueConverter(type); 
     37        } else { 
     38            this.vConverter = new ObjectValueConverter(type); 
     39        } 
    3540    } 
    3641 
     
    133138    public Class getValueType(Tag tag) { 
    134139        try { 
    135             return Class.forName(this.type); 
    136         } catch (ClassNotFoundException e) { 
     140            return this.vConverter.getType(); 
     141        } catch (Exception e) { 
    137142            throw new RuntimeException("Could not return type."); 
    138143        } 
     
    175180        throws ValueConversionException { 
    176181 
    177         Class instanceClass; 
    178         Object instance = null; 
    179  
    180182        // get the data 
    181183        String data = tag.getData(); 
    182184        if (data == null) { 
    183185            data = ""; 
    184         } 
    185  
    186         // try to get the class object 
    187         try { 
    188             instanceClass = Class.forName(this.type,true,loader); 
    189         } catch (Exception e) { 
    190             throw new ValueConversionException("Class " + this.type + " does not exist", e); 
    191186        } 
    192187 
     
    214209            cParamTypes[i] = paramDef.getValueType(tag); 
    215210        } 
    216  
    217         // try to create a new instance 
    218         try { 
    219             Constructor co = instanceClass.getConstructor(cParamTypes); 
    220             instance = co.newInstance(cParams); 
    221         } catch (Exception e){ 
    222             try { 
    223                 // no matching constructor has been found 
    224                 // try to instantiate the class without using 
    225                 // a constructor 
    226                 instance = instanceClass.newInstance(); 
    227             } catch (Exception e2) { 
    228                 throw new ValueConversionException("Could not create instance of " + this.type, e2); 
    229             } 
    230         } 
     211        Object instance = this.vConverter.convertValue(cParams, cParamTypes, loader); 
    231212         
    232213        // add attributes and child elements 
     
    266247                methodName           = att.getSetterMethod(); 
    267248                Class meParamTypes[] = {att.getValueType(tag)}; 
    268                  
    269249                Method me         = cl.getMethod(methodName, meParamTypes); 
    270250                Object meParams[] = {val}; 
    271  
     251                 
    272252                me.invoke(instance, meParams); 
    273253            } catch (Exception e) {