KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > configuration > DataConfiguration


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License")
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.configuration;
18
19 import java.awt.Color JavaDoc;
20 import java.math.BigDecimal JavaDoc;
21 import java.math.BigInteger JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Calendar JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Date JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Locale JavaDoc;
30
31 import org.apache.commons.collections.CollectionUtils;
32 import org.apache.commons.lang.ArrayUtils;
33 import org.apache.commons.lang.StringUtils;
34
35 /**
36  * Decorator providing additional getters for any Configuration. This extended
37  * Configuration supports more types: URL, Locale, Date, Calendar, Color, as
38  * well as lists and arrays for all types.
39  *
40  * <p>Let us know if you find this useful, the most frequently used getters
41  * are likely to be integrated in the Configuration interface in a future
42  * version.</p>
43  *
44  * @author <a HREF="ebourg@apache.org">Emmanuel Bourg</a>
45  * @version $Revision: 155408 $, $Date: 2005-02-26 13:56:39 +0100 (Sa, 26 Feb 2005) $
46  * @since 1.1
47  */

48 public class DataConfiguration extends AbstractConfiguration
49 {
50     /** The key of the property storing the user defined date format. */
51     public static final String JavaDoc DATE_FORMAT_KEY = "org.apache.commons.configuration.format.date";
52
53     /** The default format for dates. */
54     public static final String JavaDoc DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
55
56     protected Configuration configuration;
57
58     public DataConfiguration(Configuration configuration)
59     {
60         this.configuration = configuration;
61     }
62
63     /**
64      * Return the configuration decorated by this DataConfiguration.
65      */

66     public Configuration getConfiguration()
67     {
68         return configuration;
69     }
70
71     public Object JavaDoc getProperty(String JavaDoc key)
72     {
73         return configuration.getProperty(key);
74     }
75
76     protected void addPropertyDirect(String JavaDoc key, Object JavaDoc obj)
77     {
78         configuration.addProperty(key, obj);
79     }
80
81     public boolean isEmpty()
82     {
83         return configuration.isEmpty();
84     }
85
86     public boolean containsKey(String JavaDoc key)
87     {
88         return configuration.containsKey(key);
89     }
90
91     public void clearProperty(String JavaDoc key)
92     {
93         configuration.clearProperty(key);
94     }
95
96     public Iterator JavaDoc getKeys()
97     {
98         return configuration.getKeys();
99     }
100
101     /**
102      * Get a list of Boolean objects associated with the given
103      * configuration key. If the key doesn't map to an existing object
104      * an empty list is returned.
105      *
106      * @param key The configuration key.
107      * @return The associated Boolean list if the key is found.
108      *
109      * @throws ConversionException is thrown if the key maps to an
110      * object that is not a list of booleans.
111      */

112     public List JavaDoc getBooleanList(String JavaDoc key)
113     {
114         return getBooleanList(key, new ArrayList JavaDoc());
115     }
116
117     /**
118      * Get a list of Boolean objects associated with the given
119      * configuration key. If the key doesn't map to an existing object,
120      * the default value is returned.
121      *
122      * @param key The configuration key.
123      * @param defaultValue The default value.
124      * @return The associated List of strings.
125      *
126      * @throws ConversionException is thrown if the key maps to an
127      * object that is not a list of booleans.
128      */

129     public List JavaDoc getBooleanList(String JavaDoc key, List JavaDoc defaultValue)
130     {
131         Object JavaDoc value = getProperty(key);
132
133         List JavaDoc list = null;
134
135         if (value == null || (value instanceof String JavaDoc && StringUtils.isEmpty((String JavaDoc) value)))
136         {
137             list = defaultValue;
138         }
139         else if (value instanceof boolean[])
140         {
141             list = new ArrayList JavaDoc();
142             CollectionUtils.addAll(list, ArrayUtils.toObject((boolean[]) value));
143         }
144         else if (value instanceof Boolean JavaDoc[])
145         {
146             list = new ArrayList JavaDoc();
147             CollectionUtils.addAll(list, (Boolean JavaDoc[]) value);
148         }
149         else if (value instanceof Collection JavaDoc)
150         {
151             Collection JavaDoc values = (Collection JavaDoc) value;
152             list = new ArrayList JavaDoc();
153
154             Iterator JavaDoc it = values.iterator();
155             while (it.hasNext())
156             {
157                 list.add(PropertyConverter.toBoolean(it.next()));
158             }
159         }
160         else
161         {
162             try
163             {
164                 // attempt to convert a single value
165
list = new ArrayList JavaDoc();
166                 list.add(PropertyConverter.toBoolean(value));
167             }
168             catch (ConversionException e)
169             {
170                 throw new ConversionException('\'' + key + "' doesn't map to a list of booleans", e);
171             }
172         }
173
174         return list;
175     }
176
177     /**
178      * Get an array of boolean primitives associated with the given
179      * configuration key. If the key doesn't map to an existing object
180      * an empty array is returned.
181      *
182      * @param key The configuration key.
183      * @return The associated boolean array if the key is found.
184      *
185      * @throws ConversionException is thrown if the key maps to an
186      * object that is not a list of booleans.
187      */

188     public boolean[] getBooleanArray(String JavaDoc key)
189     {
190         return getBooleanArray(key, new boolean[0]);
191     }
192
193     /**
194      * Get an array of boolean primitives associated with the given
195      * configuration key. If the key doesn't map to an existing object,
196      * the default value is returned.
197      *
198      * @param key The configuration key.
199      * @param defaultValue The default value.
200      * @return The associated boolean array if the key is found.
201      *
202      * @throws ConversionException is thrown if the key maps to an
203      * object that is not a list of booleans.
204      */

205     public boolean[] getBooleanArray(String JavaDoc key, boolean[] defaultValue)
206     {
207         Object JavaDoc value = getProperty(key);
208
209         boolean[] array;
210
211         if (value == null || (value instanceof String JavaDoc && StringUtils.isEmpty((String JavaDoc) value)))
212         {
213             array = defaultValue;
214         }
215         else if (value instanceof boolean[])
216         {
217             array = (boolean[]) value;
218         }
219         else if (value instanceof Boolean JavaDoc[])
220         {
221             array = ArrayUtils.toPrimitive((Boolean JavaDoc[]) value);
222         }
223         else if (value instanceof Collection JavaDoc)
224         {
225             Collection JavaDoc values = (Collection JavaDoc) value;
226             array = new boolean[values.size()];
227
228             int i = 0;
229             Iterator JavaDoc it = values.iterator();
230             while (it.hasNext())
231             {
232                 array[i++] = PropertyConverter.toBoolean(it.next()).booleanValue();
233             }
234         }
235         else
236         {
237             try
238             {
239                 // attempt to convert a single value
240
array = new boolean[1];
241                 array[0] = PropertyConverter.toBoolean(value).booleanValue();
242             }
243             catch (ConversionException e)
244             {
245                 throw new ConversionException('\'' + key + "' doesn't map to a list of booleans", e);
246             }
247         }
248
249         return array;
250     }
251
252     /**
253      * Get a list of Byte objects associated with the given configuration key.
254      * If the key doesn't map to an existing object an empty list is returned.
255      *
256      * @param key The configuration key.
257      * @return The associated Byte list if the key is found.
258      *
259      * @throws ConversionException is thrown if the key maps to an
260      * object that is not a list of bytes.
261      */

262     public List JavaDoc getByteList(String JavaDoc key)
263     {
264         return getByteList(key, new ArrayList JavaDoc());
265     }
266
267     /**
268      * Get a list of Byte objects associated with the given configuration key.
269      * If the key doesn't map to an existing object, the default value is
270      * returned.
271      *
272      * @param key The configuration key.
273      * @param defaultValue The default value.
274      * @return The associated List of Bytes.
275      *
276      * @throws ConversionException is thrown if the key maps to an
277      * object that is not a list of bytes.
278      */

279     public List JavaDoc getByteList(String JavaDoc key, List JavaDoc defaultValue)
280     {
281         Object JavaDoc value = getProperty(key);
282
283         List JavaDoc list = null;
284
285         if (value == null || (value instanceof String JavaDoc && StringUtils.isEmpty((String JavaDoc) value)))
286         {
287             list = defaultValue;
288         }
289         else if (value instanceof byte[])
290         {
291             list = new ArrayList JavaDoc();
292             CollectionUtils.addAll(list, ArrayUtils.toObject((byte[]) value));
293         }
294         else if (value instanceof Byte JavaDoc[])
295         {
296             list = new ArrayList JavaDoc();
297             CollectionUtils.addAll(list, (Byte JavaDoc[]) value);
298         }
299         else if (value instanceof Collection JavaDoc)
300         {
301             Collection JavaDoc values = (Collection JavaDoc) value;
302             list = new ArrayList JavaDoc();
303
304             Iterator JavaDoc it = values.iterator();
305             while (it.hasNext())
306             {
307                 list.add(PropertyConverter.toByte(it.next()));
308             }
309         }
310         else
311         {
312             try
313             {
314                 // attempt to convert a single value
315
list = new ArrayList JavaDoc();
316                 list.add(PropertyConverter.toByte(value));
317             }
318             catch (ConversionException e)
319             {
320                 throw new ConversionException('\'' + key + "' doesn't map to a list of bytes", e);
321             }
322         }
323
324         return list;
325     }
326
327     /**
328      * Get an array of byte primitives associated with the given
329      * configuration key. If the key doesn't map to an existing object
330      * an empty array is returned.
331      *
332      * @param key The configuration key.
333      * @return The associated byte array if the key is found.
334      *
335      * @throws ConversionException is thrown if the key maps to an
336      * object that is not a list of bytes.
337      */

338     public byte[] getByteArray(String JavaDoc key)
339     {
340         return getByteArray(key, new byte[0]);
341     }
342
343     /**
344      * Get an array of byte primitives associated with the given
345      * configuration key. If the key doesn't map to an existing object
346      * an empty array is returned.
347      *
348      * @param key The configuration key.
349      * @return The associated byte array if the key is found.
350      *
351      * @throws ConversionException is thrown if the key maps to an
352      * object that is not a list of bytes.
353      */

354     public byte[] getByteArray(String JavaDoc key, byte[] defaultValue)
355     {
356         Object JavaDoc value = getProperty(key);
357
358         byte[] array;
359
360         if (value == null || (value instanceof String JavaDoc && StringUtils.isEmpty((String JavaDoc) value)))
361         {
362             array = defaultValue;
363         }
364         else if (value instanceof byte[])
365         {
366             array = (byte[]) value;
367         }
368         else if (value instanceof Byte JavaDoc[])
369         {
370             array = ArrayUtils.toPrimitive((Byte JavaDoc[]) value);
371         }
372         else if (value instanceof Collection JavaDoc)
373         {
374             Collection JavaDoc values = (Collection JavaDoc) value;
375             array = new byte[values.size()];
376
377             int i = 0;
378             Iterator JavaDoc it = values.iterator();
379             while (it.hasNext())
380             {
381                 array[i++] = PropertyConverter.toByte(it.next()).byteValue();
382             }
383         }
384         else
385         {
386             try
387             {
388                 // attempt to convert a single value
389
array = new byte[1];
390                 array[0] = PropertyConverter.toByte(value).byteValue();
391             }
392             catch (ConversionException e)
393             {
394                 throw new ConversionException('\'' + key + "' doesn't map to a list of bytes", e);
395             }
396         }
397
398         return array;
399     }
400
401     /**
402      * Get a list of Short objects associated with the given configuration key.
403      * If the key doesn't map to an existing object an empty list is returned.
404      *
405      * @param key The configuration key.
406      * @return The associated Short list if the key is found.
407      *
408      * @throws ConversionException is thrown if the key maps to an
409      * object that is not a list of shorts.
410      */

411     public List JavaDoc getShortList(String JavaDoc key)
412     {
413         return getShortList(key, new ArrayList JavaDoc());
414     }
415
416     /**
417      * Get a list of Short objects associated with the given configuration key.
418      * If the key doesn't map to an existing object, the default value is
419      * returned.
420      *
421      * @param key The configuration key.
422      * @param defaultValue The default value.
423      * @return The associated List of Shorts.
424      *
425      * @throws ConversionException is thrown if the key maps to an
426      * object that is not a list of shorts.
427      */

428     public List JavaDoc getShortList(String JavaDoc key, List JavaDoc defaultValue)
429     {
430         Object JavaDoc value = getProperty(key);
431
432         List JavaDoc list = null;
433
434         if (value == null || (value instanceof String JavaDoc && StringUtils.isEmpty((String JavaDoc) value)))
435         {
436             list = defaultValue;
437         }
438         else if (value instanceof short[])
439         {
440             list = new ArrayList JavaDoc();
441             CollectionUtils.addAll(list, ArrayUtils.toObject((short[]) value));
442         }
443         else if (value instanceof Short JavaDoc[])
444         {
445             list = new ArrayList JavaDoc();
446             CollectionUtils.addAll(list, (Short JavaDoc[]) value);
447         }
448         else if (value instanceof Collection JavaDoc)
449         {
450             Collection JavaDoc values = (Collection JavaDoc) value;
451             list = new ArrayList JavaDoc();
452
453             Iterator JavaDoc it = values.iterator();
454             while (it.hasNext())
455             {
456                 list.add(PropertyConverter.toShort(it.next()));
457             }
458         }
459         else
460         {
461             try
462             {
463                 // attempt to convert a single value
464
list = new ArrayList JavaDoc();
465                 list.add(PropertyConverter.toShort(value));
466             }
467             catch (ConversionException e)
468             {
469                 throw new ConversionException('\'' + key + "' doesn't map to a list of shorts", e);
470             }
471         }
472
473         return list;
474     }
475
476     /**
477      * Get an array of short primitives associated with the given
478      * configuration key. If the key doesn't map to an existing object
479      * an empty array is returned.
480      *
481      * @param key The configuration key.
482      * @return The associated short array if the key is found.
483      *
484      * @throws ConversionException is thrown if the key maps to an
485      * object that is not a list of shorts.
486      */

487     public short[] getShortArray(String JavaDoc key)
488     {
489         return getShortArray(key, new short[0]);
490     }
491
492     /**
493      * Get an array of short primitives associated with the given
494      * configuration key. If the key doesn't map to an existing object
495      * an empty array is returned.
496      *
497      * @param key The configuration key.
498      * @return The associated short array if the key is found.
499      *
500      * @throws ConversionException is thrown if the key maps to an
501      * object that is not a list of shorts.
502      */

503     public short[] getShortArray(String JavaDoc key, short[] defaultValue)
504     {
505         Object JavaDoc value = getProperty(key);
506
507         short[] array;
508
509         if (value == null || (value instanceof String JavaDoc && StringUtils.isEmpty((String JavaDoc) value)))
510         {
511             array = defaultValue;
512         }
513         else if (value instanceof short[])
514         {
515             array = (short[]) value;
516         }
517         else if (value instanceof Short JavaDoc[])
518         {
519             array = ArrayUtils.toPrimitive((Short JavaDoc[]) value);
520         }
521         else if (value instanceof Collection JavaDoc)
522         {
523             Collection JavaDoc values = (Collection JavaDoc) value;
524             array = new short[values.size()];
525
526             int i = 0;
527             Iterator JavaDoc it = values.iterator();
528             while (it.hasNext())
529             {
530                 array[i++] = PropertyConverter.toShort(it.next()).shortValue();
531             }
532         }
533         else
534         {
535             try
536             {
537                 // attempt to convert a single value
538
array = new short[1];
539                 array[0] = PropertyConverter.toShort(value).shortValue();
540             }
541             catch (ConversionException e)
542             {
543                 throw new ConversionException('\'' + key + "' doesn't map to a list of shorts", e);
544             }
545         }
546
547         return array;
548     }
549
550     /**
551      * Get a list of Integer objects associated with the given
552      * configuration key. If the key doesn't map to an existing object
553      * an empty list is returned.
554      *
555      * @param key The configuration key.
556      * @return The associated Integer list if the key is found.
557      *
558      * @throws ConversionException is thrown if the key maps to an
559      * object that is not a list of integers.
560      */

561     public List JavaDoc getIntegerList(String JavaDoc key)
562     {
563         return getIntegerList(key, new ArrayList JavaDoc());
564     }
565
566     /**
567      * Get a list of Integer objects associated with the given
568      * configuration key. If the key doesn't map to an existing object,
569      * the default value is returned.
570      *
571      * @param key The configuration key.
572      * @param defaultValue The default value.
573      * @return The associated List of Integers.
574      *
575      * @throws ConversionException is thrown if the key maps to an
576      * object that is not a list of integers.
577      */

578     public List JavaDoc getIntegerList(String JavaDoc key, List JavaDoc defaultValue)
579     {
580         Object JavaDoc value = getProperty(key);
581
582         List JavaDoc list = null;
583
584         if (value == null || (value instanceof String JavaDoc && StringUtils.isEmpty((String JavaDoc) value)))
585         {
586             list = defaultValue;
587         }
588         else if (value instanceof int[])
589         {
590             list = new ArrayList JavaDoc();
591             CollectionUtils.addAll(list, ArrayUtils.toObject((int[]) value));
592         }
593         else if (value instanceof Integer JavaDoc[])
594         {
595             list = new ArrayList JavaDoc();
596             CollectionUtils.addAll(list, (Integer JavaDoc[]) value);
597         }
598         else if (value instanceof Collection JavaDoc)
599         {
600             Collection JavaDoc values = (Collection JavaDoc) value;
601             list = new ArrayList JavaDoc();
602
603             Iterator JavaDoc it = values.iterator();
604             while (it.hasNext())
605             {
606                 list.add(PropertyConverter.toInteger(it.next()));
607             }
608         }
609         else
610         {
611             try
612             {
613                 // attempt to convert a single value
614
list = new ArrayList JavaDoc();
615                 list.add(PropertyConverter.toInteger(value));
616             }
617             catch (ConversionException e)
618             {
619                 throw new ConversionException('\'' + key + "' doesn't map to a list of integers", e);
620             }
621         }
622
623         return list;
624     }
625
626     /**
627      * Get an array of int primitives associated with the given
628      * configuration key. If the key doesn't map to an existing object
629      * an empty array is returned.
630      *
631      * @param key The configuration key.
632      * @return The associated int array if the key is found.
633      *
634      * @throws ConversionException is thrown if the key maps to an
635      * object that is not a list of integers.
636      */

637     public int[] getIntArray(String JavaDoc key)
638     {
639         return getIntArray(key, new int[0]);
640     }
641
642     /**
643      * Get an array of int primitives associated with the given
644      * configuration key. If the key doesn't map to an existing object
645      * an empty array is returned.
646      *
647      * @param key The configuration key.
648      * @return The associated int array if the key is found.
649      *
650      * @throws ConversionException is thrown if the key maps to an
651      * object that is not a list of integers.
652      */

653     public int[] getIntArray(String JavaDoc key, int[] defaultValue)
654     {
655         Object JavaDoc value = getProperty(key);
656
657         int[] array;
658
659         if (value == null || (value instanceof String JavaDoc && StringUtils.isEmpty((String JavaDoc) value)))
660         {
661             array = defaultValue;
662         }
663         else if (value instanceof int[])
664         {
665             array = (int[]) value;
666         }
667         else if (value instanceof Integer JavaDoc[])
668         {
669             array = ArrayUtils.toPrimitive((Integer JavaDoc[]) value);
670         }
671         else if (value instanceof Collection JavaDoc)
672         {
673             Collection JavaDoc values = (Collection JavaDoc) value;
674             array = new int[values.size()];
675
676             int i = 0;
677             Iterator JavaDoc it = values.iterator();
678             while (it.hasNext())
679             {
680                 array[i++] = PropertyConverter.toInteger(it.next()).intValue();
681             }
682         }
683         else
684         {
685             try
686             {
687                 // attempt to convert a single value
688
array = new int[1];
689                 array[0] = PropertyConverter.toInteger(value).intValue();
690             }
691             catch (ConversionException e)
692             {
693                 throw new ConversionException('\'' + key + "' doesn't map to a list of integers", e);
694             }
695         }
696
697         return array;
698     }
699
700     /**
701      * Get a list of Long objects associated with the given configuration key.
702      * If the key doesn't map to an existing object an empty list is returned.
703      *
704      * @param key The configuration key.
705      * @return The associated Long list if the key is found.
706      *
707      * @throws ConversionException is thrown if the key maps to an
708      * object that is not a list of longs.
709      */

710     public List JavaDoc getLongList(String JavaDoc key)
711     {
712         return getLongList(key, new ArrayList JavaDoc());
713     }
714
715     /**
716      * Get a list of Long objects associated with the given configuration key.
717      * If the key doesn't map to an existing object, the default value is
718      * returned.
719      *
720      * @param key The configuration key.
721      * @param defaultValue The default value.
722      * @return The associated List of Longs.
723      *
724      * @throws ConversionException is thrown if the key maps to an
725      * object that is not a list of longs.
726      */

727     public List JavaDoc getLongList(String JavaDoc key, List JavaDoc defaultValue)
728     {
729         Object JavaDoc value = getProperty(key);
730
731         List JavaDoc list = null;
732
733         if (value == null || (value instanceof String JavaDoc && StringUtils.isEmpty((String JavaDoc) value)))
734         {
735             list = defaultValue;
736         }
737         else if (value instanceof long[])
738         {
739             list = new ArrayList JavaDoc();
740             CollectionUtils.addAll(list, ArrayUtils.toObject((long[]) value));
741         }
742         else if (value instanceof Long JavaDoc[])
743         {
744             list = new ArrayList JavaDoc();
745             CollectionUtils.addAll(list, (Long JavaDoc[]) value);
746         }
747         else if (value instanceof Collection JavaDoc)
748         {
749             Collection JavaDoc values = (Collection JavaDoc) value;
750             list = new ArrayList JavaDoc();
751
752             Iterator JavaDoc it = values.iterator();
753             while (it.hasNext())
754             {
755                 list.add(PropertyConverter.toLong(it.next()));
756             }
757         }
758         else
759         {
760             try
761             {
762                 // attempt to convert a single value
763
list = new ArrayList JavaDoc();
764                 list.add(PropertyConverter.toLong(value));
765             }
766             catch (ConversionException e)
767             {
768                 throw new ConversionException('\'' + key + "' doesn't map to a list of longs", e);
769             }
770         }
771
772         return list;
773     }
774
775     /**
776      * Get an array of long primitives associated with the given
777      * configuration key. If the key doesn't map to an existing object
778      * an empty array is returned.
779      *
780      * @param key The configuration key.
781      * @return The associated long array if the key is found.
782      *
783      * @throws ConversionException is thrown if the key maps to an
784      * object that is not a list of longs.
785      */

786     public long[] getLongArray(String JavaDoc key)
787     {
788         return getLongArray(key, new long[0]);
789     }
790
791     /**
792      * Get an array of long primitives associated with the given
793      * configuration key. If the key doesn't map to an existing object
794      * an empty array is returned.
795      *
796      * @param key The configuration key.
797      * @return The associated long array if the key is found.
798      *
799      * @throws ConversionException is thrown if the key maps to an
800      * object that is not a list of longs.
801      */

802     public long[] getLongArray(String JavaDoc key, long[] defaultValue)
803     {
804         Object JavaDoc value = getProperty(key);
805
806         long[] array;
807
808         if (value == null || (value instanceof String JavaDoc && StringUtils.isEmpty((String JavaDoc) value)))