Changeset 21

Show
Ignore:
Timestamp:
07/25/05 17:34:56 (3 years ago)
Author:
schst
Message:

Cleaned up the Definition interface (bug #2) and added new definitions for CData and child elements which may be used inside the constructor definition

Files:

Legend:

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

    r16 r21  
    2020    private String name = null; 
    2121    private String type = null; 
    22     private String definition  = null; 
    2322    private String setter = null; 
    2423    private String defaultValue = null; 
     
    125124     * @throws ValueConversionException 
    126125     */ 
    127     public Object convertValue(Object val, ClassLoader loader) throws ValueConversionException { 
    128         String value = (String)val; 
     126    public Object convertValue(Tag tag, ClassLoader loader) throws ValueConversionException { 
     127        String value; 
     128 
     129        if (tag.hasAttribute(this.getName())) { 
     130            value = tag.getAttribute(name); 
     131        } else { 
     132            value = this.getDefault(); 
     133        } 
    129134        Object instance = null; 
    130135        try { 
     
    141146    } 
    142147 
    143     public void addAttribute(AttributeDefinition att) throws Exception { 
     148    /** 
     149     * Add a child definition 
     150     * 
     151     * Attributes cannot have any children.  
     152     */ 
     153    public void addChildDefinition(Definition def) throws Exception { 
    144154    } 
    145155} 
  • trunk/src/net/schst/XJConf/ConstructorDefinition.java

    r16 r21  
    44 
    55/** 
     6 * Definition for the constructor of a class 
     7 *  
    68 * @author Stephan Schmidt <stephan.schmidt@schlund.de> 
    79 */ 
    810public class ConstructorDefinition implements Definition { 
    911 
     12    /** 
     13     * Parameters of the constructor  
     14     */ 
    1015    private ArrayList params = new ArrayList(); 
    11      
    12     public void addAttribute(AttributeDefinition param) { 
    13         this.params.add(param); 
     16 
     17    /** 
     18     * Add a new child definition (equals a parameter of the constructor) 
     19     */ 
     20    public void addChildDefinition(Definition def) throws Exception { 
     21        this.params.add(def); 
    1422    } 
    1523 
     24    /** 
     25     * Get the parameters of the constructor 
     26     * @return 
     27     */ 
    1628    public ArrayList getParams() { 
    1729        return this.params; 
    1830    } 
    1931 
    20     public Object convertValue(Object value, ClassLoader loader) 
     32    /** 
     33     * Convert the constructor. 
     34     *  
     35     * This does not do anything! 
     36     */ 
     37    public Object convertValue(Tag tag, ClassLoader loader) 
    2138            throws ValueConversionException { 
    2239        return null; 
    2340    } 
     41     
     42   /**     
     43    * Get the name under which it will be stored 
     44    */ 
    2445    public String getName() { 
    2546        return "__constructor"; 
    2647    } 
     48 
     49    /** 
     50     * Get the setter method 
     51     */ 
    2752    public String getSetterMethod() { 
    2853        return null; 
    2954    } 
    30      
    31     public String getType() { 
    32         return null; 
    33     } 
    3455} 
  • trunk/src/net/schst/XJConf/Definition.java

    r16 r21  
    77 */ 
    88public interface Definition { 
     9    /** 
     10     * Get the name under which the information 
     11     * will be stored. 
     12     *  
     13     * @return  name of the value 
     14     */ 
    915    public String getName(); 
    10     public String getType(); 
    11     public Object convertValue(Object value, ClassLoader loader) throws ValueConversionException; 
     16 
     17    /** 
     18     * Get the converted value. 
     19     *  
     20     * XJConf will pass the complete tag to this method 
     21     *  
     22     * @param value 
     23     * @param loader 
     24     * @return 
     25     * @throws ValueConversionException 
     26     */ 
     27    public Object convertValue(Tag tag, ClassLoader loader) throws ValueConversionException; 
     28     
     29    /** 
     30     * Get the name of the setter method 
     31     *  
     32     * @return 
     33     */ 
    1234    public String getSetterMethod(); 
    13     public void addAttribute(AttributeDefinition att) throws Exception; 
     35     
     36    /** 
     37     * Add a child definition of any type 
     38     *  
     39     * @param def 
     40     */ 
     41    public void addChildDefinition(Definition def) throws Exception; 
    1442} 
  • trunk/src/net/schst/XJConf/DefinitionParser.java

    r16 r21  
    2121public class DefinitionParser extends DefaultHandler { 
    2222     
     23    /** 
     24     * Stack for currently open definitions 
     25     */ 
    2326    private Stack defStack = new Stack(); 
    2427     
     28    /** 
     29     * The current namespace 
     30     */ 
    2531    private String currentNamespace = "__default"; 
    2632     
     33    /** 
     34     * All extracted namespace definitions 
     35     */ 
    2736    private NamespaceDefinitions defs = new NamespaceDefinitions(); 
    2837 
     
    6776        if (qName.equals("namespace")) { 
    6877            this.currentNamespace = atts.getValue("uri"); 
    69              
    7078            return; 
    7179        } 
     
    95103        if (qName.equals("constructor")) { 
    96104            ConstructorDefinition def = new ConstructorDefinition(); 
     105            this.defStack.push(def); 
     106        } 
     107 
     108        // define the character data 
     109        if (qName.equals("cdata")) { 
     110            CDataDefinition def; 
     111            String type = atts.getValue("type"); 
     112            if (type == null) { 
     113                def = new CDataDefinition(); 
     114            } else { 
     115                def = new CDataDefinition(type); 
     116            } 
     117            String setter = atts.getValue("setter"); 
     118            if (setter != null) { 
     119                def.setSetterMethod(setter); 
     120            } 
     121            this.defStack.push(def); 
     122        } 
     123 
     124        // define a child tag data 
     125        if (qName.equals("child")) { 
     126            ChildDefinition def = new ChildDefinition(atts.getValue("name")); 
    97127            this.defStack.push(def); 
    98128        } 
     
    112142                    attDef.setDefault(defaultValue); 
    113143                } 
    114                 def.addAttribute(attDef); 
    115             } catch (Throwable t) { 
    116                 System.out.println("Jetzt ist was schief gegangen!"); 
     144                def.addChildDefinition(attDef); 
     145            } catch (Exception e) { 
     146                throw new XJConfException("Could not process attribute", e); 
    117147            } 
    118148            this.defStack.push(def); 
     
    128158     */ 
    129159    public void endElement(String namespaceURI, String sName, String qName) 
    130     throws SAXException { 
     160        throws SAXException { 
    131161 
    132162        if (qName.equals("namespace")) { 
     
    138168            ConstructorDefinition constructorDef = (ConstructorDefinition)this.defStack.pop(); 
    139169            TagDefinition tagDef = (TagDefinition)this.defStack.peek(); 
    140             tagDef.setConstructor(constructorDef); 
    141         } 
    142          
     170            try { 
     171                tagDef.addChildDefinition(constructorDef); 
     172            } catch (Exception e) { 
     173                throw new RuntimeException(e.getMessage(), e); 
     174            } 
     175        } 
     176 
     177        // set the cdata handling 
     178        if (qName.equals("cdata")) { 
     179            CDataDefinition cdataDef = (CDataDefinition)this.defStack.pop(); 
     180            Definition parentDef = (Definition)this.defStack.peek(); 
     181            try { 
     182                parentDef.addChildDefinition(cdataDef); 
     183            } catch (Exception e) { 
     184                throw new RuntimeException(e.getMessage(), e); 
     185            } 
     186        } 
     187 
     188        // set the child handling 
     189        if (qName.equals("child")) { 
     190            ChildDefinition childDef = (ChildDefinition)this.defStack.pop(); 
     191            Definition parentDef = (Definition)this.defStack.peek(); 
     192            try { 
     193                parentDef.addChildDefinition(childDef); 
     194            } catch (Exception e) { 
     195                throw new RuntimeException(e.getMessage(), e); 
     196            } 
     197        } 
     198 
    143199        if (qName.equals("tag")) { 
    144  
    145200            TagDefinition def = (TagDefinition)this.defStack.pop(); 
    146201             
  • trunk/src/net/schst/XJConf/Examples/TestConstructor.java

    r16 r21  
    2626        ConstructorColor color = (ConstructorColor)conf.getConfigValue("color"); 
    2727        System.out.println(color); 
     28        color = (ConstructorColor)conf.getConfigValue("color2"); 
     29        System.out.println(color); 
     30        color = (ConstructorColor)conf.getConfigValue("color3"); 
     31        System.out.println(color); 
    2832    } 
    2933} 
  • trunk/src/net/schst/XJConf/Tag.java

    r13 r21  
    33import java.util.ArrayList; 
    44import java.util.HashMap; 
     5import java.util.Iterator; 
    56 
    67import org.xml.sax.Attributes; 
     
    3536    private ArrayList children = new ArrayList(); 
    3637 
     38    /** 
     39     * Tag definition used for this tag 
     40     */ 
    3741    private TagDefinition def = null; 
    3842     
     
    8185    } 
    8286 
     87    public boolean hasAttribute(String name) { 
     88        return this.atts.containsKey(name); 
     89    } 
     90     
    8391   /** 
    8492    * get an attribute 
     
    100108    } 
    101109 
     110    public Tag getChild(String name) { 
     111        for (Iterator iter = this.children.iterator(); iter.hasNext();) { 
     112            Tag child = (Tag) iter.next(); 
     113            if (child.getName().equals(name)) { 
     114                return child; 
     115            } 
     116        } 
     117        return null; 
     118    } 
     119     
    102120   /** 
    103121    * Get the name of the tag 
  • trunk/src/net/schst/XJConf/TagDefinition.java

    r17 r21  
    1111 * Defintion of an XML tag 
    1212 *  
    13  * @author sschmidt 
     13 * @author Stephan Schmidt <stephan.schmidt@schlund.de> 
    1414 */ 
    1515public class TagDefinition implements Definition { 
     
    2222    private ConstructorDefinition constructor = null; 
    2323     
    24     public void setConstructor(ConstructorDefinition constructor) { 
    25         this.constructor = constructor; 
    26     } 
    27      
     24    // TODO: Eventually call the setter method for the cdata 
     25    private CDataDefinition cdata = null; 
     26 
    2827    /** 
    2928     * Constructor for simple types 
     
    3635        this.type = type; 
    3736    } 
    38      
     37 
     38    /** 
     39     * Add a new child definition 
     40     *  
     41     * Possible definitions are: 
     42     * - AttributeDefinition 
     43     * - ConstrcutorDefinition 
     44     * - CDataDefinition 
     45     */ 
     46    public void addChildDefinition(Definition def) throws Exception { 
     47        if (def instanceof AttributeDefinition) { 
     48            this.addAttribute((AttributeDefinition)def); 
     49            return; 
     50        } 
     51        if (def instanceof ConstructorDefinition) { 
     52            this.constructor = (ConstructorDefinition)def; 
     53            return; 
     54        } 
     55        if (def instanceof CDataDefinition) { 
     56            this.cdata = (CDataDefinition)def; 
     57            return; 
     58        } 
     59    } 
     60     
    3961    /** 
    4062     * Add an attribute to the tag 
     
    150172     * @throws ValueConversionException 
    151173     */ 
    152     public Object convertValue(Object v, ClassLoader loader) throws ValueConversionException { 
    153         Tag tag = (Tag)v; 
    154          
     174    public Object convertValue(Tag tag, ClassLoader loader) throws ValueConversionException { 
    155175        Class cl; 
    156176        Object instance = null; 
     
    169189            throw new ValueConversionException("Class " + this.type + " does not exist", e); 
    170190        } 
    171          
    172         // A constructor has been defined 
    173         if (this.constructor != null) { 
    174             ArrayList conParams = this.constructor.getParams(); 
    175             AttributeDefinition att; 
    176              
    177             Class[] cParamTypes = new Class[conParams.size()]; 
    178             Object[] cParams = new Object[conParams.size()];      
    179             for (int i = 0; i < conParams.size(); i++) { 
    180                 att = (AttributeDefinition)conParams.get(i); 
    181                 String attName = att.getName(); 
    182                 String attVal = tag.getAttribute(attName); 
    183                 Object val = att.convertValue(attVal,loader); 
    184                  
    185                 cParamTypes[i] = val.getClass(); 
    186                 cParams[i] = val; 
     191 
     192        // no constructor definition has been set, 
     193        // create a new one 
     194        if (this.constructor == null) { 
     195            this.constructor = new ConstructorDefinition(); 
     196            try { 
     197                this.constructor.addChildDefinition(new CDataDefinition()); 
     198            } catch (Exception e) { 
     199                throw new ValueConversionException("Could not create constructor object", e); 
    187200            } 
    188              
     201        } 
     202         
     203        ArrayList conParams = this.constructor.getParams(); 
     204        Definition paramDef; 
     205         
     206        Class[] cParamTypes = new Class[conParams.size()]; 
     207        Object[] cParams    = new Object[conParams.size()]; 
     208         
     209        // get all values and their types 
     210        for (int i = 0; i < conParams.size(); i++) { 
     211            paramDef = (Definition)conParams.get(i); 
     212            Object val = paramDef.convertValue(tag,loader);                
     213            cParamTypes[i] = val.getClass(); 
     214            cParams[i] = val; 
     215        } 
     216 
     217        // try to create a new instance 
     218        try { 
     219            Constructor co = cl.getConstructor(cParamTypes); 
     220            instance = co.newInstance(cParams); 
     221        } catch (Exception e){ 
    189222            try { 
    190                 Constructor co = cl.getConstructor(cParamTypes); 
    191                 instance = co.newInstance(cParams); 
    192             } catch (Exception e){ 
    193                 throw new ValueConversionException("Could not instantiate " + this.getType(), e); 
     223                instance = cl.newInstance(); 
     224            } catch (Exception t) { 
     225                throw new ValueConversionException("Could not create instance of " + this.type, t); 
    194226            } 
    195227        } 
    196228 
    197         if (instance == null) { 
    198             // constructor with one parameter 
    199             try { 
    200                 // get the constructor 
    201                 Class paramTypes[] = {data.getClass()}; 
    202                 Constructor co = cl.getConstructor(paramTypes); 
    203                  
    204                 // call the constructor 
    205                 String params[] = {data}; 
    206                 instance = co.newInstance(params); 
    207                  
    208             } catch (Throwable e) { 
    209                 // get a new instance without constructor parameters 
    210                 try { 
    211                     instance = cl.newInstance(); 
    212                 } catch (Exception t) { 
    213                     throw new ValueConversionException("Could not create instance of " + this.type, t); 
    214                 } 
    215             } 
    216         } 
    217  
     229        // set all attributes 
    218230        String methodName = null; 
    219          
    220         // set all attributes 
    221231        for (int i = 0; i < this.atts.size(); i++) { 
    222232             
    223233            // get the attribute definition 
    224234            AttributeDefinition att = (AttributeDefinition)this.atts.get(i); 
    225             String attName = att.getName(); 
    226             String attVal = tag.getAttribute(attName); 
    227              
    228             // attribute is not set 
    229             if (attVal == null) { 
    230                  
    231                 // check for default 
    232                 if (att.getDefault() == null) { 
    233                     continue; 
    234                 } 
    235                 attVal = att.getDefault(); 
    236             } 
    237             Object val = att.convertValue(attVal,loader); 
     235            Object val = att.convertValue(tag,loader); 
    238236             
    239237            try { 
     
    245243                 
    246244                me.invoke(instance, meParams); 
    247                  
    248245            } catch (Exception t) { 
    249                 throw new ValueConversionException("Could not set attribute " + attName + " of " + this.type, t); 
     246                throw new ValueConversionException("Could not set attribute " + att.getName() + " of " + this.type, t); 
    250247            } 
    251248        } 
     
    261258 
    262259            methodName = child.getDefinition().getSetterMethod(); 
     260             
     261            // if the method name is set to ignore, the child will 
     262            // be ignored 
     263            if (methodName != null && methodName.equals("__ignore")) { 
     264                continue; 
     265            } 
    263266            Object childValue = child.getDefinition().convertValue(child,loader); 
    264267            Object childParams[] = {childValue}; 
  • trunk/src/net/schst/XJConf/XmlReader.java

    r15 r21  
    183183            throw new InternalError("Could not configure the parser correctly.");  
    184184        } catch (SAXException e) { 
     185            e.printStackTrace(); 
     186            System.exit(0); 
    185187            throw new XJConfException(e.getMessage()); 
    186188        } catch (IOException e) { 
     
    218220    */ 
    219221    public void startElement(String namespaceURI, String sName, String qName, Attributes atts) 
    220     throws SAXException { 
     222        throws SAXException { 
    221223         
    222224        if (this.myNamespace.equals(namespaceURI) && this.depth > 0) { 
    223225            return; 
    224226        } 
    225          
    226227        this.depth++; 
    227228         
     
    266267            return; 
    267268        } 
    268  
    269269        this.depth--; 
    270270 
     
    281281        // get the last tag from the stack 
    282282        Tag tag = (Tag)this.tagStack.pop(); 
     283 
    283284         
    284285        if (this.extensions.containsKey(namespaceURI)) { 
    285286            ((Extension)(this.extensions.get(namespaceURI))).endElement(this, tag); 
    286287        } else { 
    287  
    288288            if (this.depth == 1) { 
    289289                this.config.put(tag.getKey(), tag.getConvertedValue(this.loader)); 
  • trunk/xml/defines-constructor.xml

    r16 r21  
    77        </constructor> 
    88    </tag> 
     9 
     10    <tag name="color2" type="net.schst.XJConf.Examples.ConstructorColor"> 
     11        <constructor> 
     12            <attribute name="red" type="java.lang.Integer"/> 
     13            <attribute name="green" type="java.lang.Integer"/> 
     14            <cdata type="java.lang.Integer"/> 
     15        </constructor> 
     16    </tag> 
     17 
     18    <tag name="color3" type="net.schst.XJConf.Examples.ConstructorColor"> 
     19        <constructor> 
     20            <child name="red"/> 
     21            <child name="green"/> 
     22            <child name="blue"/> 
     23        </constructor> 
     24    </tag> 
     25 
     26    <tag name="red" type="java.lang.Integer" setter="__ignore"/> 
     27    <tag name="green" type="java.lang.Integer" setter="__ignore"/> 
     28    <tag name="blue" type="java.lang.Integer" setter="__ignore"/> 
    929</defines> 
  • trunk/xml/test-constructor.xml

    r16 r21  
    11<configuration> 
    22    <color red="100" green="25" blue="10"/> 
     3     
     4    <!-- The cdata is the third parameter in the constructor --> 
     5    <color2 red="100" green="25">23</color2> 
     6 
     7    <!-- Use child tags for the constructor --> 
     8    <color3> 
     9        <red>111</red> 
     10        <green>222</green> 
     11        <blue>333</blue> 
     12    </color3> 
    313</configuration> 
  • trunk/xml/test.xml

    r1 r21  
    1717        <color red="0" green="20" blue="80" title="A dark blue">dark-blue</color> 
    1818    </complex2> 
    19  
    2019</configuration>