/[xulu]/trunk.bakup/src/appl/ext/XuluConfig.java
ViewVC logotype

Contents of /trunk.bakup/src/appl/ext/XuluConfig.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 77 - (show annotations)
Wed Feb 10 16:14:27 2010 UTC (14 years, 10 months ago) by alfonx
File size: 14574 byte(s)
backup of trunk
1 package appl.ext;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.util.Enumeration;
9 import java.util.Iterator;
10 import java.util.Properties;
11 import java.util.Set;
12 import java.util.SortedSet;
13 import java.util.TreeSet;
14
15 import org.geotools.io.DefaultFileFilter;
16
17 /**
18 * Simple Property store, which is loaded at each Xulu start and saved at each
19 * Xulu exit. Every class or plugin can store and retrieve configuration
20 * information easy through a static method. To keep the property file readable
21 * and to avoid conflicts with other plugins please prefix the key-name with a
22 * unique name, which identifies your class, followed by a dot and the property.
23 * This is important for correct display in a User-GUI (which could be
24 * implemented). <br>
25 * <br>
26 * Keyformat: <code>domain.property</code> <br>
27 * Example key: <code>DiscoveryService.className</code> <br>
28 * <br>
29 * There could be no double entries.But you can save multiple values to one key
30 * using the {@link #setMultiProperty(String, String[]) setMultiProperty}
31 * method. <br>
32 * <b>These values are internally separated with ';', so see that you don't use
33 * ';' in your values of {@link #setMultiProperty(String, String[]) Multi
34 * Properties}</b> <br>
35 * <br>
36 * First the settings of the file "DefaultProperties" are loaded. After that the
37 * values are possibly overwritten by the loading of the file "XuluProperties"
38 * in which is intended to store a actual user configuration.
39 * <b>"DefaultPropoerties" should never be modified by the user.</b> The
40 * programmer should store all default values there. <br>
41 * <br>
42 * The user may change values through the {@link ConfigurationEditorGUI}.
43 *
44 * @see ConfigurationEditorGUI
45 * @see ConfigurationEditorEngine
46 * @author Dominik Appl
47 */
48 public class XuluConfig {
49
50 private final String delimiter = ";";
51
52 // File for default values
53 File defaultFile = new File("DefaultProperties");
54
55 // File in which the props are stored
56 File file = new File("XuluProperties");
57
58 Properties propStore = null;
59
60 private static XuluConfig instance = null;
61
62 private XuluConfig() {
63 this(null);
64 }
65
66 private XuluConfig(File rootDir) {
67 if ( rootDir != null && rootDir.isDirectory() ) {
68 defaultFile = new File(rootDir, defaultFile.getName());
69 file = new File(rootDir, file.getName());
70 }
71 // try to read defaults
72 Properties defaults = new Properties();
73 try {
74 defaults.load(new FileInputStream(defaultFile));
75 } catch (Exception e) {
76 System.out
77 .println("Warning: defaults could not be loaded from file '"
78 + defaultFile + "'");
79 System.out.println(e.getMessage());
80 }
81 propStore = new Properties(defaults);
82 if (file.exists())
83 load();
84 else
85 store();
86 }
87
88 public static XuluConfig getXuluConfig() {
89 return getXuluConfig(null);
90 }
91
92 public static XuluConfig getXuluConfig(File rootDir) {
93 if (instance == null)
94 instance = new XuluConfig(rootDir);
95 return instance;
96 }
97
98 /**
99 * Saves the configuration to the file system
100 */
101 public void store() {
102 try {
103 propStore.store(new FileOutputStream(file),
104 "Stored properties of the Xulu modelling platform");
105 } catch (FileNotFoundException e) {
106 System.err.println("Could not write PropertyStore!!!");
107 e.printStackTrace();
108 } catch (IOException e) {
109 System.err.println("Could not write PropertyStore!!!");
110 e.printStackTrace();
111 }
112
113 }
114
115 /**
116 * Sets the Property file. Notice that this has only an effect if you call
117 * load afterwards.
118 *
119 * @param file
120 */
121 public void setPropertyFile(File file) {
122 this.file = file;
123 }
124
125 /**
126 * loads the Properties from the Propertyfile. No loaded values are deleted -
127 * only existing ones are overwritten;
128 */
129 public void load() {
130 try {
131 propStore.load(new FileInputStream(file));
132 } catch (FileNotFoundException e) {
133 System.err.println("Property file with name '" + file
134 + "' not found");
135 } catch (IOException e) {
136 // TODO Auto-generated catch block
137 e.printStackTrace();
138 }
139 }
140
141 /**
142 * inserts the property value with the specified key into the
143 * {@link XuluConfig}
144 *
145 * @param key
146 * the key
147 * @param value
148 * the value associated with the key
149 */
150 public void setProperty(String key, String value) {
151 if (!key.contains("."))
152 System.out
153 .println("WARNING for key "
154 + key
155 + ": Propertykeys in XuluConfig should have at least one '.' ! ");
156 propStore.setProperty(key, value);
157 }
158
159 /***************************************************************************
160 * @return the property associated with the provided key. If not found the
161 * defaults are searched. If still not found, null is returned.
162 * Gives out a warning to console if the key was not found
163 **************************************************************************/
164 public String getProperty(String key) {
165 String value = propStore.getProperty(key);
166 if (value == null)
167 System.err.println("Warning! Value for property '" + key
168 + "' was not found in property file!");
169 return value;
170 }
171
172 /***************************************************************************
173 * Same as {@link #getProperty(String)}, but you can disable the warning
174 * message
175 *
176 * @param giveWarning
177 * if true, a warning is given to the console if the specified
178 * entry was not found
179 * @return the property associated with the provided key. If not found the
180 * defaults are searched. If still not found, null is returned.
181 * Gives out a warning to console if the key was not found
182 **************************************************************************/
183 public String getProperty(String key, boolean giveWarning) {
184 String value = propStore.getProperty(key);
185 if (value == null && giveWarning)
186 System.err.println("Warning! Value for property '" + key
187 + "' was not found in property file!");
188 return value;
189 }
190
191 /***************************************************************************
192 *
193 * @return the property associated with the provided key as integer value.
194 * If not found the defaults are searched. If not found or the value
195 * is in a wrong format 0 is returned and a warning is given to the
196 * console
197 **************************************************************************/
198 public int getIntProperty(String key) {
199 String value = propStore.getProperty(key);
200 if (value == null) {
201 System.err.println("Error! Value for property '" + key
202 + "' was not found in property file!");
203 return 0;
204 }
205 int returnvalue = 0;
206 try {
207 returnvalue = Integer.valueOf(value);
208 } catch (java.lang.NumberFormatException e) {
209 System.err
210 .println("Error! Value for property '"
211 + key
212 + "' was not in the right format. Should be a Integer value! - Returning 0");
213 }
214 return returnvalue;
215 }
216
217 /***************************************************************************
218 * @return the property associated with the provided key as double value. If
219 * not found the defaults are searched. If not found or the value is
220 * in a wrong format 0 is returned and a warning is given to the
221 * console
222 **************************************************************************/
223 public double getDoubleProperty(String key) {
224 String value = propStore.getProperty(key);
225 if (value == null) {
226 System.err.println("Error! Value for property '" + key
227 + "' was not found in property file!");
228 return 0;
229 }
230 double returnvalue = 0;
231 try {
232 returnvalue = Double.valueOf(value);
233 } catch (java.lang.NumberFormatException e) {
234 System.err
235 .println("Error! Value for property '"
236 + key
237 + "' was not in the right format. Should be a Integer value! - Returning 0");
238 }
239 return returnvalue;
240 }
241
242 /**
243 * inserts the property value with the specified key into the
244 * {@link XuluConfig}
245 *
246 * @param key
247 * the key
248 * @param value
249 * the value associated with the key
250 */
251 public void setBooleanProperty(String key, boolean value) {
252 this.setProperty(key, String.valueOf(value));
253 }
254
255 /**
256 * inserts the property value with the specified key into the
257 * {@link XuluConfig}
258 *
259 * @param key
260 * the key
261 * @param value
262 * the value associated with the key
263 */
264 public void setIntProperty(String key, int value) {
265 this.setProperty(key, String.valueOf(value));
266 }
267
268 /***************************************************************************
269 * @return the property associated with the provided key as boolean value.
270 * If not found the defaults are searched. If still not found or
271 * null then false is returned. Gives out a warning to console if
272 * the key was not found
273 **************************************************************************/
274 public boolean getBooleanProperty(String key) {
275 String value = propStore.getProperty(key);
276 if (value == null) {
277 System.err.println("Error! Value for property '" + key
278 + "' was not found in property file!");
279 return false;
280 }
281 if(value.equals("1"))
282 return true;
283 boolean bool = false;
284 try {
285 bool = Boolean.valueOf(value);
286 } catch (Exception e) {
287 System.err.println("Error! Value for property '" + key
288 + "' was not a valid boolean value");
289 }
290 return bool;
291 }
292
293 /**
294 * A multiproperty stores multiple values for one key. These values are
295 * internaly seperated with spaces, so see that you dont use spaces in your
296 * values.
297 *
298 * @param key
299 * the key
300 * @param values
301 * array of the values which should be associated with the key
302 */
303 public void setMultiProperty(String key, String values[]) {
304 if (!key.contains("."))
305 System.out
306 .println("WARNING for key "
307 + key
308 + ": Propertykeys in XuluConfig should have at least one '.' ! ");
309 String value = "";
310 if (values != null)
311 for (String string : values) {
312 value += (string + delimiter);
313 }
314 propStore.setProperty(key, value);
315 }
316
317 /***************************************************************************
318 * A multiproperty stores multiple values for one key. These values are
319 * internaly seperated with ';', so do not use ';' in your values. if the
320 * key was not found it gives out a warning to console and a empty array is
321 * retured.
322 *
323 * @return the multiProperty associated with the provided key, which is in
324 * this case just a String array
325 **************************************************************************/
326
327 public String[] getMultiProperty(String key) {
328 String value = propStore.getProperty(key);
329 if (value != null)
330 return value.split(delimiter);
331 System.err.println("Error! Value for property '" + key
332 + "' was not found in property file!");
333 return new String[0];
334 }
335
336 /**
337 * removes the Property from the list
338 *
339 * @param key
340 */
341 public void removeProperty(String key) {
342 propStore.remove(key);
343 }
344
345 /**
346 * removes all keys starting with the value given. USE WITH CAUTION! If you
347 * give a String "a" all entrys starting with "a" will be removed from the
348 * registry
349 *
350 * @param key
351 */
352 public void removeAll(String key) {
353 if (key == null)
354 return;
355
356 Enumeration keys = propStore.propertyNames();
357 while (keys.hasMoreElements()) {
358 String propKey = (String) keys.nextElement();
359 if (propKey == null)
360 continue;
361 if (propKey.startsWith(key))
362 propStore.remove(propKey);
363 }
364 }
365
366 /**
367 * Gets all prefixes. A Prefix is the string of all characters up to the
368 * first dot. A Suffix is the string of all characters following this dot.
369 *
370 * @return the prefixes
371 *
372 */
373 public Set<String> getPrefixes() {
374 TreeSet set = new TreeSet();
375 Enumeration keys = propStore.propertyNames();
376 while (keys.hasMoreElements()) {
377 String propKey = (String) keys.nextElement();
378 String[] strings = propKey.split("\\.");
379 set.add(strings[0]);
380 }
381 return set;
382 }
383
384 /**
385 * Gets all suffixes for one prefix. A Prefix is the string of all
386 * characters up to the first dot. A Suffix is the string of all characters
387 * following this dot. The Strings are ordered alphabeticly
388 *
389 * @param prefix
390 * the prefix.
391 */
392 public Set<String> getSuffixesOfPrefix(String prefix) {
393 TreeSet set = new TreeSet();
394 Enumeration keys = propStore.propertyNames();
395 while (keys.hasMoreElements()) {
396 String propKey = (String) keys.nextElement();
397 String[] strings = propKey.split("\\.");
398 if (strings.length > 1 && strings[0].equals(prefix))
399 set.add(propKey.substring(strings[0].length() + 1));
400 }
401 return set;
402 }
403
404 /**
405 * Gets all entries starting with the given {@link String}
406 *
407 * @param startingString
408 * the prefixstring
409 * @return all keys starting the given prefix
410 */
411 public Set<String> getKeysStartingWith(String startingString) {
412 TreeSet set = new TreeSet();
413 Enumeration keys = propStore.propertyNames();
414 while (keys.hasMoreElements()) {
415 String propKey = (String) keys.nextElement();
416 if (propKey.startsWith(startingString))
417 set.add(propKey);
418 }
419 return set;
420 }
421
422 /**
423 * Just for testing.
424 *
425 * @deprecated
426 */
427 public static void main(String[] args) {
428 XuluConfig config = XuluConfig.getXuluConfig();
429 config.setProperty("atest2", "test3");
430 config.setProperty("test4.testproperty", "test");
431 String[] multiTest = { "value1", "value2", "value3" };
432 config.setMultiProperty("multiproperty.1", multiTest);
433 String[] multioutput = config.getMultiProperty("multiproperty.1");
434 for (String string : multioutput) {
435 System.out.println("Multivalue: " + string);
436 }
437 config.store();
438 config.setPropertyFile(new File("XuluTestfile"));
439 config.load();
440 String[] multiTest2 = { "value1", "value2", "value3" };
441 config.setMultiProperty("multiproperty.2", multiTest);
442 config.removeAll("multiproperty");
443 config.removeProperty("atest2");
444 config.store();
445 }
446 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26