KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcckit > util > ConfigParameters


1 /*
2  * Copyright 2003-2004, Franz-Josef Elmer, All rights reserved
3  *
4  * This library is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; either version 2.1 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details
13  * (http://www.gnu.org/copyleft/lesser.html).
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package jcckit.util;
20
21 import java.awt.Color JavaDoc;
22 import java.util.StringTokenizer JavaDoc;
23
24 /**
25  * Read-only class for hierarchically organized key-value pairs.
26  * The key is always a string. The following value types are
27  * supported:
28  * <ul><li><tt>String</tt>
29  * <li><tt>boolean</tt>
30  * <li><tt>int</tt>
31  * <li><tt>double</tt>
32  * <li><tt>double[]</tt>
33  * <li><tt>Color</tt>
34  * <li><tt>ConfigParameters</tt>
35  * </ul>
36  * <p>
37  * In accordance with the Strategy design pattern the retrieval of
38  * a key-value pair is delegated to an instance of
39  * {@link ConfigData}.
40  *
41  * @author Franz-Josef Elmer
42  */

43 public class ConfigParameters {
44   private final ConfigData _configData;
45
46   /** Creates an instance from the specified <tt>ConfigData</tt> object. */
47   public ConfigParameters(ConfigData configData) {
48     _configData = configData;
49   }
50
51   /**
52    * Returns the full key.
53    * @return the path concatenated with <tt>key</tt>.
54    * @see ConfigData#getFullKey
55    */

56   public String JavaDoc getFullKey(String JavaDoc key) {
57     return _configData.getFullKey(key);
58   }
59
60   /**
61    * Returns the string value associated with the specified key.
62    * @param key The (relative) key. <tt>null</tt> is not allowed.
63    * @return the corresponding value. Will always be not <tt>null</tt>.
64    * @throws IllegalArgumentException if no value exists for <tt>key</tt>.
65    * The exception message is the full key.
66    */

67   public String JavaDoc get(String JavaDoc key) {
68     String JavaDoc result = _configData.get(key);
69     if (result == null) {
70       throw new IllegalArgumentException JavaDoc(getFullKey(key));
71     }
72     return result;
73   }
74
75   /**
76    * Returns the string value associated with the specified key or
77    * <tt>defaultValue</tt> if undefined.
78    * @param key The (relative) key. <tt>null</tt> is not allowed.
79    * @param defaultValue The default value. Can be <tt>null</tt>.
80    * @return the corresponding value or <tt>defaultValue</tt>.
81    */

82   public String JavaDoc get(String JavaDoc key, String JavaDoc defaultValue) {
83     String JavaDoc result = _configData.get(key);
84     if (result == null) {
85       result = defaultValue;
86     }
87     return result;
88   }
89
90   /**
91    * Returns the boolean associated with the specified key.
92    * @param key The (relative) key. <tt>null</tt> is not allowed.
93    * @return <tt>true</tt> if the value is "true" otherwise <tt>false</tt>.
94    * @throws IllegalArgumentException if no value exists for <tt>key</tt>.
95    * The exception message is the full key.
96    * @throws NumberFormatException if the value is neither "true" nor "false".
97    */

98   public boolean getBoolean(String JavaDoc key) {
99     return parseBoolean(get(key), key);
100   }
101   
102   /**
103    * Returns the boolean associated with the specified key.
104    * @param key The (relative) key. <tt>null</tt> is not allowed.
105    * @param defaultValue The default value. Can be <tt>null</tt>.
106    * @return <tt>true</tt> if the value is "true" otherwise <tt>false</tt>.
107    * @throws NumberFormatException if the value is neither "true" nor "false".
108    */

109   public boolean getBoolean(String JavaDoc key, boolean defaultValue) {
110     String JavaDoc value = _configData.get(key);
111     return value == null ? defaultValue : parseBoolean(value, key);
112   }
113   
114   private boolean parseBoolean(String JavaDoc value, String JavaDoc key) {
115     if (value.equals("true")) {
116       return true;
117     } else if (value.equals("false")) {
118       return false;
119     } else {
120       throw createNumberFormatException("boolean", value, key);
121     }
122   }
123   
124   private NumberFormatException JavaDoc createNumberFormatException(String JavaDoc text,
125                                                             String JavaDoc value,
126                                                             String JavaDoc key) {
127     return new NumberFormatException JavaDoc("Not a " + text + ": " + getFullKey(key)
128                                      + " = " + value);
129   }
130
131   /**
132    * Returns the integer associated with the specified key.
133    * The value can be either
134    * <ul><li>a decimal number (starting with a non-zero digit),
135    * <li>a hexadecimal number (starting with <tt>0x</tt>), or
136    * <li>an octal number (starting with zero).
137    * </ul>
138    * @param key The (relative) key. <tt>null</tt> is not allowed.
139    * @return the integer value.
140    * @throws IllegalArgumentException if no value exists for <tt>key</tt>.
141    * The exception message is the full key.
142    * @throws NumberFormatException if the value is not a number.
143    * The exception message contains the full key and the invalid value.
144    */

145   public int getInt(String JavaDoc key) {
146     return parseInt(get(key), key);
147   }
148
149   /**
150    * Returns the integer associated with the specified key or
151    * <tt>defaultValue</tt> if no key-value pair exists for the specified key.
152    * The value can be either
153    * <ul><li>a decimal number (starting with a non-zero digit),
154    * <li>a hexadecimal number (starting with <tt>0x</tt>), or
155    * <li>an octal number (starting with zero).
156    * </ul>
157    * @param key The (relative) key. <tt>null</tt> is not allowed.
158    * @param defaultValue The default value. Can be <tt>null</tt>.
159    * @return the integer value.
160    * @throws NumberFormatException if the value exists but is not a number.
161    * The exception message contains the full key and the invalid value.
162    */

163   public int getInt(String JavaDoc key, int defaultValue) {
164     String JavaDoc value = _configData.get(key);
165     return value == null ? defaultValue : parseInt(value, key);
166   }
167
168   private int parseInt(String JavaDoc value, String JavaDoc key) {
169     try {
170       return Integer.decode(value).intValue();
171     } catch (NumberFormatException JavaDoc e) {
172       throw createNumberFormatException("number", value, key);
173     }
174   }
175
176   /**
177    * Returns the double associated with the specified key.
178    * @param key The (relative) key. <tt>null</tt> is not allowed.
179    * @return the double value.
180    * @throws IllegalArgumentException if no value exists for <tt>key</tt>.
181    * The exception message is the full key.
182    * @throws NumberFormatException if the value is not a valid number.
183    * The exception message contains the full key and the invalid value.
184    */

185   public double getDouble(String JavaDoc key) {
186     return parseDouble(get(key), key);
187   }
188
189   /**
190    * Returns the double associated with the specified key or
191    * <tt>defaultValue</tt> if no key-value pair exists for the specified key.
192    * @param key The (relative) key. <tt>null</tt> is not allowed.
193    * @param defaultValue The default value. Can be <tt>null</tt>.
194    * @return the double value.
195    * @throws NumberFormatException if the value exists but is not a valid
196    * number.
197    * The exception message contains the full key and the invalid value.
198    */

199   public double getDouble(String JavaDoc key, double defaultValue) {
200     String JavaDoc value = _configData.get(key);
201     return value == null ? defaultValue : parseDouble(value, key);
202   }
203
204   private double parseDouble(String JavaDoc value, String JavaDoc key) {
205     try {
206       return new Double JavaDoc(value).doubleValue();
207     } catch (NumberFormatException JavaDoc e) {
208       throw createNumberFormatException("number", value, key);
209     }
210   }
211
212   /**
213    * Returns the array of doubles associated with the specified key.
214    * The numbers are separated by whitespaces.
215    * @param key The (relative) key. <tt>null</tt> is not allowed.
216    * @return the array of double values.
217    * @throws IllegalArgumentException if no value exists for <tt>key</tt>.
218    * The exception message is the full key.
219    * @throws NumberFormatException if the value exists but is not a
220    * sequence of number. The exception message contains
221    * the full key and the invalid value.
222    */

223   public double[] getDoubleArray(String JavaDoc key) {
224     return parseDoubleArray(get(key), key);
225   }
226
227   /**
228    * Returns the array of doubles associated with the specified key
229    * or <tt>defaultValue</tt> if no key-value pair exists for
230    * the specified key. The numbers are separated by whitespaces.
231    * @param key The (relative) key. <tt>null</tt> is not allowed.
232    * @param defaultValue The default value. Can be <tt>null</tt>.
233    * @return the array of double values.
234    * @throws NumberFormatException if the value exists but is not a
235    * sequence of number. The exception message contains
236    * the full key and the invalid value.
237    */

238   public double[] getDoubleArray(String JavaDoc key, double[] defaultValue) {
239     String JavaDoc value = _configData.get(key);
240     return value == null ? defaultValue : parseDoubleArray(value, key);
241   }
242
243   private double[] parseDoubleArray(String JavaDoc value, String JavaDoc key) {
244     try {
245       StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(value);
246       double[] result = new double[tokenizer.countTokens()];
247       for (int i = 0; i < result.length; i++) {
248         result[i] = new Double JavaDoc(tokenizer.nextToken()).doubleValue();
249       }
250       return result;
251     } catch (NumberFormatException JavaDoc e) {
252       throw createNumberFormatException("sequence of numbers", value, key);
253     }
254   }
255
256   /**
257    * Returns the color associated with the specified key.
258    * The color is coded as
259    * <ul><li>a decimal number (starting with a non-zero digit),
260    * <li>a hexadecimal number (starting with <tt>0x</tt>), or
261    * <li>an octal number (starting with zero).
262    * </ul>
263    * @param key The (relative) key. <tt>null</tt> is not allowed.
264    * @return the color.
265    * @throws NumberFormatException if the value exists but is not a number.
266    * The exception message contains the full key and the invalid value.
267    */

268   public Color JavaDoc getColor(String JavaDoc key) {
269     return parseColor(get(key), key);
270   }
271
272   /**
273    * Returns the color associated with the specified key or the specified
274    * default value if no key-value pair exists for the specified key.
275    * The color is coded as
276    * <ul><li>a decimal number (starting with a non-zero digit),
277    * <li>a hexadecimal number (starting with <tt>0x</tt>), or
278    * <li>an octal number (starting with zero).
279    * </ul>
280    * @param key The (relative) key. <tt>null</tt> is not allowed.
281    * @param defaultValue The default value. Can be <tt>null</tt>.
282    * @return the color or <tt>null</tt> if the value is an empty string.
283    * @throws NumberFormatException if the value exists but is not a number.
284    * The exception message contains the full key and the invalid value.
285    */

286   public Color JavaDoc getColor(String JavaDoc key, Color JavaDoc defaultValue) {
287     String JavaDoc value = _configData.get(key);
288     return value == null ? defaultValue : parseColor(value, key);
289   }
290
291   private Color JavaDoc parseColor(String JavaDoc value, String JavaDoc key) {
292     try {
293       return value.length() == 0 ? null : Color.decode(value);
294     } catch (NumberFormatException JavaDoc e) {
295       throw createNumberFormatException("number", value, key);
296     }
297   }
298   
299   /**
300    * Returns the child node associated with the specified key.
301    * This method returns in any case a non-<tt>null</tt> result.
302    * @param key The (relative) key. <tt>null</tt> is not allowed.
303    * @return the corresponding child node which may be empty.
304    */

305   public ConfigParameters getNode(String JavaDoc key) {
306     return new ConfigParameters(_configData.getNode(key));
307   }
308 }
309
Popular Tags