Changeset 9

Show
Ignore:
Timestamp:
06/30/05 13:16:03 (3 years ago)
Author:
schst
Message:

Added a convenience method for adding new extensions

Files:

Legend:

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

    r8 r9  
    88public interface Extension { 
    99     
     10    public String getNamespace(); 
     11     
    1012    public void startElement(XmlReader reader, Tag tag) 
    1113    throws SAXException; 
  • trunk/src/net/schst/XJConf/XmlReader.java

    r8 r9  
    55import java.util.Stack; 
    66 
     7import javax.xml.parsers.ParserConfigurationException; 
    78import javax.xml.parsers.SAXParser; 
    89import javax.xml.parsers.SAXParserFactory; 
     
    6263    private Stack openFiles = new Stack(); 
    6364     
     65    private SAXParserFactory parserFactory = null; 
     66     
    6467   /** 
    6568    * Set the tag definitions 
     
    8891    } 
    8992     
     93    public void addExtension(String name) { 
     94        try { 
     95            Class c = Class.forName(name); 
     96            Extension ext = (Extension)c.newInstance(); 
     97            String ns = ext.getNamespace(); 
     98             
     99            this.addExtension(ns, ext); 
     100             
     101        } catch (Exception e) { 
     102            e.printStackTrace(); 
     103        } 
     104    } 
     105     
    90106   /** 
    91107    * Parse a configuration file, that you already 
     
    95111    */ 
    96112    public void parse(File file) { 
    97         SAXParserFactory factory = SAXParserFactory.newInstance(); 
    98         factory.setNamespaceAware(true); 
     113        if (this.parserFactory == null) { 
     114            this.parserFactory = SAXParserFactory.newInstance(); 
     115            this.parserFactory.setNamespaceAware(true); 
     116        }     
    99117         
    100118        this.openFiles.push(file); 
    101119         
     120        SAXParser saxParser; 
    102121        try { 
    103             SAXParser saxParser = factory.newSAXParser(); 
     122            saxParser = this.parserFactory.newSAXParser(); 
    104123            saxParser.parse(file, this); 
    105124        } catch (Throwable t) { 
     
    136155    public void startElement(String namespaceURI, String sName, String qName, Attributes atts) 
    137156    throws SAXException { 
     157         
    138158        if (this.myNamespace.equals(namespaceURI) && this.depth > 0) { 
    139159            return; 
    140160        } 
    141  
     161         
    142162        this.depth++; 
    143163         
     
    161181        } else { 
    162182            if (!this.tagDefs.isNamespaceDefined(namespaceURI)) { 
    163                 throw new UnknownNamespaceException("Unknown namespace " + namespaceURI); 
     183                File current = this.getCurrentFile(); 
     184                throw new UnknownNamespaceException("Unknown namespace " + namespaceURI + " in file " + current.getAbsolutePath()); 
    164185            } 
    165186            if (!this.tagDefs.isTagDefined(namespaceURI, sName)) { 
  • trunk/src/net/schst/XJConf/ext/XInclude.java

    r8 r9  
    1616public class XInclude implements Extension { 
    1717 
     18    private String namespace = "http://www.w3.org/2001/XInclude"; 
     19     
     20    public String getNamespace() { 
     21        return this.namespace; 
     22    } 
     23     
    1824    /* (non-Javadoc) 
    1925     * @see net.schst.XJConf.Extension#endElement(java.lang.String, java.lang.String) 
     
    2935            href = current.getParent() + "/" + href; 
    3036        } 
    31          
    3237        reader.parse(href); 
    3338    }