Changeset 8

Show
Ignore:
Timestamp:
06/29/05 14:55:36 (3 years ago)
Author:
schst
Message:

implemented a very basic xInclude machanism, added the internal namespace for later use

Files:

Legend:

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

    r5 r8  
    253253                } else { 
    254254                    Class childParamTypes[] = {childValue.getClass()}; 
    255                      
     255                     
    256256                    Method childMethod = cl.getMethod(methodName, childParamTypes); 
    257                    childMethod.invoke(instance, childParams); 
     257                    childMethod.invoke(instance, childParams); 
    258258                } 
    259259            } catch (Throwable t) { 
    260                 throw new ValueConversionException("Could not add child " + child.getKey() + " to " + this.getType() + ".", t); 
     260                throw new ValueConversionException("Could not add child " + child.getKey() + " to " + this.getType() + " using "+methodName+"().", t); 
    261261            } 
    262262        } 
  • trunk/src/net/schst/XJConf/XmlReader.java

    r1 r8  
    4747    private int depth = 0; 
    4848     
     49    /** 
     50     * Extensions used by the parser 
     51     */ 
     52    private HashMap extensions = new HashMap(); 
     53     
     54    /** 
     55     * Internal namespace 
     56     */ 
     57    private String myNamespace = "http://www.schst.net/XJConf"; 
     58     
     59    /** 
     60     * all files that currently are being processed 
     61     */ 
     62    private Stack openFiles = new Stack(); 
     63     
    4964   /** 
    5065    * Set the tag definitions 
     
    6176    public void setTagDefinitions(NamespaceDefinitions defs) { 
    6277        this.tagDefs = defs; 
     78    } 
     79 
     80    /** 
     81     * Add a new extension 
     82     *  
     83     * @param namespace 
     84     * @param ext 
     85     */ 
     86    public void addExtension(String namespace, Extension ext) { 
     87        this.extensions.put(namespace, ext); 
    6388    } 
    6489     
     
    7297        SAXParserFactory factory = SAXParserFactory.newInstance(); 
    7398        factory.setNamespaceAware(true); 
     99         
     100        this.openFiles.push(file); 
    74101         
    75102        try { 
     
    79106            t.printStackTrace(); 
    80107        } 
    81     } 
    82  
     108        this.openFiles.pop(); 
     109    } 
     110 
     111    /** 
     112     * Get the file that currently is being parsed 
     113     *  
     114     * @return 
     115     */ 
     116    public File getCurrentFile() { 
     117        return (File)this.openFiles.peek(); 
     118    } 
     119     
    83120   /** 
    84121    * Parse a configuration file. 
     
    99136    public void startElement(String namespaceURI, String sName, String qName, Attributes atts) 
    100137    throws SAXException { 
     138        if (this.myNamespace.equals(namespaceURI) && this.depth > 0) { 
     139            return; 
     140        } 
     141 
    101142        this.depth++; 
    102143         
     
    110151            return; 
    111152        } 
    112          
    113         if (!this.tagDefs.isNamespaceDefined(namespaceURI)) { 
    114             throw new UnknownNamespaceException("Unknown namespace " + namespaceURI); 
    115         } 
    116          
    117         if (!this.tagDefs.isTagDefined(namespaceURI, sName)) { 
    118             throw new UnknownTagException("Unknown tag " + sName); 
    119         } 
     153 
    120154        Tag tag = new Tag(qName, atts); 
    121155        // fetch the defintion for this tag 
    122156        TagDefinition tagDef = this.tagDefs.getTagDefinition(namespaceURI, sName); 
    123157        tag.setDefinition(tagDef); 
     158 
     159        if (this.extensions.containsKey(namespaceURI)) { 
     160            ((Extension)(this.extensions.get(namespaceURI))).startElement(this, tag); 
     161        } else { 
     162            if (!this.tagDefs.isNamespaceDefined(namespaceURI)) { 
     163                throw new UnknownNamespaceException("Unknown namespace " + namespaceURI); 
     164            } 
     165            if (!this.tagDefs.isTagDefined(namespaceURI, sName)) { 
     166                throw new UnknownTagException("Unknown tag " + sName); 
     167            } 
     168        } 
    124169        this.tagStack.push(tag); 
    125170    } 
     
    133178    public void endElement(String namespaceURI, String sName, String qName) 
    134179    throws SAXException { 
     180        if (this.myNamespace.equals(namespaceURI) && this.depth > 0) { 
     181            return; 
     182        } 
     183 
    135184        this.depth--; 
    136185 
     
    144193            return; 
    145194        } 
    146          
     195 
    147196        // get the last tag from the stack 
    148197        Tag tag = (Tag)this.tagStack.pop(); 
    149  
    150         if (this.depth == 1) { 
    151                this.config.put(tag.getKey(), tag.getConvertedValue()); 
     198         
     199        if (this.extensions.containsKey(namespaceURI)) { 
     200            ((Extension)(this.extensions.get(namespaceURI))).endElement(this, tag); 
    152201        } else { 
    153             Tag parent = (Tag)this.tagStack.pop(); 
    154             parent.addChild(tag); 
    155             this.tagStack.push(parent); 
     202 
     203            if (this.depth == 1) { 
     204                this.config.put(tag.getKey(), tag.getConvertedValue()); 
     205            } else { 
     206                Tag parent = (Tag)this.tagStack.pop(); 
     207                parent.addChild(tag); 
     208                this.tagStack.push(parent); 
     209            } 
    156210        } 
    157211    } 
  • trunk/xml/test-hashmap.xml

    r3 r8  
    1 <configuration
     1<xj:configuration xmlns:xj="http://www.schst.net/XJConf"
    22    <map> 
    33        <prop name="foo">bar</prop> 
     
    99        <prop name="argh">tomato</prop> 
    1010    </properties> 
    11 </configuration> 
     11</xj:configuration>