Class ValueParser
- java.lang.Object
-
- org.apache.velocity.tools.generic.SafeConfig
-
- org.apache.velocity.tools.generic.LocaleConfig
-
- org.apache.velocity.tools.generic.FormatConfig
-
- org.apache.velocity.tools.generic.ValueParser
-
- All Implemented Interfaces:
java.util.Map<java.lang.String,java.lang.Object>
@DefaultKey("parser") @InvalidScope("session") @SkipSetters public class ValueParser extends FormatConfig 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$ $Date$
- Author:
- Nathan Bubna
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringALLOWSUBKEYS_KEYThe key used for specifying whether to support subkeysstatic java.lang.StringDEFAULT_STRINGS_DELIMITERstatic java.lang.StringREADONLY_KEYThe key used for specifying whether to be read-onlystatic java.lang.StringSTRINGS_DELIMITER_FORMAT_KEY-
Fields inherited from class org.apache.velocity.tools.generic.FormatConfig
DEFAULT_FORMAT, FORMAT_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, log, LOGGER_NAME_KEY, SAFE_MODE_KEY, USE_CLASS_LOGGER_KEY
-
-
Constructor Summary
Constructors Constructor Description ValueParser()ValueParser(java.util.Map<java.lang.String,java.lang.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(java.lang.Object key)booleancontainsValue(java.lang.Object value)java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.Object>>entrySet()booleanexists(java.lang.String key)Convenience method for checking whether a certain parameter exists.java.lang.Objectget(java.lang.Object key)java.lang.Objectget(java.lang.String key)Convenience method for use in Velocity templates.protected booleangetAllowSubkeys()Are subkeys allowed ?java.lang.BooleangetBoolean(java.lang.String key)booleangetBoolean(java.lang.String key, boolean alternate)java.lang.BooleangetBoolean(java.lang.String key, java.lang.Boolean alternate)java.lang.Boolean[]getBooleans(java.lang.String key)java.lang.DoublegetDouble(java.lang.String key)doublegetDouble(java.lang.String key, double alternate)java.lang.DoublegetDouble(java.lang.String key, java.lang.Double alternate)double[]getDoubles(java.lang.String key)intgetInt(java.lang.String key, int alternate)java.lang.IntegergetInteger(java.lang.String key)java.lang.IntegergetInteger(java.lang.String key, java.lang.Integer alternate)int[]getInts(java.lang.String key)java.util.LocalegetLocale(java.lang.String key)java.util.LocalegetLocale(java.lang.String key, java.util.Locale alternate)java.util.Locale[]getLocales(java.lang.String key)java.lang.LonggetLong(java.lang.String key)java.lang.LonggetLong(java.lang.String key, java.lang.Long alternate)java.lang.NumbergetNumber(java.lang.String key)java.lang.NumbergetNumber(java.lang.String key, java.lang.Number alternate)java.lang.Number[]getNumbers(java.lang.String key)protected booleangetReadOnly()Is the Map read-only?protected java.util.Map<java.lang.String,java.lang.Object>getSource()protected java.util.Map<java.lang.String,java.lang.Object>getSource(boolean create)java.lang.StringgetString(java.lang.String key)java.lang.StringgetString(java.lang.String key, java.lang.String alternate)java.lang.String[]getStrings(java.lang.String key)ValueParsergetSubkey(java.lang.String subkey)subkey getter that returns a map subkey#2 -> value for every "subkey.subkey2" found entryjava.util.Set<java.lang.String>getSubkeys()returns the set of all possible first-level subkeys, including complete keys without dots (or returns keySet() if allowSubKeys is false)java.lang.ObjectgetValue(java.lang.String key)Returns the value mapped to the specified key in theMapreturned bygetSource().java.lang.ObjectgetValue(java.lang.String key, java.lang.Object alternate)java.lang.Object[]getValues(java.lang.String key)Returns an array of values.booleanhasSubkeys()Determines whether there are subkeys available in the source map.booleanisEmpty()java.util.Set<java.lang.String>keySet()protected java.lang.String[]parseStringList(java.lang.String value)java.lang.Objectput(java.lang.String key, java.lang.Object value)voidputAll(java.util.Map<? extends java.lang.String,? extends java.lang.Object> m)java.lang.Objectremove(java.lang.Object key)protected voidsetAllowSubkeys(boolean allow)allow or disallow subkeysprotected voidsetReadOnly(boolean ro)Set or unset read-only behaviourprotected voidsetSource(java.util.Map<java.lang.String,java.lang.Object> source)protected voidsetStringsDelimiter(java.lang.String stringsDelimiter)Sets the delimiter used for separating values in a single String value.intsize()java.lang.StringtoString()java.util.Collectionvalues()-
Methods inherited from class org.apache.velocity.tools.generic.FormatConfig
getFormat, setFormat
-
Methods inherited from class org.apache.velocity.tools.generic.LocaleConfig
getLocale, setLocale, toLocale
-
Methods inherited from class org.apache.velocity.tools.generic.SafeConfig
configure, getLog, initLogger, isConfigLocked, isSafeMode, setLockConfig, setSafeMode
-
-
-
-
Field Detail
-
STRINGS_DELIMITER_FORMAT_KEY
public static final java.lang.String STRINGS_DELIMITER_FORMAT_KEY
- See Also:
- Constant Field Values
-
DEFAULT_STRINGS_DELIMITER
public static final java.lang.String DEFAULT_STRINGS_DELIMITER
- See Also:
- Constant Field Values
-
ALLOWSUBKEYS_KEY
public static final java.lang.String ALLOWSUBKEYS_KEY
The key used for specifying whether to support subkeys- See Also:
- Constant Field Values
-
READONLY_KEY
public static final java.lang.String READONLY_KEY
The key used for specifying whether to be read-only- See Also:
- Constant Field Values
-
-
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(boolean create)
-
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- flag value
-
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- flag value
-
setStringsDelimiter
protected final void setStringsDelimiter(java.lang.String stringsDelimiter)
Sets the delimiter used for separating values in a single String value. The default string delimiter is a comma.- Parameters:
stringsDelimiter- strings delimiter- See Also:
getValues(String)
-
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 classFormatConfig- Parameters:
values- configuration values
-
exists
public boolean exists(java.lang.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 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
nullif there is no matching parameter
-
getValue
public java.lang.Object getValue(java.lang.String key)
Returns the value mapped to the specified key in theMapreturned bygetSource(). If there is no source, then this will always returnnull.- Parameters:
key- property key- Returns:
- property value, or null
-
getValue
public java.lang.Object getValue(java.lang.String key, java.lang.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
-
parseStringList
protected java.lang.String[] parseStringList(java.lang.String value)
-
getValues
public java.lang.Object[] getValues(java.lang.String key)
Returns an array of values. If the internal value is a string, it is split using the configured delimitor (',' by default).
If the internal value is not an array or is a string without any delimiter, a singletin array is returned.
- Parameters:
key- the desired parameter's key- Returns:
- array of values, or null of the key has not been found. specified alternate Object 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
nullif 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 keyalternate- 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
Booleanobject for the specified key ornullif no matching parameter is found
-
getBoolean
public boolean getBoolean(java.lang.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
-
getBoolean
public java.lang.Boolean getBoolean(java.lang.String key, java.lang.Boolean alternate)- Parameters:
key- the desired parameter's keyalternate- the alternateBoolean- Returns:
- a
Booleanfor 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
Integerfor the specified key ornullif 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 keyalternate- The alternate Integer- Returns:
- an Integer for the specified key or the specified alternate if no matching parameter is found
-
getLong
public java.lang.Long getLong(java.lang.String key)
- Parameters:
key- the desired parameter's key- Returns:
- a
Longfor the specified key ornullif no matching parameter is found
-
getLong
public java.lang.Long getLong(java.lang.String key, java.lang.Long alternate)- Parameters:
key- the desired parameter's keyalternate- The alternate Long- Returns:
- a Long 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
Doublefor the specified key ornullif 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 keyalternate- 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
Numberfor the specified key ornullif no matching parameter is found
-
getLocale
public java.util.Locale getLocale(java.lang.String key)
- Parameters:
key- the desired parameter's key- Returns:
- a
Localefor the specified key ornullif 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 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(java.lang.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(java.lang.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 java.util.Locale getLocale(java.lang.String key, java.util.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 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
nullif 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
nullif 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
nullif 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
nullif 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
nullif Locales are not associated with it.
-
hasSubkeys
public boolean hasSubkeys()
Determines whether there are subkeys available in the source map.- Returns:
trueif there are subkeys (key names containing a dot)
-
getSubkeys
public java.util.Set<java.lang.String> getSubkeys()
returns the set of all possible first-level subkeys, including complete keys without dots (or returns keySet() if allowSubKeys is false)- Returns:
- the set of all possible first-level subkeys
-
getSubkey
public ValueParser getSubkey(java.lang.String subkey)
subkey getter that returns a map subkey#2 -> 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:
sizein interfacejava.util.Map<java.lang.String,java.lang.Object>
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmptyin interfacejava.util.Map<java.lang.String,java.lang.Object>
-
containsKey
public boolean containsKey(java.lang.Object key)
- Specified by:
containsKeyin interfacejava.util.Map<java.lang.String,java.lang.Object>
-
containsValue
public boolean containsValue(java.lang.Object value)
- Specified by:
containsValuein interfacejava.util.Map<java.lang.String,java.lang.Object>
-
get
public java.lang.Object get(java.lang.Object key)
- Specified by:
getin interfacejava.util.Map<java.lang.String,java.lang.Object>
-
put
public java.lang.Object put(java.lang.String key, java.lang.Object value)- Specified by:
putin interfacejava.util.Map<java.lang.String,java.lang.Object>
-
remove
public java.lang.Object remove(java.lang.Object key)
- Specified by:
removein interfacejava.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:
putAllin interfacejava.util.Map<java.lang.String,java.lang.Object>
-
clear
public void clear()
- Specified by:
clearin interfacejava.util.Map<java.lang.String,java.lang.Object>
-
keySet
public java.util.Set<java.lang.String> keySet()
- Specified by:
keySetin interfacejava.util.Map<java.lang.String,java.lang.Object>
-
values
public java.util.Collection values()
- Specified by:
valuesin interfacejava.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:
entrySetin interfacejava.util.Map<java.lang.String,java.lang.Object>
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-