org.apache.velocity.tools.generic
Class ValueParser

java.lang.Object
  extended by org.apache.velocity.tools.generic.ValueParser
Direct Known Subclasses:
ParameterParser

public class ValueParser
extends java.lang.Object

Utility class for easy parsing of String values held in a Map.

 Template example(s):
   $parser.foo                ->  bar
   $parser.getNumber('baz')   ->  12.6
   $parser.getInt('baz')      ->  12
   $parser.getNumbers('baz')  ->  [12.6]

 Toolbox configuration:
 <tool>
   <key>parser</key>
   <class>org.apache.velocity.generic.ValueParser</class>
 </tool>
 

This comes in very handy when parsing parameters.

Since:
VelocityTools 1.2
Version:
$Revision: 497988 $ $Date: 2007-01-19 14:48:13 -0800 (Fri, 19 Jan 2007) $
Author:
Nathan Bubna

Field Summary
private  java.lang.String delimiter
           
private  java.util.Map source
           
 
Constructor Summary
ValueParser()
           
ValueParser(java.util.Map source)
           
 
Method Summary
 boolean exists(java.lang.String key)
          Convenience method for checking whether a certain parameter exists.
 java.lang.Object get(java.lang.String key)
          Convenience method for use in Velocity templates.
 java.lang.Boolean getBoolean(java.lang.String key)
           
 boolean getBoolean(java.lang.String key, boolean alternate)
           
 java.lang.Boolean getBoolean(java.lang.String key, java.lang.Boolean alternate)
           
 java.lang.Boolean[] getBooleans(java.lang.String key)
           
 java.lang.Double getDouble(java.lang.String key)
           
 double getDouble(java.lang.String key, double alternate)
           
 java.lang.Double getDouble(java.lang.String key, java.lang.Double alternate)
           
 double[] getDoubles(java.lang.String key)
           
 int getInt(java.lang.String key, int alternate)
           
 java.lang.Integer getInteger(java.lang.String key)
           
 java.lang.Integer getInteger(java.lang.String key, java.lang.Integer alternate)
           
 int[] getInts(java.lang.String key)
           
 java.util.Locale getLocale(java.lang.String key)
           
 java.util.Locale getLocale(java.lang.String key, java.util.Locale alternate)
           
 java.util.Locale[] getLocales(java.lang.String key)
           
 java.lang.Number getNumber(java.lang.String key)
           
 java.lang.Number getNumber(java.lang.String key, java.lang.Number alternate)
           
 java.lang.Number[] getNumbers(java.lang.String key)
           
protected  java.util.Map getSource()
           
 java.lang.String getString(java.lang.String key)
           
 java.lang.String getString(java.lang.String key, java.lang.String alternate)
           
 java.lang.String[] getStrings(java.lang.String key)
           
protected  java.lang.String getStringsDelimiter()
          Returns the delimiter used for separating values in a single String value.
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.
protected  java.util.Locale parseLocale(java.lang.String value)
          Converts a String value into a Locale.
protected  java.lang.Number parseNumber(java.lang.String value)
          Converts a parameter value into a Number This is used as the base for all numeric parsing methods.
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 delimiter.
protected  void setSource(java.util.Map source)
           
protected  void setStringsDelimiter(java.lang.String delimiter)
          Sets the delimiter used for separating values in a single String value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

source

private java.util.Map source

delimiter

private java.lang.String delimiter
Constructor Detail

ValueParser

public ValueParser()

ValueParser

public ValueParser(java.util.Map source)
Method Detail

setSource

protected void setSource(java.util.Map source)

getSource

protected java.util.Map getSource()

setStringsDelimiter

protected final void setStringsDelimiter(java.lang.String delimiter)
Sets the delimiter used for separating values in a single String value. The default delimiter is a comma.

Since:
VelocityTools 1.3
See Also:
parseStringList(java.lang.String)

getStringsDelimiter

protected final java.lang.String getStringsDelimiter()
Returns the delimiter used for separating values in a single String value. The default delimiter is a comma.

Since:
VelocityTools 1.3
See Also:
parseStringList(java.lang.String)

exists

public boolean exists(java.lang.String key)
Convenience method for checking whether a certain parameter exists.

Parameters:
key - the parameter's key
Returns:
true if a parameter exists for the specified key; otherwise, returns false.

get

public java.lang.Object get(java.lang.String key)
Convenience method for use in Velocity templates. This allows for easy "dot" access to parameters. e.g. $params.foo instead of $params.getString('foo')

Parameters:
key - the parameter's key
Returns:
parameter matching the specified key or null if there is no matching parameter

getString

public java.lang.String getString(java.lang.String key)
Parameters:
key - the parameter's key
Returns:
parameter matching the specified key or null if there is no matching parameter

getString

public java.lang.String getString(java.lang.String key,
                                  java.lang.String alternate)
Parameters:
key - the desired parameter's key
alternate - The alternate value
Returns:
parameter matching the specified key or the specified alternate String if there is no matching parameter

