Changeset 10

Show
Ignore:
Timestamp:
07/01/05 11:24:56 (3 years ago)
Author:
schst
Message:

Improved the exception handling

Files:

Legend:

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

    r1 r10  
    123123     * @param    val   value to convert  
    124124     * @return         converted value 
     125     * @throws ValueConversionException 
    125126     */ 
    126     public Object convertValue(Object val)
     127    public Object convertValue(Object val) throws ValueConversionException
    127128        String value = (String)val; 
    128129        Object instance = null; 
     
    134135            String params[] = {value}; 
    135136            instance = co.newInstance(params); 
    136         } catch (Throwable t) { 
     137        } catch (Exception t) { 
    137138            throw new ValueConversionException("Could not create attribute " + this.name + " of type " + this.type, t); 
    138139        } 
  • trunk/src/net/schst/XJConf/Examples/Example1.java

    r2 r10  
    3131 
    3232        XmlReader conf = new XmlReader(); 
    33         conf.setTagDefinitions(defs); 
    34  
    35         conf.parse("xml/test.xml"); 
     33        try { 
     34            conf.setTagDefinitions(defs); 
     35     
     36            conf.parse("xml/test.xml"); 
     37        } catch (Exception e) { 
     38            e.printStackTrace(); 
     39            System.exit(0); 
     40        } 
    3641         
    3742        String foo = (String)conf.getConfigValue("foo"); 
  • trunk/src/net/schst/XJConf/Examples/Example2.java

    r2 r10  
    2525        conf.setTagDefinitions(defs); 
    2626 
    27         conf.parse("xml/test2.xml"); 
     27        try { 
     28            conf.parse("xml/test2.xml"); 
     29        } catch (Exception e) { 
     30            e.printStackTrace(); 
     31            System.exit(0); 
     32        } 
    2833         
    2934        Integer one = (Integer)conf.getConfigValue("one"); 
  • trunk/src/net/schst/XJConf/Examples/Example3.java

    r2 r10  
    2929        conf.setTagDefinitions(defs); 
    3030         
    31         conf.parse("xml/test3.xml"); 
     31        try { 
     32            conf.parse("xml/test3.xml"); 
     33        } catch (Exception e) { 
     34            e.printStackTrace(); 
     35            System.exit(0); 
     36        } 
    3237         
    3338        Integer zahl = (Integer)conf.getConfigValue("zahl"); 
  • trunk/src/net/schst/XJConf/Examples/ExampleCollection.java

    r5 r10  
    1919        conf.setTagDefinitions(defs); 
    2020 
    21         conf.parse("xml/test-collection.xml"); 
     21        try { 
     22            conf.parse("xml/test-collection.xml"); 
     23        } catch (Exception e) { 
     24            e.printStackTrace(); 
     25            System.exit(0); 
     26        } 
    2227         
    2328        ArrayList list = (ArrayList)conf.getConfigValue("list"); 
  • trunk/src/net/schst/XJConf/Examples/ExampleHashMap.java

    r5 r10  
    2020        conf.setTagDefinitions(defs); 
    2121 
    22         conf.parse("xml/test-hashmap.xml"); 
     22        try { 
     23            conf.parse("xml/test-hashmap.xml"); 
     24        } catch (Exception e) { 
     25            e.printStackTrace(); 
     26            System.exit(0); 
     27        } 
    2328         
    2429        HashMap map = (HashMap)conf.getConfigValue("map"); 
  • trunk/src/net/schst/XJConf/Examples/ExampleInclude.java

    r8 r10  
    2323        conf.addExtension("http://www.w3.org/2001/XInclude", xinc); 
    2424 
    25         conf.parse("xml/test-xinclude.xml"); 
     25        try { 
     26            conf.parse("xml/test-xinclude.xml"); 
     27        } catch (Exception e) { 
     28            e.printStackTrace(); 
     29            System.exit(0); 
     30        } 
    2631 
    2732        HashMap map = (HashMap)conf.getConfigValue("map"); 
  • trunk/src/net/schst/XJConf/Extension.java

    r9 r10  
    11package net.schst.XJConf; 
    22 
    3 import org.xml.sax.SAXException; 
    43 
    54/** 
     
    1110     
    1211    public void startElement(XmlReader reader, Tag tag) 
    13     throws SAXException; 
     12        throws XJConfException; 
    1413 
    15     public void endElement(XmlReader reader, Tag tag); 
     14    public void endElement(XmlReader reader, Tag tag) 
     15        throws XJConfException; 
    1616     
    1717} 
  • trunk/src/net/schst/XJConf/TagDefinition.java

    r8 r10  
    140140     * @param v    value to convert 
    141141     * @return     converted value 
    142      */ 
    143     public Object convertValue(Object v) { 
     142     * @throws ValueConversionException 
     143     */ 
     144    public Object convertValue(Object v) throws ValueConversionException { 
    144145        Tag tag = (Tag)v; 
    145146         
     
    157158            // get the class object 
    158159            cl = Class.forName(this.type); 
    159         } catch (Throwable e) { 
     160        } catch (Exception e) { 
    160161            throw new ValueConversionException("Class " + this.type + " does not exist", e); 
    161162        } 
     
    175176            try { 
    176177                instance = cl.newInstance(); 
    177             } catch (Throwable t) { 
     178            } catch (Exception t) { 
    178179                throw new ValueConversionException("Could not create instance of " + this.type, t); 
    179180            } 
     
    209210                me.invoke(instance, meParams); 
    210211                 
    211             } catch (Throwable t) { 
     212            } catch (Exception t) { 
    212213                throw new ValueConversionException("Could not set attribute " + attName + " of " + this.type, t); 
    213214            } 
     
    257258                    childMethod.invoke(instance, childParams); 
    258259                } 
    259             } catch (Throwable t) { 
     260            } catch (Exception t) { 
    260261                throw new ValueConversionException("Could not add child " + child.getKey() + " to " + this.getType() + " using "+methodName+"().", t); 
    261262            } 
  • trunk/src/net/schst/XJConf/UnknownNamespaceException.java

    r1 r10  
    11package net.schst.XJConf; 
     2 
    23 
    34/** 
     
    67 * @author Stephan Schmidt <stephan.schmidt@schlund.de> 
    78 */ 
    8 public class UnknownNamespaceException extends RuntimeException { 
     9public class UnknownNamespaceException extends XJConfException { 
    910    public UnknownNamespaceException(String message) { 
    1011        super(message); 
  • trunk/src/net/schst/XJConf/UnknownTagException.java

    r1 r10  
    11package net.schst.XJConf; 
     2 
    23 
    34/** 
     
    67 * @author Stephan Schmidt <stephan.schmidt@schlund.de> 
    78 */ 
    8 public class UnknownTagException extends RuntimeException { 
     9public class UnknownTagException extends XJConfException { 
    910    public UnknownTagException(String message) { 
    1011        super(message); 
    1112    } 
     13 
    1214    public UnknownTagException(String message, Exception cause) { 
    1315        super(message, (Exception) cause); 
  • trunk/src/net/schst/XJConf/ValueConversionException.java

    r1 r10  
    66 * @author Stephan Schmidt <stephan.schmidt@schlund.de> 
    77 */ 
    8 public class ValueConversionException extends RuntimeException { 
    9     public ValueConversionException(String message, Throwable cause) { 
    10         super(message, cause); 
     8public class ValueConversionException extends XJConfException { 
     9     
     10    /** 
     11     * @param e 
     12     */ 
     13    public ValueConversionException(Exception e) { 
     14        super(e); 
     15    } 
     16     
     17    /** 
     18     * @param message 
     19     */ 
     20    public ValueConversionException(String message) { 
     21        super(message); 
     22    } 
     23     
     24    /** 
     25     * @param message 
     26     * @param e 
     27     */ 
     28    public ValueConversionException(String message, Exception e) { 
     29        super(message, e); 
    1130    } 
    1231} 
  • trunk/src/net/schst/XJConf/XmlReader.java

    r9 r10  
    22 
    33import java.io.File; 
     4import java.io.IOException; 
    45import java.util.HashMap; 
    56import java.util.Stack; 
     
    6364    private Stack openFiles = new Stack(); 
    6465     
     66    /** 
     67     * The factory for all parsers 
     68     */ 
    6569    private SAXParserFactory parserFactory = null; 
    6670     
     
    9195    } 
    9296     
    93     public void addExtension(String name) { 
     97    /** 
     98     * Add a new extension 
     99     *  
     100     * @param name      full-qualified class name 
     101     * @throws UnknownExtensionException 
     102     */ 
     103    public void addExtension(String name) throws UnknownExtensionException { 
     104        Extension ext; 
    94105        try { 
    95106            Class c = Class.forName(name); 
    96             Extension ext = (Extension)c.newInstance(); 
    97             String ns = ext.getNamespace(); 
    98              
    99             this.addExtension(ns, ext); 
    100              
     107            ext = (Extension)c.newInstance(); 
    101108        } catch (Exception e) { 
    102             e.printStackTrace(); 
    103         } 
     109            throw new UnknownExtensionException("The extension " + name + " could not be loaded."); 
     110        } 
     111        String ns = ext.getNamespace(); 
     112        this.addExtension(ns, ext); 
    104113    } 
    105114     
     
    109118    *  
    110119    * @param    file      File object to parse 
    111     */ 
    112     public void parse(File file) { 
     120    * @throws   XJConfException 
     121    * @throws IOException 
     122    */ 
     123    public void parse(File file) throws XJConfException, IOException { 
    113124        if (this.parserFactory == null) { 
    114125            this.parserFactory = SAXParserFactory.newInstance(); 
     
    122133            saxParser = this.parserFactory.newSAXParser(); 
    123134            saxParser.parse(file, this); 
    124         } catch (Throwable t) { 
    125             t.printStackTrace(); 
     135        } catch (XJConfException e) { 
     136            throw e; 
     137        } catch (ParserConfigurationException e) { 
     138            throw new InternalError("Could not configure the parser correctly.");  
     139        } catch (SAXException e) { 
     140            throw new XJConfException(e.getMessage()); 
     141        } catch (IOException e) { 
     142            throw e; 
    126143        } 
    127144        this.openFiles.pop(); 
     
    141158    *  
    142159    * @param    filename    filename of the configuration file 
     160    * @throws IOException 
     161    * @throws XJConfException 
    143162    */ 
    144     public void parse(String filename)
     163    public void parse(String filename) throws XJConfException, IOException
    145164        File file = new File(filename); 
    146165        this.parse(file); 
  • trunk/src/net/schst/XJConf/ext/XInclude.java

    r9 r10  
    77import net.schst.XJConf.XmlReader; 
    88 
    9 import org.xml.sax.SAXException; 
    10  
    119/** 
    1210 * Very basic xInclude mechanism 
     
    1614public class XInclude implements Extension { 
    1715 
     16    /** 
     17     * Namespace of the extension 
     18     */ 
    1819    private String namespace = "http://www.w3.org/2001/XInclude"; 
    1920     
     
    2526     * @see net.schst.XJConf.Extension#endElement(java.lang.String, java.lang.String) 
    2627     */ 
    27     public void endElement(XmlReader reader, Tag tag)
     28    public void endElement(XmlReader reader, Tag tag) throws XIncludeException
    2829        String href = tag.getAttribute("href"); 
    2930        if (href == null) { 
     
    3536            href = current.getParent() + "/" + href; 
    3637        } 
    37         reader.parse(href); 
     38        try { 
     39            reader.parse(href); 
     40        } catch (Exception e) { 
     41            throw new XIncludeException("Could not xInclude " + href, e); 
     42        } 
    3843    } 
    3944     
     
    4146     * @see net.schst.XJConf.Extension#startElement(java.lang.String, org.xml.sax.Attributes) 
    4247     */ 
    43     public void startElement(XmlReader reader, Tag tag) throws SAXException
     48    public void startElement(XmlReader reader, Tag tag)
    4449    } 
    4550}