|
||||||||||
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 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
|
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. |
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)
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. |
Date |
parseDate(String value,
Object locale)
Converts a string to an instance of Date using the
configured date format and specified Locale to parse it. |
Date |
parseDate(String value,
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. |
Date |
parseDate(String value,
String format,
Object locale)
Converts a string to an instance of Date using the
specified format and Locale to parse it. |
Date |
parseDate(String value,
String format,
Object locale,
TimeZone timezone)
Converts a string to an instance of Date using the
specified format, Locale , and 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)
Converts an object to an instance of Number using the
configured number format and the specified 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)
Converts an object to an instance of Number using the
specified format and 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)
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. |
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)
|
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 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
Constructor Detail |
---|
public ConversionTool()
Method Detail |
---|
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 String
public Boolean toBoolean(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 Integer toInteger(Object value)
value
- the object to be converted
Integer
for the specified value or
null
if the value is null or the conversion failedpublic Double toDouble(Object value)
value
- the object to be converted
Double
for the specified value or
null
if the value is null or the conversion failedpublic Number toNumber(Object value)
value
- the object to be converted
Number
for the specified value or
null
if the value is null or the conversion failedpublic Locale toLocale(Object value)
value
- the object to be converted
Locale
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 convert
Date
or null
if no
conversion is possiblepublic Calendar toCalendar(Object value)
public String[] toStrings(Object value)
value
- the value to be converted
public Boolean[] toBooleans(Object value)
value
- the value to be converted
null
.public Boolean[] toBooleans(Collection values)
values
- the collection of values to be converted
null
.public Number[] toNumbers(Object value)
value
- the value to be converted
null
.public Number[] toNumbers(Collection values)
values
- the collection of values to be converted
null
.public int[] toInts(Object value)
value
- the value to be converted
null
.public int[] toIntegers(Object value)
value
- the value to be converted
null
.public double[] toDoubles(Object value)
value
- the value to be converted
null
.public Locale[] toLocales(Object value)
value
- the value to be converted
null
.public Locale[] toLocales(Collection values)
values
- the collection of values to be converted
null
.public Date[] toDates(Object value)
value
- the value to be converted
null
.public Date[] toDates(Collection values)
values
- the collection of values to be converted
null
.public Calendar[] toCalendars(Object value)
value
- the value to be converted
null
.public Calendar[] toCalendars(Collection values)
values
- the collection of values to be converted
null
.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 parsed
Boolean
protected String[] parseStringList(String value)
protected Locale parseLocale(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 parse
Number
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 in
Number
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 use
Number
or null
if no
conversion is possibleNumberFormat.parse(java.lang.String, java.text.ParsePosition)
public Number parseNumber(String value, String format, 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 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 convert
Date
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 in
Date
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 use
Date
or null
if no
conversion is possibleSimpleDateFormat.parse(java.lang.String, java.text.ParsePosition)
public Date parseDate(String value, String format, 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 Date parseDate(String value, String format, Object locale, 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 |