getBoolean

public java.lang.Boolean getBoolean(java.lang.String key)
Parameters:
key - the desired parameter's key
Returns:
a Boolean object for the specified key or null if no matching parameter is found

getBoolean

public boolean getBoolean(java.lang.String key,
                          boolean alternate)
Parameters:
key - the desired parameter's key
alternate - The alternate boolean value
Returns:
boolean value for the specified key or the alternate boolean is no value is found

getBoolean

public java.lang.Boolean getBoolean(java.lang.String key,
                                    java.lang.Boolean alternate)
Parameters:
key - the desired parameter's key
alternate - the alternate Boolean
Returns:
a Boolean for the specified key or the specified alternate if no matching parameter is found

getInteger

public java.lang.Integer getInteger(java.lang.String key)
Parameters:
key - the desired parameter's key
Returns:
a Integer for the specified key or null if no matching parameter is found

getInteger

public java.lang.Integer getInteger(java.lang.String key,
                                    java.lang.Integer alternate)
Parameters:
key - the desired parameter's key
alternate - The alternate Integer
Returns:
an Integer for the specified key or the specified alternate if no matching parameter is found

getDouble

public java.lang.Double getDouble(java.lang.String key)
Parameters:
key - the desired parameter's key
Returns:
a Double for the specified key or null if no matching parameter is found

getDouble

public java.lang.Double getDouble(java.lang.String key,
                                  java.lang.Double alternate)
Parameters:
key - the desired parameter's key
alternate - The alternate Double
Returns:
an Double for the specified key or the specified alternate if no matching parameter is found

getNumber

public java.lang.Number getNumber(java.lang.String key)
Parameters:
key - the desired parameter's key
Returns:
a Number for the specified key or null if no matching parameter is found

getLocale

public java.util.Locale getLocale(java.lang.String key)
Parameters:
key - the desired parameter's key
Returns:
a Locale for the specified key or null if no matching parameter is found

getNumber

public java.lang.Number getNumber(java.lang.String key,
                                  java.lang.Number alternate)
Parameters:
key - the desired parameter's key
alternate - The alternate Number
Returns:
a Number for the specified key or the specified alternate if no matching parameter is found

getInt

public int getInt(java.lang.String key,
                  int alternate)
Parameters:
key - the desired parameter's key
alternate - The alternate int value
Returns:
the int value for the specified key or the specified alternate value if no matching parameter is found

getDouble

public double getDouble(java.lang.String key,
                        double alternate)
Parameters:
key - the desired parameter's key
alternate - The alternate double value
Returns:
the double value for the specified key or the specified alternate value if no matching parameter is found

getLocale

public java.util.Locale getLocale(java.lang.String key,
                                  java.util.Locale alternate)
Parameters:
key - the desired parameter's key
alternate - The alternate Locale
Returns:
a Locale for the specified key or the specified alternate if no matching parameter is found

getStrings

public java.lang.String[] getStrings(java.lang.String key)
Parameters:
key - the key for the desired parameter
Returns:
an array of String objects containing all of the values associated with the given key, or null if the no values are associated with the given key

getBooleans

public java.lang.Boolean[] getBooleans(java.lang.String key)
Parameters:
key - the key for the desired parameter
Returns:
an array of Boolean objects associated with the given key.

getNumbers

public java.lang.Number[] getNumbers(java.lang.String key)
Parameters:
key - the key for the desired parameter
Returns:
an array of Number objects associated with the given key, or null if Numbers are not associated with it.

getInts

public int[] getInts(java.lang.String key)
Parameters:
key - the key for the desired parameter
Returns:
an array of int values associated with the given key, or null if numbers are not associated with it.

getDoubles

public double[] getDoubles(java.lang.String key)
Parameters:
key - the key for the desired parameter
Returns:
an array of double values associated with the given key, or null if numbers are not associated with it.

getLocales

public java.util.Locale[] getLocales(java.lang.String key)
Parameters:
key - the key for the desired parameter
Returns:
an array of Locale objects associated with the given key, or null if Locales are not associated with it.

parseNumber

protected java.lang.Number parseNumber(java.lang.String value)
                                throws java.lang.NumberFormatException
Converts a parameter value into a Number This is used as the base for all numeric parsing methods. So, sub-classes can override to allow for customized number parsing. (e.g. to handle fractions, compound numbers, etc.)

Parameters:
value - the string to be parsed
Returns:
the value as a Number
Throws:
java.lang.NumberFormatException

parseBoolean

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. (e.g. to handle "Yes/No" or "T/F")

Parameters:
value - the string to be parsed
Returns:
the value as a Boolean

parseStringList

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 delimiter. The default delimiter is a comma.

Since:
VelocityTools 1.3

parseLocale

protected java.util.Locale parseLocale(java.lang.String value)
Converts a String value into a Locale.

Since:
VelocityTools 1.3


Copyright (c) 2003-2007 Apache Software Foundation