001: /*
002: * Created on 12.07.2005 for PIROL
003: *
004: * SVN header information:
005: * $Author: javamap $
006: * $Rev: 856 $
007: * $Date: 2007-06-18 21:15:27 -0700 (Mon, 18 Jun 2007) $
008: * $Id: PropertiesHandler.java 856 2007-06-19 04:15:27Z javamap $
009: */
010: package de.fho.jump.pirol.utilities.Properties;
011:
012: import java.awt.Color;
013: import java.io.File;
014: import java.io.FileInputStream;
015: import java.io.FileOutputStream;
016: import java.io.IOException;
017: import java.util.Enumeration;
018: import java.util.HashMap;
019: import java.util.Map;
020: import java.util.NoSuchElementException;
021: import java.util.Properties;
022: import java.util.Set;
023:
024: import de.fho.jump.pirol.utilities.apiTools.HandlerToMakeYourLifeEasier;
025: import de.fho.jump.pirol.utilities.settings.PirolPlugInSettings;
026:
027: /**
028: * Class that enables easy access for reading and writing properties files.
029: *
030: * @author Ole Rahn
031: * <br>
032: * <br>FH Osnabrück - University of Applied Sciences Osnabrück,
033: * <br>Project: PIROL (2005),
034: * <br>Subproject: Daten- und Wissensmanagement
035: *
036: * @version $Rev: 856 $
037: *
038: */
039: public class PropertiesHandler
040: implements
041: de.fho.jump.pirol.utilities.apiTools.HandlerToMakeYourLifeEasier {
042:
043: protected String propertiesFile = null;
044: protected Properties properties = null;
045:
046: public final static String propertiesFileEnding = ".properties";
047:
048: /**
049: * Constructor
050: *@param propertiesFileName the file name (with out path!) of the properties file, that is to be read or written. It will automatically be placed in the config directory.
051: *@see PirolPlugInSettings#configDirectory()
052: */
053: public PropertiesHandler(String propertiesFileName) {
054: this .propertiesFile = de.fho.jump.pirol.utilities.settings.PirolPlugInSettings
055: .configDirectory().getAbsolutePath()
056: + File.separator + propertiesFileName;
057: this .properties = new Properties();
058: }
059:
060: /**
061: *@see Properties
062: */
063: public boolean contains(Object value) {
064: return properties.contains(value);
065: }
066:
067: /**
068: *@see Properties
069: */
070: public boolean containsKey(Object key) {
071: return properties.containsKey(key);
072: }
073:
074: /**
075: *@see Properties
076: */
077: public boolean containsValue(Object value) {
078: return properties.containsValue(value);
079: }
080:
081: /**
082: *@see Properties
083: */
084: public Enumeration elements() {
085: return properties.elements();
086: }
087:
088: /**
089: *@see Properties
090: */
091: public String getProperty(String key, String defaultValue) {
092: return properties.getProperty(key, defaultValue);
093: }
094:
095: /**
096: *@see Properties
097: */
098: public String getProperty(String key) {
099: return properties.getProperty(key);
100: }
101:
102: /**
103: *@see Properties
104: */
105: public boolean isEmpty() {
106: return properties.isEmpty();
107: }
108:
109: /**
110: *@see Properties
111: */
112: public Enumeration keys() {
113: return properties.keys();
114: }
115:
116: /**
117: *@see Properties
118: */
119: public Set keySet() {
120: return properties.keySet();
121: }
122:
123: /**
124: * load the properties from the file
125: *@throws IOException
126: */
127: public void load() throws IOException {
128: if (!this .properties.isEmpty()) {
129: this .properties.clear();
130: }
131: FileInputStream fis = new FileInputStream(this .propertiesFile);
132: this .properties.load(fis);
133: fis.close();
134: }
135:
136: /**
137: *@see Properties
138: */
139: public void putAll(Map<String, Object> arg0) {
140: properties.putAll(arg0);
141: }
142:
143: /**
144: * get all properties as Map object (e.g. to feed into an other map...)
145: * @return
146: */
147: public Map<String, Object> getAll() {
148: HashMap<String, Object> props = new HashMap<String, Object>();
149: Map.Entry[] entries = (Map.Entry[]) this .properties.entrySet()
150: .toArray(new Map.Entry[0]);
151: for (int i = 0; i < entries.length; i++) {
152: props.put(entries[i].getKey().toString(), entries[i]
153: .getValue());
154: }
155:
156: return props;
157: }
158:
159: /**
160: *@see Properties
161: */
162: public Object remove(Object arg0) {
163: return properties.remove(arg0);
164: }
165:
166: /**
167: * Sets a property key-value pair, replaces a pair with the same key!
168: *@param key the key for the pair
169: *@param value the value
170: *@return return value like Properties would return
171: *@see Properties
172: */
173: public Object setProperty(String key, String value) {
174: if (this .properties.containsKey(key))
175: this .properties.remove(key);
176: return properties.setProperty(key, value);
177: }
178:
179: /**
180: * Sets a property key-value pair, replaces a pair with the same key!
181: *@param key the key for the pair
182: *@param value the value
183: *@return return value like Properties would return
184: *@see Properties
185: */
186: public Object setProperty(String key, Color value) {
187: if (this .properties.containsKey(key))
188: this .properties.remove(key);
189: return properties.setProperty(key, Integer.toString(value
190: .getRGB()));
191: }
192:
193: /**
194: * Stores the current properties map to the file.
195: *@param comments comments that will appear in the first lines of the file
196: *@throws IOException
197: */
198: public void store(String comments) throws IOException {
199: File propFile = new File(this .propertiesFile);
200:
201: String pathString = this .propertiesFile.indexOf(File.separator) > -1 ? this .propertiesFile
202: .substring(0, this .propertiesFile
203: .lastIndexOf(File.separator))
204: : "";
205:
206: File propFilePath = new File(pathString);
207: if (!propFile.exists()) {
208: try {
209: propFilePath.mkdirs();
210: } catch (RuntimeException e) {
211: }
212: }
213:
214: // TODO: find a way store the properties in alphabetical order (keys)
215:
216: FileOutputStream fos = new FileOutputStream(propFile);
217: this .properties.store(fos, comments);
218: fos.close();
219: }
220:
221: /**
222: * Stores the current properties map to the file.
223: *@throws IOException
224: */
225: public void store() throws IOException {
226: this .store(null);
227: }
228:
229: /**
230: * Gets the property value with the key <code>key</code> and parses it
231: * to an <code>int</code> if possible. An exception will be thrown,
232: * if this key is not found within the properties and if the value could not
233: * be parsed as desired.
234: *@param key the key to get the value of
235: *@return the value of the property
236: */
237: public int getPropertyAsInt(String key) {
238: if (this .properties.containsKey(key)) {
239: return Integer
240: .parseInt(this .properties.get(key).toString());
241: }
242: throw new NoSuchElementException("key: \"" + key + "\"");
243: }
244:
245: /**
246: * Gets the property value with the key <code>key</code> and parses it
247: * to an <code>int</code> if possible.
248: * If this key is not found within the properties the given default-Value will be returned.
249: * An exception will be thrown, if the value is existent, but could not be parsed as desired.
250: *@param key the key to get the value of
251: *@param defaultValue value to be filled in, if the given key wasn't found
252: *@return the value of the property
253: */
254: public int getPropertyAsInt(String key, int defaultValue) {
255: if (this .properties.containsKey(key)) {
256: return this .getPropertyAsInt(key);
257: }
258: this .properties.setProperty(key, new Integer(defaultValue)
259: .toString());
260: return defaultValue;
261: }
262:
263: /**
264: * Gets the property value with the key <code>key</code> and parses it
265: * to an <code>boolean</code> if possible. An exception will be thrown,
266: * if this key is not found within the properties and if the value could not
267: * be parsed as desired.
268: *@param key the key to get the value of
269: *@return the value of the property
270: */
271: public boolean getPropertyAsBoolean(String key) {
272: if (this .properties.containsKey(key)) {
273: return Boolean.valueOf(this .properties.get(key).toString())
274: .booleanValue();
275: }
276: throw new NoSuchElementException("key: \"" + key + "\"");
277: }
278:
279: /**
280: * Gets the property value with the key <code>key</code> and parses it
281: * to an <code>boolean</code> if possible.
282: * If this key is not found within the properties the given default-Value will be returned.
283: * An exception will be thrown, if the value is existent, but could not be parsed as desired.
284: *@param key the key to get the value of
285: *@param defaultValue value to be filled in, if the given key wasn't found
286: *@return the value of the property
287: */
288: public boolean getPropertyAsBoolean(String key, boolean defaultValue) {
289: if (this .properties.containsKey(key)) {
290: return this .getPropertyAsBoolean(key);
291: }
292: this .properties.setProperty(key, new Boolean(defaultValue)
293: .toString());
294: return defaultValue;
295: }
296:
297: /**
298: * Gets the property value with the key <code>key</code> and parses it
299: * to a <code>double</code> if possible. An exception will be thrown,
300: * if this key is not found within the properties and if the value could not
301: * be parsed as desired.
302: *@param key the key to get the value of
303: *@return the value of the property
304: */
305: public double getPropertyAsDouble(String key) {
306: if (this .properties.containsKey(key)) {
307: return Double.parseDouble(this .properties.get(key)
308: .toString());
309: }
310: throw new NoSuchElementException("key: \"" + key + "\"");
311: }
312:
313: /**
314: * Gets the property value with the key <code>key</code> and parses it
315: * to a <code>double</code> if possible.
316: * If this key is not found within the properties the given default-Value will be returned.
317: * An exception will be thrown, if the value is existent, but could not be parsed as desired.
318: *@param key the key to get the value of
319: *@param defaultValue value to be filled in, if the given key wasn't found
320: *@return the value of the property
321: */
322: public double getPropertyAsDouble(String key, double defaultValue) {
323: if (this .properties.containsKey(key)) {
324: return this .getPropertyAsDouble(key);
325: }
326: this .properties.setProperty(key, new Double(defaultValue)
327: .toString());
328: return defaultValue;
329: }
330:
331: /**
332: * Gets the property value with the key <code>key</code> and parses it
333: * to a <code>Color</code> if possible. An exception will be thrown,
334: * if this key is not found within the properties and if the value could not
335: * be parsed as desired.
336: *@param key the key to get the value of
337: *@return the value of the property
338: */
339: public Color getPropertyAsColor(String key) {
340: if (this .properties.containsKey(key)) {
341: return Color.decode(this .properties.get(key).toString());
342: }
343: throw new NoSuchElementException("key: \"" + key + "\"");
344: }
345:
346: /**
347: * Gets the property value with the key <code>key</code> and parses it
348: * to a <code>Color</code> if possible.
349: * If this key is not found within the properties the given default-Value will be returned.
350: * An exception will be thrown, if the value is existent, but could not be parsed as desired.
351: *@param key the key to get the value of
352: *@param defaultValue value to be filled in, if the given key wasn't found
353: *@return the value of the property
354: */
355: public Color getPropertyAsColor(String key, Color defaultValue) {
356: if (this .properties.containsKey(key)) {
357: return this .getPropertyAsColor(key);
358: }
359: this .setProperty(key, defaultValue);
360: return defaultValue;
361: }
362:
363: /**
364: *
365: *@return the file name of the properties file handled by this instance
366: */
367: public String getPropertiesFile() {
368: return propertiesFile;
369: }
370:
371: }
|