Class ValueParser
- java.lang.Object
-
- org.apache.velocity.tools.generic.SafeConfig
-
- org.apache.velocity.tools.generic.LocaleConfig
-
- org.apache.velocity.tools.generic.ConversionTool
-
- org.apache.velocity.tools.generic.ValueParser
-
- Direct Known Subclasses:
ParameterTool
@DefaultKey("parser") public class ValueParser extends ConversionTool implements Map<String,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$ $Date$
- Author:
- Nathan Bubna
-
-
Field Summary
Fields Modifier and Type Field Description static StringALLOWSUBKEYS_KEYThe key used for specifying whether to support subkeysstatic StringREADONLY_KEYThe key used for specifying whether to be read-only-
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
Constructors Constructor Description ValueParser()ValueParser(Map<String,Object> source)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()protected voidconfigure(ValueParser values)Does the actual configuration.booleancontainsKey(Object key)booleancontainsValue(Object value)Set<Map.Entry<String,Object>>entrySet()booleanexists(String key)Convenience method for checking whether a certain parameter exists.Objectget(Object key)Objectget(String key)Convenience method for use in Velocity templates.protected booleangetAllowSubkeys()Are subkeys allowed ?BooleangetBoolean(String key)booleangetBoolean(String key, boolean alternate)BooleangetBoolean(String key, Boolean alternate)Boolean[]getBooleans(String key)DoublegetDouble(String key)doublegetDouble(String key, double alternate)DoublegetDouble(String key, Double alternate)double[]getDoubles(String key)intgetInt(String key, int alternate)IntegergetInteger(String key)IntegergetInteger(String key, Integer alternate)int[]getInts(String key)LocalegetLocale(String key)LocalegetLocale(String key, Locale alternate)Locale[]getLocales(String key)NumbergetNumber(String key)NumbergetNumber(String key, Number alternate)Number[]getNumbers(String key)protected booleangetReadOnly()Is the Map read-only?protected Map<String,Object>getSource()StringgetString(String key)StringgetString(String key, String alternate)String[]getStrings(String key)protected ValueParsergetSubkey(String subkey)subkey getter that returns a map-> value for every "subkey.subkey2" found entry ObjectgetValue(String key)Returns the value mapped to the specified key in theMapreturned bygetSource().ObjectgetValue(String key, Object alternate)Object[]getValues(String key)booleanhasSubkeys()Determines whether there are subkeys available in the source map.booleanisEmpty()Set<String>keySet()Objectput(String key, Object value)voidputAll(Map<? extends String,? extends Object> m)Objectremove(Object key)protected voidsetAllowSubkeys(boolean allow)allow or disallow subkeysprotected voidsetReadOnly(boolean ro)Set or unset read-only behaviourprotected voidsetSource(Map<String,Object> source)intsize()StringtoString()Collectionvalues()-
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, toIntegers, 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
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
-
-
-
-
Field Detail
-
ALLOWSUBKEYS_KEY
public static final String ALLOWSUBKEYS_KEY
The key used for specifying whether to support subkeys- See Also:
- Constant Field Values
-
READONLY_KEY
public static final String READONLY_KEY
The key used for specifying whether to be read-only- See Also:
- Constant Field Values
-
-
Method Detail
-
getAllowSubkeys
protected boolean getAllowSubkeys()
Are subkeys allowed ?- Returns:
- yes/no
-
setAllowSubkeys
protected void setAllowSubkeys(boolean allow)
allow or disallow subkeys- Parameters:
allow-
-
getReadOnly
protected boolean getReadOnly()
Is the Map read-only?- Returns:
- yes/no
-
setReadOnly
protected void setReadOnly(boolean ro)
Set or unset read-only behaviour- Parameters:
ro-
-
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 classConversionTool
-
exists
public boolean exists(String key)
Convenience method for checking whether a certain parameter exists.- Parameters:
key- the parameter's key- Returns:
trueif a parameter exists for the specified key; otherwise, returnsfalse.
-
get
public Object get(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
nullif there is no matching parameter
-
getValue
public Object getValue(String key)
Returns the value mapped to the specified key in theMapreturned bygetSource(). If there is no source, then this will always returnnull.
-
getValue
public Object getValue(String key, Object alternate)
- Parameters:
key- the desired parameter's keyalternate- The alternate value- Returns:
- parameter matching the specified key or the specified alternate Object if there is no matching parameter
-
getString
public String getString(String key)
- Parameters:
key- the parameter's key- Returns:
- parameter matching the specified key or
nullif there is no matching parameter
-
getString
public String getString(String key, String alternate)
- Parameters:
key- the desired parameter's keyalternate- The alternate value- Returns:
- parameter matching the specified key or the specified alternate String if there is no matching parameter
-
getBoolean
public Boolean getBoolean(String key)
- Parameters:
key- the desired parameter's key- Returns:
- a
Booleanobject for the specified key ornullif no matching parameter is found
-
getBoolean
public boolean getBoolean(String key, boolean alternate)
- Parameters:
key- the desired parameter's keyalternate- The alternate boolean value- Returns:
- boolean value for the specified key or the alternate boolean is no value is found
-
getInteger
public Integer getInteger(String key)
- Parameters:
key- the desired parameter's key- Returns:
- a
Integerfor the specified key ornullif no matching parameter is found
-
getInteger
public Integer getInteger(String key, Integer alternate)
- Parameters:
key- the desired parameter's keyalternate- The alternate Integer- Returns:
- an Integer for the specified key or the specified alternate if no matching parameter is found
-
getDouble
public Double getDouble(String key)
- Parameters:
key- the desired parameter's key- Returns:
- a
Doublefor the specified key ornullif no matching parameter is found
-
getDouble
public Double getDouble(String key, Double alternate)
- Parameters:
key- the desired parameter's keyalternate- The alternate Double- Returns:
- an Double for the specified key or the specified alternate if no matching parameter is found
-
getNumber
public Number getNumber(String key)
- Parameters:
key- the desired parameter's key- Returns:
- a
Numberfor the specified key ornullif no matching parameter is found
-
getLocale
public Locale getLocale(String key)
- Parameters:
key- the desired parameter's key- Returns:
- a
Localefor the specified key ornullif no matching parameter is found
-
getNumber
public Number getNumber(String key, Number alternate)
- Parameters:
key- the desired parameter's keyalternate- The alternate Number- Returns:
- a Number for the specified key or the specified alternate if no matching parameter is found
-
getInt
public int getInt(String key, int alternate)
- Parameters:
key- the desired parameter's keyalternate- 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(String key, double alternate)
- Parameters:
key- the desired parameter's keyalternate- 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 Locale getLocale(String key, Locale alternate)
- Parameters:
key- the desired parameter's keyalternate- The alternate Locale- Returns:
- a Locale for the specified key or the specified alternate if no matching parameter is found
-
getStrings
public String[] getStrings(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
nullif the no values are associated with the given key
-
getBooleans
public Boolean[] getBooleans(String key)
- Parameters:
key- the key for the desired parameter- Returns:
- an array of Boolean objects associated with the given key.
-
getNumbers
public Number[] getNumbers(String key)
- Parameters:
key- the key for the desired parameter- Returns:
- an array of Number objects associated with the given key,
or
nullif Numbers are not associated with it.
-
getInts
public int[] getInts(String key)
- Parameters:
key- the key for the desired parameter- Returns:
- an array of int values associated with the given key,
or
nullif numbers are not associated with it.
-
getDoubles
public double[] getDoubles(String key)
- Parameters:
key- the key for the desired parameter- Returns:
- an array of double values associated with the given key,
or
nullif numbers are not associated with it.
-
getLocales
public Locale[] getLocales(String key)
- Parameters:
key- the key for the desired parameter- Returns:
- an array of Locale objects associated with the given key,
or
nullif Locales are not associated with it.
-
hasSubkeys
public boolean hasSubkeys()
Determines whether there are subkeys available in the source map.
-
getSubkey
protected ValueParser getSubkey(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
-
containsKey
public boolean containsKey(Object key)
- Specified by:
containsKeyin interfaceMap<String,Object>
-
containsValue
public boolean containsValue(Object value)
- Specified by:
containsValuein interfaceMap<String,Object>
-
values
public Collection values()
-
-