@DefaultKey(value="convert") @SkipSetters public class ConversionTool extends LocaleConfig
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.
Modifier and Type | Field and Description |
---|---|
static String |
DATE_FORMAT_KEY |
static String |
DEFAULT_DATE_FORMAT |
static String |
DEFAULT_NUMBER_FORMAT |
static String |
DEFAULT_STRINGS_DELIMITER |
static boolean |
DEFAULT_STRINGS_TRIM |
static String |
NUMBER_FORMAT_KEY |
static String |
STRINGS_DELIMITER_FORMAT_KEY |
static String |
STRINGS_TRIM_KEY |
DEFAULT_LOCALE
LOCK_CONFIG_KEY, OLD_LOCK_CONFIG_KEY, SAFE_MODE_KEY
Constructor and Description |
---|
ConversionTool() |
Modifier and Type | Method and Description |
---|---|
protected void |
configure(ValueParser values)
Does the actual configuration.
|
String |
getDateFormat() |
String |
getNumberFormat() |
String |
getStringsDelimiter() |
boolean |
getStringsTrim() |
protected Boolean |
parseBoolean(String value)
Converts a parameter value into a
Boolean
Sub-classes can override to allow for customized boolean parsing. |
Date |
parseDate(String value)
|
Date |
parseDate(String value,
Object locale)
|
Date |
parseDate(String value,
String format)
|
Date |
parseDate(String value,
String format,
Object locale)
|
Date |
parseDate(String value,
String format,
Object locale,
TimeZone timezone)
|
protected Locale |
parseLocale(String value)
Converts a String value into a Locale.
|
Number |
parseNumber(String value)
Converts an object to an instance of
Number using the
format returned by getNumberFormat() and the default Locale
if the object is not already an instance of Number. |
Number |
parseNumber(String value,
Object locale)
|
Number |
parseNumber(String value,
String format)
Converts an object to an instance of
Number using the
specified format and the Locale returned by
LocaleConfig.getLocale() . |
Number |
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 void |
setDateFormat(String format) |
protected void |
setNumberFormat(String format) |
protected void |
setStringsDelimiter(String stringsDelimiter)
Sets the delimiter used for separating values in a single String value.
|
protected void |
setStringsTrim(boolean stringsTrim)
Sets whether strings should be trimmed when separated from
a delimited string value.
|
Boolean |
toBoolean(Object value) |
Boolean[] |
toBooleans(Collection values) |
Boolean[] |
toBooleans(Object value) |
Calendar |
toCalendar(Object value) |
Calendar[] |
toCalendars(Collection values) |
Calendar[] |
toCalendars(Object value) |
Date |
toDate(Object value)
|
Date[] |
toDates(Collection values) |
Date[] |
toDates(Object value) |
Double |
toDouble(Object value) |
double[] |
toDoubles(Object value) |
Integer |
toInteger(Object value) |
int[] |
toIntegers(Object value) |
int[] |
toInts(Object value) |
Locale |
toLocale(Object value) |
Locale[] |
toLocales(Collection values) |
Locale[] |
toLocales(Object value) |
Number |
toNumber(Object value) |
Number[] |
toNumbers(Collection values) |
Number[] |
toNumbers(Object value) |
String |
toString(Object value)
Converts objects to String in a more Tools-ish way than
String.valueOf(Object), especially with nulls, Arrays and Collections.
|
String[] |
toStrings(Object value) |
getLocale, setLocale
configure, isConfigLocked, isSafeMode, setLockConfig, setSafeMode
public static final String STRINGS_DELIMITER_FORMAT_KEY
public static final String STRINGS_TRIM_KEY
public static final String DATE_FORMAT_KEY
public static final String NUMBER_FORMAT_KEY
public static final String DEFAULT_STRINGS_DELIMITER
public static final boolean DEFAULT_STRINGS_TRIM
public static final String DEFAULT_NUMBER_FORMAT
public static final String DEFAULT_DATE_FORMAT
protected void configure(ValueParser values)
configure
in class LocaleConfig
protected final void setStringsDelimiter(String stringsDelimiter)
parseStringList(java.lang.String)
public final String getStringsDelimiter()
protected final void setStringsTrim(boolean stringsTrim)
parseStringList(java.lang.String)
public final boolean getStringsTrim()
protected final void setNumberFormat(String format)
public final String getNumberFormat()
protected final void setDateFormat(String format)
public final String getDateFormat()
public String toString(Object value)
value
- the object to be turned into a Stringpublic Boolean toBoolean(Object value)
value
- the object to be convertedBoolean
object for the specified value or
null
if the value is null or the conversion failedpublic Integer toInteger(Object value)
value
- the object to be convertedInteger
for the specified value or
null
if the value is null or the conversion failedpublic Double toDouble(Object value)
value
- the object to be convertedDouble
for the specified value or
null
if the value is null or the conversion failedpublic Number toNumber(Object value)
value
- the object to be convertedNumber
for the specified value or
null
if the value is null or the conversion failedpublic Locale toLocale(Object value)
value
- the object to be convertedLocale
for the specified value or
null
if the value is null or the conversion failedpublic Date toDate(Object value)
Date
, when necessary
using the configured date parsing format, the configured default
Locale
, and the system's default TimeZone
to parse
the string value of the specified object.value
- the date to convertDate
or null
if no
conversion is possiblepublic String[] toStrings(Object value)
value
- the value to be convertedpublic Boolean[] toBooleans(Object value)
value
- the value to be convertednull
.public Boolean[] toBooleans(Collection values)
values
- the collection of values to be convertednull
.public Number[] toNumbers(Object value)
value
- the value to be convertednull
.public Number[] toNumbers(Collection values)
values
- the collection of values to be convertednull
.public int[] toInts(Object value)
value
- the value to be convertednull
.public int[] toIntegers(Object value)
value
- the value to be convertednull
.public double[] toDoubles(Object value)
value
- the value to be convertednull
.public Locale[] toLocales(Object value)
value
- the value to be convertednull
.public Locale[] toLocales(Collection values)
values
- the collection of values to be convertednull
.public Date[] toDates(Object value)
value
- the value to be convertednull
.public Date[] toDates(Collection values)
values
- the collection of values to be convertednull
.public Calendar[] toCalendars(Object value)
value
- the value to be convertednull
.public Calendar[] toCalendars(Collection values)
values
- the collection of values to be convertednull
.protected Boolean parseBoolean(String value)
Boolean
Sub-classes can override to allow for customized boolean parsing.
(e.g. to handle "Yes/No" or "T/F")value
- the string to be parsedBoolean
protected String[] parseStringList(String value)
public Number parseNumber(String value)
Number
using the
format returned by getNumberFormat()
and the default Locale
if the object is not already an instance of Number.value
- the string to parseNumber
or null
if no
conversion is possiblepublic Number parseNumber(String value, String format)
Number
using the
specified format and the Locale
returned by
LocaleConfig.getLocale()
.value
- - the string to parseformat
- - the format the number is inNumber
or null
if no
conversion is possibleparseNumber(String value, String format, Object locale)
public Number parseNumber(String value, Object locale)
Number
using the
configured number format and the specified Locale
.value
- - the string to parselocale
- - the Locale to useNumber
or null
if no
conversion is possibleNumberFormat.parse(java.lang.String, java.text.ParsePosition)
public Number parseNumber(String value, String format, Object locale)
value
- - the string to parseformat
- - the format the number is inlocale
- - the Locale to useNumber
or null
if no
conversion is possibleNumberFormat.parse(java.lang.String, java.text.ParsePosition)
public Date parseDate(String value)
Date
,
using the configured date parsing format, the configured default
Locale
, and the system's default TimeZone
to parse it.value
- the date to convertDate
or null
if no
conversion is possiblepublic Date parseDate(String value, String format)
Date
using the
specified format,the configured default Locale
,
and the system's default TimeZone
to parse it.value
- - the date to convertformat
- - the format the date is inDate
or null
if no
conversion is possibleConversionUtils.toDate(String str, String format, Locale locale, TimeZone timezone)
public Date parseDate(String value, Object locale)
Date
using the
configured date format and specified Locale
to parse it.value
- - the date to convertlocale
- - the Locale to useDate
or null
if no
conversion is possibleSimpleDateFormat.parse(java.lang.String, java.text.ParsePosition)
public Date parseDate(String value, String format, Object locale)
value
- - the date to convertformat
- - the format the date is inlocale
- - the Locale to useDate
or null
if no
conversion is possibleSimpleDateFormat.parse(java.lang.String, java.text.ParsePosition)
public Date parseDate(String value, String format, Object locale, TimeZone timezone)
value
- - the date to convertformat
- - the format the date is inlocale
- - the Locale to usetimezone
- - the TimeZone
Date
or null
if no
conversion is possiblegetDateFormat()
,
SimpleDateFormat.parse(java.lang.String, java.text.ParsePosition)
Copyright © 2002–2016 Apache Software Foundation. All rights reserved.