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
@DefaultKey("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.
- Since:
- VelocityTools 2.0
- Version:
- $Revision$ $Date: 2007-02-26 11:24:39 -0800 (Mon, 26 Feb 2007) $
- Author:
- Nathan Bubna
-
-
Field Summary
Fields Modifier and Type Field Description static StringDATE_FORMAT_KEYstatic StringDEFAULT_DATE_FORMATstatic StringDEFAULT_NUMBER_FORMATstatic StringDEFAULT_STRINGS_DELIMITERstatic booleanDEFAULT_STRINGS_TRIMstatic StringNUMBER_FORMAT_KEYstatic StringSTRINGS_DELIMITER_FORMAT_KEYstatic StringSTRINGS_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
Constructors Constructor Description ConversionTool()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidconfigure(ValueParser values)Does the actual configuration.StringgetDateFormat()StringgetNumberFormat()StringgetStringsDelimiter()booleangetStringsTrim()protected BooleanparseBoolean(String value)Converts a parameter value into aBooleanSub-classes can override to allow for customized boolean parsing.DateparseDate(String value)DateparseDate(String value, Object locale)DateparseDate(String value, String format)DateparseDate(String value, String format, Object locale)DateparseDate(String value, String format, Object locale, TimeZone timezone)protected LocaleparseLocale(String value)Converts a String value into a Locale.NumberparseNumber(String value)Converts an object to an instance ofNumberusing the format returned bygetNumberFormat()and the defaultLocaleif the object is not already an instance of Number.NumberparseNumber(String value, Object locale)NumberparseNumber(String value, String format)Converts an object to an instance ofNumberusing the specified format and theLocalereturned byLocaleConfig.getLocale().NumberparseNumber(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 voidsetDateFormat(String format)protected voidsetNumberFormat(String format)protected voidsetStringsDelimiter(String stringsDelimiter)Sets the delimiter used for separating values in a single String value.protected voidsetStringsTrim(boolean stringsTrim)Sets whether strings should be trimmed when separated from a delimited string value.BooleantoBoolean(Object value)Boolean[]toBooleans(Object value)Boolean[]toBooleans(Collection values)CalendartoCalendar(Object value)Calendar[]toCalendars(Object value)Calendar[]toCalendars(Collection values)DatetoDate(Object value)Date[]toDates(Object value)Date[]toDates(Collection values)DoubletoDouble(Object value)double[]toDoubles(Object value)IntegertoInteger(Object value)int[]toIntegers(Object value)int[]toInts(Object value)LocaletoLocale(Object value)Locale[]toLocales(Object value)Locale[]toLocales(Collection values)NumbertoNumber(Object value)Number[]toNumbers(Object value)Number[]toNumbers(Collection values)StringtoString(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
-
-
-
-
Field Detail
-
STRINGS_DELIMITER_FORMAT_KEY
public static final String STRINGS_DELIMITER_FORMAT_KEY
- See Also:
- Constant Field Values
-
STRINGS_TRIM_KEY
public static final String STRINGS_TRIM_KEY
- See Also:
- Constant Field Values
-
DATE_FORMAT_KEY
public static final String DATE_FORMAT_KEY
- See Also:
- Constant Field Values
-
NUMBER_FORMAT_KEY
public static final String NUMBER_FORMAT_KEY
- See Also:
- Constant Field Values
-
DEFAULT_STRINGS_DELIMITER
public static final String DEFAULT_STRINGS_DELIMITER
- See Also:
- Constant Field Values
-
DEFAULT_STRINGS_TRIM
public static final boolean DEFAULT_STRINGS_TRIM
- See Also:
- Constant Field Values
-
DEFAULT_NUMBER_FORMAT
public static final String DEFAULT_NUMBER_FORMAT
- See Also:
- Constant Field Values
-
DEFAULT_DATE_FORMAT
public static final String DEFAULT_DATE_FORMAT
- See Also:
- Constant Field Values
-
-
Method Detail
-
configure
protected void configure(ValueParser values)
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:
configurein classLocaleConfig
-
setStringsDelimiter
protected final void setStringsDelimiter(String stringsDelimiter)
Sets the delimiter used for separating values in a single String value. The default string delimiter is a comma.- See Also:
parseStringList(java.lang.String)
-
getStringsDelimiter
public final String 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:
parseStringList(java.lang.String)
-
getStringsTrim
public final boolean getStringsTrim()
-
setNumberFormat
protected final void setNumberFormat(String format)
-
getNumberFormat
public final String getNumberFormat()
-
setDateFormat
protected final void setDateFormat(String format)
-
getDateFormat
public final String getDateFormat()
-
toString
public String toString(Object value)
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
public Boolean toBoolean(Object value)
- Parameters:
value- the object to be converted- Returns:
- a
Booleanobject for the specified value ornullif the value is null or the conversion failed
-
toInteger
public Integer toInteger(Object value)
- Parameters:
value- the object to be converted- Returns:
- a
Integerfor the specified value ornullif the value is null or the conversion failed
-
toDouble
public Double toDouble(Object value)
- Parameters:
value- the object to be converted- Returns:
- a
Doublefor the specified value ornullif the value is null or the conversion failed
-
toNumber
public Number toNumber(Object value)
- Parameters:
value- the object to be converted- Returns:
- a
Numberfor the specified value ornullif the value is null or the conversion failed
-
toLocale
public Locale toLocale(Object value)
- Parameters:
value- the object to be converted- Returns:
- a
Localefor the specified value ornullif the value is null or the conversion failed
-
toDate
public Date toDate(Object value)
Converts an object to an instance ofDate, when necessary using the configured date parsing format, the configured defaultLocale, and the system's defaultTimeZoneto parse the string value of the specified object.- Parameters:
value- the date to convert- Returns:
- the object as a
Dateornullif no conversion is possible
-
toStrings
public String[] toStrings(Object value)
- 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
public Boolean[] toBooleans(Object value)
- Parameters:
value- the value to be converted- Returns:
- an array of Boolean objects derived from the specified value,
or
null.
-
toBooleans
public Boolean[] toBooleans(Collection values)
- Parameters:
values- the collection of values to be converted- Returns:
- an array of Boolean objects derived from the specified values,
or
null.
-
toNumbers
public Number[] toNumbers(Object value)
- Parameters:
value- the value to be converted- Returns:
- an array of Number objects derived from the specified value,
or
null.
-
toNumbers
public Number[] toNumbers(Collection values)
- Parameters:
values- the collection of values to be converted- Returns:
- an array of Number objects derived from the specified values,
or
null.
-
toInts
public int[] toInts(Object value)
- Parameters:
value- the value to be converted- Returns:
- an array of int values derived from the specified value,
or
null.
-
toIntegers
public int[] toIntegers(Object value)
- Parameters:
value- the value to be converted- Returns:
- an array of int values derived from the specified value,
or
null.
-
toDoubles
public double[] toDoubles(Object value)
- Parameters:
value- the value to be converted- Returns:
- an array of double values derived from the specified value,
or
null.
-
toLocales
public Locale[] toLocales(Object value)
- Parameters:
value- the value to be converted- Returns:
- an array of Locale objects derived from the specified value,
or
null.
-
toLocales
public Locale[] toLocales(Collection values)
- Parameters:
values- the collection of values to be converted- Returns:
- an array of Locale objects derived from the specified values,
or
null.
-
toDates
public Date[] toDates(Object value)
- Parameters:
value- the value to be converted- Returns:
- an array of Date objects derived from the specified value,
or
null.
-
toDates
public Date[] toDates(Collection values)
- Parameters:
values- the collection of values to be converted- Returns:
- an array of Date objects derived from the specified values,
or
null.
-
toCalendars
public Calendar[] toCalendars(Object value)
- Parameters:
value- the value to be converted- Returns:
- an array of Calendar objects derived from the specified value,
or
null.
-
toCalendars
public Calendar[] toCalendars(Collection values)
- Parameters:
values- the collection of values to be converted- Returns:
- an array of Calendar objects derived from the specified values,
or
null.
-
parseBoolean
protected Boolean parseBoolean(String value)
Converts a parameter value into aBooleanSub-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
protected String[] parseStringList(String value)
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.
-
parseNumber
public Number parseNumber(String value)
Converts an object to an instance ofNumberusing the format returned bygetNumberFormat()and the defaultLocaleif the object is not already an instance of Number.- Parameters:
value- the string to parse- Returns:
- the string as a
Numberornullif no conversion is possible
-
parseNumber
public Number parseNumber(String value, String format)
Converts an object to an instance ofNumberusing the specified format and theLocalereturned byLocaleConfig.getLocale().- Parameters:
value- - the string to parseformat- - the format the number is in- Returns:
- the string as a
Numberornullif no conversion is possible - See Also:
parseNumber(String value, String format, Object locale)
-
parseNumber
public Number parseNumber(String value, Object locale)
Converts an object to an instance ofNumberusing the configured number format and the specifiedLocale.- Parameters:
value- - the string to parselocale- - the Locale to use- Returns:
- the string as a
Numberornullif no conversion is possible - See Also:
NumberFormat.parse(java.lang.String, java.text.ParsePosition)
-
parseNumber
public Number parseNumber(String value, String format, Object locale)
- Parameters:
value- - the string to parseformat- - the format the number is inlocale- - the Locale to use- Returns:
- the string as a
Numberornullif no conversion is possible - See Also:
NumberFormat.parse(java.lang.String, java.text.ParsePosition)
-
parseDate
public Date parseDate(String value)
Converts a string to an instance ofDate, using the configured date parsing format, the configured defaultLocale, and the system's defaultTimeZoneto parse it.- Parameters:
value- the date to convert- Returns:
- the object as a
Dateornullif no conversion is possible
-
parseDate
public Date parseDate(String value, String format)
Converts a string to an instance ofDateusing the specified format,the configured defaultLocale, and the system's defaultTimeZoneto parse it.- Parameters:
value- - the date to convertformat- - the format the date is in- Returns:
- the string as a
Dateornullif no conversion is possible - See Also:
ConversionUtils.toDate(String str, String format, Locale locale, TimeZone timezone)
-
parseDate
public Date parseDate(String value, Object locale)
Converts a string to an instance ofDateusing the configured date format and specifiedLocaleto parse it.- Parameters:
value- - the date to convertlocale- - the Locale to use- Returns:
- the string as a
Dateornullif no conversion is possible - See Also:
SimpleDateFormat.parse(java.lang.String, java.text.ParsePosition)
-
parseDate
public Date parseDate(String value, String format, Object locale)
- Parameters:
value- - the date to convertformat- - the format the date is inlocale- - the Locale to use- Returns:
- the string as a
Dateornullif no conversion is possible - See Also:
SimpleDateFormat.parse(java.lang.String, java.text.ParsePosition)
-
parseDate
public Date parseDate(String value, String format, Object locale, TimeZone timezone)
- Parameters:
value- - the date to convertformat- - the format the date is inlocale- - the Locale to usetimezone- - theTimeZone- Returns:
- the string as a
Dateornullif no conversion is possible - See Also:
getDateFormat(),SimpleDateFormat.parse(java.lang.String, java.text.ParsePosition)
-
-