Changeset 39

Show
Ignore:
Timestamp:
08/03/05 13:00:09 (3 years ago)
Author:
schst
Message:

First working version of extensions that are able to return values to the surrounding tag

Files:

Legend:

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

    r37 r39  
    167167     * @return  Class object 
    168168     */ 
    169     public Class getValueType(DefinedTag tag, ClassLoader loader) { 
     169    public Class getValueType(Tag tag, ClassLoader loader) { 
    170170        try { 
    171171            return this.vConverter.getType(loader); 
     
    198198     * @throws ValueConversionException 
    199199     */ 
    200     public Object convertValue(DefinedTag tag, ClassLoader loader) 
     200    public Object convertValue(Tag tag, ClassLoader loader) 
    201201        throws ValueConversionException { 
    202202        String value; 
  • trunk/src/net/schst/XJConf/CDataDefinition.java

    r37 r39  
    7474     * Convert the character data to any type 
    7575     */ 
    76     public Object convertValue(DefinedTag tag, ClassLoader loader) 
     76    public Object convertValue(Tag tag, ClassLoader loader) 
    7777            throws ValueConversionException { 
    7878 
    79         Object params[]    = {tag.getData()}; 
    80         Class paramTypes[] = {String.class}; 
     79        Object[] params    = {tag.getContent()}; 
     80        Class[] paramTypes = new Class[1]; 
     81        if (tag.getContent() == null) { 
     82            paramTypes[0] = String.class; 
     83        } else { 
     84            paramTypes[0] = tag.getContent().getClass(); 
     85        } 
    8186         
    8287        Object instance = this.vConverter.convertValue(params, paramTypes, loader); 
     
    96101     * @return  Class object 
    97102     */ 
    98     public Class getValueType(DefinedTag tag, ClassLoader loader) { 
     103    public Class getValueType(Tag tag, ClassLoader loader) { 
    99104        try { 
    100105            return this.vConverter.getType(loader); 
  • trunk/src/net/schst/XJConf/ChildDefinition.java

    r37 r39  
    4141     * Convert the value 
    4242     */ 
    43     public Object convertValue(DefinedTag tag, ClassLoader loader) 
     43    public Object convertValue(Tag tag, ClassLoader loader) 
    4444            throws ValueConversionException { 
    45         DefinedTag child = tag.getChild(this.getName()); 
     45        Tag child = tag.getChild(this.getName()); 
    4646        if (child == null) { 
    4747            throw new RuntimeException("Child element " + this.getName() + " does not exist"); 
     
    5555     * @return  Class object 
    5656     */ 
    57     public Class getValueType(DefinedTag tag, ClassLoader loader) { 
    58         DefinedTag child = tag.getChild(this.getName()); 
     57    public Class getValueType(Tag tag, ClassLoader loader) { 
     58        Tag child = tag.getChild(this.getName()); 
    5959        if (child == null) { 
    6060            throw new RuntimeException("Child element " + this.getName() + " does not exist"); 
  • trunk/src/net/schst/XJConf/ConstructorDefinition.java

    r37 r39  
    3737     * This does not do anything! 
    3838     */ 
    39     public Object convertValue(DefinedTag tag, ClassLoader loader) 
     39    public Object convertValue(Tag tag, ClassLoader loader) 
    4040            throws ValueConversionException { 
    4141        return null; 
     
    5454     * @return  Always returns null  
    5555     */ 
    56     public Class getValueType(DefinedTag tag, ClassLoader loader) { 
     56    public Class getValueType(Tag tag, ClassLoader loader) { 
    5757        return null; 
    5858    } 
  • trunk/src/net/schst/XJConf/DefinedTag.java

    r38 r39  
    1717 * @author Stephan Schmidt 
    1818 */ 
    19 public class DefinedTag
     19public class DefinedTag implements Tag
    2020     
    2121   /** 
     
    2828    */ 
    2929    private StringBuffer data = new StringBuffer(); 
     30     
     31     
     32    private Object content = null; 
    3033     
    3134   /** 
     
    7477    * @return       number of childs added 
    7578    */ 
    76     public int addChild(DefinedTag child) { 
     79    public int addChild(Tag child) { 
    7780        this.children.add(child); 
    7881        return this.children.size(); 
     
    123126     * @return 
    124127     */ 
    125     public DefinedTag getChild(String name) { 
     128    public Tag getChild(String name) { 
    126129        for (Iterator iter = this.children.iterator(); iter.hasNext();) { 
    127130            DefinedTag child = (DefinedTag) iter.next(); 
     
    183186     * @return  Class object 
    184187     */ 
    185     public Class getValueType(DefinedTag tag, ClassLoader loader) { 
     188    public Class getValueType(Tag tag, ClassLoader loader) { 
    186189        return this.def.getValueType(tag, loader); 
    187190    } 
     
    196199        return this.def.getKey(this); 
    197200    } 
     201 
     202    public String getSetterMethod() { 
     203        return this.def.getSetterMethod(); 
     204    }     
     205 
     206    public void setContent(Object content) { 
     207        this.content = content; 
     208    } 
     209 
     210    public Object getContent() { 
     211        if (this.content != null) { 
     212            return this.content; 
     213        } 
     214        return this.getData(); 
     215    } 
     216     
     217    public boolean supportsIndexedChildren() { 
     218        return this.def.supportsIndexedChildren(); 
     219    }    
    198220} 
  • trunk/src/net/schst/XJConf/Definition.java

    r37 r39  
    2727     * @throws ValueConversionException 
    2828     */ 
    29     public Object convertValue(DefinedTag tag, ClassLoader loader) throws ValueConversionException; 
     29    public Object convertValue(Tag tag, ClassLoader loader) throws ValueConversionException; 
    3030     
    3131    /** 
     
    3535     * @return 
    3636     */ 
    37     public Class getValueType(DefinedTag tag, ClassLoader loader); 
     37    public Class getValueType(Tag tag, ClassLoader loader); 
    3838     
    3939    /** 
  • trunk/src/net/schst/XJConf/Extension.java

    r37 r39  
    2525     * @throws XJConfException 
    2626     */ 
    27     public void startElement(XmlReader reader, DefinedTag tag) 
     27    public void startElement(XmlReader reader, Tag tag) 
    2828        throws XJConfException; 
    2929 
     
    3535     * @throws XJConfException 
    3636     */ 
    37     public Object endElement(XmlReader reader, DefinedTag tag) 
     37    public Tag endElement(XmlReader reader, Tag tag) 
    3838        throws XJConfException;    
    3939    } 
  • trunk/src/net/schst/XJConf/GenericTag.java

    r38 r39  
    2828 
    2929    /** 
     30     * Content of the tag (overrides data) 
     31     */ 
     32    private Object content = null; 
     33     
     34    /** 
    3035     * attributes of the tag 
    3136     */ 
     
    3742    private ArrayList children = new ArrayList(); 
    3843 
     44    /** 
     45     * value of the tag 
     46     */ 
     47    private Object value = null; 
     48     
     49    /** 
     50     * Key of the tag 
     51     */ 
     52    private String key = null; 
     53     
    3954    /** 
    4055     * Create a new tag without attributes 
     
    6176    } 
    6277 
    63     /** 
    64      * Add text data 
    65      */ 
    66     public int addData(char buf[], int offset, int len) { 
    67         String s = new String(buf, offset, len); 
    68         this.data.append(s); 
    69         return this.data.length(); 
    70     } 
    71  
    72     /** 
    73      * Check, whether the tag has a certain attribute 
    74      *  
    75      * @param name 
    76      * @return 
    77      */ 
    78     public boolean hasAttribute(String name) { 
    79         return this.atts.containsKey(name); 
    80     } 
    81      
    82    /** 
    83     * get an attribute 
    84     *  
    85     * @param    name of the attribute 
    86     * @return   value of the attribute 
    87     */ 
    88     public String getAttribute(String name) { 
    89         return (String)this.atts.get(name); 
    90     } 
    91  
    92    /** 
    93     * Get all children of the tag 
    94     *  
    95     * @return   children 
    96     */ 
    97     public ArrayList getChildren() { 
    98         return this.children; 
    99     } 
    100  
    101     /** 
    102      * Get the child with a specific name 
    103      *  
    104      * @param name 
    105      * @return 
    106      */ 
    107     public Tag getChild(String name) { 
    108         for (Iterator iter = this.children.iterator(); iter.hasNext();) { 
    109             Tag child = (Tag) iter.next(); 
    110             if (child.getName().equals(name)) { 
    111                 return child; 
    112             } 
    113         } 
    114         return null; 
    115     } 
    116  
    117    /** 
    118     * Get the name of the tag 
    119     *  
    120     * @return   name of the tag 
    121     */ 
    122     public String getName() { 
    123         return this.name; 
    124     } 
    125      
    126    /** 
    127     * Get the character data of the tag 
    128     *  
    129     * @return   character data 
    130     */ 
    131     public String getData() { 
    132         return this.data.toString().trim(); 
    133     } 
    134      
    135      
     78    /** 
     79     * Add text data 
     80     */ 
     81    public int addData(char buf[], int offset, int len) { 
     82        String s = new String(buf, offset, len); 
     83        this.data.append(s); 
     84        return this.data.length(); 
     85    } 
     86 
     87    /** 
     88     * Check, whether the tag has a certain attribute 
     89     *  
     90     * @param name 
     91     * @return 
     92     */ 
     93    public boolean hasAttribute(String name) { 
     94        return this.atts.containsKey(name); 
     95    } 
     96 
     97    /** 
     98     * get an attribute 
     99     *  
     100     * @param    name of the attribute 
     101     * @return   value of the attribute 
     102     */ 
     103    public String getAttribute(String name) { 
     104        return (String) this.atts.get(name); 
     105    } 
     106 
     107    /** 
     108     * Get all children of the tag 
     109     *  
     110     * @return   children 
     111     */ 
     112    public ArrayList getChildren() { 
     113        return this.children; 
     114    } 
     115 
     116    /** 
     117     * Get the child with a specific name 
     118     *  
     119     * @param name 
     120     * @return 
     121     */ 
     122    public Tag getChild(String name) { 
     123        for (Iterator iter = this.children.iterator(); iter.hasNext();) { 
     124            Tag child = (Tag) iter.next(); 
     125            if (child.getName().equals(name)) { 
     126                return child; 
     127            } 
     128        } 
     129        return null; 
     130    } 
     131 
     132    /** 
     133     * Get the name of the tag 
     134     *  
     135     * @return   name of the tag 
     136     */ 
     137    public String getName() { 
     138        return this.name; 
     139    } 
     140 
     141    /** 
     142     * Get the character data of the tag 
     143     *  
     144     * @return   character data 
     145     */ 
     146    public String getData() { 
     147        return this.data.toString().trim(); 
     148    } 
     149 
     150    /** 
     151     * Add a new child to this tag. 
     152     *  
     153     * @param child  child to add 
     154     * @return       number of childs added 
     155     */ 
    136156    public int addChild(Tag child) { 
    137         return 0; 
    138     } 
    139  
     157        this.children.add(child); 
     158        return this.children.size(); 
     159    } 
     160 
     161    /** 
     162     * Fetch the value 
     163     *  
     164     * @param   loader 
     165     * @retun               the value of the tag 
     166     */ 
    140167    public Object getConvertedValue(ClassLoader loader) 
    141168            throws ValueConversionException { 
    142         return null; 
    143     } 
    144  
     169        return this.value; 
     170    } 
     171 
     172    /** 
     173     * Get the key under which the value will be stored 
     174     */ 
    145175    public String getKey() { 
    146         // TODO Auto-generated method stub 
    147         return null; 
    148     } 
    149  
    150     public Class getValueType(DefinedTag tag, ClassLoader loader) { 
    151         // TODO Auto-generated method stub 
    152         return null; 
    153     } 
     176        return this.key; 
     177    } 
     178 
     179    /** 
     180     * Set the value of the tag 
     181     *  
     182     * @param value 
     183     */ 
     184    public void setValue(Object value) { 
     185        this.value = value; 
     186    } 
     187     
     188    /** 
     189     * Get the type of the value 
     190     */ 
     191    public Class getValueType(Tag tag, ClassLoader loader) { 
     192        if (this.value == null) { 
     193            return null; 
     194        } 
     195        return this.value.getClass(); 
     196    } 
     197 
     198    /** 
     199     * Set the key 
     200     *  
     201     * @param key 
     202     */ 
     203    public void setKey(String key) { 
     204        this.key = key; 
     205    } 
     206 
     207    /** 
     208     * Get the setter method 
     209     */ 
     210    public String getSetterMethod() { 
     211        if (this.key == null) { 
     212            return null; 
     213        } 
     214        return "set" + this.key.substring(0,1).toUpperCase() + this.key.substring(1); 
     215    } 
     216 
     217    /** 
     218     * Set the content (overrides the character data) 
     219     */ 
     220    public void setContent(Object content) { 
     221        this.content = content; 
     222    } 
     223 
     224    /** 
     225     * Get the content 
     226     */ 
     227    public Object getContent() { 
     228        if (this.content != null) { 
     229            return this.content; 
     230        } 
     231        return this.getData(); 
     232    } 
     233 
     234    /** 
     235     * Checks, whether the tag supports indexed children 
     236     */ 
     237    public boolean supportsIndexedChildren() { 
     238        return true; 
     239    }    
    154240} 
  • trunk/src/net/schst/XJConf/ObjectValueConverter.java

    r32 r39  
    4646        // try to create a new instance 
    4747        try { 
    48             Constructor co = instanceClass.getConstructor(types); 
     48            Constructor co; 
     49            try { 
     50                co = instanceClass.getConstructor(types); 
     51            } catch (NoSuchMethodException e) { 
     52                // try to convert the values to a string 
     53                for (int i = 0; i < types.length; i++) { 
     54                    types[i]  = String.class; 
     55                    values[i] = values[0].toString(); 
     56                } 
     57                co = instanceClass.getConstructor(types);                
     58            } 
    4959            instance = co.newInstance(values); 
    5060        } catch (Exception e){ 
  • trunk/src/net/schst/XJConf/Tag.java

    r38 r39  
    22 
    33import java.util.ArrayList; 
    4  
    54import net.schst.XJConf.exceptions.ValueConversionException; 
    65 
     6/** 
     7 * Interface for tag containers 
     8 *  
     9 * @author Stephan Schmidt <me@schst.net> 
     10 */ 
    711public interface Tag { 
    812    public int addChild(Tag child); 
    913    public int addData(char buf[], int offset, int len); 
     14    public void setContent(Object content); 
    1015    public boolean hasAttribute(String name); 
    1116    public String getAttribute(String name); 
     
    1419    public String getName(); 
    1520    public String getData(); 
     21    public Object getContent(); 
    1622    public Object getConvertedValue(ClassLoader loader) throws ValueConversionException; 
    17     public Class getValueType(DefinedTag tag, ClassLoader loader); 
     23    public Class getValueType(Tag tag, ClassLoader loader); 
    1824    public String getKey(); 
     25    public String getSetterMethod(); 
     26    public boolean supportsIndexedChildren(); 
    1927} 
  • trunk/src/net/schst/XJConf/TagDefinition.java

    r37 r39  
    4141            throw new XJConfException("TagDefinition needs a type."); 
    4242        } 
    43          
     43 
    4444        this.name    = name; 
    4545        this.tagName = name; 
     
    148148     * @return  Class object 
    149149     */ 
    150     public Class getValueType(DefinedTag tag, ClassLoader loader) { 
     150    public Class getValueType(Tag tag, ClassLoader loader) { 
    151151        try { 
    152152            return this.vConverter.getType(loader); 
     
    189189     * @throws ValueConversionException 
    190190     */ 
    191     public Object convertValue(DefinedTag tag, ClassLoader loader) 
     191    public Object convertValue(Tag tag, ClassLoader loader) 
    192192        throws ValueConversionException { 
    193193 
    194194        // get the data 
    195         String data = tag.getData(); 
     195        Object data = tag.getContent(); 
    196196        if (data == null) { 
    197197            data = ""; 
     
    237237     * @throws ValueConversionException 
    238238     */ 
    239     private void addAttributesToValue(Object instance, DefinedTag tag, ClassLoader loader) 
     239    private void addAttributesToValue(Object instance, Tag tag, ClassLoader loader) 
    240240        throws ValueConversionException { 
    241241         
     
    277277     * @throws ValueConversionException 
    278278     */ 
    279     private void addChildrenToValue(Object instance, DefinedTag tag, ClassLoader loader) 
     279    private void addChildrenToValue(Object instance, Tag tag, ClassLoader loader) 
    280280        throws ValueConversionException { 
    281281         
     
    292292 
    293293        for (int i = 0; i < children.size(); i++) { 
    294             DefinedTag child = (DefinedTag)children.get(i); 
     294            Tag child = (Tag)children.get(i); 
    295295 
    296296            if (ignore.contains(child.getName())) { 
     
    298298            } 
    299299             
    300             methodName = child.getDefinition().getSetterMethod(); 
    301             Object childValue = child.getDefinition().convertValue(child,loader); 
     300            methodName = child.getSetterMethod(); 
     301            Object childValue = child.getConvertedValue(loader); 
    302302            Object childParams[] = {childValue}; 
    303303             
     
    356356        } 
    357357    } 
     358 
     359    /** 
     360     * Check, whether the value supports indexed children 
     361     *   
     362     * @return  true or false 
     363     */ 
     364    public boolean supportsIndexedChildren() { 
     365        // TODO: Find a better (and working) way to do this check. 
     366        if (this.type.equals("java.util.ArrayList")) { 
     367            return true; 
     368        } 
     369        return false; 
     370    } 
    358371     
    359372    /** 
  • trunk/src/net/schst/XJConf/XmlReader.java

    r37 r39  
    236236        if (this.depth == 1) { 
    237237            return; 
    238         } 
    239  
    240         DefinedTag tag = new DefinedTag(sName, atts); 
    241         // fetch the defintion for this tag 
    242         TagDefinition tagDef = this.tagDefs.getTagDefinition(namespaceURI, sName); 
    243         tag.setDefinition(tagDef); 
    244  
     238        }       
     239 
     240        // This tag needs to be handled by an extension 
    245241        if (this.extensions.containsKey(namespaceURI)) { 
     242            Tag tag = new GenericTag(sName, atts); 
    246243            ((Extension)(this.extensions.get(namespaceURI))).startElement(this, tag); 
     244            this.tagStack.push(tag); 
     245             
     246        // This tag has been defined internally 
    247247        } else { 
    248248            if (!this.tagDefs.isNamespaceDefined(namespaceURI)) { 
     
    253253                throw new UnknownTagException("Unknown tag " + sName + " in namespace " + namespaceURI); 
    254254            } 
    255         } 
    256         this.tagStack.push(tag); 
     255            DefinedTag tag = new DefinedTag(sName, atts); 
     256            // fetch the defintion for this tag 
     257            TagDefinition tagDef = this.tagDefs.getTagDefinition(namespaceURI, sName); 
     258            tag.setDefinition(tagDef); 
     259            this.tagStack.push(tag); 
     260        } 
    257261    } 
    258262 
     
    281285 
    282286        // get the last tag from the stack 
    283         DefinedTag tag = (DefinedTag)this.tagStack.pop(); 
    284  
     287        Tag tag = (Tag)this.tagStack.pop(); 
    285288        if (this.extensions.containsKey(namespaceURI)) { 
    286             Object result = ((Extension)(this.extensions.get(namespaceURI))).endElement(this, tag); 
    287              
     289            Tag result = ((Extension)(this.extensions.get(namespaceURI))).endElement(this, tag); 
     290            if (result != null) { 
     291                if (this.depth == 1) { 
     292                    this.config.put(tag.getKey(), result.getConvertedValue(this.loader)); 
     293                } else { 
     294                    Tag parent = (Tag)this.tagStack.pop(); 
     295                    if (result.getKey() == null && !parent.supportsIndexedChildren()) { 
     296                        parent.setContent(result.getConvertedValue(loader)); 
     297                    } else { 
     298                        parent.addChild(result); 
     299                    } 
     300                    this.tagStack.push(parent); 
     301                } 
     302            } 
    288303        } else { 
    289             if (this.depth == 1) { 
    290                 this.config.put(tag.getKey(), tag.getConvertedValue(this.loader)); 
    291             } else { 
    292                 DefinedTag parent = (DefinedTag)this.tagStack.pop(); 
    293                 parent.addChild(tag); 
    294                 this.tagStack.push(parent); 
    295             } 
    296         } 
     304            if (this.depth == 1) { 
     305                this.config.put(tag.getKey(), tag.getConvertedValue(this.loader)); 
     306            } else { 
     307                Tag parent = (Tag)this.tagStack.pop(); 
     308                parent.addChild(tag); 
     309                this.tagStack.push(parent); 
     310            } 
     311        } 
     312 
    297313    } 
    298314 
  • trunk/src/net/schst/XJConf/ext/XInclude.java

    r37 r39  
    44 
    55import net.schst.XJConf.Extension; 
    6 import net.schst.XJConf.DefinedTag; 
     6import net.schst.XJConf.Tag; 
    77import net.schst.XJConf.XmlReader; 
     8import net.schst.XJConf.exceptions.UnknownTagException; 
    89import net.schst.XJConf.ext.xinc.XIncludeException; 
    910 
     
    3536     * child elements. 
    3637     */ 
    37     public void startElement(XmlReader reader, DefinedTag tag) { 
     38    public void startElement(XmlReader reader, Tag tag) { 
    3839    } 
    3940 
     
    4344     * Does the actual x-include. 
    4445     */ 
    45     public Object endElement(XmlReader reader, DefinedTag tag) 
    46         throws XIncludeException
     46    public Tag endElement(XmlReader reader, Tag tag) 
     47        throws XIncludeException, UnknownTagException
    4748         
    4849        if (tag.getName().equals("include")) { 
     
    5960            try { 
    6061                reader.parse(href); 
     62                return null; 
    6163            } catch (Exception e) { 
    6264                throw new XIncludeException("Could not xInclude " + href, e); 
    6365            }            
    6466        } 
    65         return null
     67        throw new UnknownTagException("Unknown tag " + tag.getName() + " in XInclude namespace.")
    6668    } 
    6769} 
  • trunk/xml/defines-hashmap.xml

    r3 r39  
    33    <tag name="map" type="java.util.HashMap"/> 
    44    <tag name="prop" type="java.lang.String" keyAttribute="name"/> 
     5    <tag name="number" type="java.lang.Integer" keyAttribute="name"/> 
    56</defines>