Clover coverage report - JGAP 3.1
Coverage timestamp: Mo Dez 11 2006 21:16:18 CET
file stats: LOC: 158   Methods: 7
NCLOC: 67   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ConfigFileReader.java 100% 93,1% 100% 95,5%
coverage coverage
 1    /*
 2    * This file is part of JGAP.
 3    *
 4    * JGAP offers a dual license model containing the LGPL as well as the MPL.
 5    *
 6    * For licencing information please see the file license.txt included with JGAP
 7    * or have a look at the top of class org.jgap.Chromosome which representatively
 8    * includes the JGAP license policy applicable for any file delivered with JGAP.
 9    */
 10    package org.jgap.data.config;
 11   
 12    import java.util.*;
 13    import java.io.*;
 14   
 15    /**
 16    * This is a Singleton Helper class to read a JGAP config file and provide a
 17    * simple interface to the config properties.
 18    *
 19    * @author Siddhartha Azad
 20    * @since 2.3
 21    */
 22    public class ConfigFileReader {
 23    private final static String CVS_REVISION = "$Revision: 1.5 $";
 24   
 25    // Name of the config file to read
 26    private String m_fileName;
 27   
 28    // Properties read from the config file
 29    private Properties m_props;
 30   
 31    // namespace of the property
 32    private String m_ns;
 33   
 34    /**
 35    * Singleton Instance of ConfigFileReader
 36    */
 37    private static ConfigFileReader m_cfReader;
 38   
 39    /**
 40    * Method to create and access the Singleton ConfigFileReader instance.
 41    * @return instance of the ConfigFileReader
 42    *
 43    * @author Siddhartha Azad
 44    * @since 2.3
 45    */
 46  5 public static ConfigFileReader instance() {
 47  5 if (m_cfReader == null) {
 48  1 m_cfReader = new ConfigFileReader();
 49    }
 50  5 return m_cfReader;
 51    }
 52   
 53    /**
 54    * Private Constructor.
 55    *
 56    * @author Siddhartha Azad
 57    * @since 2.3
 58    */
 59  1 private ConfigFileReader() {
 60  1 m_props = new Properties();
 61    }
 62   
 63    /**
 64    * Retrieve the value for the property with the name as in param name.
 65    * @param a_name name of the property of which the value is required
 66    * @return value for the property with the name as in param name, null if
 67    * property not found
 68    *
 69    * @author Siddhartha Azad
 70    * @since 2.3
 71    */
 72  1 public String getValue(final String a_name) {
 73  1 String tmpName = m_ns + "." + a_name;
 74  1 String val = m_props.getProperty(tmpName);
 75  1 return val;
 76    }
 77   
 78    /**
 79    * Retrieve the values for the property with the name as in param name.
 80    *
 81    * @param a_name the name of the property of which the value is required
 82    * @return ArrayList of Strings with values for the property with the name as
 83    * in param name, null if property not found
 84    *
 85    * @author Siddhartha Azad
 86    * */
 87  2 public List getValues(final String a_name) {
 88  2 String val = "";
 89  2 boolean done = false;
 90  2 String tmpName = "";
 91  2 int idx = 0;
 92  2 List values = Collections.synchronizedList(new ArrayList());
 93  2 while (!done) {
 94  3 tmpName = m_ns + "." + a_name + "[" + idx + "]";
 95  3 val = m_props.getProperty(tmpName);
 96  3 if (val == null) {
 97  2 done = true;
 98    }
 99    else {
 100  1 values.add(val);
 101  1 idx++;
 102    }
 103    }
 104  2 if (idx == 0) {
 105  1 return null;
 106    }
 107    else {
 108  1 return values;
 109    }
 110    }
 111   
 112    /**
 113    * Set the namespace for the properties that are being read from the
 114    * config file at this point.
 115    *
 116    * @param a_ns namespace for the properties being read
 117    *
 118    * @author Siddhartha Azad
 119    */
 120  1 public void setNS(final String a_ns) {
 121  1 m_ns = a_ns;
 122    }
 123   
 124    /**
 125    * Set the config file to load from. Everytime this method is called,
 126    * properties are reloaded from the config file.
 127    *
 128    * @param a_fileName Name of the config file.
 129    * @throws ConfigException
 130    *
 131    * @author Siddhartha Azad
 132    * @since 2.3
 133    */
 134  1 public void setFileName(final String a_fileName)
 135    throws ConfigException {
 136  1 m_fileName = a_fileName;
 137  1 load();
 138    }
 139   
 140    /**
 141    * Load the config properties file into a Properties instance.
 142    * @throws ConfigException
 143    *
 144    * @author Siddhartha Azad
 145    * @since 2.3
 146    */
 147  1 private void load()
 148    throws ConfigException {
 149  1 try {
 150  1 m_props.load(new FileInputStream(m_fileName));
 151    }
 152    catch (Exception ex) {
 153  0 String dir = new File(".").getAbsolutePath();
 154  0 throw new ConfigException("Error reading Config file " + m_fileName
 155    + " in directory " + dir);
 156    }
 157    }
 158    }