Changeset 34

Show
Ignore:
Timestamp:
08/02/05 22:29:04 (3 years ago)
Author:
schst
Message:

Handle all exceptions gracefully, delegate to HashMaps? instead of extending them (seems cleaner to me)

Files:

Legend:

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

    r32 r34  
    4545    * @return   NamespaceDefinition 
    4646    */ 
    47     public NamespaceDefinitions parse(String filename) { 
     47    public NamespaceDefinitions parse(String filename) 
     48        throws XJConfException { 
    4849        return this.parse(new File(filename)); 
    4950    } 
     
    5556    * @return   NamespaceDefinition 
    5657    */ 
    57     public NamespaceDefinitions parse(File file) { 
     58    public NamespaceDefinitions parse(File file) 
     59        throws XJConfException { 
    5860        SAXParserFactory factory = SAXParserFactory.newInstance(); 
    5961        try { 
    6062            SAXParser saxParser = factory.newSAXParser(); 
    6163            saxParser.parse(file, this); 
    62         }  catch (Throwable t) { 
    63             t.printStackTrace(); 
     64        }  catch (Exception e) { 
     65            throw new XJConfException("Could not read definition file.", e); 
    6466        } 
    6567        return this.defs; 
     
    223225            TagDefinition def = (TagDefinition)this.defStack.pop(); 
    224226             
    225             if (!this.defs.containsKey(this.currentNamespace)) { 
    226                 this.defs.put(this.currentNamespace, new NamespaceDefinition(this.currentNamespace)); 
    227             } 
    228             NamespaceDefinition nsDef = (NamespaceDefinition)this.defs.get(this.currentNamespace); 
     227            if (!this.defs.namespaceExists(this.currentNamespace)) { 
     228                this.defs.addNamespaceDefinition(this.currentNamespace, new NamespaceDefinition(this.currentNamespace)); 
     229            } 
     230            NamespaceDefinition nsDef = (NamespaceDefinition)this.defs.getNamespaceDefinition(this.currentNamespace); 
    229231            nsDef.addTagDefinition(def); 
    230232        } 
  • trunk/src/net/schst/XJConf/Examples/Example1.java

    r10 r34  
    1313import net.schst.XJConf.NamespaceDefinitions; 
    1414import net.schst.XJConf.XmlReader; 
     15import net.schst.XJConf.exceptions.XJConfException; 
    1516 
    1617/** 
     
    2223public class Example1 { 
    2324 
    24     public static void main(String[] args)
     25    public static void main(String[] args) throws XJConfException
    2526        DefinitionParser tagParser = new DefinitionParser(); 
    2627        File defines = new File("xml/defines.xml"); 
  • trunk/src/net/schst/XJConf/Examples/Example2.java

    r10 r34  
    77import net.schst.XJConf.NamespaceDefinitions; 
    88import net.schst.XJConf.XmlReader; 
     9import net.schst.XJConf.exceptions.XJConfException; 
    910 
    1011/** 
     
    1516public class Example2 { 
    1617 
    17     public static void main(String[] args)
     18    public static void main(String[] args) throws XJConfException
    1819        DefinitionParser tagParser = new DefinitionParser(); 
    1920        File defines = new File("xml/defines2.xml"); 
  • trunk/src/net/schst/XJConf/Examples/Example3.java

    r32 r34  
    77import net.schst.XJConf.NamespaceDefinitions; 
    88import net.schst.XJConf.XmlReader; 
     9import net.schst.XJConf.exceptions.XJConfException; 
    910 
    1011/** 
     
    1516public class Example3 { 
    1617 
    17     public static void main(String[] args)
     18    public static void main(String[] args) throws XJConfException
    1819        DefinitionParser tagParser = new DefinitionParser(); 
    1920        File defines = new File("xml/defines3.xml"); 
  • trunk/src/net/schst/XJConf/Examples/ExampleCollection.java

    r10 r34  
    66import net.schst.XJConf.NamespaceDefinitions; 
    77import net.schst.XJConf.XmlReader; 
     8import net.schst.XJConf.exceptions.XJConfException; 
    89 
    910/** 
     
    1213public class ExampleCollection { 
    1314 
    14     public static void main(String[] args)
     15    public static void main(String[] args) throws XJConfException
    1516        DefinitionParser tagParser = new DefinitionParser(); 
    1617        NamespaceDefinitions defs = tagParser.parse("xml/defines-collection.xml"); 
  • trunk/src/net/schst/XJConf/Examples/ExampleHashMap.java

    r21 r34  
    77import net.schst.XJConf.NamespaceDefinitions; 
    88import net.schst.XJConf.XmlReader; 
     9import net.schst.XJConf.exceptions.XJConfException; 
    910 
    1011/** 
     
    1314public class ExampleHashMap { 
    1415 
    15     public static void main(String[] args)
     16    public static void main(String[] args) throws XJConfException
    1617        DefinitionParser tagParser = new DefinitionParser(); 
    1718        NamespaceDefinitions defs = tagParser.parse("xml/defines-hashmap.xml"); 
  • trunk/src/net/schst/XJConf/Examples/ExampleInclude.java

    r10 r34  
    77import net.schst.XJConf.NamespaceDefinitions; 
    88import net.schst.XJConf.XmlReader; 
     9import net.schst.XJConf.exceptions.XJConfException; 
    910import net.schst.XJConf.ext.XInclude; 
    1011 
     
    1314 */ 
    1415public class ExampleInclude { 
    15     public static void main(String[] args)
     16    public static void main(String[] args) throws XJConfException
    1617        DefinitionParser tagParser = new DefinitionParser(); 
    1718        NamespaceDefinitions defs = tagParser.parse("xml/defines-hashmap.xml"); 
  • trunk/src/net/schst/XJConf/Examples/TestAttributesRequired.java

    r26 r34  
    1212    public static void main(String[] args) { 
    1313        DefinitionParser tagParser = new DefinitionParser(); 
    14         NamespaceDefinitions defs = tagParser.parse("xml/defines-attributes-required.xml"); 
    15  
    16         XmlReader conf = new XmlReader(); 
    17         conf.setTagDefinitions(defs); 
    18  
    19         try { 
     14        NamespaceDefinitions defs; 
     15        try { 
     16            defs = tagParser.parse("xml/defines-attributes-required.xml"); 
     17            XmlReader conf = new XmlReader(); 
     18            conf.setTagDefinitions(defs); 
    2019            conf.parse("xml/test-attributes-required.xml"); 
     20            Color color = (Color)conf.getConfigValue("red"); 
     21            System.out.println(color); 
    2122        } catch (Exception e) { 
    2223            e.printStackTrace(); 
    2324            System.exit(0); 
    24         } 
    25          
    26         Color color = (Color)conf.getConfigValue("red"); 
    27         System.out.println(color); 
     25        }         
    2826    } 
    2927} 
  • trunk/src/net/schst/XJConf/Examples/TestCDataSetter.java

    r21 r34  
    44import net.schst.XJConf.NamespaceDefinitions; 
    55import net.schst.XJConf.XmlReader; 
     6import net.schst.XJConf.exceptions.XJConfException; 
    67 
    78/** 
     
    1011public class TestCDataSetter { 
    1112 
    12     public static void main(String[] args)
     13    public static void main(String[] args) throws XJConfException
    1314        DefinitionParser tagParser = new DefinitionParser(); 
    1415        NamespaceDefinitions defs = tagParser.parse("xml/defines-set-cdata.xml"); 
  • trunk/src/net/schst/XJConf/Examples/TestConstructor.java

    r26 r34  
    44import net.schst.XJConf.NamespaceDefinitions; 
    55import net.schst.XJConf.XmlReader; 
     6import net.schst.XJConf.exceptions.XJConfException; 
    67 
    78/** 
     
    1011public class TestConstructor { 
    1112 
    12     public static void main(String[] args)
     13    public static void main(String[] args) throws XJConfException
    1314        DefinitionParser tagParser = new DefinitionParser(); 
    1415        NamespaceDefinitions defs = tagParser.parse("xml/defines-constructor.xml"); 
  • trunk/src/net/schst/XJConf/Examples/TestInterfaces.java

    r31 r34  
    44import net.schst.XJConf.NamespaceDefinitions; 
    55import net.schst.XJConf.XmlReader; 
     6import net.schst.XJConf.exceptions.XJConfException; 
    67 
    78public class TestInterfaces { 
     
    910    /** 
    1011     * @param args 
     12     * @throws XJConfException  
    1113     */ 
    12     public static void main(String[] args)
     14    public static void main(String[] args) throws XJConfException
    1315        DefinitionParser tagParser = new DefinitionParser(); 
    1416        NamespaceDefinitions defs = tagParser.parse("xml/defines-interfaces.xml"); 
  • trunk/src/net/schst/XJConf/Examples/TestPrimitives.java

    r30 r34  
    44import net.schst.XJConf.NamespaceDefinitions; 
    55import net.schst.XJConf.XmlReader; 
     6import net.schst.XJConf.exceptions.XJConfException; 
    67 
    78/** 
     
    1011public class TestPrimitives { 
    1112 
    12     public static void main(String[] args)
     13    public static void main(String[] args) throws XJConfException
    1314 
    1415        DefinitionParser tagParser = new DefinitionParser(); 
  • trunk/src/net/schst/XJConf/NamespaceDefinition.java

    r1 r34  
    88 * @author Stephan Schmidt <stephan.schmidt@schlund.de> 
    99 */ 
    10 public class NamespaceDefinition extends HashMap
     10public class NamespaceDefinition
    1111     
     12    private HashMap tagDefinitions = new HashMap(); 
     13     
     14    /** 
     15     * URI of this namespace 
     16     */ 
    1217    private String namespaceURI = null; 
    1318     
     
    2833    */ 
    2934    public void addTagDefinition(TagDefinition tag) { 
    30         this.put(tag.getTagName(), tag); 
     35        this.tagDefinitions.put(tag.getTagName(), tag); 
    3136    } 
    3237 
     
    3742    */ 
    3843    public int countTagDefinitions() { 
    39         return this.size(); 
     44        return this.tagDefinitions.size(); 
    4045    } 
    4146 
     
    4752    */ 
    4853    public boolean isDefined(String tagName){ 
    49         return this.containsKey(tagName); 
     54        return this.tagDefinitions.containsKey(tagName); 
    5055    } 
    5156 
     
    5863    */ 
    5964    public TagDefinition getDefinition(String tagName){ 
    60         return (TagDefinition)this.get(tagName); 
     65        return (TagDefinition)this.tagDefinitions.get(tagName); 
    6166    } 
     67 
     68    /** 
     69     * Get the URI for this namespace 
     70     *  
     71     * @return 
     72     */ 
     73    public String getNamespaceURI() { 
     74        return this.namespaceURI; 
     75    }     
    6276} 
  • trunk/src/net/schst/XJConf/NamespaceDefinitions.java

    r15 r34  
    77/** 
    88 * Stores definitions of several namespaces. 
    9  *  
    10  * This extends a HashMap with some convinience methods. 
    11  *  
     9 *   
    1210 * @author Stephan Schmidt <stephan.schmidt@schlund.de> 
    1311 */ 
    14 public class NamespaceDefinitions extends HashMap
     12public class NamespaceDefinitions
    1513 
     14    private HashMap namespaces = new HashMap(); 
     15     
    1616   /** 
    1717    * Add the definition for a namespace 
     
    2121    */ 
    2222    public void addNamespaceDefinition(String namespace, NamespaceDefinition defs) { 
    23         this.put(namespace, defs); 
     23        this.namespaces.put(namespace, defs); 
    2424    } 
    2525 
     
    3131    */ 
    3232    public NamespaceDefinition getNamespaceDefinition(String namespace) { 
    33         return (NamespaceDefinition)this.get(namespace); 
     33        return (NamespaceDefinition)this.namespaces.get(namespace); 
    3434    } 
    3535     
     
    4141    */ 
    4242    public boolean isNamespaceDefined(String namespace) { 
    43         return this.containsKey(namespace); 
     43        return this.namespaces.containsKey(namespace); 
    4444    } 
    4545 
     
    5050     */ 
    5151     public Set getDefinedNamespaces() { 
    52          return this.keySet(); 
     52         return this.namespaces.keySet(); 
    5353     } 
    5454     
     
    112112        } 
    113113    } 
     114     
     115    public boolean namespaceExists(String namespaceURI) { 
     116        return this.namespaces.containsKey(namespaceURI); 
     117    } 
    114118} 
  • trunk/src/net/schst/XJConf/XmlReader.java

    r32 r34  
    188188            throw new InternalError("Could not configure the parser correctly.");  
    189189        } catch (SAXException e) { 
    190             e.printStackTrace(); 
    191             System.exit(0); 
    192190            throw new XJConfException(e.getMessage()); 
    193         } catch (IOException e) { 
    194             throw e; 
    195191        } 
    196192        this.openFiles.pop();