org.apache.commons.configuration

Class PropertyConverter

public final class PropertyConverter extends Object

A utility class to convert the configuration properties into any type.

Since: 1.1

Version: $Revision: 490321 $, $Date: 2006-12-26 17:19:07 +0100 (Di, 26 Dez 2006) $

Author: Emmanuel Bourg

Field Summary
static Class[]CONSTR_ARGS
Constant for the argument classes of the Number constructor that takes a String.
static StringHEX_PREFIX
Constant for the prefix of hex numbers.
static intHEX_RADIX
Constant for the radix of hex numbers.
static StringLIST_ESCAPE
Constant for the list delimiter escaping character.
Constructor Summary
PropertyConverter()
Private constructor prevents instances from being created.
Method Summary
static StringescapeDelimiters(String s, char delimiter)
Escapes the delimiters that might be contained in the given string.
static Objectinterpolate(Object value, AbstractConfiguration config)
Performs interpolation of the specified value.
static Listsplit(String s, char delimiter)
Split a string on the specified delimiter.
static BigDecimaltoBigDecimal(Object value)
Convert the specified object into a BigDecimal.
static BigIntegertoBigInteger(Object value)
Convert the specified object into a BigInteger.
static BooleantoBoolean(Object value)
Convert the specified object into a Boolean.
static BytetoByte(Object value)
Convert the specified object into a Byte.
static CalendartoCalendar(Object value, String format)
Convert the specified object into a Calendar.
static ColortoColor(Object value)
Convert the specified object into a Color.
static DatetoDate(Object value, String format)
Convert the specified object into a Date.
static DoubletoDouble(Object value)
Convert the specified object into a Double.
static FloattoFloat(Object value)
Convert the specified object into a Float.
static IntegertoInteger(Object value)
Convert the specified object into an Integer.
static IteratortoIterator(Object value, char delimiter)
Return an iterator over the simple values of a composite value.
static LocaletoLocale(Object value)
Convert the specified object into a Locale.
static LongtoLong(Object value)
Convert the specified object into a Long.
static NumbertoNumber(Object value, Class targetClass)
Tries to convert the specified object into a number object.
static ShorttoShort(Object value)
Convert the specified object into a Short.
static URLtoURL(Object value)
Convert the specified object into an URL.

Field Detail

CONSTR_ARGS

private static final Class[] CONSTR_ARGS
Constant for the argument classes of the Number constructor that takes a String.

HEX_PREFIX

private static final String HEX_PREFIX
Constant for the prefix of hex numbers.

HEX_RADIX

private static final int HEX_RADIX
Constant for the radix of hex numbers.

LIST_ESCAPE

static final String LIST_ESCAPE
Constant for the list delimiter escaping character.

Constructor Detail

PropertyConverter

private PropertyConverter()
Private constructor prevents instances from being created.

Method Detail

escapeDelimiters

public static String escapeDelimiters(String s, char delimiter)
Escapes the delimiters that might be contained in the given string. This method ensures that list delimiter characters that are part of a property's value are correctly escaped when a configuration is saved to a file. Otherwise when loaded again the property will be treated as a list property.

Parameters: s the string with the value delimiter the list delimiter to use

Returns: the correctly esaped string

interpolate

public static Object interpolate(Object value, AbstractConfiguration config)
Performs interpolation of the specified value. This method checks if the given value contains variables of the form ${...}. If this is the case, all occurrances will be substituted by their current values.

Parameters: value the value to be interpolated config the current configuration object

Returns: the interpolated value

split

public static List split(String s, char delimiter)
Split a string on the specified delimiter. To be removed when commons-lang has a better replacement available (Tokenizer?). todo: replace with a commons-lang equivalent

Parameters: s the string to split delimiter the delimiter

Returns: a list with the single tokens

toBigDecimal

public static BigDecimal toBigDecimal(Object value)
Convert the specified object into a BigDecimal.

Parameters: value the value to convert

Returns: the converted value

Throws: ConversionException thrown if the value cannot be converted to a BigDecimal

toBigInteger

public static BigInteger toBigInteger(Object value)
Convert the specified object into a BigInteger.

Parameters: value the value to convert

Returns: the converted value

Throws: ConversionException thrown if the value cannot be converted to a BigInteger

toBoolean

public static Boolean toBoolean(Object value)
Convert the specified object into a Boolean. Internally the org.apache.commons.lang.BooleanUtils class from the Commons Lang project is used to perform this conversion. This class accepts some more tokens for the boolean value of true, e.g. yes and on. Please refer to the documentation of this class for more details.

Parameters: value the value to convert

Returns: the converted value

Throws: ConversionException thrown if the value cannot be converted to a boolean

toByte

public static Byte toByte(Object value)
Convert the specified object into a Byte.

Parameters: value the value to convert

Returns: the converted value

Throws: ConversionException thrown if the value cannot be converted to a byte

toCalendar

public static Calendar toCalendar(Object value, String format)
Convert the specified object into a Calendar.

Parameters: value the value to convert format the DateFormat pattern to parse String values

Returns: the converted value

Throws: ConversionException thrown if the value cannot be converted to a Calendar

toColor

public static Color toColor(Object value)
Convert the specified object into a Color. If the value is a String, the format allowed is (#)?[0-9A-F]{6}([0-9A-F]{2})?. Examples:

Parameters: value the value to convert

Returns: the converted value

Throws: ConversionException thrown if the value cannot be converted to a Color

toDate

public static Date toDate(Object value, String format)
Convert the specified object into a Date.

Parameters: value the value to convert format the DateFormat pattern to parse String values

Returns: the converted value

Throws: ConversionException thrown if the value cannot be converted to a Calendar

toDouble

public static Double toDouble(Object value)
Convert the specified object into a Double.

Parameters: value the value to convert

Returns: the converted value

Throws: ConversionException thrown if the value cannot be converted to a Double

toFloat

public static Float toFloat(Object value)
Convert the specified object into a Float.

Parameters: value the value to convert

Returns: the converted value

Throws: ConversionException thrown if the value cannot be converted to a Float

toInteger

public static Integer toInteger(Object value)
Convert the specified object into an Integer.

Parameters: value the value to convert

Returns: the converted value

Throws: ConversionException thrown if the value cannot be converted to an integer

toIterator

public static Iterator toIterator(Object value, char delimiter)
Return an iterator over the simple values of a composite value. The value specified is handled depending on its type:

Parameters: value the value to "split" delimiter the delimiter for String values

Returns: an iterator for accessing the single values

toLocale

public static Locale toLocale(Object value)
Convert the specified object into a Locale.

Parameters: value the value to convert

Returns: the converted value

Throws: ConversionException thrown if the value cannot be converted to a Locale

toLong

public static Long toLong(Object value)
Convert the specified object into a Long.

Parameters: value the value to convert

Returns: the converted value

Throws: ConversionException thrown if the value cannot be converted to a Long

toNumber

static Number toNumber(Object value, Class targetClass)
Tries to convert the specified object into a number object. This method is used by the conversion methods for number types. Note that the return value is not in always of the specified target class, but only if a new object has to be created.

Parameters: value the value to be converted (must not be null) targetClass the target class of the conversion (must be derived from java.lang.Number)

Returns: the converted number

Throws: ConversionException if the object cannot be converted

toShort

public static Short toShort(Object value)
Convert the specified object into a Short.

Parameters: value the value to convert

Returns: the converted value

Throws: ConversionException thrown if the value cannot be converted to a short

toURL

public static URL toURL(Object value)
Convert the specified object into an URL.

Parameters: value the value to convert

Returns: the converted value

Throws: ConversionException thrown if the value cannot be converted to an URL