0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017:
0018: package org.apache.commons.configuration;
0019:
0020: import java.awt.Color;
0021: import java.math.BigDecimal;
0022: import java.math.BigInteger;
0023: import java.net.URL;
0024: import java.util.ArrayList;
0025: import java.util.Calendar;
0026: import java.util.Collection;
0027: import java.util.Date;
0028: import java.util.Iterator;
0029: import java.util.List;
0030: import java.util.Locale;
0031: import java.io.Serializable;
0032:
0033: import org.apache.commons.collections.CollectionUtils;
0034: import org.apache.commons.lang.ArrayUtils;
0035: import org.apache.commons.lang.StringUtils;
0036:
0037: /**
0038: * Decorator providing additional getters for any Configuration. This extended
0039: * Configuration supports more types: URL, Locale, Date, Calendar, Color, as
0040: * well as lists and arrays for all types.
0041: *
0042: * <p>Let us know if you find this useful, the most frequently used getters
0043: * are likely to be integrated in the Configuration interface in a future
0044: * version.</p>
0045: *
0046: * @author <a href="ebourg@apache.org">Emmanuel Bourg</a>
0047: * @version $Revision: 439648 $, $Date: 2006-09-02 22:42:10 +0200 (Sa, 02 Sep 2006) $
0048: * @since 1.1
0049: */
0050: public class DataConfiguration extends AbstractConfiguration implements
0051: Serializable {
0052: /** The key of the property storing the user defined date format. */
0053: public static final String DATE_FORMAT_KEY = "org.apache.commons.configuration.format.date";
0054:
0055: /** The default format for dates. */
0056: public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
0057:
0058: /**
0059: * The serial version UID.
0060: */
0061: private static final long serialVersionUID = -69011336405718640L;
0062:
0063: /** Stores the wrapped configuration.*/
0064: protected Configuration configuration;
0065:
0066: /**
0067: * Creates a new instance of <code>DataConfiguration</code> and sets the
0068: * wrapped configuration.
0069: *
0070: * @param configuration the wrapped configuration
0071: */
0072: public DataConfiguration(Configuration configuration) {
0073: this .configuration = configuration;
0074: }
0075:
0076: /**
0077: * Return the configuration decorated by this DataConfiguration.
0078: *
0079: * @return the wrapped configuration
0080: */
0081: public Configuration getConfiguration() {
0082: return configuration;
0083: }
0084:
0085: public Object getProperty(String key) {
0086: return configuration.getProperty(key);
0087: }
0088:
0089: protected void addPropertyDirect(String key, Object obj) {
0090: configuration.addProperty(key, obj);
0091: }
0092:
0093: public boolean isEmpty() {
0094: return configuration.isEmpty();
0095: }
0096:
0097: public boolean containsKey(String key) {
0098: return configuration.containsKey(key);
0099: }
0100:
0101: public void clearProperty(String key) {
0102: configuration.clearProperty(key);
0103: }
0104:
0105: public Iterator getKeys() {
0106: return configuration.getKeys();
0107: }
0108:
0109: /**
0110: * Get a list of Boolean objects associated with the given
0111: * configuration key. If the key doesn't map to an existing object
0112: * an empty list is returned.
0113: *
0114: * @param key The configuration key.
0115: * @return The associated Boolean list if the key is found.
0116: *
0117: * @throws ConversionException is thrown if the key maps to an
0118: * object that is not a list of booleans.
0119: */
0120: public List getBooleanList(String key) {
0121: return getBooleanList(key, new ArrayList());
0122: }
0123:
0124: /**
0125: * Get a list of Boolean objects associated with the given
0126: * configuration key. If the key doesn't map to an existing object,
0127: * the default value is returned.
0128: *
0129: * @param key The configuration key.
0130: * @param defaultValue The default value.
0131: * @return The associated List of strings.
0132: *
0133: * @throws ConversionException is thrown if the key maps to an
0134: * object that is not a list of booleans.
0135: */
0136: public List getBooleanList(String key, List defaultValue) {
0137: Object value = getProperty(key);
0138:
0139: List list;
0140:
0141: if (value == null
0142: || (value instanceof String && StringUtils
0143: .isEmpty((String) value))) {
0144: list = defaultValue;
0145: } else if (value instanceof boolean[]) {
0146: list = new ArrayList();
0147: CollectionUtils.addAll(list, ArrayUtils
0148: .toObject((boolean[]) value));
0149: } else if (value instanceof Boolean[]) {
0150: list = new ArrayList();
0151: CollectionUtils.addAll(list, (Boolean[]) value);
0152: } else if (value instanceof Collection) {
0153: Collection values = (Collection) value;
0154: list = new ArrayList();
0155:
0156: Iterator it = values.iterator();
0157: while (it.hasNext()) {
0158: list.add(PropertyConverter.toBoolean(interpolate(it
0159: .next())));
0160: }
0161: } else {
0162: try {
0163: // attempt to convert a single value
0164: list = new ArrayList();
0165: list.add(PropertyConverter
0166: .toBoolean(interpolate(value)));
0167: } catch (ConversionException e) {
0168: throw new ConversionException('\'' + key
0169: + "' doesn't map to a list of booleans", e);
0170: }
0171: }
0172:
0173: return list;
0174: }
0175:
0176: /**
0177: * Get an array of boolean primitives associated with the given
0178: * configuration key. If the key doesn't map to an existing object
0179: * an empty array is returned.
0180: *
0181: * @param key The configuration key.
0182: * @return The associated boolean array if the key is found.
0183: *
0184: * @throws ConversionException is thrown if the key maps to an
0185: * object that is not a list of booleans.
0186: */
0187: public boolean[] getBooleanArray(String key) {
0188: return getBooleanArray(key, new boolean[0]);
0189: }
0190:
0191: /**
0192: * Get an array of boolean primitives associated with the given
0193: * configuration key. If the key doesn't map to an existing object,
0194: * the default value is returned.
0195: *
0196: * @param key The configuration key.
0197: * @param defaultValue The default value.
0198: * @return The associated boolean array if the key is found.
0199: *
0200: * @throws ConversionException is thrown if the key maps to an
0201: * object that is not a list of booleans.
0202: */
0203: public boolean[] getBooleanArray(String key, boolean[] defaultValue) {
0204: Object value = getProperty(key);
0205:
0206: boolean[] array;
0207:
0208: if (value == null
0209: || (value instanceof String && StringUtils
0210: .isEmpty((String) value))) {
0211: array = defaultValue;
0212: } else if (value instanceof boolean[]) {
0213: array = (boolean[]) value;
0214: } else if (value instanceof Boolean[]) {
0215: array = ArrayUtils.toPrimitive((Boolean[]) value);
0216: } else if (value instanceof Collection) {
0217: Collection values = (Collection) value;
0218: array = new boolean[values.size()];
0219:
0220: int i = 0;
0221: Iterator it = values.iterator();
0222: while (it.hasNext()) {
0223: array[i++] = PropertyConverter.toBoolean(
0224: interpolate(it.next())).booleanValue();
0225: }
0226: } else {
0227: try {
0228: // attempt to convert a single value
0229: array = new boolean[1];
0230: array[0] = PropertyConverter.toBoolean(
0231: interpolate(value)).booleanValue();
0232: } catch (ConversionException e) {
0233: throw new ConversionException('\'' + key
0234: + "' doesn't map to a list of booleans", e);
0235: }
0236: }
0237:
0238: return array;
0239: }
0240:
0241: /**
0242: * Get a list of Byte objects associated with the given configuration key.
0243: * If the key doesn't map to an existing object an empty list is returned.
0244: *
0245: * @param key The configuration key.
0246: * @return The associated Byte list if the key is found.
0247: *
0248: * @throws ConversionException is thrown if the key maps to an
0249: * object that is not a list of bytes.
0250: */
0251: public List getByteList(String key) {
0252: return getByteList(key, new ArrayList());
0253: }
0254:
0255: /**
0256: * Get a list of Byte objects associated with the given configuration key.
0257: * If the key doesn't map to an existing object, the default value is
0258: * returned.
0259: *
0260: * @param key The configuration key.
0261: * @param defaultValue The default value.
0262: * @return The associated List of Bytes.
0263: *
0264: * @throws ConversionException is thrown if the key maps to an
0265: * object that is not a list of bytes.
0266: */
0267: public List getByteList(String key, List defaultValue) {
0268: Object value = getProperty(key);
0269:
0270: List list;
0271:
0272: if (value == null
0273: || (value instanceof String && StringUtils
0274: .isEmpty((String) value))) {
0275: list = defaultValue;
0276: } else if (value instanceof byte[]) {
0277: list = new ArrayList();
0278: CollectionUtils.addAll(list, ArrayUtils
0279: .toObject((byte[]) value));
0280: } else if (value instanceof Byte[]) {
0281: list = new ArrayList();
0282: CollectionUtils.addAll(list, (Byte[]) value);
0283: } else if (value instanceof Collection) {
0284: Collection values = (Collection) value;
0285: list = new ArrayList();
0286:
0287: Iterator it = values.iterator();
0288: while (it.hasNext()) {
0289: list.add(PropertyConverter
0290: .toByte(interpolate(it.next())));
0291: }
0292: } else {
0293: try {
0294: // attempt to convert a single value
0295: list = new ArrayList();
0296: list.add(PropertyConverter.toByte(interpolate(value)));
0297: } catch (ConversionException e) {
0298: throw new ConversionException('\'' + key
0299: + "' doesn't map to a list of bytes", e);
0300: }
0301: }
0302:
0303: return list;
0304: }
0305:
0306: /**
0307: * Get an array of byte primitives associated with the given
0308: * configuration key. If the key doesn't map to an existing object
0309: * an empty array is returned.
0310: *
0311: * @param key The configuration key.
0312: * @return The associated byte array if the key is found.
0313: *
0314: * @throws ConversionException is thrown if the key maps to an
0315: * object that is not a list of bytes.
0316: */
0317: public byte[] getByteArray(String key) {
0318: return getByteArray(key, new byte[0]);
0319: }
0320:
0321: /**
0322: * Get an array of byte primitives associated with the given
0323: * configuration key. If the key doesn't map to an existing object
0324: * an empty array is returned.
0325: *
0326: * @param key The configuration key.
0327: * @param defaultValue the default value, which will be returned if the property is not found
0328: * @return The associated byte array if the key is found.
0329: *
0330: * @throws ConversionException is thrown if the key maps to an
0331: * object that is not a list of bytes.
0332: */
0333: public byte[] getByteArray(String key, byte[] defaultValue) {
0334: Object value = getProperty(key);
0335:
0336: byte[] array;
0337:
0338: if (value == null
0339: || (value instanceof String && StringUtils
0340: .isEmpty((String) value))) {
0341: array = defaultValue;
0342: } else if (value instanceof byte[]) {
0343: array = (byte[]) value;
0344: } else if (value instanceof Byte[]) {
0345: array = ArrayUtils.toPrimitive((Byte[]) value);
0346: } else if (value instanceof Collection) {
0347: Collection values = (Collection) value;
0348: array = new byte[values.size()];
0349:
0350: int i = 0;
0351: Iterator it = values.iterator();
0352: while (it.hasNext()) {
0353: array[i++] = PropertyConverter.toByte(
0354: interpolate(it.next())).byteValue();
0355: }
0356: } else {
0357: try {
0358: // attempt to convert a single value
0359: array = new byte[1];
0360: array[0] = PropertyConverter.toByte(interpolate(value))
0361: .byteValue();
0362: } catch (ConversionException e) {
0363: throw new ConversionException('\'' + key
0364: + "' doesn't map to a list of bytes", e);
0365: }
0366: }
0367:
0368: return array;
0369: }
0370:
0371: /**
0372: * Get a list of Short objects associated with the given configuration key.
0373: * If the key doesn't map to an existing object an empty list is returned.
0374: *
0375: * @param key The configuration key.
0376: * @return The associated Short list if the key is found.
0377: *
0378: * @throws ConversionException is thrown if the key maps to an
0379: * object that is not a list of shorts.
0380: */
0381: public List getShortList(String key) {
0382: return getShortList(key, new ArrayList());
0383: }
0384:
0385: /**
0386: * Get a list of Short objects associated with the given configuration key.
0387: * If the key doesn't map to an existing object, the default value is
0388: * returned.
0389: *
0390: * @param key The configuration key.
0391: * @param defaultValue The default value.
0392: * @return The associated List of Shorts.
0393: *
0394: * @throws ConversionException is thrown if the key maps to an
0395: * object that is not a list of shorts.
0396: */
0397: public List getShortList(String key, List defaultValue) {
0398: Object value = getProperty(key);
0399:
0400: List list;
0401:
0402: if (value == null
0403: || (value instanceof String && StringUtils
0404: .isEmpty((String) value))) {
0405: list = defaultValue;
0406: } else if (value instanceof short[]) {
0407: list = new ArrayList();
0408: CollectionUtils.addAll(list, ArrayUtils
0409: .toObject((short[]) value));
0410: } else if (value instanceof Short[]) {
0411: list = new ArrayList();
0412: CollectionUtils.addAll(list, (Short[]) value);
0413: } else if (value instanceof Collection) {
0414: Collection values = (Collection) value;
0415: list = new ArrayList();
0416:
0417: Iterator it = values.iterator();
0418: while (it.hasNext()) {
0419: list.add(PropertyConverter.toShort(interpolate(it
0420: .next())));
0421: }
0422: } else {
0423: try {
0424: // attempt to convert a single value
0425: list = new ArrayList();
0426: list.add(PropertyConverter.toShort(interpolate(value)));
0427: } catch (ConversionException e) {
0428: throw new ConversionException('\'' + key
0429: + "' doesn't map to a list of shorts", e);
0430: }
0431: }
0432:
0433: return list;
0434: }
0435:
0436: /**
0437: * Get an array of short primitives associated with the given
0438: * configuration key. If the key doesn't map to an existing object
0439: * an empty array is returned.
0440: *
0441: * @param key The configuration key.
0442: * @return The associated short array if the key is found.
0443: *
0444: * @throws ConversionException is thrown if the key maps to an
0445: * object that is not a list of shorts.
0446: */
0447: public short[] getShortArray(String key) {
0448: return getShortArray(key, new short[0]);
0449: }
0450:
0451: /**
0452: * Get an array of short primitives associated with the given
0453: * configuration key. If the key doesn't map to an existing object
0454: * an empty array is returned.
0455: *
0456: * @param key The configuration key.
0457: * @param defaultValue the default value, which will be returned if the property is not found
0458: * @return The associated short array if the key is found.
0459: *
0460: * @throws ConversionException is thrown if the key maps to an
0461: * object that is not a list of shorts.
0462: */
0463: public short[] getShortArray(String key, short[] defaultValue) {
0464: Object value = getProperty(key);
0465:
0466: short[] array;
0467:
0468: if (value == null
0469: || (value instanceof String && StringUtils
0470: .isEmpty((String) value))) {
0471: array = defaultValue;
0472: } else if (value instanceof short[]) {
0473: array = (short[]) value;
0474: } else if (value instanceof Short[]) {
0475: array = ArrayUtils.toPrimitive((Short[]) value);
0476: } else if (value instanceof Collection) {
0477: Collection values = (Collection) value;
0478: array = new short[values.size()];
0479:
0480: int i = 0;
0481: Iterator it = values.iterator();
0482: while (it.hasNext()) {
0483: array[i++] = PropertyConverter.toShort(
0484: interpolate(it.next())).shortValue();
0485: }
0486: } else {
0487: try {
0488: // attempt to convert a single value
0489: array = new short[1];
0490: array[0] = PropertyConverter
0491: .toShort(interpolate(value)).shortValue();
0492: } catch (ConversionException e) {
0493: throw new ConversionException('\'' + key
0494: + "' doesn't map to a list of shorts", e);
0495: }
0496: }
0497:
0498: return array;
0499: }
0500:
0501: /**
0502: * Get a list of Integer objects associated with the given
0503: * configuration key. If the key doesn't map to an existing object
0504: * an empty list is returned.
0505: *
0506: * @param key The configuration key.
0507: * @return The associated Integer list if the key is found.
0508: *
0509: * @throws ConversionException is thrown if the key maps to an
0510: * object that is not a list of integers.
0511: */
0512: public List getIntegerList(String key) {
0513: return getIntegerList(key, new ArrayList());
0514: }
0515:
0516: /**
0517: * Get a list of Integer objects associated with the given
0518: * configuration key. If the key doesn't map to an existing object,
0519: * the default value is returned.
0520: *
0521: * @param key The configuration key.
0522: * @param defaultValue The default value.
0523: * @return The associated List of Integers.
0524: *
0525: * @throws ConversionException is thrown if the key maps to an
0526: * object that is not a list of integers.
0527: */
0528: public List getIntegerList(String key, List defaultValue) {
0529: Object value = getProperty(key);
0530:
0531: List list;
0532:
0533: if (value == null
0534: || (value instanceof String && StringUtils
0535: .isEmpty((String) value))) {
0536: list = defaultValue;
0537: } else if (value instanceof int[]) {
0538: list = new ArrayList();
0539: CollectionUtils.addAll(list, ArrayUtils
0540: .toObject((int[]) value));
0541: } else if (value instanceof Integer[]) {
0542: list = new ArrayList();
0543: CollectionUtils.addAll(list, (Integer[]) value);
0544: } else if (value instanceof Collection) {
0545: Collection values = (Collection) value;
0546: list = new ArrayList();
0547:
0548: Iterator it = values.iterator();
0549: while (it.hasNext()) {
0550: list.add(PropertyConverter.toInteger(interpolate(it
0551: .next())));
0552: }
0553: } else {
0554: try {
0555: // attempt to convert a single value
0556: list = new ArrayList();
0557: list.add(PropertyConverter
0558: .toInteger(interpolate(value)));
0559: } catch (ConversionException e) {
0560: throw new ConversionException('\'' + key
0561: + "' doesn't map to a list of integers", e);
0562: }
0563: }
0564:
0565: return list;
0566: }
0567:
0568: /**
0569: * Get an array of int primitives associated with the given
0570: * configuration key. If the key doesn't map to an existing object
0571: * an empty array is returned.
0572: *
0573: * @param key The configuration key.
0574: * @return The associated int array if the key is found.
0575: *
0576: * @throws ConversionException is thrown if the key maps to an
0577: * object that is not a list of integers.
0578: */
0579: public int[] getIntArray(String key) {
0580: return getIntArray(key, new int[0]);
0581: }
0582:
0583: /**
0584: * Get an array of int primitives associated with the given
0585: * configuration key. If the key doesn't map to an existing object
0586: * an empty array is returned.
0587: *
0588: * @param key The configuration key.
0589: * @param defaultValue the default value, which will be returned if the property is not found
0590: * @return The associated int array if the key is found.
0591: *
0592: * @throws ConversionException is thrown if the key maps to an
0593: * object that is not a list of integers.
0594: */
0595: public int[] getIntArray(String key, int[] defaultValue) {
0596: Object value = getProperty(key);
0597:
0598: int[] array;
0599:
0600: if (value == null
0601: || (value instanceof String && StringUtils
0602: .isEmpty((String) value))) {
0603: array = defaultValue;
0604: } else if (value instanceof int[]) {
0605: array = (int[]) value;
0606: } else if (value instanceof Integer[]) {
0607: array = ArrayUtils.toPrimitive((Integer[]) value);
0608: } else if (value instanceof Collection) {
0609: Collection values = (Collection) value;
0610: array = new int[values.size()];
0611:
0612: int i = 0;
0613: Iterator it = values.iterator();
0614: while (it.hasNext()) {
0615: array[i++] = PropertyConverter.toInteger(
0616: interpolate(it.next())).intValue();
0617: }
0618: } else {
0619: try {
0620: // attempt to convert a single value
0621: array = new int[1];
0622: array[0] = PropertyConverter.toInteger(
0623: interpolate(value)).intValue();
0624: } catch (ConversionException e) {
0625: throw new ConversionException('\'' + key
0626: + "' doesn't map to a list of integers", e);
0627: }
0628: }
0629:
0630: return array;
0631: }
0632:
0633: /**
0634: * Get a list of Long objects associated with the given configuration key.
0635: * If the key doesn't map to an existing object an empty list is returned.
0636: *
0637: * @param key The configuration key.
0638: * @return The associated Long list if the key is found.
0639: *
0640: * @throws ConversionException is thrown if the key maps to an
0641: * object that is not a list of longs.
0642: */
0643: public List getLongList(String key) {
0644: return getLongList(key, new ArrayList());
0645: }
0646:
0647: /**
0648: * Get a list of Long objects associated with the given configuration key.
0649: * If the key doesn't map to an existing object, the default value is
0650: * returned.
0651: *
0652: * @param key The configuration key.
0653: * @param defaultValue The default value.
0654: * @return The associated List of Longs.
0655: *
0656: * @throws ConversionException is thrown if the key maps to an
0657: * object that is not a list of longs.
0658: */
0659: public List getLongList(String key, List defaultValue) {
0660: Object value = getProperty(key);
0661:
0662: List list;
0663:
0664: if (value == null
0665: || (value instanceof String && StringUtils
0666: .isEmpty((String) value))) {
0667: list = defaultValue;
0668: } else if (value instanceof long[]) {
0669: list = new ArrayList();
0670: CollectionUtils.addAll(list, ArrayUtils
0671: .toObject((long[]) value));
0672: } else if (value instanceof Long[]) {
0673: list = new ArrayList();
0674: CollectionUtils.addAll(list, (Long[]) value);
0675: } else if (value instanceof Collection) {
0676: Collection values = (Collection) value;
0677: list = new ArrayList();
0678:
0679: Iterator it = values.iterator();
0680: while (it.hasNext()) {
0681: list.add(PropertyConverter
0682: .toLong(interpolate(it.next())));
0683: }
0684: } else {
0685: try {
0686: // attempt to convert a single value
0687: list = new ArrayList();
0688: list.add(PropertyConverter.toLong(interpolate(value)));
0689: } catch (ConversionException e) {
0690: throw new ConversionException('\'' + key
0691: + "' doesn't map to a list of longs", e);
0692: }
0693: }
0694:
0695: return list;
0696: }
0697:
0698: /**
0699: * Get an array of long primitives associated with the given
0700: * configuration key. If the key doesn't map to an existing object
0701: * an empty array is returned.
0702: *
0703: * @param key The configuration key.
0704: * @return The associated long array if the key is found.
0705: *
0706: * @throws ConversionException is thrown if the key maps to an
0707: * object that is not a list of longs.
0708: */
0709: public long[] getLongArray(String key) {
0710: return getLongArray(key, new long[0]);
0711: }
0712:
0713: /**
0714: * Get an array of long primitives associated with the given
0715: * configuration key. If the key doesn't map to an existing object
0716: * an empty array is returned.
0717: *
0718: * @param key The configuration key.
0719: * @param defaultValue the default value, which will be returned if the property is not found
0720: * @return The associated long array if the key is found.
0721: *
0722: * @throws ConversionException is thrown if the key maps to an
0723: * object that is not a list of longs.
0724: */
0725: public long[] getLongArray(String key, long[] defaultValue) {
0726: Object value = getProperty(key);
0727:
0728: long[] array;
0729:
0730: if (value == null
0731: || (value instanceof String && StringUtils
0732: .isEmpty((String) value))) {
0733: array = defaultValue;
0734: } else if (value instanceof long[]) {
0735: array = (long[]) value;
0736: } else if (value instanceof Long[]) {
0737: array = ArrayUtils.toPrimitive((Long[]) value);
0738: } else if (value instanceof Collection) {
0739: Collection values = (Collection) value;
0740: array = new long[values.size()];
0741:
0742: int i = 0;
0743: Iterator it = values.iterator();
0744: while (it.hasNext()) {
0745: array[i++] = PropertyConverter.toLong(
0746: interpolate(it.next())).longValue();
0747: }
0748: } else {
0749: try {
0750: // attempt to convert a single value
0751: array = new long[1];
0752: array[0] = PropertyConverter.toLong(interpolate(value))
0753: .longValue();
0754: } catch (ConversionException e) {
0755: throw new ConversionException('\'' + key
0756: + "' doesn't map to a list of longs", e);
0757: }
0758: }
0759:
0760: return array;
0761: }
0762:
0763: /**
0764: * Get a list of Float objects associated with the given configuration key.
0765: * If the key doesn't map to an existing object an empty list is returned.
0766: *
0767: * @param key The configuration key.
0768: * @return The associated Float list if the key is found.
0769: *
0770: * @throws ConversionException is thrown if the key maps to an
0771: * object that is not a list of floats.
0772: */
0773: public List getFloatList(String key) {
0774: return getFloatList(key, new ArrayList());
0775: }
0776:
0777: /**
0778: * Get a list of Float objects associated with the given
0779: * configuration key. If the key doesn't map to an existing object,
0780: * the default value is returned.
0781: *
0782: * @param key The configuration key.
0783: * @param defaultValue The default value.
0784: * @return The associated List of Floats.
0785: *
0786: * @throws ConversionException is thrown if the key maps to an
0787: * object that is not a list of floats.
0788: */
0789: public List getFloatList(String key, List defaultValue) {
0790: Object value = getProperty(key);
0791:
0792: List list;
0793:
0794: if (value == null
0795: || (value instanceof String && StringUtils
0796: .isEmpty((String) value))) {
0797: list = defaultValue;
0798: } else if (value instanceof float[]) {
0799: list = new ArrayList();
0800: CollectionUtils.addAll(list, ArrayUtils
0801: .toObject((float[]) value));
0802: } else if (value instanceof Float[]) {
0803: list = new ArrayList();
0804: CollectionUtils.addAll(list, (Float[]) value);
0805: } else if (value instanceof Collection) {
0806: Collection values = (Collection) value;
0807: list = new ArrayList();
0808:
0809: Iterator it = values.iterator();
0810: while (it.hasNext()) {
0811: list.add(PropertyConverter.toFloat(interpolate(it
0812: .next())));
0813: }
0814: } else {
0815: try {
0816: // attempt to convert a single value
0817: list = new ArrayList();
0818: list.add(PropertyConverter.toFloat(interpolate(value)));
0819: } catch (ConversionException e) {
0820: throw new ConversionException('\'' + key
0821: + "' doesn't map to a list of floats", e);
0822: }
0823: }
0824:
0825: return list;
0826: }
0827:
0828: /**
0829: * Get an array of float primitives associated with the given
0830: * configuration key. If the key doesn't map to an existing object
0831: * an empty array is returned.
0832: *
0833: * @param key The configuration key.
0834: * @return The associated float array if the key is found.
0835: *
0836: * @throws ConversionException is thrown if the key maps to an
0837: * object that is not a list of floats.
0838: */
0839: public float[] getFloatArray(String key) {
0840: return getFloatArray(key, new float[0]);
0841: }
0842:
0843: /**
0844: * Get an array of float primitives associated with the given
0845: * configuration key. If the key doesn't map to an existing object
0846: * an empty array is returned.
0847: *
0848: * @param key The configuration key.
0849: * @param defaultValue the default value, which will be returned if the property is not found
0850: * @return The associated float array if the key is found.
0851: *
0852: * @throws ConversionException is thrown if the key maps to an
0853: * object that is not a list of floats.
0854: */
0855: public float[] getFloatArray(String key, float[] defaultValue) {
0856: Object value = getProperty(key);
0857:
0858: float[] array;
0859:
0860: if (value == null
0861: || (value instanceof String && StringUtils
0862: .isEmpty((String) value))) {
0863: array = defaultValue;
0864: } else if (value instanceof float[]) {
0865: array = (float[]) value;
0866: } else if (value instanceof Float[]) {
0867: array = ArrayUtils.toPrimitive((Float[]) value);
0868: } else if (value instanceof Collection) {
0869: Collection values = (Collection) value;
0870: array = new float[values.size()];
0871:
0872: int i = 0;
0873: Iterator it = values.iterator();
0874: while (it.hasNext()) {
0875: array[i++] = PropertyConverter.toFloat(
0876: interpolate(it.next())).floatValue();
0877: }
0878: } else {
0879: try {
0880: // attempt to convert a single value
0881: array = new float[1];
0882: array[0] = PropertyConverter
0883: .toFloat(interpolate(value)).floatValue();
0884: } catch (ConversionException e) {
0885: throw new ConversionException('\'' + key
0886: + "' doesn't map to a list of floats", e);
0887: }
0888: }
0889:
0890: return array;
0891: }
0892:
0893: /**
0894: * Get a list of Double objects associated with the given
0895: * configuration key. If the key doesn't map to an existing object
0896: * an empty list is returned.
0897: *
0898: * @param key The configuration key.
0899: * @return The associated Double list if the key is found.
0900: *
0901: * @throws ConversionException is thrown if the key maps to an
0902: * object that is not a list of doubles.
0903: */
0904: public List getDoubleList(String key) {
0905: return getDoubleList(key, new ArrayList());
0906: }
0907:
0908: /**
0909: * Get a list of Double objects associated with the given
0910: * configuration key. If the key doesn't map to an existing object,
0911: * the default value is returned.
0912: *
0913: * @param key The configuration key.
0914: * @param defaultValue The default value.
0915: * @return The associated List of Doubles.
0916: *
0917: * @throws ConversionException is thrown if the key maps to an
0918: * object that is not a list of doubles.
0919: */
0920: public List getDoubleList(String key, List defaultValue) {
0921: Object value = getProperty(key);
0922:
0923: List list;
0924:
0925: if (value == null
0926: || (value instanceof String && StringUtils
0927: .isEmpty((String) value))) {
0928: list = defaultValue;
0929: } else if (value instanceof double[]) {
0930: list = new ArrayList();
0931: CollectionUtils.addAll(list, ArrayUtils
0932: .toObject((double[]) value));
0933: } else if (value instanceof Double[]) {
0934: list = new ArrayList();
0935: CollectionUtils.addAll(list, (Double[]) value);
0936: } else if (value instanceof Collection) {
0937: Collection values = (Collection) value;
0938: list = new ArrayList();
0939:
0940: Iterator it = values.iterator();
0941: while (it.hasNext()) {
0942: list.add(PropertyConverter.toDouble(interpolate(it
0943: .next())));
0944: }
0945: } else {
0946: try {
0947: // attempt to convert a single value
0948: list = new ArrayList();
0949: list
0950: .add(PropertyConverter
0951: .toDouble(interpolate(value)));
0952: } catch (ConversionException e) {
0953: throw new ConversionException('\'' + key
0954: + "' doesn't map to a list of doubles", e);
0955: }
0956: }
0957:
0958: return list;
0959: }
0960:
0961: /**
0962: * Get an array of double primitives associated with the given
0963: * configuration key. If the key doesn't map to an existing object
0964: * an empty array is returned.
0965: *
0966: * @param key The configuration key.
0967: * @return The associated double array if the key is found.
0968: *
0969: * @throws ConversionException is thrown if the key maps to an
0970: * object that is not a list of doubles.
0971: */
0972: public double[] getDoubleArray(String key) {
0973: return getDoubleArray(key, new double[0]);
0974: }
0975:
0976: /**
0977: * Get an array of double primitives associated with the given
0978: * configuration key. If the key doesn't map to an existing object
0979: * an empty array is returned.
0980: *
0981: * @param key The configuration key.
0982: * @param defaultValue the default value, which will be returned if the property is not found
0983: * @return The associated double array if the key is found.
0984: *
0985: * @throws ConversionException is thrown if the key maps to an
0986: * object that is not a list of doubles.
0987: */
0988: public double[] getDoubleArray(String key, double[] defaultValue) {
0989: Object value = getProperty(key);
0990:
0991: double[] array;
0992:
0993: if (value == null
0994: || (value instanceof String && StringUtils
0995: .isEmpty((String) value))) {
0996: array = defaultValue;
0997: } else if (value instanceof double[]) {
0998: array = (double[]) value;
0999: } else if (value instanceof Double[]) {
1000: array = ArrayUtils.toPrimitive((Double[]) value);
1001: } else if (value instanceof Collection) {
1002: Collection values = (Collection) value;
1003: array = new double[values.size()];
1004:
1005: int i = 0;
1006: Iterator it = values.iterator();
1007: while (it.hasNext()) {
1008: array[i++] = PropertyConverter.toDouble(
1009: interpolate(it.next())).doubleValue();
1010: }
1011: } else {
1012: try {
1013: // attempt to convert a single value
1014: array = new double[1];
1015: array[0] = PropertyConverter.toDouble(
1016: interpolate(value)).doubleValue();
1017: } catch (ConversionException e) {
1018: throw new ConversionException('\'' + key
1019: + "' doesn't map to a list of doubles", e);
1020: }
1021: }
1022:
1023: return array;
1024: }
1025:
1026: /**
1027: * Get a list of BigIntegers associated with the given configuration key.
1028: * If the key doesn't map to an existing object an empty list is returned.
1029: *
1030: * @param key The configuration key.
1031: * @return The associated BigInteger list if the key is found.
1032: *
1033: * @throws ConversionException is thrown if the key maps to an
1034: * object that is not a list of BigIntegers.
1035: */
1036: public List getBigIntegerList(String key) {
1037: return getBigIntegerList(key, new ArrayList());
1038: }
1039:
1040: /**
1041: * Get a list of BigIntegers associated with the given configuration key.
1042: * If the key doesn't map to an existing object, the default value is
1043: * returned.
1044: *
1045: * @param key The configuration key.
1046: * @param defaultValue The default value.
1047: * @return The associated List of BigIntegers.
1048: *
1049: * @throws ConversionException is thrown if the key maps to an
1050: * object that is not a list of BigIntegers.
1051: */
1052: public List getBigIntegerList(String key, List defaultValue) {
1053: Object value = getProperty(key);
1054:
1055: List list;
1056:
1057: if (value == null
1058: || (value instanceof String && StringUtils
1059: .isEmpty((String) value))) {
1060: list = defaultValue;
1061: } else if (value instanceof BigInteger[]) {
1062: list = new ArrayList();
1063: CollectionUtils.addAll(list, (BigInteger[]) value);
1064: } else if (value instanceof Collection) {
1065: Collection values = (Collection) value;
1066: list = new ArrayList();
1067:
1068: Iterator it = values.iterator();
1069: while (it.hasNext()) {
1070: list.add(PropertyConverter.toBigInteger(interpolate(it
1071: .next())));
1072: }
1073: } else {
1074: try {
1075: // attempt to convert a single value
1076: list = new ArrayList();
1077: list.add(PropertyConverter
1078: .toBigInteger(interpolate(value)));
1079: } catch (ConversionException e) {
1080: throw new ConversionException('\'' + key
1081: + "' doesn't map to a list of big integers", e);
1082: }
1083: }
1084:
1085: return list;
1086: }
1087:
1088: /**
1089: * Get an array of BigIntegers associated with the given
1090: * configuration key. If the key doesn't map to an existing object
1091: * an empty array is returned.
1092: *
1093: * @param key The configuration key.
1094: * @return The associated BigInteger array if the key is found.
1095: *
1096: * @throws ConversionException is thrown if the key maps to an
1097: * object that is not a list of BigIntegers.
1098: */
1099: public BigInteger[] getBigIntegerArray(String key) {
1100: return getBigIntegerArray(key, new BigInteger[0]);
1101: }
1102:
1103: /**
1104: * Get an array of BigIntegers associated with the given
1105: * configuration key. If the key doesn't map to an existing object
1106: * an empty array is returned.
1107: *
1108: * @param key The configuration key.
1109: * @param defaultValue the default value, which will be returned if the property is not found
1110: * @return The associated BigInteger array if the key is found.
1111: *
1112: * @throws ConversionException is thrown if the key maps to an
1113: * object that is not a list of BigIntegers.
1114: */
1115: public BigInteger[] getBigIntegerArray(String key,
1116: BigInteger[] defaultValue) {
1117: List list = getBigIntegerList(key);
1118: if (list.isEmpty()) {
1119: return defaultValue;
1120: } else {
1121: return (BigInteger[]) list.toArray(new BigInteger[list
1122: .size()]);
1123: }
1124: }
1125:
1126: /**
1127: * Get a list of BigDecimals associated with the given configuration key.
1128: * If the key doesn't map to an existing object an empty list is returned.
1129: *
1130: * @param key The configuration key.
1131: * @return The associated BigDecimal list if the key is found.
1132: *
1133: * @throws ConversionException is thrown if the key maps to an
1134: * object that is not a list of BigDecimals.
1135: */
1136: public List getBigDecimalList(String key) {
1137: return getBigDecimalList(key, new ArrayList());
1138: }
1139:
1140: /**
1141: * Get a list of BigDecimals associated with the given configuration key.
1142: * If the key doesn't map to an existing object, the default value is
1143: * returned.
1144: *
1145: * @param key The configuration key.
1146: * @param defaultValue The default value.
1147: * @return The associated List of BigDecimals.
1148: *
1149: * @throws ConversionException is thrown if the key maps to an
1150: * object that is not a list of BigDecimals.
1151: */
1152: public List getBigDecimalList(String key, List defaultValue) {
1153: Object value = getProperty(key);
1154:
1155: List list;
1156:
1157: if (value == null
1158: || (value instanceof String && StringUtils
1159: .isEmpty((String) value))) {
1160: list = defaultValue;
1161: } else if (value instanceof BigDecimal[]) {
1162: list = new ArrayList();
1163: CollectionUtils.addAll(list, (BigDecimal[]) value);
1164: } else if (value instanceof Collection) {
1165: Collection values = (Collection) value;
1166: list = new ArrayList();
1167:
1168: Iterator it = values.iterator();
1169: while (it.hasNext()) {
1170: list.add(PropertyConverter.toBigDecimal(interpolate(it
1171: .next())));
1172: }
1173: } else {
1174: try {
1175: // attempt to convert a single value
1176: list = new ArrayList();
1177: list.add(PropertyConverter
1178: .toBigDecimal(interpolate(value)));
1179: } catch (ConversionException e) {
1180: throw new ConversionException('\'' + key
1181: + "' doesn't map to a list of big decimals", e);
1182: }
1183: }
1184:
1185: return list;
1186: }
1187:
1188: /**
1189: * Get an array of BigDecimals associated with the given
1190: * configuration key. If the key doesn't map to an existing object
1191: * an empty array is returned.
1192: *
1193: * @param key The configuration key.
1194: * @return The associated BigDecimal array if the key is found.
1195: *
1196: * @throws ConversionException is thrown if the key maps to an
1197: * object that is not a list of BigDecimals.
1198: */
1199: public BigDecimal[] getBigDecimalArray(String key) {
1200: return getBigDecimalArray(key, new BigDecimal[0]);
1201: }
1202:
1203: /**
1204: * Get an array of BigDecimals associated with the given
1205: * configuration key. If the key doesn't map to an existing object
1206: * an empty array is returned.
1207: *
1208: * @param key The configuration key.
1209: * @param defaultValue the default value, which will be returned if the property is not found
1210: * @return The associated BigDecimal array if the key is found.
1211: *
1212: * @throws ConversionException is thrown if the key maps to an
1213: * object that is not a list of BigDecimals.
1214: */
1215: public BigDecimal[] getBigDecimalArray(String key,
1216: BigDecimal[] defaultValue) {
1217: List list = getBigDecimalList(key);
1218: if (list.isEmpty()) {
1219: return defaultValue;
1220: } else {
1221: return (BigDecimal[]) list.toArray(new BigDecimal[list
1222: .size()]);
1223: }
1224: }
1225:
1226: /**
1227: * Get an URL associated with the given configuration key.
1228: *
1229: * @param key The configuration key.
1230: * @return The associated URL.
1231: *
1232: * @throws ConversionException is thrown if the key maps to an
1233: * object that is not an URL.
1234: */
1235: public URL getURL(String key) {
1236: return getURL(key, null);
1237: }
1238:
1239: /**
1240: * Get an URL associated with the given configuration key.
1241: * If the key doesn't map to an existing object, the default value
1242: * is returned.
1243: *
1244: * @param key The configuration key.
1245: * @param defaultValue The default value.
1246: * @return The associated URL.
1247: *
1248: * @throws ConversionException is thrown if the key maps to an
1249: * object that is not an URL.
1250: */
1251: public URL getURL(String key, URL defaultValue) {
1252: Object value = resolveContainerStore(key);
1253:
1254: if (value == null) {
1255: return defaultValue;
1256: } else {
1257: try {
1258: return PropertyConverter.toURL(interpolate(value));
1259: } catch (ConversionException e) {
1260: throw new ConversionException('\'' + key
1261: + "' doesn't map to an URL", e);
1262: }
1263: }
1264: }
1265:
1266: /**
1267: * Get a list of URLs associated with the given configuration key.
1268: * If the key doesn't map to an existing object an empty list is returned.
1269: *
1270: * @param key The configuration key.
1271: * @return The associated URL list if the key is found.
1272: *
1273: * @throws ConversionException is thrown if the key maps to an
1274: * object that is not a list of URLs.
1275: */
1276: public List getURLList(String key) {
1277: return getURLList(key, new ArrayList());
1278: }
1279:
1280: /**
1281: * Get a list of URLs associated with the given configuration key.
1282: * If the key doesn't map to an existing object, the default value is
1283: * returned.
1284: *
1285: * @param key The configuration key.
1286: * @param defaultValue The default value.
1287: * @return The associated List of URLs.
1288: *
1289: * @throws ConversionException is thrown if the key maps to an
1290: * object that is not a list of URLs.
1291: */
1292: public List getURLList(String key, List defaultValue) {
1293: Object value = getProperty(key);
1294:
1295: List list;
1296:
1297: if (value == null
1298: || (value instanceof String && StringUtils
1299: .isEmpty((String) value))) {
1300: list = defaultValue;
1301: } else if (value instanceof URL[]) {
1302: list = new ArrayList();
1303: CollectionUtils.addAll(list, (URL[]) value);
1304: } else if (value instanceof Collection) {
1305: Collection values = (Collection) value;
1306: list = new ArrayList();
1307:
1308: Iterator it = values.iterator();
1309: while (it.hasNext()) {
1310: list.add(PropertyConverter
1311: .toURL(interpolate(it.next())));
1312: }
1313: } else {
1314: try {
1315: // attempt to convert a single value
1316: list = new ArrayList();
1317: list.add(PropertyConverter.toURL(interpolate(value)));
1318: } catch (ConversionException e) {
1319: throw new ConversionException('\'' + key
1320: + "' doesn't map to a list of URLs", e);
1321: }
1322: }
1323:
1324: return list;
1325: }
1326:
1327: /**
1328: * Get an array of URLs associated with the given configuration key.
1329: * If the key doesn't map to an existing object an empty array is returned.
1330: *
1331: * @param key The configuration key.
1332: * @return The associated URL array if the key is found.
1333: *
1334: * @throws ConversionException is thrown if the key maps to an
1335: * object that is not a list of URLs.
1336: */
1337: public URL[] getURLArray(String key) {
1338: return getURLArray(key, new URL[0]);
1339: }
1340:
1341: /**
1342: * Get an array of URLs associated with the given configuration key.
1343: * If the key doesn't map to an existing object an empty array is returned.
1344: *
1345: * @param key The configuration key.
1346: * @param defaultValue the default value, which will be returned if the property is not found
1347: * @return The associated URL array if the key is found.
1348: *
1349: * @throws ConversionException is thrown if the key maps to an
1350: * object that is not a list of URLs.
1351: */
1352: public URL[] getURLArray(String key, URL[] defaultValue) {
1353: List list = getURLList(key);
1354: if (list.isEmpty()) {
1355: return defaultValue;
1356: } else {
1357: return (URL[]) list.toArray(new URL[list.size()]);
1358: }
1359: }
1360:
1361: /**
1362: * Get a Date associated with the given configuration key. If the property
1363: * is a String, it will be parsed with the format defined by the user in
1364: * the {@link #DATE_FORMAT_KEY} property, or if it's not defined with the
1365: * {@link #DEFAULT_DATE_FORMAT} pattern.
1366: *
1367: * @param key The configuration key.
1368: * @return The associated Date.
1369: *
1370: * @throws ConversionException is thrown if the key maps to an
1371: * object that is not a Date.
1372: */
1373: public Date getDate(String key) {
1374: return getDate(key, getDefaultDateFormat());
1375: }
1376:
1377: /**
1378: * Get a Date associated with the given configuration key. If the property
1379: * is a String, it will be parsed with the specified format pattern.
1380: *
1381: * @param key The configuration key.
1382: * @param format The non-localized {@link java.text.DateFormat} pattern.
1383: * @return The associated Date
1384: *
1385: * @throws ConversionException is thrown if the key maps to an
1386: * object that is not a Date.
1387: */
1388: public Date getDate(String key, String format) {
1389: return getDate(key, null, format);
1390: }
1391:
1392: /**
1393: * Get a Date associated with the given configuration key. If the property
1394: * is a String, it will be parsed with the format defined by the user in
1395: * the {@link #DATE_FORMAT_KEY} property, or if it's not defined with the
1396: * {@link #DEFAULT_DATE_FORMAT} pattern. If the key doesn't map to an
1397: * existing object, the default value is returned.
1398: *
1399: * @param key The configuration key.
1400: * @param defaultValue The default value.
1401: * @return The associated Date.
1402: *
1403: * @throws ConversionException is thrown if the key maps to an
1404: * object that is not a Date.
1405: */
1406: public Date getDate(String key, Date defaultValue) {
1407: return getDate(key, defaultValue, getDefaultDateFormat());
1408: }
1409:
1410: /**
1411: * Get a Date associated with the given configuration key. If the property
1412: * is a String, it will be parsed with the specified format pattern.
1413: * If the key doesn't map to an existing object, the default value
1414: * is returned.
1415: *
1416: * @param key The configuration key.
1417: * @param defaultValue The default value.
1418: * @param format The non-localized {@link java.text.DateFormat} pattern.
1419: * @return The associated Date.
1420: *
1421: * @throws ConversionException is thrown if the key maps to an
1422: * object that is not a Date.
1423: */
1424: public Date getDate(String key, Date defaultValue, String format) {
1425: Object value = resolveContainerStore(key);
1426:
1427: if (value == null) {
1428: return defaultValue;
1429: } else {
1430: try {
1431: return PropertyConverter.toDate(interpolate(value),
1432: format);
1433: } catch (ConversionException e) {
1434: throw new ConversionException('\'' + key
1435: + "' doesn't map to a Date", e);
1436: }
1437: }
1438: }
1439:
1440: /**
1441: * Get a list of Dates associated with the given configuration key.
1442: * If the property is a list of Strings, they will be parsed with the
1443: * format defined by the user in the {@link #DATE_FORMAT_KEY} property,
1444: * or if it's not defined with the {@link #DEFAULT_DATE_FORMAT} pattern.
1445: * If the key doesn't map to an existing object an empty list is returned.
1446: *
1447: * @param key The configuration key.
1448: * @return The associated Date list if the key is found.
1449: *
1450: * @throws ConversionException is thrown if the key maps to an
1451: * object that is not a list of Dates.
1452: */
1453: public List getDateList(String key) {
1454: return getDateList(key, new ArrayList());
1455: }
1456:
1457: /**
1458: * Get a list of Dates associated with the given configuration key.
1459: * If the property is a list of Strings, they will be parsed with the
1460: * specified format pattern. If the key doesn't map to an existing object
1461: * an empty list is returned.
1462: *
1463: * @param key The configuration key.
1464: * @param format The non-localized {@link java.text.DateFormat} pattern.
1465: * @return The associated Date list if the key is found.
1466: *
1467: * @throws ConversionException is thrown if the key maps to an
1468: * object that is not a list of Dates.
1469: */
1470: public List getDateList(String key, String format) {
1471: return getDateList(key, new ArrayList(), format);
1472: }
1473:
1474: /**
1475: * Get a list of Dates associated with the given configuration key.
1476: * If the property is a list of Strings, they will be parsed with the
1477: * format defined by the user in the {@link #DATE_FORMAT_KEY} property,
1478: * or if it's not defined with the {@link #DEFAULT_DATE_FORMAT} pattern.
1479: * If the key doesn't map to an existing object, the default value is
1480: * returned.
1481: *
1482: * @param key The configuration key.
1483: * @param defaultValue The default value.
1484: * @return The associated Date list if the key is found.
1485: *
1486: * @throws ConversionException is thrown if the key maps to an
1487: * object that is not a list of Dates.
1488: */
1489: public List getDateList(String key, List defaultValue) {
1490: return getDateList(key, defaultValue, getDefaultDateFormat());
1491: }
1492:
1493: /**
1494: * Get a list of Dates associated with the given configuration key.
1495: * If the property is a list of Strings, they will be parsed with the
1496: * specified format pattern. If the key doesn't map to an existing object,
1497: * the default value is returned.
1498: *
1499: * @param key The configuration key.
1500: * @param defaultValue The default value.
1501: * @param format The non-localized {@link java.text.DateFormat} pattern.
1502: * @return The associated Date list if the key is found.
1503: *
1504: * @throws ConversionException is thrown if the key maps to an
1505: * object that is not a list of Dates.
1506: */
1507: public List getDateList(String key, List defaultValue, String format) {
1508: Object value = getProperty(key);
1509:
1510: List list;
1511:
1512: if (value == null
1513: || (value instanceof String && StringUtils
1514: .isEmpty((String) value))) {
1515: list = defaultValue;
1516: } else if (value instanceof Date[]) {
1517: list = new ArrayList();
1518: CollectionUtils.addAll(list, (Date[]) value);
1519: } else if (value instanceof Calendar[]) {
1520: list = new ArrayList();
1521: Calendar[] values = (Calendar[]) value;
1522:
1523: for (int i = 0; i < values.length; i++) {
1524: list.add(values[i].getTime());
1525: }
1526: } else if (value instanceof Collection) {
1527: Collection values = (Collection) value;
1528: list = new ArrayList();
1529:
1530: Iterator it = values.iterator();
1531: while (it.hasNext()) {
1532: list.add(PropertyConverter.toDate(
1533: interpolate(it.next()), format));
1534: }
1535: } else {
1536: try {
1537: // attempt to convert a single value
1538: list = new ArrayList();
1539: list.add(PropertyConverter.toDate(interpolate(value),
1540: format));
1541: } catch (ConversionException e) {
1542: throw new ConversionException('\'' + key
1543: + "' doesn't map to a list of Dates", e);
1544: }
1545: }
1546:
1547: return list;
1548: }
1549:
1550: /**
1551: * Get an array of Dates associated with the given configuration key.
1552: * If the property is a list of Strings, they will be parsed with the
1553: * format defined by the user in the {@link #DATE_FORMAT_KEY} property,
1554: * or if it's not defined with the {@link #DEFAULT_DATE_FORMAT} pattern.
1555: * If the key doesn't map to an existing object an empty array is returned.
1556: *
1557: * @param key The configuration key.
1558: * @return The associated Date array if the key is found.
1559: *
1560: * @throws ConversionException is thrown if the key maps to an
1561: * object that is not a list of Dates.
1562: */
1563: public Date[] getDateArray(String key) {
1564: return getDateArray(key, new Date[0]);
1565: }
1566:
1567: /**
1568: * Get an array of Dates associated with the given configuration key.
1569: * If the property is a list of Strings, they will be parsed with the
1570: * specified format pattern. If the key doesn't map to an existing object
1571: * an empty array is returned.
1572: *
1573: * @param key The configuration key.
1574: * @param format The non-localized {@link java.text.DateFormat} pattern.
1575: * @return The associated Date array if the key is found.
1576: *
1577: * @throws ConversionException is thrown if the key maps to an
1578: * object that is not a list of Dates.
1579: */
1580: public Date[] getDateArray(String key, String format) {
1581: return getDateArray(key, new Date[0], format);
1582: }
1583:
1584: /**
1585: * Get an array of Dates associated with the given configuration key.
1586: * If the property is a list of Strings, they will be parsed with the
1587: * format defined by the user in the {@link #DATE_FORMAT_KEY} property,
1588: * or if it's not defined with the {@link #DEFAULT_DATE_FORMAT} pattern.
1589: * If the key doesn't map to an existing object an empty array is returned.
1590: *
1591: * @param key The configuration key.
1592: * @param defaultValue the default value, which will be returned if the property is not found
1593: * @return The associated Date array if the key is found.
1594: *
1595: * @throws ConversionException is thrown if the key maps to an
1596: * object that is not a list of Dates.
1597: */
1598: public Date[] getDateArray(String key, Date[] defaultValue) {
1599: return getDateArray(key, defaultValue, getDefaultDateFormat());
1600: }
1601:
1602: /**
1603: * Get an array of Dates associated with the given configuration key.
1604: * If the property is a list of Strings, they will be parsed with the
1605: * specified format pattern. If the key doesn't map to an existing object,
1606: * the default value is returned.
1607: *
1608: * @param key The configuration key.
1609: * @param defaultValue The default value.
1610: * @param format The non-localized {@link java.text.DateFormat} pattern.
1611: * @return The associated Date array if the key is found.
1612: *
1613: * @throws ConversionException is thrown if the key maps to an
1614: * object that is not a list of Dates.
1615: */
1616: public Date[] getDateArray(String key, Date[] defaultValue,
1617: String format) {
1618: List list = getDateList(key, format);
1619: if (list.isEmpty()) {
1620: return defaultValue;
1621: } else {
1622: return (Date[]) list.toArray(new Date[list.size()]);
1623: }
1624: }
1625:
1626: /**
1627: * Get a Calendar associated with the given configuration key. If the
1628: * property is a String, it will be parsed with the format defined by the
1629: * user in the {@link #DATE_FORMAT_KEY} property, or if it's not defined
1630: * with the {@link #DEFAULT_DATE_FORMAT} pattern.
1631: *
1632: * @param key The configuration key.
1633: * @return The associated Calendar.
1634: *
1635: * @throws ConversionException is thrown if the key maps to an
1636: * object that is not a Calendar.
1637: */
1638: public Calendar getCalendar(String key) {
1639: return getCalendar(key, getDefaultDateFormat());
1640: }
1641:
1642: /**
1643: * Get a Calendar associated with the given configuration key. If the
1644: * property is a String, it will be parsed with the specified format
1645: * pattern.
1646: *
1647: * @param key The configuration key.
1648: * @param format The non-localized {@link java.text.DateFormat} pattern.
1649: * @return The associated Calendar
1650: *
1651: * @throws ConversionException is thrown if the key maps to an
1652: * object that is not a Calendar.
1653: */
1654: public Calendar getCalendar(String key, String format) {
1655: return getCalendar(key, null, format);
1656: }
1657:
1658: /**
1659: * Get a Calendar associated with the given configuration key. If the
1660: * property is a String, it will be parsed with the format defined by the
1661: * user in the {@link #DATE_FORMAT_KEY} property, or if it's not defined
1662: * with the {@link #DEFAULT_DATE_FORMAT} pattern. If the key doesn't map
1663: * to an existing object, the default value is returned.
1664: *
1665: * @param key The configuration key.
1666: * @param defaultValue The default value.
1667: * @return The associated Calendar.
1668: *
1669: * @throws ConversionException is thrown if the key maps to an
1670: * object that is not a Calendar.
1671: */
1672: public Calendar getCalendar(String key, Calendar defaultValue) {
1673: return getCalendar(key, defaultValue, getDefaultDateFormat());
1674: }
1675:
1676: /**
1677: * Get a Calendar associated with the given configuration key. If the
1678: * property is a String, it will be parsed with the specified format
1679: * pattern. If the key doesn't map to an existing object, the default
1680: * value is returned.
1681: *
1682: * @param key The configuration key.
1683: * @param defaultValue The default value.
1684: * @param format The non-localized {@link java.text.DateFormat} pattern.
1685: * @return The associated Calendar.
1686: *
1687: * @throws ConversionException is thrown if the key maps to an
1688: * object that is not a Calendar.
1689: */
1690: public Calendar getCalendar(String key, Calendar defaultValue,
1691: String format) {
1692: Object value = resolveContainerStore(key);
1693:
1694: if (value == null) {
1695: return defaultValue;
1696: } else {
1697: try {
1698: return PropertyConverter.toCalendar(interpolate(value),
1699: format);
1700: } catch (ConversionException e) {
1701: throw new ConversionException('\'' + key
1702: + "' doesn't map to a Calendar", e);
1703: }
1704: }
1705: }
1706:
1707: /**
1708: * Get a list of Calendars associated with the given configuration key.
1709: * If the property is a list of Strings, they will be parsed with the
1710: * format defined by the user in the {@link #DATE_FORMAT_KEY} property,
1711: * or if it's not defined with the {@link #DEFAULT_DATE_FORMAT} pattern.
1712: * If the key doesn't map to an existing object an empty list is returned.
1713: *
1714: * @param key The configuration key.
1715: * @return The associated Calendar list if the key is found.
1716: *
1717: * @throws ConversionException is thrown if the key maps to an
1718: * object that is not a list of Calendars.
1719: */
1720: public List getCalendarList(String key) {
1721: return getCalendarList(key, new ArrayList());
1722: }
1723:
1724: /**
1725: * Get a list of Calendars associated with the given configuration key.
1726: * If the property is a list of Strings, they will be parsed with the
1727: * specified format pattern. If the key doesn't map to an existing object
1728: * an empty list is returned.
1729: *
1730: * @param key The configuration key.
1731: * @param format The non-localized {@link java.text.DateFormat} pattern.
1732: * @return The associated Calendar list if the key is found.
1733: *
1734: * @throws ConversionException is thrown if the key maps to an
1735: * object that is not a list of Calendars.
1736: */
1737: public List getCalendarList(String key, String format) {
1738: return getCalendarList(key, new ArrayList(), format);
1739: }
1740:
1741: /**
1742: * Get a list of Calendars associated with the given configuration key.
1743: * If the property is a list of Strings, they will be parsed with the
1744: * format defined by the user in the {@link #DATE_FORMAT_KEY} property,
1745: * or if it's not defined with the {@link #DEFAULT_DATE_FORMAT} pattern.
1746: * If the key doesn't map to an existing object, the default value is
1747: * returned.
1748: *
1749: * @param key The configuration key.
1750: * @param defaultValue The default value.
1751: * @return The associated Calendar list if the key is found.
1752: *
1753: * @throws ConversionException is thrown if the key maps to an
1754: * object that is not a list of Calendars.
1755: */
1756: public List getCalendarList(String key, List defaultValue) {
1757: return getCalendarList(key, defaultValue,
1758: getDefaultDateFormat());
1759: }
1760:
1761: /**
1762: * Get a list of Calendars associated with the given configuration key.
1763: * If the property is a list of Strings, they will be parsed with the
1764: * specified format pattern. If the key doesn't map to an existing object,
1765: * the default value is returned.
1766: *
1767: * @param key The configuration key.
1768: * @param defaultValue The default value.
1769: * @param format The non-localized {@link java.text.DateFormat} pattern.
1770: * @return The associated Calendar list if the key is found.
1771: *
1772: * @throws ConversionException is thrown if the key maps to an
1773: * object that is not a list of Calendars.
1774: */
1775: public List getCalendarList(String key, List defaultValue,
1776: String format) {
1777: Object value = getProperty(key);
1778:
1779: List list;
1780:
1781: if (value == null
1782: || (value instanceof String && StringUtils
1783: .isEmpty((String) value))) {
1784: list = defaultValue;
1785: } else if (value instanceof Calendar[]) {
1786: list = new ArrayList();
1787: CollectionUtils.addAll(list, (Calendar[]) value);
1788: } else if (value instanceof Date[]) {
1789: list = new ArrayList();
1790: Date[] values = (Date[]) value;
1791:
1792: for (int i = 0; i < values.length; i++) {
1793: Calendar calendar = Calendar.getInstance();
1794: calendar.setTime(values[i]);
1795: list.add(calendar);
1796: }
1797: } else if (value instanceof Collection) {
1798: Collection values = (Collection) value;
1799: list = new ArrayList();
1800:
1801: Iterator it = values.iterator();
1802: while (it.hasNext()) {
1803: list.add(PropertyConverter.toCalendar(interpolate(it
1804: .next()), format));
1805: }
1806: } else {
1807: try {
1808: // attempt to convert a single value
1809: list = new ArrayList();
1810: list.add(PropertyConverter.toCalendar(
1811: interpolate(value), format));
1812: } catch (ConversionException e) {
1813: throw new ConversionException('\'' + key
1814: + "' doesn't map to a list of Calendars", e);
1815: }
1816: }
1817:
1818: return list;
1819: }
1820:
1821: /**
1822: * Get an array of Calendars associated with the given configuration key.
1823: * If the property is a list of Strings, they will be parsed with the
1824: * format defined by the user in the {@link #DATE_FORMAT_KEY} property,
1825: * or if it's not defined with the {@link #DEFAULT_DATE_FORMAT} pattern.
1826: * If the key doesn't map to an existing object an empty array is returned.
1827: *
1828: * @param key The configuration key.
1829: * @return The associated Calendar array if the key is found.
1830: *
1831: * @throws ConversionException is thrown if the key maps to an
1832: * object that is not a list of Calendars.
1833: */
1834: public Calendar[] getCalendarArray(String key) {
1835: return getCalendarArray(key, new Calendar[0]);
1836: }
1837:
1838: /**
1839: * Get an array of Calendars associated with the given configuration key.
1840: * If the property is a list of Strings, they will be parsed with the
1841: * specified format pattern. If the key doesn't map to an existing object
1842: * an empty array is returned.
1843: *
1844: * @param key The configuration key.
1845: * @param format The non-localized {@link java.text.DateFormat} pattern.
1846: * @return The associated Calendar array if the key is found.
1847: *
1848: * @throws ConversionException is thrown if the key maps to an
1849: * object that is not a list of Calendars.
1850: */
1851: public Calendar[] getCalendarArray(String key, String format) {
1852: return getCalendarArray(key, new Calendar[0], format);
1853: }
1854:
1855: /**
1856: * Get an array of Calendars associated with the given configuration key.
1857: * If the property is a list of Strings, they will be parsed with the
1858: * format defined by the user in the {@link #DATE_FORMAT_KEY} property,
1859: * or if it's not defined with the {@link #DEFAULT_DATE_FORMAT} pattern.
1860: * If the key doesn't map to an existing object an empty array is returned.
1861: *
1862: * @param key The configuration key.
1863: * @param defaultValue the default value, which will be returned if the property is not found
1864: * @return The associated Calendar array if the key is found.
1865: *
1866: * @throws ConversionException is thrown if the key maps to an
1867: * object that is not a list of Calendars.
1868: */
1869: public Calendar[] getCalendarArray(String key,
1870: Calendar[] defaultValue) {
1871: return getCalendarArray(key, defaultValue,
1872: getDefaultDateFormat());
1873: }
1874:
1875: /**
1876: * Get an array of Calendars associated with the given configuration key.
1877: * If the property is a list of Strings, they will be parsed with the
1878: * specified format pattern. If the key doesn't map to an existing object,
1879: * the default value is returned.
1880: *
1881: * @param key The configuration key.
1882: * @param defaultValue The default value.
1883: * @param format The non-localized {@link java.text.DateFormat} pattern.
1884: * @return The associated Calendar array if the key is found.
1885: *
1886: * @throws ConversionException is thrown if the key maps to an
1887: * object that is not a list of Calendars.
1888: */
1889: public Calendar[] getCalendarArray(String key,
1890: Calendar[] defaultValue, String format) {
1891: List list = getCalendarList(key, format);
1892: if (list.isEmpty()) {
1893: return defaultValue;
1894: } else {
1895: return (Calendar[]) list.toArray(new Calendar[list.size()]);
1896: }
1897: }
1898:
1899: /**
1900: * Returns the date format specified by the user in the DATE_FORMAT_KEY
1901: * property, or the default format otherwise.
1902: *
1903: * @return the default date format
1904: */
1905: private String getDefaultDateFormat() {
1906: return getString(DATE_FORMAT_KEY, DEFAULT_DATE_FORMAT);
1907: }
1908:
1909: /**
1910: * Get a Locale associated with the given configuration key.
1911: *
1912: * @param key The configuration key.
1913: * @return The associated Locale.
1914: *
1915: * @throws ConversionException is thrown if the key maps to an
1916: * object that is not a Locale.
1917: */
1918: public Locale getLocale(String key) {
1919: return getLocale(key, null);
1920: }
1921:
1922: /**
1923: * Get a Locale associated with the given configuration key.
1924: * If the key doesn't map to an existing object, the default value
1925: * is returned.
1926: *
1927: * @param key The configuration key.
1928: * @param defaultValue The default value.
1929: * @return The associated Locale.
1930: *
1931: * @throws ConversionException is thrown if the key maps to an
1932: * object that is not a Locale.
1933: */
1934: public Locale getLocale(String key, Locale defaultValue) {
1935: Object value = resolveContainerStore(key);
1936:
1937: if (value == null) {
1938: return defaultValue;
1939: } else {
1940: try {
1941: return PropertyConverter.toLocale(interpolate(value));
1942: } catch (ConversionException e) {
1943: throw new ConversionException('\'' + key
1944: + "' doesn't map to a Locale", e);
1945: }
1946: }
1947: }
1948:
1949: /**
1950: * Get a list of Locales associated with the given configuration key.
1951: * If the key doesn't map to an existing object an empty list is returned.
1952: *
1953: * @param key The configuration key.
1954: * @return The associated Locale list if the key is found.
1955: *
1956: * @throws ConversionException is thrown if the key maps to an
1957: * object that is not a list of Locales.
1958: */
1959: public List getLocaleList(String key) {
1960: return getLocaleList(key, new ArrayList());
1961: }
1962:
1963: /**
1964: * Get a list of Locales associated with the given configuration key.
1965: * If the key doesn't map to an existing object, the default value is
1966: * returned.
1967: *
1968: * @param key The configuration key.
1969: * @param defaultValue The default value.
1970: * @return The associated List of Locales.
1971: *
1972: * @throws ConversionException is thrown if the key maps to an
1973: * object that is not a list of Locales.
1974: */
1975: public List getLocaleList(String key, List defaultValue) {
1976: Object value = getProperty(key);
1977:
1978: List list;
1979:
1980: if (value == null
1981: || (value instanceof String && StringUtils
1982: .isEmpty((String) value))) {
1983: list = defaultValue;
1984: } else if (value instanceof Locale[]) {
1985: list = new ArrayList();
1986: CollectionUtils.addAll(list, (Locale[]) value);
1987: } else if (value instanceof Collection) {
1988: Collection values = (Collection) value;
1989: list = new ArrayList();
1990:
1991: Iterator it = values.iterator();
1992: while (it.hasNext()) {
1993: list.add(PropertyConverter.toLocale(interpolate(it
1994: .next())));
1995: }
1996: } else {
1997: try {
1998: // attempt to convert a single value
1999: list = new ArrayList();
2000: list
2001: .add(PropertyConverter
2002: .toLocale(interpolate(value)));
2003: } catch (ConversionException e) {
2004: throw new ConversionException('\'' + key
2005: + "' doesn't map to a list of Locales", e);
2006: }
2007: }
2008:
2009: return list;
2010: }
2011:
2012: /**
2013: * Get an array of Locales associated with the given
2014: * configuration key. If the key doesn't map to an existing object
2015: * an empty array is returned.
2016: *
2017: * @param key The configuration key.
2018: * @return The associated Locale array if the key is found.
2019: *
2020: * @throws ConversionException is thrown if the key maps to an
2021: * object that is not a list of Locales.
2022: */
2023: public Locale[] getLocaleArray(String key) {
2024: return getLocaleArray(key, new Locale[0]);
2025: }
2026:
2027: /**
2028: * Get an array of Locales associated with the given
2029: * configuration key. If the key doesn't map to an existing object
2030: * an empty array is returned.
2031: *
2032: * @param key The configuration key.
2033: * @param defaultValue the default value, which will be returned if the property is not found
2034: * @return The associated Locale array if the key is found.
2035: *
2036: * @throws ConversionException is thrown if the key maps to an
2037: * object that is not a list of Locales.
2038: */
2039: public Locale[] getLocaleArray(String key, Locale[] defaultValue) {
2040: List list = getLocaleList(key);
2041: if (list.isEmpty()) {
2042: return defaultValue;
2043: } else {
2044: return (Locale[]) list.toArray(new Locale[list.size()]);
2045: }
2046: }
2047:
2048: /**
2049: * Get a Color associated with the given configuration key.
2050: *
2051: * @param key The configuration key.
2052: * @return The associated Color.
2053: *
2054: * @throws ConversionException is thrown if the key maps to an
2055: * object that is not a Color.
2056: */
2057: public Color getColor(String key) {
2058: return getColor(key, null);
2059: }
2060:
2061: /**
2062: * Get a Color associated with the given configuration key.
2063: * If the key doesn't map to an existing object, the default value
2064: * is returned.
2065: *
2066: * @param key The configuration key.
2067: * @param defaultValue The default value.
2068: * @return The associated Color.
2069: *
2070: * @throws ConversionException is thrown if the key maps to an
2071: * object that is not a Color.
2072: */
2073: public Color getColor(String key, Color defaultValue) {
2074: Object value = resolveContainerStore(key);
2075:
2076: if (value == null) {
2077: return defaultValue;
2078: } else {
2079: try {
2080: return PropertyConverter.toColor(interpolate(value));
2081: } catch (ConversionException e) {
2082: throw new ConversionException('\'' + key
2083: + "' doesn't map to a Color", e);
2084: }
2085: }
2086: }
2087:
2088: /**
2089: * Get a list of Colors associated with the given configuration key.
2090: * If the key doesn't map to an existing object an empty list is returned.
2091: *
2092: * @param key The configuration key.
2093: * @return The associated Color list if the key is found.
2094: *
2095: * @throws ConversionException is thrown if the key maps to an
2096: * object that is not a list of Colors.
2097: */
2098: public List getColorList(String key) {
2099: return getColorList(key, new ArrayList());
2100: }
2101:
2102: /**
2103: * Get a list of Colors associated with the given configuration key.
2104: * If the key doesn't map to an existing object, the default value is
2105: * returned.
2106: *
2107: * @param key The configuration key.
2108: * @param defaultValue The default value.
2109: * @return The associated List of Colors.
2110: *
2111: * @throws ConversionException is thrown if the key maps to an
2112: * object that is not a list of Colors.
2113: */
2114: public List getColorList(String key, List defaultValue) {
2115: Object value = getProperty(key);
2116:
2117: List list;
2118:
2119: if (value == null
2120: || (value instanceof String && StringUtils
2121: .isEmpty((String) value))) {
2122: list = defaultValue;
2123: } else if (value instanceof Color[]) {
2124: list = new ArrayList();
2125: CollectionUtils.addAll(list, (Color[]) value);
2126: } else if (value instanceof Collection) {
2127: Collection values = (Collection) value;
2128: list = new ArrayList();
2129:
2130: Iterator it = values.iterator();
2131: while (it.hasNext()) {
2132: list.add(PropertyConverter.toColor(interpolate(it
2133: .next())));
2134: }
2135: } else {
2136: try {
2137: // attempt to convert a single value
2138: list = new ArrayList();
2139: list.add(PropertyConverter.toColor(interpolate(value)));
2140: } catch (ConversionException e) {
2141: throw new ConversionException('\'' + key
2142: + "' doesn't map to a list of Colors", e);
2143: }
2144: }
2145:
2146: return list;
2147: }
2148:
2149: /**
2150: * Get an array of Colors associated with the given
2151: * configuration key. If the key doesn't map to an existing object
2152: * an empty array is returned.
2153: *
2154: * @param key The configuration key.
2155: * @return The associated Color array if the key is found.
2156: *
2157: * @throws ConversionException is thrown if the key maps to an
2158: * object that is not a list of Colors.
2159: */
2160: public Color[] getColorArray(String key) {
2161: return getColorArray(key, new Color[0]);
2162: }
2163:
2164: /**
2165: * Get an array of Colors associated with the given
2166: * configuration key. If the key doesn't map to an existing object
2167: * an empty array is returned.
2168: *
2169: * @param key The configuration key.
2170: * @param defaultValue the default value, which will be returned if the property is not found
2171: * @return The associated Color array if the key is found.
2172: *
2173: * @throws ConversionException is thrown if the key maps to an
2174: * object that is not a list of Colors.
2175: */
2176: public Color[] getColorArray(String key, Color[] defaultValue) {
2177: List list = getColorList(key);
2178: if (list.isEmpty()) {
2179: return defaultValue;
2180: } else {
2181: return (Color[]) list.toArray(new Color[list.size()]);
2182: }
2183: }
2184:
2185: }
|