Source code: org/jempeg/empeg/emplode/util/PropertiesManager.java
1 /**
2 * Copyright (c) 2001, Mike Schrag & Daniel Zimmerman
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * Neither the name of Mike Schrag, Daniel Zimmerman, nor the names of any
16 * other contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31 package org.jempeg.empeg.emplode.util;
32
33 import java.io.File;
34 import java.io.FileInputStream;
35 import java.io.FileOutputStream;
36 import java.io.IOException;
37 import java.util.Properties;
38 import java.util.StringTokenizer;
39 import java.util.Vector;
40
41 /**
42 * PropertiesManager provides a series of
43 * type-safe interfaces to a Properties
44 * file (that is a multiple line "key=value"
45 * format).
46 *
47 * The primary use in Emplode is that this
48 * interface is used to save runtime
49 * configuration properties for future
50 * sessions.
51 *
52 * @author Mike Schrag
53 * @version $Revision: 1.7 $
54 */
55 public class PropertiesManager {
56 private Properties myProperties;
57 private File myPropertiesFile;
58
59 public PropertiesManager(File _propertiesFile) {
60 myProperties = new Properties();
61 myPropertiesFile = _propertiesFile;
62 }
63
64 public Properties getProperties() {
65 return myProperties;
66 }
67
68 public String getProperty(String _property, String _default) {
69 return myProperties.getProperty(_property, _default);
70 }
71
72 public void setProperty(String _name, String _value) {
73 if (_value == null) {
74 removeProperty(_name);
75 } else {
76 myProperties.put(_name, _value);
77 }
78 }
79
80 public void removeProperty(String _name) {
81 myProperties.remove(_name);
82 }
83
84 public void setIntProperty(String _name, int _value) {
85 myProperties.put(_name, String.valueOf(_value));
86 }
87
88 public int getIntProperty(String _intProperty, int _default) {
89 String strValue = getProperty(_intProperty, null);
90 int intValue;
91 try {
92 intValue = Integer.parseInt(strValue);
93 }
94 catch (NumberFormatException e) {
95 intValue = _default;
96 }
97 return intValue;
98 }
99
100 public int[] getIntArrayProperty(String _intArrayProperty, int[] _default) {
101 int[] values;
102 String prop = getProperty(_intArrayProperty, null);
103 if (prop == null) {
104 values = _default;
105 } else {
106 Vector valuesVec = new Vector();
107 StringTokenizer tokenizer = new StringTokenizer(prop, ",");
108 while (tokenizer.hasMoreElements()) {
109 String valueStr = tokenizer.nextToken().trim();
110 valuesVec.addElement(valueStr);
111 }
112 values = new int[valuesVec.size()];
113 for (int i = 0; i < values.length; i ++) {
114 String value = (String)valuesVec.elementAt(i);
115 values[i] = Integer.parseInt(value);
116 }
117 }
118 return values;
119 }
120
121 public void setIntArrayProperty(String _intArrayProperty, int[] _values) {
122 StringBuffer valuesStr = new StringBuffer();
123 for (int i = 0; i < _values.length; i ++) {
124 valuesStr.append(_values[i]);
125 valuesStr.append(',');
126 }
127 if (valuesStr.length() > 0) {
128 valuesStr.setLength(valuesStr.length() - 1);
129 }
130 myProperties.put(_intArrayProperty, valuesStr.toString());
131 }
132
133 public boolean getBooleanProperty(String _booleanProperty, boolean _default) {
134 String prop = getProperty(_booleanProperty, String.valueOf(_default));
135 boolean propValue;
136 propValue = (prop.equals("true"));
137 return propValue;
138 }
139
140 public void setBooleanProperty(String _booleanProperty, boolean _value) {
141 setProperty(_booleanProperty, String.valueOf(_value));
142 }
143
144 public void save() throws IOException {
145 FileOutputStream fos = new FileOutputStream(myPropertiesFile);
146 myProperties.save(fos, "JEmpeg Properties");
147 fos.close();
148 }
149
150 public void load() throws IOException {
151 if (myPropertiesFile.exists()) {
152 FileInputStream fis = new FileInputStream(myPropertiesFile);
153 myProperties.load(fis);
154 fis.close();
155 }
156 }
157 }