org.apache.velocity.tools.generic
Class ValueParser

java.lang.Object
  extended by org.apache.velocity.tools.generic.SafeConfig
      extended by org.apache.velocity.tools.generic.LocaleConfig
          extended by org.apache.velocity.tools.generic.ConversionTool
              extended by org.apache.velocity.tools.generic.ValueParser
All Implemented Interfaces:
java.util.Map<java.lang.String,java.lang.Object>
Direct Known Subclasses:
ParameterTool

@DefaultKey(value="parser")
public class ValueParser
extends ConversionTool
implements java.util.Map<java.lang.String,java.lang.Object>

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

This comes in very handy when parsing parameters.

When subkeys are allowed, getValue("foo") will also search for all keys of the form "foo.bar" and return a ValueParser of the type "bar" -> value for all found values.

TODO: someone doing java configuration ought to be able to put a source Map in the tool properties, allowing this to be used like other tools

Since:
VelocityTools 1.2
Version:
$Revision: 749732 $ $Date: 2009-03-03 12:23:59 -0800 (Tue, 03 Mar 2009) $
Author:
Nathan Bubna

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
java.util.Map.Entry<K,V>
 
Field Summary
private  boolean allowSubkeys
           
static java.lang.String ALLOWSUBKEYS_KEY
          The key used for specifying a whether to support subkeys
private  java.lang.Boolean hasSubkeys
           
private  java.util.Map<java.lang.String,java.lang.Object> source
           
 
Fields inherited from class org.apache.velocity.tools.generic.ConversionTool
DATE_FORMAT_KEY, DEFAULT_DATE_FORMAT, DEFAULT_NUMBER_FORMAT, DEFAULT_STRINGS_DELIMITER, DEFAULT_STRINGS_TRIM, NUMBER_FORMAT_KEY, STRINGS_DELIMITER_FORMAT_KEY, 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
ValueParser()
           
ValueParser(java.util.Map<java.lang.String,java.lang.Object> source)
           
 
Method Summary
 void clear()
           
protected  void configure(ValueParser values)
          Does the actual configuration.
 boolean containsKey(java.lang.Object key)
           
 boolean containsValue(java.lang.Object value)
           
 java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.Object>> entrySet()
           
 boolean exists(java.lang.String key)
          Convenience method for checking whether a certain parameter exists.
 java.lang.Object get(java.lang.Object key)
           
 java.lang.Object get(java.lang.String key)
          Convenience method for use in Velocity templates.
protected  boolean getAllowSubkeys()
          Are subkeys allowed ?
 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<java.lang.String,java.lang.Object> 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  ValueParser getSubkey(java.lang.String subkey)
          subkey getter that returns a map -> value for every "subkey.subkey2" found entry
 java.lang.Object getValue(java.lang.String key)
          Returns the value mapped to the specified key in the Map returned by getSource().
 java.lang.Object getValue(java.lang.String key, java.lang.Object alternate)
           
 java.lang.Object[] getValues(java.lang.String key)
           
 boolean hasSubkeys()
          Determines whether there are subkeys available in the source map.
 boolean isEmpty()
           
 java.util.Set<java.lang.String> keySet()
           
 java.lang.Object put(java.lang.String key, java.lang.Object value)
           
 void putAll(java.util.Map<? extends java.lang.String,? extends java.lang.Object> m)
           
 java.lang.Object remove(java.lang.Object key)
           
protected  void setAllowSubkeys(boolean allow)
          allow or disallow subkeys
protected  void setSource(java.util.Map<java.lang.String,java.lang.Object> source)
           
 int size()
           
 java.lang.String toString()
           
 java.util.Collection values()
           
 
Methods inherited from class org.apache.velocity.tools.generic.ConversionTool
getDateFormat, getNumberFormat, getStringsDelimiter, getStringsTrim, parseBoolean, parseDate, parseDate, parseDate, parseDate, parseDate, parseLocale, parseNumber, parseNumber, parseNumber, parseNumber, parseStringList, setDateFormat, setNumberFormat, setStringsDelimiter, setStringsTrim, toBoolean, toBooleans, toBooleans, toCalendar, toCalendars, toCalendars, toDate, toDates, toDates, toDouble, toDoubles, toInteger, toInts, toLocale, toLocales, toLocales, toNumber, toNumbers, toNumbers, toString, toStrings
 
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, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Field Detail

source

private java.util.Map<java.lang.String,java.lang.Object> source

allowSubkeys

private boolean allowSubkeys

hasSubkeys

private java.lang.Boolean hasSubkeys

ALLOWSUBKEYS_KEY

public static final java.lang.String ALLOWSUBKEYS_KEY
The key used for specifying a whether to support subkeys

See Also:
Constant Field Values
Constructor Detail

ValueParser

public ValueParser()

ValueParser

public ValueParser(java.util.Map<java.lang.String,java.lang.Object> source)
Method Detail

setSource

protected void setSource(java.util.Map<java.lang.String,java.lang.Object> source)

getSource

protected java.util.Map<java.lang.String,java.lang.Object> getSource()

getAllowSubkeys

protected boolean getAllowSubkeys()
Are subkeys allowed ?

Returns:
yes/no

setAllowSubkeys

protected void setAllowSubkeys(boolean allow)
allow or disallow subkeys

Parameters:
allow -

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:
configure in class ConversionTool

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

getValue

public java.lang.Object getValue(java.lang.String key)
Returns the value mapped to the specified key in the Map returned by getSource(). If there is no source, then this will always return null.


getValue

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

getValues

public java.lang.Object[] getValues(java.lang.String key)

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.

hasSubkeys

public boolean hasSubkeys()
Determines whether there are subkeys available in the source map.


getSubkey

protected ValueParser getSubkey(java.lang.String subkey)
subkey getter that returns a map -> value for every "subkey.subkey2" found entry

Parameters:
subkey - subkey to search for
Returns:
the map of found values

size

public int size()
Specified by:
size in interface java.util.Map<java.lang.String,java.lang.Object>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface java.util.Map<java.lang.String,java.lang.Object>

containsKey

public boolean containsKey(java.lang.Object key)
Specified by:
containsKey in interface java.util.Map<java.lang.String,java.lang.Object>

containsValue

public boolean containsValue(java.lang.Object value)
Specified by:
containsValue in interface java.util.Map<java.lang.String,java.lang.Object>

get

public java.lang.Object get(java.lang.Object key)
Specified by:
get in interface java.util.Map<java.lang.String,java.lang.Object>

put

public java.lang.Object put(java.lang.String key,
                            java.lang.Object value)
Specified by:
put in interface java.util.Map<java.lang.String,java.lang.Object>

remove

public java.lang.Object remove(java.lang.Object key)
Specified by:
remove in interface java.util.Map<java.lang.String,java.lang.Object>

putAll

public void putAll(java.util.Map<? extends java.lang.String,? extends java.lang.Object> m)
Specified by:
putAll in interface java.util.Map<java.lang.String,java.lang.Object>

clear

public void clear()
Specified by:
clear in interface java.util.Map<java.lang.String,java.lang.Object>

keySet

public java.util.Set<java.lang.String> keySet()
Specified by:
keySet in interface java.util.Map<java.lang.String,java.lang.Object>

values

public java.util.Collection values()
Specified by:
values in interface java.util.Map<java.lang.String,java.lang.Object>

entrySet

public java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.Object>> entrySet()
Specified by:
entrySet in interface java.util.Map<java.lang.String,java.lang.Object>

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object


Copyright (c) 2003-2007 Apache Software Foundation