Class ConversionTool
java.lang.Object
org.apache.velocity.tools.generic.SafeConfig
org.apache.velocity.tools.generic.LocaleConfig
org.apache.velocity.tools.generic.ConversionTool
- Direct Known Subclasses:
ValueParser
Utility class for easy conversion of String values to richer types.
Template example(s): $convert.toNumber('12.6') -> 12.6 $convert.toInt('12.6') -> 12 $convert.toNumbers('12.6,42') -> [12.6, 42] Toolbox configuration: <tools> <toolbox scope="application"> <tool class="org.apache.velocity.tools.generic.ConversionTool" dateFormat="yyyy-MM-dd"/> </toolbox> </tools>
This comes in very handy when parsing anything.
- Since:
- VelocityTools 2.0
- Version:
- $Revision: 932578 $ $Date: 2007-02-26 11:24:39 -0800 (Mon, 26 Feb 2007) $
- Author:
- Nathan Bubna
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
private String
static final String
static final String
static final String
static final boolean
static final String
private String
static final String
static final String
private String
private boolean
Fields inherited from class org.apache.velocity.tools.generic.LocaleConfig
DEFAULT_LOCALE
Fields inherited from class org.apache.velocity.tools.generic.SafeConfig
LOCK_CONFIG_KEY, OLD_LOCK_CONFIG_KEY, SAFE_MODE_KEY
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected void
configure
(ValueParser values) Does the actual configuration.final String
final String
final String
final boolean
protected Boolean
parseBoolean
(String value) Converts a parameter value into aBoolean
Sub-classes can override to allow for customized boolean parsing.protected Locale
parseLocale
(String value) Converts a String value into a Locale.parseNumber
(String value) Converts an object to an instance ofNumber
using the format returned bygetNumberFormat()
and the defaultLocale
if the object is not already an instance of Number.parseNumber
(String value, Object locale) parseNumber
(String value, String format) Converts an object to an instance ofNumber
using the specified format and theLocale
returned byLocaleConfig.getLocale()
.parseNumber
(String value, String format, Object locale) protected String[]
parseStringList
(String value) Converts a single String value into an array of Strings by splitting it on the tool's set stringsDelimiter.protected final void
setDateFormat
(String format) protected final void
setNumberFormat
(String format) protected final void
setStringsDelimiter
(String stringsDelimiter) Sets the delimiter used for separating values in a single String value.protected final void
setStringsTrim
(boolean stringsTrim) Sets whether strings should be trimmed when separated from a delimited string value.Boolean[]
toBooleans
(Object value) Boolean[]
toBooleans
(Collection values) toCalendar
(Object value) Calendar[]
toCalendars
(Object value) Calendar[]
toCalendars
(Collection values) Date[]
Date[]
toDates
(Collection values) double[]
int[]
toIntegers
(Object value) int[]
Locale[]
Locale[]
toLocales
(Collection values) Number[]
Number[]
toNumbers
(Collection values) Converts objects to String in a more Tools-ish way than String.valueOf(Object), especially with nulls, Arrays and Collections.String[]
Methods inherited from class org.apache.velocity.tools.generic.LocaleConfig
getLocale, setLocale
Methods inherited from class org.apache.velocity.tools.generic.SafeConfig
configure, isConfigLocked, isSafeMode, setLockConfig, setSafeMode
-
Field Details
-
STRINGS_DELIMITER_FORMAT_KEY
- See Also:
-
STRINGS_TRIM_KEY
- See Also:
-
DATE_FORMAT_KEY
- See Also:
-
NUMBER_FORMAT_KEY
- See Also:
-
DEFAULT_STRINGS_DELIMITER
- See Also:
-
DEFAULT_STRINGS_TRIM
public static final boolean DEFAULT_STRINGS_TRIM- See Also:
-
DEFAULT_NUMBER_FORMAT
- See Also:
-
DEFAULT_DATE_FORMAT
- See Also:
-
stringsDelimiter
-
stringsTrim
private boolean stringsTrim -
numberFormat
-
dateFormat
-
-
Constructor Details
-
ConversionTool
public ConversionTool()
-
-
Method Details
-
configure
Does the actual configuration. This is protected, so subclasses may share the same ValueParser and call configure at any time, while preventing templates from doing so when configure(Map) is locked.- Overrides:
configure
in classLocaleConfig
-
setStringsDelimiter
Sets the delimiter used for separating values in a single String value. The default string delimiter is a comma.- See Also:
-
getStringsDelimiter
-
setStringsTrim
protected final void setStringsTrim(boolean stringsTrim) Sets whether strings should be trimmed when separated from a delimited string value. The default is true.- See Also:
-
getStringsTrim
public final boolean getStringsTrim() -
setNumberFormat
-
getNumberFormat
-
setDateFormat
-
getDateFormat
-
toString
Converts objects to String in a more Tools-ish way than String.valueOf(Object), especially with nulls, Arrays and Collections. Null returns null, Arrays and Collections return the toString(Object) of their first value, or null if they have no values.- Parameters:
value
- the object to be turned into a String- Returns:
- the string value of the object or null if the value is null or it is an array or collection whose first value is null
-
toBoolean
- Parameters:
value
- the object to be converted- Returns:
- a
Boolean
object for the specified value ornull
if the value is null or the conversion failed
-
toInteger
- Parameters:
value
- the object to be converted- Returns:
- a
Integer
for the specified value ornull
if the value is null or the conversion failed
-
toDouble
- Parameters:
value
- the object to be converted- Returns:
- a
Double
for the specified value ornull
if the value is null or the conversion failed
-
toNumber
- Parameters:
value
- the object to be converted- Returns:
- a
Number
for the specified value ornull
if the value is null or the conversion failed
-
toLocale
- Parameters:
value
- the object to be converted- Returns:
- a
Locale
for the specified value ornull
if the value is null or the conversion failed
-
toDate
Converts an object to an instance ofDate
, when necessary using the configured date parsing format, the configured defaultLocale
, and the system's defaultTimeZone
to parse the string value of the specified object.- Parameters:
value
- the date to convert- Returns:
- the object as a
Date
ornull
if no conversion is possible
-
toCalendar
-
toStrings
- Parameters:
value
- the value to be converted- Returns:
- an array of String objects containing all of the values derived from the specified array, Collection, or delimited String
-
toBooleans
- Parameters:
value
- the value to be converted- Returns:
- an array of Boolean objects derived from the specified value,
or
null
.
-
toBooleans
- Parameters:
values
- the collection of values to be converted- Returns:
- an array of Boolean objects derived from the specified values,
or
null
.
-
toNumbers
- Parameters:
value
- the value to be converted- Returns:
- an array of Number objects derived from the specified value,
or
null
.
-
toNumbers
- Parameters:
values
- the collection of values to be converted- Returns:
- an array of Number objects derived from the specified values,
or
null
.
-
toInts
- Parameters:
value
- the value to be converted- Returns:
- an array of int values derived from the specified value,
or
null
.
-
toIntegers
- Parameters:
value
- the value to be converted- Returns:
- an array of int values derived from the specified value,
or
null
.
-
toDoubles
- Parameters:
value
- the value to be converted- Returns:
- an array of double values derived from the specified value,
or
null
.
-
toLocales
- Parameters:
value
- the value to be converted- Returns:
- an array of Locale objects derived from the specified value,
or
null
.
-
toLocales
- Parameters:
values
- the collection of values to be converted- Returns:
- an array of Locale objects derived from the specified values,
or
null
.
-
toDates
- Parameters:
value
- the value to be converted- Returns:
- an array of Date objects derived from the specified value,
or
null
.
-
toDates
- Parameters:
values
- the collection of values to be converted- Returns:
- an array of Date objects derived from the specified values,
or
null
.
-
toCalendars
- Parameters:
value
- the value to be converted- Returns:
- an array of Calendar objects derived from the specified value,
or
null
.
-
toCalendars
- Parameters:
values
- the collection of values to be converted- Returns:
- an array of Calendar objects derived from the specified values,
or
null
.
-
parseBoolean
Converts a parameter value into aBoolean
Sub-classes can override to allow for customized boolean parsing. (e.g. to handle "Yes/No" or "T/F")- Parameters:
value
- the string to be parsed- Returns:
- the value as a
Boolean
-
parseStringList
Converts a single String value into an array of Strings by splitting it on the tool's set stringsDelimiter. The default stringsDelimiter is a comma, and by default, all strings parsed out are trimmed before returning. -
parseLocale
Converts a String value into a Locale. -
parseNumber
Converts an object to an instance ofNumber
using the format returned bygetNumberFormat()
and the defaultLocale
if the object is not already an instance of Number.- Parameters:
value
- the string to parse- Returns:
- the string as a
Number
ornull
if no conversion is possible
-
parseNumber
Converts an object to an instance ofNumber
using the specified format and theLocale
returned byLocaleConfig.getLocale()
.- Parameters:
value
- - the string to parseformat
- - the format the number is in- Returns:
- the string as a
Number
ornull
if no conversion is possible - See Also:
-
parseNumber
Converts an object to an instance ofNumber
using the configured number format and the specifiedLocale
.- Parameters:
value
- - the string to parselocale
- - the Locale to use- Returns:
- the string as a
Number
ornull
if no conversion is possible - See Also:
-
parseNumber
- Parameters:
value
- - the string to parseformat
- - the format the number is inlocale
- - the Locale to use- Returns:
- the string as a
Number
ornull
if no conversion is possible - See Also:
-
parseDate
Converts a string to an instance ofDate
, using the configured date parsing format, the configured defaultLocale
, and the system's defaultTimeZone
to parse it.- Parameters:
value
- the date to convert- Returns:
- the object as a
Date
ornull
if no conversion is possible
-
parseDate
Converts a string to an instance ofDate
using the specified format,the configured defaultLocale
, and the system's defaultTimeZone
to parse it.- Parameters:
value
- - the date to convertformat
- - the format the date is in- Returns:
- the string as a
Date
ornull
if no conversion is possible - See Also:
-
parseDate
Converts a string to an instance ofDate
using the configured date format and specifiedLocale
to parse it.- Parameters:
value
- - the date to convertlocale
- - the Locale to use- Returns:
- the string as a
Date
ornull
if no conversion is possible - See Also:
-
parseDate
- Parameters:
value
- - the date to convertformat
- - the format the date is inlocale
- - the Locale to use- Returns:
- the string as a
Date
ornull
if no conversion is possible - See Also:
-
parseDate
-