Clover coverage report - QedeqKernelSe Coverage Report
Coverage timestamp: Do Mrz 27 2008 21:46:26 CET
file stats: LOC: 308   Methods: 23
NCLOC: 115   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
QedeqConfig.java 75% 88,4% 95,7% 87,8%
coverage coverage
 1    /* $Id: QedeqConfig.java,v 1.6 2008/03/27 05:16:25 m31 Exp $
 2    *
 3    * This file is part of the project "Hilbert II" - http://www.qedeq.org
 4    *
 5    * Copyright 2000-2008, 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.IOException;
 22    import java.util.List;
 23   
 24    import org.qedeq.kernel.utility.IoUtility;
 25   
 26   
 27    /**
 28    * This class gives a type save access to properties of the application.
 29    *
 30    * @version $Revision: 1.6 $
 31    * @author Michael Meyling
 32    */
 33    public class QedeqConfig {
 34   
 35    /** Default location for newly created QEDEQ modules. */
 36    private static final String DEFAULT_LOCAL_MODULES_DIRECTORY
 37    = "local";
 38   
 39    /** Default location for locally buffered module files. */
 40    private static final String DEFAULT_LOCAL_BUFFER
 41    = "buffer";
 42   
 43    /** Default location for generated module and document files. */
 44    private static final String DEFAULT_GENERATED
 45    = "generated";
 46   
 47    /** Default log file path. */
 48    private static final String DEFAULT_LOG_FILE
 49    = "log/log.txt";
 50   
 51    /** This class organizes the access to the config parameters. */
 52    private final ConfigAccess configAccess;
 53   
 54    /** Basis directory of application for all variable data. Basis for all relative paths. */
 55    private final File basisDirectory;
 56   
 57    /**
 58    * Constructor.
 59    *
 60    * @param configFile Config file.
 61    * @param description Config file description.
 62    * @param basisDirectory Basis directory of application for all variable data. Basis for all
 63    * relative paths
 64    * @throws IOException Config file couldn't be loaded.
 65    */
 66  31 public QedeqConfig(final File configFile, final String description, final File basisDirectory)
 67    throws IOException {
 68  31 configAccess = new ConfigAccess(configFile, description);
 69  31 this.basisDirectory = basisDirectory;
 70    }
 71   
 72    /**
 73    * Store properties in config file.
 74    *
 75    * @throws IOException
 76    */
 77  34 public final void store() throws IOException {
 78  34 configAccess.store();
 79    }
 80   
 81    /**
 82    * Get local file directory to save generated files in.
 83    *
 84    * @return Generation directory.
 85    */
 86  22 public final File getGenerationDirectory() {
 87  22 String location = getKeyValue("generationLocation");
 88  22 if (location == null) {
 89  0 location = QedeqConfig.DEFAULT_GENERATED;
 90    }
 91  22 return createAbsolutePath(location);
 92    }
 93   
 94    /**
 95    * Set local file directory for generated files.
 96    *
 97    * @param location generation directory.
 98    */
 99  3 public final void setGenerationDirectory(final File location) {
 100  3 final String relative = createRelativePath(location);
 101  3 setKeyValue("generationLocation", relative);
 102    }
 103   
 104    /**
 105    * Get local file directory for module buffering.
 106    *
 107    * @return Buffer directory.
 108    */
 109  367 public final File getBufferDirectory() {
 110  367 String location = getKeyValue("bufferLocation");
 111  367 if (location == null) {
 112  0 location = QedeqConfig.DEFAULT_LOCAL_BUFFER;
 113    }
 114  367 return createAbsolutePath(location);
 115    }
 116   
 117   
 118    /**
 119    * Set local file directory for module buffering.
 120    * After changing this location the buffer should eventually be cleared.
 121    *
 122    * @param location buffer directory.
 123    */
 124  3 public final void setBufferDirectory(final File location) {
 125  3 final String relative = createRelativePath(location);
 126  3 setKeyValue("bufferLocation", relative);
 127    }
 128   
 129    /**
 130    * Get directory for newly created QEDEQ module files.
 131    *
 132    * @return Directory for newly created QEDEQ modules.
 133    */
 134  3 public final File getLocalModulesDirectory() {
 135  3 String location = getKeyValue("localModulesDirectory");
 136  3 if (location == null) {
 137  0 location = QedeqConfig.DEFAULT_LOCAL_MODULES_DIRECTORY;
 138    }
 139  3 return createAbsolutePath(location);
 140    }
 141   
 142   
 143    /**
 144    * Set directory for newly created module files.
 145    * After changing this location the buffer should eventually be cleared.
 146    *
 147    * @param location Buffer directory.
 148    */
 149  3 public final void setLocalModulesDirectory(final File location) {
 150  3 final String relative = createRelativePath(location);
 151  3 setKeyValue("localModulesDirectory", relative);
 152    }
 153   
 154    /**
 155    * Get local file location for log file.
 156    *
 157    * @return Log file path.
 158    */
 159  4 public final String getLogFile() {
 160  4 final String location = getKeyValue("logLocation");
 161  4 if (location == null) {
 162  4 return QedeqConfig.DEFAULT_LOG_FILE;
 163    }
 164  0 return location;
 165    }
 166   
 167    /**
 168    * Get history of modules, which were tried to load.
 169    *
 170    * @return list of modules.
 171    */
 172  2 public final String[] getModuleHistory() {
 173  2 return configAccess.getStringProperties("moduleHistory.");
 174    }
 175   
 176    /**
 177    * Save history of modules, which were tried to load.
 178    *
 179    * @param modules list of modules.
 180    */
 181  6 public final void saveModuleHistory(final List modules) {
 182  6 configAccess.removeProperties(("moduleHistory."));
 183  6 for (int i = 0; i < modules.size(); i++) {
 184  60 setKeyValue("moduleHistory." + (i + 101),
 185    modules.get(i).toString());
 186    }
 187    }
 188   
 189    /**
 190    * Get list of previously checked modules.
 191    *
 192    * @return list of modules.
 193    */
 194  0 public final String[] getPreviouslyCheckedModules() {
 195  0 return configAccess.getStringProperties("checkedModule.");
 196    }
 197   
 198    /**
 199    * Set successfully list of successfully loaded QEDEQ modules.
 200    *
 201    * @param moduleAddresses This modules were successfully checked.
 202    */
 203  31 public final void setLoadedModules(final String[] moduleAddresses) {
 204  31 configAccess.removeProperties("checkedModule.");
 205  31 for (int i = 0; i < moduleAddresses.length; i++) {
 206  106 setKeyValue("checkedModule." + (i + 1), moduleAddresses[i]);
 207    }
 208    }
 209   
 210    /**
 211    * Get basis directory of this application.
 212    *
 213    * @return Basis directory of application for all variable data. Basis for all relative paths.
 214    */
 215  408 public final File getBasisDirectory() {
 216  408 return basisDirectory;
 217    }
 218   
 219    /**
 220    * Get file path starting from basis directory of this application.
 221    *
 222    * @param path Go to this path starting from basis directory.
 223    * @return File path resolved against basis application directory.
 224    */
 225  392 public final File createAbsolutePath(final String path) {
 226  392 return new File(getBasisDirectory(), path);
 227    }
 228   
 229    /**
 230    * Create relative file path starting from basis directory of this application.
 231    *
 232    * @param path Reach this path starting from basis directory.
 233    * @return File path relative to basis application directory.
 234    */
 235  9 private final String createRelativePath(final File path) {
 236  9 return IoUtility.createRelativePath(getBasisDirectory(), path);
 237    }
 238   
 239    /**
 240    * Get auto reload of last session successfully loaded modules.
 241    *
 242    * @return auto reload enabled?
 243    */
 244  34 public boolean isAutoReloadLastSessionChecked() {
 245  34 return "true".equals(
 246    getKeyValue("sessionAutoReload", "true"));
 247    }
 248   
 249    /**
 250    * Set auto reload checked modules of last session mode.
 251    *
 252    * @param mode enable auto reload?
 253    */
 254  32 public final void setAutoReloadLastSessionChecked(final boolean mode) {
 255  32 setKeyValue("sessionAutoReload", (mode ? "true" : "false"));
 256    }
 257   
 258    /**
 259    * Should old HTML code be generated?
 260    *
 261    * @return Old HTML code?
 262    */
 263  3 public final boolean isOldHtml() {
 264  3 return "true".equals(getKeyValue("oldHtml", "true"));
 265    }
 266   
 267    /**
 268    * Set old HTML code generation flag.
 269    *
 270    * @param mode Set old HTML code generation?
 271    */
 272  3 public final void setOldHtml(final boolean mode) {
 273  3 setKeyValue("oldHtml", (mode ? "true" : "false"));
 274    }
 275   
 276    /**
 277    * Get value for given key.
 278    *
 279    * @param key Get value for this key.
 280    * @return Value, maybe <code>null</code>.
 281    */
 282  398 protected String getKeyValue(final String key) {
 283  398 return configAccess.getString(key);
 284    }
 285   
 286    /**
 287    * Get value for given key.
 288    *
 289    * @param key Get value for this key.
 290    * @param defaultValue Default value..
 291    * @return Value. If value for key is originally <code>null</code> <code>defaultValue</code>
 292    * is returned..
 293    */
 294  787 protected String getKeyValue(final String key, final String defaultValue) {
 295  787 return configAccess.getString(key, defaultValue);
 296    }
 297   
 298    /**
 299    * Set value for given key.
 300    *
 301    * @param key For this key.
 302    * @param value Set this value.
 303    */
 304  221 protected void setKeyValue(final String key, final String value) {
 305  221 configAccess.setString(key, value);
 306    }
 307   
 308    }