Clover coverage report - QedeqKernelSe Coverage Report
Coverage timestamp: Sa Jan 26 2008 14:11:34 CET
file stats: LOC: 282   Methods: 15
NCLOC: 126   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ConfigAccess.java 62,5% 63,6% 73,3% 65,1%
coverage coverage
 1    /* $Id: ConfigAccess.java,v 1.5 2007/12/21 23:33:46 m31 Exp $
 2    *
 3    * This file is part of the project "Hilbert II" - http://www.qedeq.org
 4    *
 5    * Copyright 2000-2007, Michael Meyling <mime@qedeq.org>.
 6    *
 7    * "Hilbert II" is free software; you can redistribute
 8    * it and/or modify it under the terms of the GNU General Public
 9    * License as published by the Free Software Foundation; either
 10    * version 2 of the License, or (at your option) any later version.
 11    *
 12    * This program is distributed in the hope that it will be useful,
 13    * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 15    * GNU General Public License for more details.
 16    */
 17   
 18    package org.qedeq.kernel.config;
 19   
 20    import java.io.File;
 21    import java.io.FileInputStream;
 22    import java.io.FileOutputStream;
 23    import java.io.IOException;
 24    import java.io.InputStream;
 25    import java.io.OutputStream;
 26    import java.util.ArrayList;
 27    import java.util.Collections;
 28    import java.util.Enumeration;
 29    import java.util.List;
 30    import java.util.Properties;
 31   
 32   
 33    /**
 34    * This class reads entries from property files. This class should not
 35    * be used outside this package.
 36    *
 37    * @version $Revision: 1.5 $
 38    * @author Michael Meyling
 39    */
 40    final class ConfigAccess {
 41   
 42    /** Config file. */
 43    private final File configFile;
 44   
 45    /** Collector for properties. */
 46    private Properties properties = new Properties();
 47   
 48    /** Config file description. */
 49    private final String description;
 50   
 51    /**
 52    * Get access for a config file.
 53    *
 54    * @param configFile Config file.
 55    * @param description Config file description
 56    * @throws IOException Config file couldn't be loaded.
 57    */
 58  27 public ConfigAccess(final File configFile, final String description) throws IOException {
 59  27 this.configFile = configFile;
 60  27 this.description = description;
 61  27 try {
 62  27 load(new FileInputStream(configFile));
 63    } catch (IOException e) {
 64  0 System.out.println("no config file found, using default values");
 65    }
 66  27 setString("configFileLocation", configFile.getCanonicalPath());
 67    }
 68   
 69    /**
 70    * Get config file.
 71    *
 72    * @return Config file.
 73    */
 74  27 public final File getConfigFile() {
 75  27 return configFile;
 76    }
 77   
 78    /**
 79    * Get description for config file.
 80    *
 81    * @return Config file description.
 82    */
 83  27 public final String getConfigDescription() {
 84  27 return description;
 85    }
 86   
 87    /**
 88    * Get properties.
 89    *
 90    * @return properties.
 91    */
 92  1905 private final Properties getProperties() {
 93  1905 return properties;
 94    }
 95   
 96    /**
 97    * Load properties from stream. The properties are
 98    * added to the previous ones.
 99    *
 100    * @param inStream load from this stream
 101    * @throws IOException loading failed
 102    */
 103  27 private final void load(final InputStream inStream) throws IOException {
 104  27 getProperties().load(inStream);
 105    }
 106   
 107    /**
 108    * Store properties in config file.
 109    *
 110    * @throws IOException Saving failed.
 111    */
 112  27 public final void store() throws IOException {
 113  27 OutputStream out = null;
 114  27 try {
 115  27 out = new FileOutputStream(getConfigFile());
 116  27 getProperties().store(out, getConfigDescription());
 117    } finally {
 118  27 if (out != null) {
 119  27 try {
 120  27 out.close();
 121    } catch (IOException e) {
 122  0 throw e;
 123    } catch (Exception e) {
 124  0 throw new IOException(e.toString());
 125    }
 126    }
 127    }
 128    }
 129   
 130    /**
 131    * Return String property.
 132    *
 133    * @param name Get this property.
 134    * @return String for looked property. <code>null</code>, if property is missing.
 135    */
 136  293 public final String getString(final String name) {
 137  293 return getProperties().getProperty(name);
 138    }
 139   
 140    /**
 141    * Return String property.
 142    *
 143    * @param name Look for this String property.
 144    * @param defaultValue Return this value if property doesn't exist.
 145    * @return Value of property. Equal to default value if parameter doesn't exist.
 146    */
 147  463 public final String getString(final String name, final String defaultValue) {
 148  463 final String value = getProperties().getProperty(name);
 149  463 if (value == null) {
 150  0 setString(name, defaultValue);
 151  0 return defaultValue;
 152    } else {
 153  463 return value;
 154    }
 155    }
 156   
 157    /**
 158    * Set String property.
 159    *
 160    * @param name Set this property.
 161    * @param value Set property to this value.
 162    */
 163  265 public final void setString(final String name, final String value) {
 164  265 getProperties().setProperty(name, value);
 165    }
 166   
 167    /**
 168    * Get list of String properties with certain prefix.
 169    * Example:
 170    * <ul>
 171    * <li>module1=bluebird</li>
 172    * <li>module2=tiger</li>
 173    * <li>module3=tulip</li>
 174    * </ul>
 175    * The sequence of resulting properties is sorted by their keys.
 176    *
 177    * @param namePrefix Prefix of seeked property name.
 178    * @return List of key sorted string properties (maybe empty).
 179    */
 180  57 public final String[] getStringProperties(final String namePrefix) {
 181  57 final List list = new ArrayList();
 182  57 final Enumeration keys = getProperties().keys();
 183  57 final List keyList = Collections.list(keys);
 184  57 Collections.sort(keyList);
 185  57 for (int i = 0; i < keyList.size(); i++) {
 186  1643 final String key = (String) keyList.get(i);
 187  1643 if (key.startsWith(namePrefix)) {
 188  500 list.add(getProperties().get(key));
 189    }
 190    }
 191  57 return (String []) list.toArray(new String[] {});
 192    }
 193   
 194    /**
 195    * Set int property.
 196    *
 197    * @param name Set this property.
 198    * @param value Set property to this value.
 199    */
 200  0 public final void setInteger(final String name, final int value) {
 201  0 setString(name, "" + value);
 202    }
 203   
 204   
 205    /**
 206    * Get int property.
 207    *
 208    * @param name look for this property
 209    * @return property
 210    * @throws IllegalArgumentException Property is no valid int value
 211    * @throws NullPointerException Property doesn't exist
 212    */
 213  0 public final int getInteger(final String name) {
 214  0 final String intPropAsString = getProperties().getProperty(name);
 215  0 if (intPropAsString != null) {
 216  0 try {
 217  0 return Integer.parseInt(intPropAsString);
 218    } catch (NumberFormatException ex) {
 219  0 throw new IllegalArgumentException(
 220    "int property " + intPropAsString + " has invalid format");
 221    }
 222    } else {
 223  0 throw new NullPointerException("property \"" + name + "\" not found");
 224    }
 225    }
 226   
 227    /**
 228    * Return int property.
 229    *
 230    * @param name Look for this integer property.
 231    * @param defaultValue Return this value if property doesn't exist.
 232    * @return int value of property. Equal to default value if parameter doesn't exist.
 233    * @throws IllegalArgumentException Property is no valid int value.
 234    */
 235  0 public final int getInteger(final String name, final int defaultValue) {
 236  0 final String intPropAsString = getProperties().getProperty(name);
 237  0 if (intPropAsString != null) {
 238  0 try {
 239  0 return Integer.parseInt(intPropAsString);
 240    } catch (NumberFormatException ex) {
 241  0 throw new IllegalArgumentException(
 242    "Integer-Property " + intPropAsString + " has invalid format");
 243    }
 244    } else {
 245  0 setInteger(name, defaultValue);
 246  0 return defaultValue;
 247    }
 248    }
 249   
 250    /**
 251    * Remove property.
 252    *
 253    * @param name Property to delete.
 254    */
 255  0 public final void removeProperty(final String name) {
 256  0 getProperties().remove(name);
 257    }
 258   
 259    /**
 260    * Remove properties with certain prefix.
 261    *
 262    * Example:
 263    * <ul>
 264    * <li>module1=bluebird</li>
 265    * <li>module2=tiger</li>
 266    * <li>module3=tulip</li>
 267    * </ul>
 268    * Calling with value <code>module</code> deletes all.
 269    *
 270    * @param namePrefix Prefix of seeked property name.
 271    */
 272  28 public final void removeProperties(final String namePrefix) {
 273  28 final Enumeration keys = getProperties().keys();
 274  28 while (keys.hasMoreElements()) {
 275  806 final String key = (String) keys.nextElement();
 276  806 if (key.startsWith(namePrefix)) {
 277  245 getProperties().remove(key);
 278    }
 279    }
 280    }
 281   
 282    }