|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.velocity.tools.generic.SafeConfig
org.apache.velocity.tools.generic.LocaleConfig
org.apache.velocity.tools.generic.ConversionTool
@DefaultKey(value="convert") @SkipSetters public class ConversionTool
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.
Field Summary | |
---|---|
static java.lang.String |
DATE_FORMAT_KEY
|
private java.lang.String |
dateFormat
|
static java.lang.String |
DEFAULT_DATE_FORMAT
|
static java.lang.String |
DEFAULT_NUMBER_FORMAT
|
static java.lang.String |
DEFAULT_STRINGS_DELIMITER
|
static boolean |
DEFAULT_STRINGS_TRIM
|
static java.lang.String |
NUMBER_FORMAT_KEY
|
private java.lang.String |
numberFormat
|
static java.lang.String |
STRINGS_DELIMITER_FORMAT_KEY
|
static java.lang.String |
STRINGS_TRIM_KEY
|
private java.lang.String |
stringsDelimiter
|
private boolean |
stringsTrim
|
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 | |
---|---|
ConversionTool()
|
Method Summary | |
---|---|
protected void |
configure(ValueParser values)
Does the actual configuration. |
java.lang.String |
getDateFormat()
|
java.lang.String |
getNumberFormat()
|
java.lang.String |
getStringsDelimiter()
|
boolean |
getStringsTrim()
|
protected java.lang.Boolean |
parseBoolean(java.lang.String value)
Converts a parameter value into a Boolean
Sub-classes can override to allow for customized boolean parsing. |
java.util.Date |
parseDate(java.lang.String value)
Converts a string to an instance of Date ,
using the configured date parsing format, the configured default
Locale , and the system's default TimeZone to parse it. |
java.util.Date |
parseDate(java.lang.String value,
java.lang.Object locale)
Converts a string to an instance of Date using the
configured date format and specified Locale to parse it. |
java.util.Date |
parseDate(java.lang.String value,
java.lang.String format)
Converts a string to an instance of Date using the
specified format,the configured default Locale ,
and the system's default TimeZone to parse it. |
java.util.Date |
parseDate(java.lang.String value,
java.lang.String format,
java.lang.Object locale)
Converts a string to an instance of Date using the
specified format and Locale to parse it. |
java.util.Date |
parseDate(java.lang.String value,
java.lang.String format,
java.lang.Object locale,
java.util.TimeZone timezone)
Converts a string to an instance of Date using the
specified format, Locale , and TimeZone . |
protected java.util.Locale |
parseLocale(java.lang.String value)
Converts a String value into a Locale. |
java.lang.Number |
parseNumber(java.lang.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. |
java.lang.Number |
parseNumber(java.lang.String value,
java.lang.Object locale)
Converts an object to an instance of Number using the
configured number format and the specified Locale . |
java.lang.Number |
parseNumber(java.lang.String value,
java.lang.String format)
Converts an object to an instance of Number using the
specified format and the Locale returned by
LocaleConfig.getLocale() . |
java.lang.Number |
parseNumber(java.lang.String value,
java.lang.String format,
java.lang.Object locale)
Converts an object to an instance of Number using the
specified format and Locale . |
protected java.lang.String[] |
parseStringList(java.lang.String value)
Converts a single String value into an array of Strings by splitting it on the tool's set stringsDelimiter. |
protected void |
setDateFormat(java.lang.String format)
|
protected void |
setNumberFormat(java.lang.String format)
|
protected void |
setStringsDelimiter(java.lang.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. |
java.lang.Boolean |
toBoolean(java.lang.Object value)
|
java.lang.Boolean[] |
toBooleans(java.util.Collection values)
|
java.lang.Boolean[] |
toBooleans(java.lang.Object value)
|
java.util.Calendar |
toCalendar(java.lang.Object value)
|
java.util.Calendar[] |
toCalendars(java.util.Collection values)
|
java.util.Calendar[] |
toCalendars(java.lang.Object value)
|
java.util.Date |
toDate(java.lang.Object value)
Converts an object to an instance of 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. |
java.util.Date[] |
toDates(java.util.Collection values)
|
java.util.Date[] |
toDates(java.lang.Object value)
|
java.lang.Double |
toDouble(java.lang.Object value)
|
double[] |
toDoubles(java.lang.Object value)
|
java.lang.Integer |
toInteger(java.lang.Object value)
|
int[] |
toIntegers(java.lang.Object value)
|
int[] |
toInts(java.lang.Object value)
|
java.util.Locale |
toLocale(java.lang.Object value)
|
java.util.Locale[] |
toLocales(java.util.Collection values)
|
java.util.Locale[] |
toLocales(java.lang.Object value)
|
java.lang.Number |
toNumber(java.lang.Object value)
|
java.lang.Number[] |
toNumbers(java.util.Collection values)
|
java.lang.Number[] |
toNumbers(java.lang.Object value)
|
java.lang.String |
toString(java.lang.Object value)
Converts objects to String in a more Tools-ish way than String.valueOf(Object), especially with nulls, Arrays and Collections. |
java.lang.String[] |
toStrings(java.lang.Object value)
|
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 |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final java.lang.String STRINGS_DELIMITER_FORMAT_KEY
public static final java.lang.String STRINGS_TRIM_KEY
public static final java.lang.String DATE_FORMAT_KEY
public static final java.lang.String NUMBER_FORMAT_KEY
public static final java.lang.String DEFAULT_STRINGS_DELIMITER
public static final boolean DEFAULT_STRINGS_TRIM
public static final java.lang.String DEFAULT_NUMBER_FORMAT
public static final java.lang.String DEFAULT_DATE_FORMAT
private java.lang.String stringsDelimiter
private boolean stringsTrim
private java.lang.String numberFormat
private java.lang.String dateFormat
Constructor Detail |
---|
public ConversionTool()
Method Detail |
---|
protected void configure(ValueParser values)
configure
in class LocaleConfig
protected final void setStringsDelimiter(java.lang.String stringsDelimiter)
parseStringList(java.lang.String)
public final java.lang.String getStringsDelimiter()
protected final void setStringsTrim(boolean stringsTrim)
parseStringList(java.lang.String)
public final boolean getStringsTrim()
protected final void setNumberFormat(java.lang.String format)
public final java.lang.String getNumberFormat()
protected final void setDateFormat(java.lang.String format)
public final java.lang.String getDateFormat()
public java.lang.String toString(java.lang.Object value)
value
- the object to be turned into a String
public java.lang.Boolean toBoolean(java.lang.Object value)
value
- the object to be converted
Boolean
object for the specified value or
null
if the value is null or the conversion failedpublic java.lang.Integer toInteger(java.lang.Object value)
value
- the object to be converted
Integer
for the specified value or
null
if the value is null or the conversion failedpublic java.lang.Double toDouble(java.lang.Object value)
value
- the object to be converted
Double
for the specified value or
null
if the value is null or the conversion failedpublic java.lang.Number toNumber(java.lang.Object value)
value
- the object to be converted
Number
for the specified value or
null
if the value is null or the conversion failedpublic java.util.Locale toLocale(java.lang.Object value)
value
- the object to be converted
Locale
for the specified value or
null
if the value is null or the conversion failedpublic java.util.Date toDate(java.lang.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 convert
Date
or null
if no
conversion is possiblepublic java.util.Calendar toCalendar(java.lang.Object value)
public java.lang.String[] toStrings(java.lang.Object value)
value
- the value to be converted
public java.lang.Boolean[] toBooleans(java.lang.Object value)
value
- the value to be converted
null
.public java.lang.Boolean[] toBooleans(java.util.Collection values)
values
- the collection of values to be converted
null
.public java.lang.Number[] toNumbers(java.lang.Object value)
value
- the value to be converted
null
.public java.lang.Number[] toNumbers(java.util.Collection values)
values
- the collection of values to be converted
null
.public int[] toInts(java.lang.Object value)
value
- the value to be converted
null
.public int[] toIntegers(java.lang.Object value)
value
- the value to be converted
null
.public double[] toDoubles(java.lang.Object value)
value
- the value to be converted
null
.public java.util.Locale[] toLocales(java.lang.Object value)
value
- the value to be converted
null
.public java.util.Locale[] toLocales(java.util.Collection values)
values
- the collection of values to be converted
null
.public java.util.Date[] toDates(java.lang.Object value)
value
- the value to be converted
null
.public java.util.Date[] toDates(java.util.Collection values)
values
- the collection of values to be converted
null
.public java.util.Calendar[] toCalendars(java.lang.Object value)
value
- the value to be converted
null
.public java.util.Calendar[] toCalendars(java.util.Collection values)
values
- the collection of values to be converted
null
.protected java.lang.Boolean parseBoolean(java.lang.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 parsed
Boolean
protected java.lang.String[] parseStringList(java.lang.String value)
protected java.util.Locale parseLocale(java.lang.String value)
public java.lang.Number parseNumber(java.lang.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 parse
Number
or null
if no
conversion is possiblepublic java.lang.Number parseNumber(java.lang.String value, java.lang.String format)
Number
using the
specified format and the Locale
returned by
LocaleConfig.getLocale()
.
value
- - the string to parseformat
- - the format the number is in
Number
or null
if no
conversion is possibleparseNumber(String value, String format, Object locale)
public java.lang.Number parseNumber(java.lang.String value, java.lang.Object locale)
Number
using the
configured number format and the specified Locale
.
value
- - the string to parselocale
- - the Locale to use
Number
or null
if no
conversion is possibleNumberFormat.parse(java.lang.String, java.text.ParsePosition)
public java.lang.Number parseNumber(java.lang.String value, java.lang.String format, java.lang.Object locale)
Number
using the
specified format and Locale
.
value
- - the string to parseformat
- - the format the number is inlocale
- - the Locale to use
Number
or null
if no
conversion is possibleNumberFormat.parse(java.lang.String, java.text.ParsePosition)
public java.util.Date parseDate(java.lang.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 convert
Date
or null
if no
conversion is possiblepublic java.util.Date parseDate(java.lang.String value, java.lang.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 in
Date
or null
if no
conversion is possibleConversionUtils.toDate(String str, String format, Locale locale, TimeZone timezone)
public java.util.Date parseDate(java.lang.String value, java.lang.Object locale)
Date
using the
configured date format and specified Locale
to parse it.
value
- - the date to convertlocale
- - the Locale to use
Date
or null
if no
conversion is possibleSimpleDateFormat.parse(java.lang.String, java.text.ParsePosition)
public java.util.Date parseDate(java.lang.String value, java.lang.String format, java.lang.Object locale)
Date
using the
specified format and Locale
to parse it.
value
- - the date to convertformat
- - the format the date is inlocale
- - the Locale to use
Date
or null
if no
conversion is possibleSimpleDateFormat.parse(java.lang.String, java.text.ParsePosition)
public java.util.Date parseDate(java.lang.String value, java.lang.String format, java.lang.Object locale, java.util.TimeZone timezone)
Date
using the
specified format, Locale
, and 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)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |