Clover coverage report -
Coverage timestamp: Do Okt 21 2004 12:21:23 CEST
file stats: LOC: 150   Methods: 8
NCLOC: 88   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
PropertiesFileHandler.java 50% 52,2% 87,5% 56,1%
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.FileInputStream;
 11    import java.io.FileWriter;
 12    import java.io.InputStream;
 13    import java.util.Enumeration;
 14    import java.util.Properties;
 15   
 16    import org.jconfig.Configuration;
 17    import org.jconfig.ConfigurationManagerException;
 18    import org.jconfig.DefaultConfiguration;
 19    import org.jconfig.error.ErrorReporter;
 20    import org.jconfig.utils.ResourceLocator;
 21    /**
 22    * The PropertiesFileHandler can read a java-properties file
 23    * and store all entries inside the default category (general)
 24    * with the given property name and value. It is meant to be
 25    * a helper to convert property files into the structure of jConfig.
 26    * After you have read a property you can use for example the XMLFileHandler
 27    * to save it in XML form.
 28    *
 29    * @author Andreas Mecky <andreas.mecky@xcom.de>
 30    * @author Terry Dye <terry.dye@xcom.de>
 31    */
 32    public class PropertiesFileHandler extends AbstractHandler {
 33   
 34    private File file;
 35   
 36    /**
 37    * Default constructor
 38    */
 39  1 public PropertiesFileHandler() {
 40    }
 41   
 42    /**
 43    * Constructor with filename
 44    * @param filename
 45    */
 46  2 public PropertiesFileHandler(String filename) {
 47  2 this(new File(filename));
 48    }
 49   
 50    /**
 51    * Constructor with File
 52    * @param file
 53    */
 54  3 public PropertiesFileHandler(File file) {
 55  3 this.file = file;
 56  3 addFileListener(this);
 57    }
 58   
 59    /**
 60    * This method sets the file that will be processed
 61    *
 62    * @param file the file
 63    */
 64  1 public void setFile(File file) {
 65  1 this.file = file;
 66    }
 67   
 68    /**
 69    * @throws ConfigurationManagerException */
 70  3 public synchronized Configuration load(String configName) throws ConfigurationManagerException {
 71  3 return loadPropertiesFile(file,configName);
 72    }
 73   
 74  3 private Configuration loadPropertiesFile(File file,String configName) throws ConfigurationManagerException {
 75  3 Configuration config = new DefaultConfiguration(configName);
 76  3 if ( file == null ) {
 77  0 ErrorReporter.getErrorHandler().reportError("The file is NULL");
 78  0 throw new ConfigurationManagerException("The file is NULL");
 79    }
 80  3 try {
 81  3 InputStream inputStream = null;
 82  3 if(file.exists()) {
 83  1 inputStream = new FileInputStream(file);
 84    } else {
 85  2 ResourceLocator locator = new ResourceLocator(file.getName());
 86  2 inputStream = locator.getInputStream();
 87    }
 88  3 if ( inputStream == null ) {
 89  0 ErrorReporter.getErrorHandler().reportError("Cannot find the properties file");
 90  0 throw new ConfigurationManagerException("Cannot find the properties file");
 91    }
 92  3 Properties myProperties = new Properties();
 93  3 myProperties.load(inputStream);
 94  3 Enumeration propertyNames = myProperties.propertyNames();
 95  3 while (propertyNames.hasMoreElements()) {
 96  29 String name = (String) propertyNames.nextElement();
 97  29 String value = myProperties.getProperty(name);
 98  29 config.setProperty(name, value);
 99    }
 100    } catch (Exception e) {
 101  0 ErrorReporter.getErrorHandler().reportError("The file cannot be loaded",e);
 102  0 throw new ConfigurationManagerException("The file cannot be loaded:"+e.getMessage());
 103    }
 104  3 config.resetCreated();
 105  3 return config;
 106    }
 107   
 108    /** This method should store all categories and properties.
 109    * @throws ConfigurationManagerException
 110    */
 111  0 public void store(Configuration configuration) throws ConfigurationManagerException {
 112    // this will store the properties for a certain category
 113  0 String[] categories = configuration.getCategoryNames();
 114  0 try {
 115    // let's open the file
 116  0 FileWriter fw = new FileWriter(file);
 117    // and write the header
 118  0 fw.write("#\n");
 119  0 fw.write("# automatically generated properties file\n");
 120  0 fw.write("#\n");
 121    // now we walk through all categories
 122  0 for ( int i = 0; i < categories.length;i++) {
 123    // write the category as comment
 124  0 fw.write("#\n");
 125  0 fw.write("# category: " + categories[i] + "\n");
 126  0 fw.write("#\n");
 127    // now we get all properties for this category
 128  0 String[] propNames = configuration.getPropertyNames(categories[i]);
 129    // here we walk through the properties
 130  0 for ( int j = 0; j < propNames.length;j++) {
 131    // and write it to the file
 132    // form category.property=value
 133  0 fw.write(categories[i] + "." + propNames[j] + "=" + configuration.getProperty(propNames[j],"",categories[i]) + "\n");
 134    }
 135    }
 136    // close the file because we are done
 137  0 fw.close();
 138    } catch (Exception e) {
 139  0 ErrorReporter.getErrorHandler().reportError("The file cannot be saved",e);
 140  0 throw new ConfigurationManagerException("The file cannot be saved");
 141    }
 142    }
 143   
 144    /**
 145    * @see org.jconfig.handler.AbstractHandler#getFile()
 146    */
 147  6 public File getFile() {
 148  6 return file;
 149    }
 150    }