Clover coverage report -
Coverage timestamp: Do Okt 21 2004 12:21:23 CEST
file stats: LOC: 162   Methods: 8
NCLOC: 95   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
InputStreamHandler.java 50% 61% 87,5% 63,6%
coverage coverage
 1    /*
 2    * ConfigurationFileLoader.java
 3    *
 4    * Created on 28. April 2002, 14:45
 5    */
 6   
 7    package org.jconfig.handler;
 8   
 9    import java.io.File;
 10    import java.io.IOException;
 11    import java.io.InputStream;
 12    import java.io.OutputStreamWriter;
 13    import java.io.PrintWriter;
 14   
 15    import javax.xml.parsers.DocumentBuilder;
 16    import javax.xml.parsers.DocumentBuilderFactory;
 17    import javax.xml.parsers.ParserConfigurationException;
 18   
 19    import org.jconfig.Configuration;
 20    import org.jconfig.ConfigurationManagerException;
 21    import org.jconfig.error.ErrorReporter;
 22    import org.jconfig.parser.ConfigurationParser;
 23    import org.jconfig.parser.ConfigurationParserFactory;
 24    import org.jconfig.utils.ConfigErrorHandler;
 25    import org.jconfig.utils.ConfigurationUtils;
 26    import org.jconfig.utils.ResourceLocator;
 27    import org.w3c.dom.Document;
 28    import org.xml.sax.SAXException;
 29    /**
 30    * This class is an implementation of the ConfigurationHandler interface.
 31    * It tries to find the file with the given name inside the classpath with
 32    * getClassLoader().getResourceAsStream(fileName). This handler is used
 33    * as default by the ConfigurationManager.
 34    *
 35    * @author Andreas Mecky andreas.mecky@xcom.de
 36    * @author Terry Dye terry.dye@xcom.de
 37    */
 38    public class InputStreamHandler extends BaseXMLHandler implements ConfigurationHandler {
 39   
 40    private boolean validate = false;
 41    private String filename;
 42   
 43    /**
 44    * Default constructor
 45    */
 46  1 public InputStreamHandler() {
 47    }
 48   
 49    /**
 50    * Constructor with filename
 51    * @param filename
 52    */
 53  36 public InputStreamHandler(String filename) {
 54  36 this.filename = filename;
 55    }
 56   
 57    /**
 58    * Sets the filename
 59    *
 60    * @param the name of the file that the InputStreamHandler will search in the classpath
 61    */
 62  1 public void setFileName(String fileName) {
 63  1 this.filename = fileName;
 64    }
 65   
 66    /**
 67    * Defines if the xml file should be validated using a DTD or not.
 68    * The default setting is false.
 69    *
 70    * @param if true then it will validate the xml file.
 71    */
 72  0 public void setValidation(boolean validate) {
 73  0 this.validate = validate;
 74    }
 75   
 76    /**
 77    * Loads the configuration from a xml file.
 78    * @throws ConfigurationManagerException
 79    */
 80  37 public synchronized Configuration load(String configurationName) throws ConfigurationManagerException {
 81  37 InputStream is = null;
 82  37 try {
 83  37 is = new ResourceLocator(filename).getInputStream();
 84  32 if ( is == null ) {
 85  0 ErrorReporter.getErrorHandler().reportError("File not found:"+filename);
 86  0 is = new ResourceLocator("config.xml").getInputStream();
 87    }
 88    } catch (IOException e) {
 89  5 ErrorReporter.getErrorHandler().reportError("Cannot load file:"+filename,e);
 90  5 throw new ConfigurationManagerException(e.getLocalizedMessage());
 91    }
 92  32 if ( is != null ) {
 93  32 return load(is,configurationName);
 94    }
 95    else {
 96  0 ErrorReporter.getErrorHandler().reportError("The file:"+filename+" not found in classpath");
 97  0 throw new ConfigurationManagerException("The file could not be found in the classpath");
 98    }
 99    }
 100   
 101    /**
 102    * This method will read in a file and generate the properties
 103    *
 104    *@param is The inputstream
 105    *@throws ConfigurationManagerException if the file cannot be processed
 106    */
 107  32 private Configuration load(InputStream is,String configurationName) throws ConfigurationManagerException {
 108  32 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 109    // set the validation flag (true if it should be validated)
 110  32 dbf.setValidating(validate);
 111  32 DocumentBuilder db = null;
 112  32 try {
 113  32 db = dbf.newDocumentBuilder();
 114    } catch (ParserConfigurationException pce) {
 115  0 ErrorReporter.getErrorHandler().reportError("The parser cannot create a new document builder",pce);
 116  0 throw new ConfigurationManagerException("The parser cannot create a new document builder: " + pce.getMessage());
 117    }
 118    // if we have to validate then we need to define an error handler here
 119  32 if (validate) {
 120  0 try {
 121    // Set an ErrorHandler before parsing
 122  0 OutputStreamWriter errorWriter =
 123    new OutputStreamWriter(System.err, "UTF-8");
 124  0 db.setErrorHandler(new ConfigErrorHandler(new PrintWriter(errorWriter, true)));
 125    //new MyErrorHandler(new PrintWriter(errorWriter, true)));
 126    } catch (Exception e) {
 127  0 ErrorReporter.getErrorHandler().reportError("The parser cannot set up the error handler",e);
 128  0 throw new ConfigurationManagerException("The parser cannot set up the error handler");
 129    }
 130    }
 131  32 Document doc = null;
 132  32 try {
 133  32 doc = db.parse(is);
 134    } catch (SAXException se) {
 135  0 ErrorReporter.getErrorHandler().reportError("The parser cannot parse the file",se);
 136  0 throw new ConfigurationManagerException("The parser cannot parse the file: " + se.getMessage());
 137    } catch (IOException ioe) {
 138  0 ErrorReporter.getErrorHandler().reportError("The parser cannot open the file",ioe);
 139  0 throw new ConfigurationManagerException("The parser cannot open the file: " + ioe.getMessage());
 140    }
 141  32 ConfigurationParser parser = ConfigurationParserFactory.getParser(configurationName);
 142  32 Configuration config = parser.parse(doc, configurationName);
 143  32 config.resetCreated();
 144  32 return config;
 145    }
 146   
 147   
 148    /**
 149    * This method should store all categories and properties.
 150    */
 151  2 public void store(Configuration configuration) throws ConfigurationManagerException {
 152  2 store( ConfigurationUtils.getFileFromInputStream(filename),configuration);
 153    }
 154   
 155    /**
 156    * @see org.jconfig.handler.ConfigurationHandler#getFile()
 157    */
 158  30 public File getFile() {
 159  30 return ConfigurationUtils.getFileFromInputStream(filename);
 160    }
 161   
 162    }