@DefaultKey(value="convert") @SkipSetters @Deprecated public class ConversionTool extends LocaleConfig implements Serializable
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
Deprecated.
|
static String |
DEFAULT_DATE_FORMAT
Deprecated.
|
static String |
DEFAULT_NUMBER_FORMAT
Deprecated.
|
static String |
DEFAULT_STRINGS_DELIMITER
Deprecated.
|
static boolean |
DEFAULT_STRINGS_TRIM
Deprecated.
|
static String |
NUMBER_FORMAT_KEY
Deprecated.
|
static String |
STRINGS_DELIMITER_FORMAT_KEY
Deprecated.
|
DEFAULT_LOCALE
LOCK_CONFIG_KEY, log, LOGGER_NAME_KEY, SAFE_MODE_KEY, USE_CLASS_LOGGER_KEY
Constructor and Description |
---|
ConversionTool()
Deprecated.
|
Modifier and Type | Method and Description |
---|---|
protected void |
configure(ValueParser values)
Deprecated.
Does the actual configuration.
|
String |
getDateFormat()
Deprecated.
use
DateTool getDateFormat() methods |
String |
getNumberFormat()
Deprecated.
use
NumberTool format |
String |
getStringsDelimiter()
Deprecated.
Gets the delimiter used for separating values in a single String value.
|
boolean |
getStringsTrim()
Deprecated.
|
protected Boolean |
parseBoolean(String value)
Deprecated.
Converts a parameter value into a
Boolean
Sub-classes can override to allow for customized boolean parsing. |
Date |
parseDate(String value)
Deprecated.
|
Date |
parseDate(String value,
Object locale)
Deprecated.
|
Date |
parseDate(String value,
String format)
Deprecated.
|
Date |
parseDate(String value,
String format,
Object locale)
Deprecated.
|
Date |
parseDate(String value,
String format,
Object locale,
TimeZone timezone)
Deprecated.
|
protected Locale |
parseLocale(String value)
Deprecated.
Converts a String value into a Locale.
|
Number |
parseNumber(String value)
Deprecated.
|
Number |
parseNumber(String value,
Object locale)
Deprecated.
|
Number |
parseNumber(String value,
String format)
Deprecated.
|
Number |
parseNumber(String value,
String format,
Object locale)
Deprecated.
|
protected String[] |
parseStringList(String value)
Deprecated.
Converts a single String value into an array of Strings by splitting
it on the tool's set stringsDelimiter.
|
protected void |
setDateFormat(String format)
Deprecated.
use
DateTool setDateFormat() methods |
protected void |
setNumberFormat(String format)
Deprecated.
use
NumberTool format |
protected void |
setStringsDelimiter(String stringsDelimiter)
Deprecated.
|
protected void |
setStringsTrim(boolean stringsTrim)
Deprecated.
|
Boolean |
toBoolean(Object value)
Deprecated.
|
Boolean[] |
toBooleans(Collection values)
Deprecated.
|
Boolean[] |
toBooleans(Object value)
Deprecated.
|
Calendar |
toCalendar(Object value)
Deprecated.
|
Calendar[] |
toCalendars(Collection values)
Deprecated.
|
Calendar[] |
toCalendars(Object value)
Deprecated.
|
Date |
toDate(Object value)
Deprecated.
|
Date[] |
toDates(Collection values)
Deprecated.
|
Date[] |
toDates(Object value)
Deprecated.
|
Double |
toDouble(Object value)
Deprecated.
|
double[] |
toDoubles(Object value)
Deprecated.
|
Integer |
toInteger(Object value)
Deprecated.
|
int[] |
toIntegers(Object value)
Deprecated.
|
int[] |
toInts(Object value)
Deprecated.
|
List |
toList(Object value)
Deprecated.
Convert a singleton or an array as List
|
Locale |
toLocale(Object value)
Deprecated.
use
DateTool .toLocale(Object) |
Locale[] |
toLocales(Collection values)
Deprecated.
|
Locale[] |
toLocales(Object value)
Deprecated.
|
Number |
toNumber(Object value)
Deprecated.
|
Number[] |
toNumbers(Collection values)
Deprecated.
|
Number[] |
toNumbers(Object value)
Deprecated.
|
String |
toString(Object value)
Deprecated.
Converts objects to String in a more Tools-ish way than
String.valueOf(Object), especially with nulls, Arrays and Collections.
|
String[] |
toStrings(Object value)
Deprecated.
|
getLocale, setLocale
configure, getLog, initLogger, isConfigLocked, isSafeMode, setLockConfig, setSafeMode
public static final String STRINGS_DELIMITER_FORMAT_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
values
- configuration valuesprotected final void setStringsDelimiter(String stringsDelimiter)
CollectionTool.setStringsDelimiter(String)
stringsDelimiter
- strings delimiter to useparseStringList(java.lang.String)
public final String getStringsDelimiter()
protected final void setStringsTrim(boolean stringsTrim)
CollectionTool.setStringsTrim(boolean)
stringsTrim
- flag valueparseStringList(java.lang.String)
public final boolean getStringsTrim()
CollectionTool.getStringsTrim()
protected final void setNumberFormat(String format)
NumberTool
formatformat
- numbers formatpublic final String getNumberFormat()
NumberTool
formatprotected final void setDateFormat(String format)
DateTool
setDateFormat() methodsformat
- date formatpublic final String getDateFormat()
DateTool
getDateFormat() methodspublic 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)
DateTool
.toLocale(Object)toLocale
in class LocaleConfig
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)
DateTool.toDate(Object)
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 Calendar toCalendar(Object value)
DateTool.toCalendar(Object)
value
- target objectpublic String[] toStrings(Object value)
CollectionTool.split(String)
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
.public List toList(Object value)
value
- Object to be convertedprotected 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)
value
- string list to parseprotected Locale parseLocale(String value)
value
- string of locale to parsepublic Number parseNumber(String value)
NumberTool.toNumber(Object)
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)
NumberTool.toNumber(String, Object)
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)
NumberTool.toNumber(String, 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)
NumberTool.toNumber(String, 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)
DateTool.toDate(Object)
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)
DateTool.toDate(String, Object)
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)
DateTool.toDate(String, 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)
DateTool.toDate(String, 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)
DateTool.toDate(String, Object, Locale, 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–2021 The Apache Software Foundation. All rights reserved.