001: /*
002: * Copyright 2004-2005 OpenSymphony
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy
006: * of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations
014: * under the License.
015: *
016: */
017:
018: /*
019: * Previously Copyright (c) 2001-2004 James House
020: */
021: package org.quartz.utils;
022:
023: import java.util.ArrayList;
024: import java.util.Enumeration;
025: import java.util.HashSet;
026: import java.util.Properties;
027: import java.util.StringTokenizer;
028:
029: /**
030: * <p>
031: * This is an utility calss used to parse the properties.
032: * </p>
033: *
034: * @author James House
035: */
036: public class PropertiesParser {
037:
038: /*
039: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
040: *
041: * Data members.
042: *
043: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
044: */
045:
046: Properties props = null;
047:
048: /*
049: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
050: *
051: * Constructors.
052: *
053: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
054: */
055:
056: public PropertiesParser(Properties props) {
057: this .props = props;
058: }
059:
060: /*
061: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
062: *
063: * Interface.
064: *
065: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
066: */
067:
068: public Properties getUnderlyingProperties() {
069: return props;
070: }
071:
072: /**
073: * Get the trimmed String value of the property with the given
074: * <code>name</code>. If the value the empty String (after
075: * trimming), then it returns null.
076: */
077: public String getStringProperty(String name) {
078: return getStringProperty(name, null);
079: }
080:
081: /**
082: * Get the trimmed String value of the property with the given
083: * <code>name</code> or the given default value if the value is
084: * null or empty after trimming.
085: */
086: public String getStringProperty(String name, String def) {
087: String val = props.getProperty(name, def);
088: if (val == null) {
089: return def;
090: }
091:
092: val = val.trim();
093:
094: return (val.length() == 0) ? def : val;
095: }
096:
097: public String[] getStringArrayProperty(String name) {
098: return getStringArrayProperty(name, null);
099: }
100:
101: public String[] getStringArrayProperty(String name, String[] def) {
102: String vals = getStringProperty(name);
103: if (vals == null) {
104: return def;
105: }
106:
107: StringTokenizer stok = new StringTokenizer(vals, ",");
108: ArrayList strs = new ArrayList();
109: try {
110: while (stok.hasMoreTokens()) {
111: strs.add(stok.nextToken().trim());
112: }
113:
114: return (String[]) strs.toArray(new String[strs.size()]);
115: } catch (Exception e) {
116: return def;
117: }
118: }
119:
120: public boolean getBooleanProperty(String name) {
121: return getBooleanProperty(name, false);
122: }
123:
124: public boolean getBooleanProperty(String name, boolean def) {
125: String val = getStringProperty(name);
126:
127: return (val == null) ? def : Boolean.valueOf(val)
128: .booleanValue();
129: }
130:
131: public byte getByteProperty(String name)
132: throws NumberFormatException {
133: String val = getStringProperty(name);
134: if (val == null) {
135: throw new NumberFormatException(" null string");
136: }
137:
138: try {
139: return Byte.parseByte(val);
140: } catch (NumberFormatException nfe) {
141: throw new NumberFormatException(" '" + val + "'");
142: }
143: }
144:
145: public byte getByteProperty(String name, byte def)
146: throws NumberFormatException {
147: String val = getStringProperty(name);
148: if (val == null) {
149: return def;
150: }
151:
152: try {
153: return Byte.parseByte(val);
154: } catch (NumberFormatException nfe) {
155: throw new NumberFormatException(" '" + val + "'");
156: }
157: }
158:
159: public char getCharProperty(String name) {
160: return getCharProperty(name, '\0');
161: }
162:
163: public char getCharProperty(String name, char def) {
164: String param = getStringProperty(name);
165: return (param == null) ? def : param.charAt(0);
166: }
167:
168: public double getDoubleProperty(String name)
169: throws NumberFormatException {
170: String val = getStringProperty(name);
171: if (val == null) {
172: throw new NumberFormatException(" null string");
173: }
174:
175: try {
176: return Double.parseDouble(val);
177: } catch (NumberFormatException nfe) {
178: throw new NumberFormatException(" '" + val + "'");
179: }
180: }
181:
182: public double getDoubleProperty(String name, double def)
183: throws NumberFormatException {
184: String val = getStringProperty(name);
185: if (val == null) {
186: return def;
187: }
188:
189: try {
190: return Double.parseDouble(val);
191: } catch (NumberFormatException nfe) {
192: throw new NumberFormatException(" '" + val + "'");
193: }
194: }
195:
196: public float getFloatProperty(String name)
197: throws NumberFormatException {
198: String val = getStringProperty(name);
199: if (val == null) {
200: throw new NumberFormatException(" null string");
201: }
202:
203: try {
204: return Float.parseFloat(val);
205: } catch (NumberFormatException nfe) {
206: throw new NumberFormatException(" '" + val + "'");
207: }
208: }
209:
210: public float getFloatProperty(String name, float def)
211: throws NumberFormatException {
212: String val = getStringProperty(name);
213: if (val == null) {
214: return def;
215: }
216:
217: try {
218: return Float.parseFloat(val);
219: } catch (NumberFormatException nfe) {
220: throw new NumberFormatException(" '" + val + "'");
221: }
222: }
223:
224: public int getIntProperty(String name) throws NumberFormatException {
225: String val = getStringProperty(name);
226: if (val == null) {
227: throw new NumberFormatException(" null string");
228: }
229:
230: try {
231: return Integer.parseInt(val);
232: } catch (NumberFormatException nfe) {
233: throw new NumberFormatException(" '" + val + "'");
234: }
235: }
236:
237: public int getIntProperty(String name, int def)
238: throws NumberFormatException {
239: String val = getStringProperty(name);
240: if (val == null) {
241: return def;
242: }
243:
244: try {
245: return Integer.parseInt(val);
246: } catch (NumberFormatException nfe) {
247: throw new NumberFormatException(" '" + val + "'");
248: }
249: }
250:
251: public int[] getIntArrayProperty(String name)
252: throws NumberFormatException {
253: return getIntArrayProperty(name, null);
254: }
255:
256: public int[] getIntArrayProperty(String name, int[] def)
257: throws NumberFormatException {
258: String vals = getStringProperty(name);
259: if (vals == null) {
260: return def;
261: }
262:
263: StringTokenizer stok = new StringTokenizer(vals, ",");
264: ArrayList ints = new ArrayList();
265: try {
266: while (stok.hasMoreTokens()) {
267: try {
268: ints.add(new Integer(stok.nextToken().trim()));
269: } catch (NumberFormatException nfe) {
270: throw new NumberFormatException(" '" + vals + "'");
271: }
272: }
273:
274: int[] outInts = new int[ints.size()];
275: for (int i = 0; i < ints.size(); i++) {
276: outInts[i] = ((Integer) ints.get(i)).intValue();
277: }
278: return outInts;
279: } catch (Exception e) {
280: return def;
281: }
282: }
283:
284: public long getLongProperty(String name)
285: throws NumberFormatException {
286: String val = getStringProperty(name);
287: if (val == null) {
288: throw new NumberFormatException(" null string");
289: }
290:
291: try {
292: return Long.parseLong(val);
293: } catch (NumberFormatException nfe) {
294: throw new NumberFormatException(" '" + val + "'");
295: }
296: }
297:
298: public long getLongProperty(String name, long def)
299: throws NumberFormatException {
300: String val = getStringProperty(name);
301: if (val == null) {
302: return def;
303: }
304:
305: try {
306: return Long.parseLong(val);
307: } catch (NumberFormatException nfe) {
308: throw new NumberFormatException(" '" + val + "'");
309: }
310: }
311:
312: public short getShortProperty(String name)
313: throws NumberFormatException {
314: String val = getStringProperty(name);
315: if (val == null) {
316: throw new NumberFormatException(" null string");
317: }
318:
319: try {
320: return Short.parseShort(val);
321: } catch (NumberFormatException nfe) {
322: throw new NumberFormatException(" '" + val + "'");
323: }
324: }
325:
326: public short getShortProperty(String name, short def)
327: throws NumberFormatException {
328: String val = getStringProperty(name);
329: if (val == null) {
330: return def;
331: }
332:
333: try {
334: return Short.parseShort(val);
335: } catch (NumberFormatException nfe) {
336: throw new NumberFormatException(" '" + val + "'");
337: }
338: }
339:
340: public String[] getPropertyGroups(String prefix) {
341: Enumeration keys = props.propertyNames();
342: HashSet groups = new HashSet(10);
343:
344: if (!prefix.endsWith(".")) {
345: prefix += ".";
346: }
347:
348: while (keys.hasMoreElements()) {
349: String key = (String) keys.nextElement();
350: if (key.startsWith(prefix)) {
351: String groupName = key.substring(prefix.length(), key
352: .indexOf('.', prefix.length()));
353: groups.add(groupName);
354: }
355: }
356:
357: return (String[]) groups.toArray(new String[groups.size()]);
358: }
359:
360: public Properties getPropertyGroup(String prefix) {
361: return getPropertyGroup(prefix, false, null);
362: }
363:
364: public Properties getPropertyGroup(String prefix,
365: boolean stripPrefix) {
366: return getPropertyGroup(prefix, stripPrefix, null);
367: }
368:
369: /**
370: * Get all properties that start with the given prefix.
371: *
372: * @param prefix The prefix for which to search. If it does not end in
373: * a "." then one will be added to it for search purposes.
374: * @param stripPrefix Whether to strip off the given <code>prefix</code>
375: * in the result's keys.
376: * @param excludedPrefixes Optional array of fully qualified prefixes to
377: * exclude. For example if <code>prefix</code> is "a.b.c", then
378: * <code>excludedPrefixes</code> might be "a.b.c.ignore".
379: *
380: * @return Group of <code>Properties</code> that start with the given prefix,
381: * optionally have that prefix removed, and do not include properties
382: * that start with one of the given excluded prefixes.
383: */
384: public Properties getPropertyGroup(String prefix,
385: boolean stripPrefix, String[] excludedPrefixes) {
386: Enumeration keys = props.propertyNames();
387: Properties group = new Properties();
388:
389: if (!prefix.endsWith(".")) {
390: prefix += ".";
391: }
392:
393: while (keys.hasMoreElements()) {
394: String key = (String) keys.nextElement();
395: if (key.startsWith(prefix)) {
396:
397: boolean exclude = false;
398: if (excludedPrefixes != null) {
399: for (int i = 0; (i < excludedPrefixes.length)
400: && (exclude == false); i++) {
401: exclude = key.startsWith(excludedPrefixes[i]);
402: }
403: }
404:
405: if (exclude == false) {
406: String value = getStringProperty(key, "");
407:
408: if (stripPrefix) {
409: group
410: .put(key.substring(prefix.length()),
411: value);
412: } else {
413: group.put(key, value);
414: }
415: }
416: }
417: }
418:
419: return group;
420: }
421: }
|