KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > utils > PropertiesParser


1 /*
2  * Copyright 2004-2005 OpenSymphony
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy
6  * of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  *
16  */

17
18 /*
19  * Previously Copyright (c) 2001-2004 James House
20  */

21 package org.quartz.utils;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Properties JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28
29 /**
30  * <p>
31  * This is an utility calss used to parse the properties.
32  * </p>
33  *
34  * @author James House
35  */

36 public class PropertiesParser {
37
38     /*
39      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
40      *
41      * Data members.
42      *
43      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
44      */

45
46     Properties JavaDoc props = null;
47
48     /*
49      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
50      *
51      * Constructors.
52      *
53      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
54      */

55
56     public PropertiesParser(Properties JavaDoc props) {
57         this.props = props;
58     }
59
60     /*
61      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
62      *
63      * Interface.
64      *
65      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
66      */

67
68     public Properties JavaDoc getUnderlyingProperties() {
69         return props;
70     }
71
72     /**
73      * Get the trimmed String value of the property with the given
74      * <code>name</code>. If the value the empty String (after
75      * trimming), then it returns null.
76      */

77     public String JavaDoc getStringProperty(String JavaDoc name) {
78         return getStringProperty(name, null);
79     }
80
81     /**
82      * Get the trimmed String value of the property with the given
83      * <code>name</code> or the given default value if the value is
84      * null or empty after trimming.
85      */

86     public String JavaDoc getStringProperty(String JavaDoc name, String JavaDoc def) {
87         String JavaDoc val = props.getProperty(name, def);
88         if (val == null) {
89             return def;
90         }
91         
92         val = val.trim();
93         
94         return (val.length() == 0) ? def : val;
95     }
96
97     public String JavaDoc[] getStringArrayProperty(String JavaDoc name) {
98         return getStringArrayProperty(name, null);
99     }
100
101     public String JavaDoc[] getStringArrayProperty(String JavaDoc name, String JavaDoc[] def) {
102         String JavaDoc vals = getStringProperty(name);
103         if (vals == null) {
104             return def;
105         }
106
107         StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(vals, ",");
108         ArrayList JavaDoc strs = new ArrayList JavaDoc();
109         try {
110             while (stok.hasMoreTokens()) {
111                 strs.add(stok.nextToken().trim());
112             }
113             
114             return (String JavaDoc[])strs.toArray(new String JavaDoc[strs.size()]);
115         } catch (Exception JavaDoc e) {
116             return def;
117         }
118     }
119
120     public boolean getBooleanProperty(String JavaDoc name) {
121         return getBooleanProperty(name, false);
122     }
123
124     public boolean getBooleanProperty(String JavaDoc name, boolean def) {
125         String JavaDoc val = getStringProperty(name);
126         
127         return (val == null) ? def : Boolean.valueOf(val).booleanValue();
128     }
129
130     public byte getByteProperty(String JavaDoc name) throws NumberFormatException JavaDoc {
131         String JavaDoc val = getStringProperty(name);
132         if (val == null) {
133             throw new NumberFormatException JavaDoc(" null string");
134         }
135
136         try {
137             return Byte.parseByte(val);
138         } catch (NumberFormatException JavaDoc nfe) {
139             throw new NumberFormatException JavaDoc(" '" + val + "'");
140         }
141     }
142
143     public byte getByteProperty(String JavaDoc name, byte def)
144         throws NumberFormatException JavaDoc {
145         String JavaDoc val = getStringProperty(name);
146         if (val == null) {
147             return def;
148         }
149
150         try {
151             return Byte.parseByte(val);
152         } catch (NumberFormatException JavaDoc nfe) {
153             throw new NumberFormatException JavaDoc(" '" + val + "'");
154         }
155     }
156
157     public char getCharProperty(String JavaDoc name) {
158         return getCharProperty(name, '\0');
159     }
160
161     public char getCharProperty(String JavaDoc name, char def) {
162         String JavaDoc param = getStringProperty(name);
163         return (param == null) ? def : param.charAt(0);
164     }
165
166     public double getDoubleProperty(String JavaDoc name) throws NumberFormatException JavaDoc {
167         String JavaDoc val = getStringProperty(name);
168         if (val == null) {
169             throw new NumberFormatException JavaDoc(" null string");
170         }
171
172         try {
173             return Double.parseDouble(val);
174         } catch (NumberFormatException JavaDoc nfe) {
175             throw new NumberFormatException JavaDoc(" '" + val + "'");
176         }
177     }
178
179     public double getDoubleProperty(String JavaDoc name, double def)
180         throws NumberFormatException JavaDoc {
181         String JavaDoc val = getStringProperty(name);
182         if (val == null) {
183             return def;
184         }
185
186         try {
187             return Double.parseDouble(val);
188         } catch (NumberFormatException JavaDoc nfe) {
189             throw new NumberFormatException JavaDoc(" '" + val + "'");
190         }
191     }
192
193     public float getFloatProperty(String JavaDoc name) throws NumberFormatException JavaDoc {
194         String JavaDoc val = getStringProperty(name);
195         if (val == null) {
196             throw new NumberFormatException JavaDoc(" null string");
197         }
198
199         try {
200             return Float.parseFloat(val);
201         } catch (NumberFormatException JavaDoc nfe) {
202             throw new NumberFormatException JavaDoc(" '" + val + "'");
203         }
204     }
205
206     public float getFloatProperty(String JavaDoc name, float def)
207         throws NumberFormatException JavaDoc {
208         String JavaDoc val = getStringProperty(name);
209         if (val == null) {
210             return def;
211         }
212
213         try {
214             return Float.parseFloat(val);
215         } catch (NumberFormatException JavaDoc nfe) {
216             throw new NumberFormatException JavaDoc(" '" + val + "'");
217         }
218     }
219
220     public int getIntProperty(String JavaDoc name) throws NumberFormatException JavaDoc {
221         String JavaDoc val = getStringProperty(name);
222         if (val == null) {
223             throw new NumberFormatException JavaDoc(" null string");
224         }
225
226         try {
227             return Integer.parseInt(val);
228         } catch (NumberFormatException JavaDoc nfe) {
229             throw new NumberFormatException JavaDoc(" '" + val + "'");
230         }
231     }
232
233     public int getIntProperty(String JavaDoc name, int def)
234         throws NumberFormatException JavaDoc {
235         String JavaDoc val = getStringProperty(name);
236         if (val == null) {
237             return def;
238         }
239
240         try {
241             return Integer.parseInt(val);
242         } catch (NumberFormatException JavaDoc nfe) {
243             throw new NumberFormatException JavaDoc(" '" + val + "'");
244         }
245     }
246
247     public int[] getIntArrayProperty(String JavaDoc name) throws NumberFormatException JavaDoc {
248         return getIntArrayProperty(name, null);
249     }
250
251     public int[] getIntArrayProperty(String JavaDoc name, int[] def)
252         throws NumberFormatException JavaDoc {
253         String JavaDoc vals = getStringProperty(name);
254         if (vals == null) {
255             return def;
256         }
257
258         StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(vals, ",");
259         ArrayList JavaDoc ints = new ArrayList JavaDoc();
260         try {
261             while (stok.hasMoreTokens()) {
262                 try {
263                     ints.add(new Integer JavaDoc(stok.nextToken().trim()));
264                 } catch (NumberFormatException JavaDoc nfe) {
265                     throw new NumberFormatException JavaDoc(" '" + vals + "'");
266                 }
267             }
268                         
269             int[] outInts = new int[ints.size()];
270             for (int i = 0; i < ints.size(); i++) {
271                 outInts[i] = ((Integer JavaDoc)ints.get(i)).intValue();
272             }
273             return outInts;
274         } catch (Exception JavaDoc e) {
275             return def;
276         }
277     }
278
279     public long getLongProperty(String JavaDoc name) throws NumberFormatException JavaDoc {
280         String JavaDoc val = getStringProperty(name);
281         if (val == null) {
282             throw new NumberFormatException JavaDoc(" null string");
283         }
284
285         try {
286             return Long.parseLong(val);
287         } catch (NumberFormatException JavaDoc nfe) {
288             throw new NumberFormatException JavaDoc(" '" + val + "'");
289         }
290     }
291
292     public long getLongProperty(String JavaDoc name, long def)
293         throws NumberFormatException JavaDoc {
294         String JavaDoc val = getStringProperty(name);
295         if (val == null) {
296             return def;
297         }
298
299         try {
300             return Long.parseLong(val);
301         } catch (NumberFormatException JavaDoc nfe) {
302             throw new NumberFormatException JavaDoc(" '" + val + "'");
303         }
304     }
305
306     public short getShortProperty(String JavaDoc name) throws NumberFormatException JavaDoc {
307         String JavaDoc val = getStringProperty(name);
308         if (val == null) {
309             throw new NumberFormatException JavaDoc(" null string");
310         }
311
312         try {
313             return Short.parseShort(val);
314         } catch (NumberFormatException JavaDoc nfe) {
315             throw new NumberFormatException JavaDoc(" '" + val + "'");
316         }
317     }
318
319     public short getShortProperty(String JavaDoc name, short def)
320         throws NumberFormatException JavaDoc {
321         String JavaDoc val = getStringProperty(name);
322         if (val == null) {
323             return def;
324         }
325
326         try {
327             return Short.parseShort(val);
328         } catch (NumberFormatException JavaDoc nfe) {
329             throw new NumberFormatException JavaDoc(" '" + val + "'");
330         }
331     }
332
333     public String JavaDoc[] getPropertyGroups(String JavaDoc prefix) {
334         Enumeration JavaDoc keys = props.propertyNames();
335         HashSet JavaDoc groups = new HashSet JavaDoc(10);
336
337         if (!prefix.endsWith(".")) {
338             prefix += ".";
339         }
340
341         while (keys.hasMoreElements()) {
342             String JavaDoc key = (String JavaDoc) keys.nextElement();
343             if (key.startsWith(prefix)) {
344                 String JavaDoc groupName = key.substring(prefix.length(), key.indexOf(
345                         '.', prefix.length()));
346                 groups.add(groupName);
347             }
348         }
349
350         return (String JavaDoc[]) groups.toArray(new String JavaDoc[groups.size()]);
351     }
352
353     public Properties JavaDoc getPropertyGroup(String JavaDoc prefix) {
354         return getPropertyGroup(prefix, false, null);
355     }
356
357     public Properties JavaDoc getPropertyGroup(String JavaDoc prefix, boolean stripPrefix) {
358         return getPropertyGroup(prefix, stripPrefix, null);
359     }
360
361     /**
362      * Get all properties that start with the given prefix.
363      *
364      * @param prefix The prefix for which to search. If it does not end in
365      * a "." then one will be added to it for search purposes.
366      * @param stripPrefix Whether to strip off the given <code>prefix</code>
367      * in the result's keys.
368      * @param excludedPrefixes Optional array of fully qualified prefixes to
369      * exclude. For example if <code>prefix</code> is "a.b.c", then
370      * <code>excludedPrefixes</code> might be "a.b.c.ignore".
371      *
372      * @return Group of <code>Properties</code> that start with the given prefix,
373      * optionally have that prefix removed, and do not include properties
374      * that start with one of the given excluded prefixes.
375      */

376     public Properties JavaDoc getPropertyGroup(String JavaDoc prefix, boolean stripPrefix, String JavaDoc[] excludedPrefixes) {
377         Enumeration JavaDoc keys = props.propertyNames();
378         Properties JavaDoc group = new Properties JavaDoc();
379
380         if (!prefix.endsWith(".")) {
381             prefix += ".";
382         }
383
384         while (keys.hasMoreElements()) {
385             String JavaDoc key = (String JavaDoc) keys.nextElement();
386             if (key.startsWith(prefix)) {
387                 
388                 boolean exclude = false;
389                 if (excludedPrefixes != null) {
390                     for (int i = 0; (i < excludedPrefixes.length) && (exclude == false); i++) {
391                         exclude = key.startsWith(excludedPrefixes[i]);
392                     }
393                 }
394
395                 if (exclude == false) {
396                     String JavaDoc value = getStringProperty(key, "");
397                     
398                     if (stripPrefix) {
399                         group.put(key.substring(prefix.length()), value);
400                     } else {
401                         group.put(key, value);
402                     }
403                 }
404             }
405         }
406
407         return group;
408     }
409 }
410
Popular Tags