Clover coverage report -
Coverage timestamp: Do Okt 21 2004 12:21:23 CEST
file stats: LOC: 153   Methods: 8
NCLOC: 104   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
XMLFileHandler.java 37,5% 66,7% 87,5% 65,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.BufferedInputStream;
 10    import java.io.File;
 11    import java.io.FileInputStream;
 12    import java.io.IOException;
 13    import java.io.OutputStreamWriter;
 14    import java.io.PrintWriter;
 15   
 16    import javax.xml.parsers.DocumentBuilder;
 17    import javax.xml.parsers.DocumentBuilderFactory;
 18    import javax.xml.parsers.ParserConfigurationException;
 19   
 20    import org.jconfig.Configuration;
 21    import org.jconfig.ConfigurationManagerException;
 22    import org.jconfig.error.ErrorReporter;
 23    import org.jconfig.parser.ConfigurationParser;
 24    import org.jconfig.parser.ConfigurationParserFactory;
 25    import org.jconfig.utils.ConfigErrorHandler;
 26    import org.w3c.dom.Document;
 27    import org.xml.sax.InputSource;
 28    import org.xml.sax.SAXException;
 29    /**
 30    * This class is nearly the same as the InputStreamHandler beside
 31    * that it takes a file and not the filename.
 32    *
 33    * @author Andreas Mecky andreas.mecky@xcom.de
 34    * @author Terry Dye terry.dye@xcom.de
 35    */
 36    public class XMLFileHandler extends BaseXMLHandler implements ConfigurationHandler {
 37   
 38    private boolean validate = false;
 39    private File file;
 40   
 41  3 public XMLFileHandler() {
 42    }
 43   
 44  1 public XMLFileHandler(String filename) {
 45  1 file = new File(filename);
 46  1 addFileListener(this);
 47    }
 48    /**
 49    * Sets the file that will be processed
 50    * @param file the file
 51    */
 52  2 public void setFile(File file) {
 53  2 this.file = file;
 54    }
 55    /**
 56    * This defines if the file will be vaidated using a DTD. The default
 57    * is false.
 58    *
 59    * @param validate defines the validation
 60    */
 61  0 public void setValidation(boolean validate) {
 62  0 this.validate = validate;
 63    }
 64   
 65    /**
 66    * @throws ConfigurationManagerException */
 67  3 public synchronized Configuration load(String configurationName)
 68    throws ConfigurationManagerException {
 69  3 return load(file,configurationName);
 70    }
 71   
 72    /**
 73    * @param file
 74    * @throws ConfigurationManagerException */
 75  4 public synchronized Configuration load(File file,String configName)
 76    throws ConfigurationManagerException {
 77  4 if (file == null) {
 78  0 ErrorReporter.getErrorHandler().reportError("The file is NULL");
 79  0 throw new ConfigurationManagerException("The file is NULL");
 80    }
 81  4 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 82    // set the validation flag (true if it should be validated)
 83  4 dbf.setValidating(validate);
 84  4 DocumentBuilder db = null;
 85  4 try {
 86  4 db = dbf.newDocumentBuilder();
 87    } catch (ParserConfigurationException pce) {
 88  0 ErrorReporter.getErrorHandler().reportError("The parser cannot create a new document builder",pce);
 89  0 throw new ConfigurationManagerException("The parser cannot create a new document builder: "
 90    + pce.getMessage());
 91    }
 92    // if we have to validate then we need to define an error handler here
 93  4 if (validate) {
 94  0 try {
 95    // Set an ErrorHandler before parsing
 96  0 OutputStreamWriter errorWriter =
 97    new OutputStreamWriter(System.err, "UTF-8");
 98  0 db.setErrorHandler(
 99    new ConfigErrorHandler(new PrintWriter(errorWriter, true)));
 100    } catch (Exception e) {
 101  0 ErrorReporter.getErrorHandler().reportError("The parser cannot set up the error handler");
 102  0 throw new ConfigurationManagerException("The parser cannot set up the error handler");
 103    }
 104    }
 105  4 Document doc = null;
 106  4 try {
 107  4 FileInputStream fis = new FileInputStream(file);
 108   
 109  4 InputSource ins = new InputSource(fis);
 110  4 BufferedInputStream buffy = new BufferedInputStream(fis);
 111  4 ins = new InputSource(buffy);
 112  4 doc = db.parse(file);
 113    } catch (SAXException se) {
 114  0 ErrorReporter.getErrorHandler().reportError("The parser cannot parse the file",se);
 115  0 throw new ConfigurationManagerException(
 116    "The parser cannot parse the file: " + se.getMessage());
 117    } catch (IOException ioe) {
 118  0 ErrorReporter.getErrorHandler().reportError("The parser cannot parse the file",ioe);
 119  0 throw new ConfigurationManagerException(
 120    "The parser cannot open the file: " + ioe.getMessage());
 121    }
 122  4 ConfigurationParser parser = ConfigurationParserFactory.getParser(configName);
 123  4 Configuration config = parser.parse(doc, configName);
 124  4 try {
 125  4 java.io.BufferedReader in = new java.io.BufferedReader(new java.io.FileReader(file));
 126  4 String str;
 127  ? while ((str = in.readLine()) != null) {
 128  0 if(str.indexOf("encoding") > -1) config.setEncoding(getEncodingType(str));
 129    }
 130  4 in.close();
 131    }
 132    catch (Exception e) {
 133    // no problem here
 134    }
 135  4 config.resetCreated();
 136  4 return config;
 137    }
 138   
 139    /** This method should store all categories and properties.
 140    * @throws ConfigurationManagerException
 141    */
 142  3 public void store(Configuration configuration)
 143    throws ConfigurationManagerException {
 144  3 store(file, configuration);
 145    }
 146    /* (non-Javadoc)
 147    * @see org.jconfig.handler.AbstractHandler#getFile()
 148    */
 149  1 public File getFile() {
 150  1 return file;
 151    }
 152   
 153    }