Clover Coverage Report
Coverage timestamp: Sa Aug 2 2008 13:56:27 CEST
../../../../img/srcFileCovDistChart6.png 69% of files have more coverage
43   308   29   1,87
16   115   0,67   23
23     1,26  
1    
 
  QedeqConfig       Line # 33 43 29 52,4% 0.5243902
 
  (3)
 
1    /* $Id: QedeqConfig.java,v 1.7 2008/07/26 07:59:40 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.base.io.IoUtility;
25   
26   
27    /**
28    * This class gives a type save access to properties of the application.
29    *
30    * @version $Revision: 1.7 $
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  39 toggle public QedeqConfig(final File configFile, final String description, final File basisDirectory)
67    throws IOException {
68  39 configAccess = new ConfigAccess(configFile, description);
69  39 this.basisDirectory = basisDirectory;
70    }
71   
72    /**
73    * Store properties in config file.
74    *
75    * @throws IOException
76    */
 
77  39 toggle public final void store() throws IOException {
78  39 configAccess.store();
79    }
80   
81    /**
82    * Get local file directory to save generated files in.
83    *
84    * @return Generation directory.
85    */
 
86  57 toggle public final File getGenerationDirectory() {
87  57 String location = getKeyValue("generationLocation");
88  57 if (location == null) {
89  57 location = QedeqConfig.DEFAULT_GENERATED;
90    }
91  57 return createAbsolutePath(location);
92    }
93   
94    /**
95    * Set local file directory for generated files.
96    *
97    * @param location generation directory.
98    */
 
99  0 toggle public final void setGenerationDirectory(final File location) {
100  0 final String relative = createRelativePath(location);
101  0 setKeyValue("generationLocation", relative);
102    }
103   
104    /**
105    * Get local file directory for module buffering.
106    *
107    * @return Buffer directory.
108    */
 
109  134 toggle public final File getBufferDirectory() {
110  134 String location = getKeyValue("bufferLocation");
111  134 if (location == null) {
112  134 location = QedeqConfig.DEFAULT_LOCAL_BUFFER;
113    }
114  134 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  0 toggle public final void setBufferDirectory(final File location) {
125  0 final String relative = createRelativePath(location);
126  0 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  0 toggle public final File getLocalModulesDirectory() {
135  0 String location = getKeyValue("localModulesDirectory");
136  0 if (location == null) {
137  0 location = QedeqConfig.DEFAULT_LOCAL_MODULES_DIRECTORY;
138    }
139  0 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  0 toggle public final void setLocalModulesDirectory(final File location) {
150  0 final String relative = createRelativePath(location);
151  0 setKeyValue("localModulesDirectory", relative);
152    }
153   
154    /**
155    * Get local file location for log file.
156    *
157    * @return Log file path.
158    */
 
159  39 toggle public final String getLogFile() {
160  39 final String location = getKeyValue("logLocation");
161  39 if (location == null) {
162  39 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  0 toggle public final String[] getModuleHistory() {
173  0 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  0 toggle public final void saveModuleHistory(final List modules) {
182  0 configAccess.removeProperties(("moduleHistory."));
183  0 for (int i = 0; i < modules.size(); i++) {
184  0 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 toggle 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  39 toggle public final void setLoadedModules(final String[] moduleAddresses) {
204  39 configAccess.removeProperties("checkedModule.");
205  156 for (int i = 0; i < moduleAddresses.length; i++) {
206  117 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  230 toggle public final File getBasisDirectory() {
216  230 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  191 toggle public final File createAbsolutePath(final String path) {
226  191 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  0 toggle private final String createRelativePath(final File path) {
236  0 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  39 toggle public boolean isAutoReloadLastSessionChecked() {
245  39 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  39 toggle public final void setAutoReloadLastSessionChecked(final boolean mode) {
255  39 setKeyValue("sessionAutoReload", (mode ? "true" : "false"));
256    }
257   
258    /**
259    * Should old HTML code be generated?
260    *
261    * @return Old HTML code?
262    */
 
263  0 toggle public final boolean isOldHtml() {
264  0 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  0 toggle public final void setOldHtml(final boolean mode) {
273  0 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  230 toggle protected String getKeyValue(final String key) {
283  230 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  39 toggle protected String getKeyValue(final String key, final String defaultValue) {
295  39 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  156 toggle protected void setKeyValue(final String key, final String value) {
305  156 configAccess.setString(key, value);
306    }
307   
308    }