org.apache.velocity.tools.generic

## 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:
  Map<String,Object>

    @DefaultKey(value="parser")
     @InvalidScope(value="session")
     @SkipSetters
    public class ValueParser
    extends FormatConfig
    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

### Nested Class Summary

### Nested classes/interfaces inherited from interface java.util.Map

`Map.Entry<K,V>`

### Field Summary

Fields
`static String` `ALLOWSUBKEYS_KEY` The key used for specifying whether to support subkeys
`static String` `DEFAULT_STRINGS_DELIMITER`
`static String` `READONLY_KEY` The key used for specifying whether to be read-only
`static String` `STRINGS_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 and Description
`ValueParser()`
`ValueParser(Map<String,Object> source)`

### Method Summary

All Methods Instance Methods Concrete Methods
`void` `clear()`
`protected void` `configure(ValueParser values)` Does the actual configuration.
`boolean` `containsKey(Object key)`
`boolean` `containsValue(Object value)`
`Set<Map.Entry<String,Object>>` `entrySet()`
`boolean` `exists(String key)` Convenience method for checking whether a certain parameter exists.
`Object` `get(Object key)`
`Object` `get(String key)` Convenience method for use in Velocity templates.
`protected boolean` `getAllowSubkeys()` Are subkeys allowed ?
`Boolean` `getBoolean(String key)`
`boolean` `getBoolean(String key, boolean alternate)`
`Boolean` `getBoolean(String key, Boolean alternate)`
`Boolean[]` `getBooleans(String key)`
`Double` `getDouble(String key)`
`double` `getDouble(String key, double alternate)`
`Double` `getDouble(String key, Double alternate)`
`double[]` `getDoubles(String key)`
`int` `getInt(String key, int alternate)`
`Integer` `getInteger(String key)`
`Integer` `getInteger(String key, Integer alternate)`
`int[]` `getInts(String key)`
`Locale` `getLocale(String key)`
`Locale` `getLocale(String key, Locale alternate)`
`Locale[]` `getLocales(String key)`
`Long` `getLong(String key)`
`Long` `getLong(String key, Long alternate)`
`Number` `getNumber(String key)`
`Number` `getNumber(String key, Number alternate)`
`Number[]` `getNumbers(String key)`
`protected boolean` `getReadOnly()` Is the Map read-only?
`protected Map<String,Object>` `getSource()`
`protected Map<String,Object>` `getSource(boolean create)`
`String` `getString(String key)`
`String` `getString(String key, String alternate)`
`String[]` `getStrings(String key)`
`ValueParser` `getSubkey(String subkey)` subkey getter that returns a map subkey#2 -> value for every "subkey.subkey2" found entry
`Set<String>` `getSubkeys()` returns the set of all possible first-level subkeys, including complete keys without dots (or returns keySet() if allowSubKeys is false)
`Object` `getValue(String key)` Returns the value mapped to the specified key in the `Map` returned by `getSource()`.
`Object` `getValue(String key, Object alternate)`
`Object[]` `getValues(String key)` Returns an array of values.
`boolean` `hasSubkeys()` Determines whether there are subkeys available in the source map.
`boolean` `isEmpty()`
`Set<String>` `keySet()`
`protected String[]` `parseStringList(String value)`
`Object` `put(String key, Object value)`
`void` `putAll(Map<? extends String,? extends Object> m)`
`Object` `remove(Object key)`
`protected void` `setAllowSubkeys(boolean allow)` allow or disallow subkeys
`protected void` `setReadOnly(boolean ro)` Set or unset read-only behaviour
`protected void` `setSource(Map<String,Object> source)`
`protected void` `setStringsDelimiter(String stringsDelimiter)` Sets the delimiter used for separating values in a single String value.
`int` `size()`
`String` `toString()`
`Collection` `values()`

### 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`

### 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

#### STRINGS_DELIMITER_FORMAT_KEY

    public static final String STRINGS_DELIMITER_FORMAT_KEY

See Also:
  Constant Field Values

#### DEFAULT_STRINGS_DELIMITER

    public static final String DEFAULT_STRINGS_DELIMITER

See Also:
  Constant Field Values

#### 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

### Constructor Detail

#### ValueParser

    public ValueParser()

#### ValueParser

    public ValueParser(Map<String,Object> source)

### Method Detail

#### setSource

    protected void setSource(Map<String,Object> source)

#### getSource

    protected Map<String,Object> getSource(boolean create)

#### getSource

    protected Map<String,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(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:
  `configure` in class `FormatConfig`
Parameters:
  `values` - configuration values

#### exists

    public boolean exists(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 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 `null` if there is no matching parameter

#### getValue

    public Object getValue(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`.

Parameters:
  `key` - property key
Returns:
  property value, or null

#### getValue

    public Object getValue(String key,
                           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

#### parseStringList

    protected String[] parseStringList(String value)

#### getValues

    public Object[] getValues(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 String getString(String key)

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

#### getString

    public String getString(String key,
                            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 Boolean getBoolean(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(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 Boolean getBoolean(String key,
                              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 Integer getInteger(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 Integer getInteger(String key,
                              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

#### getLong

    public Long getLong(String key)

Parameters:
  `key` - the desired parameter's key
Returns:
  a `Long` for the specified key or `null` if no matching parameter is found

#### getLong

    public Long getLong(String key,
                        Long alternate)

Parameters:
  `key` - the desired parameter's key
  `alternate` - The alternate Long
Returns:
  a Long 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 `Double` for the specified key or `null` if no matching parameter is found

#### getDouble

    public Double getDouble(String key,
                            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 Number getNumber(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 Locale getLocale(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 Number getNumber(String key,
                            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(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(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 Locale getLocale(String key,
                            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 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 `null` if 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 `null` if 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 `null` if 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 `null` if 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 `null` if Locales are not associated with it.

#### hasSubkeys

    public boolean hasSubkeys()

Determines whether there are subkeys available in the source map.

Returns:
  `true` if there are subkeys (key names containing a dot)

#### getSubkeys

    public Set<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(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:
  `size` in interface `Map<String,Object>`

#### isEmpty

    public boolean isEmpty()

Specified by:
  `isEmpty` in interface `Map<String,Object>`

#### containsKey

    public boolean containsKey(Object key)

Specified by:
  `containsKey` in interface `Map<String,Object>`

#### containsValue

    public boolean containsValue(Object value)

Specified by:
  `containsValue` in interface `Map<String,Object>`

#### get

    public Object get(Object key)

Specified by:
  `get` in interface `Map<String,Object>`

#### put

    public Object put(String key,
                      Object value)

Specified by:
  `put` in interface `Map<String,Object>`

#### remove

    public Object remove(Object key)

Specified by:
  `remove` in interface `Map<String,Object>`

#### putAll

    public void putAll(Map<? extends String,? extends Object> m)

Specified by:
  `putAll` in interface `Map<String,Object>`

#### clear

    public void clear()

Specified by:
  `clear` in interface `Map<String,Object>`

#### keySet

    public Set<String> keySet()

Specified by:
  `keySet` in interface `Map<String,Object>`

#### values

    public Collection values()

Specified by:
  `values` in interface `Map<String,Object>`

#### entrySet

    public Set<Map.Entry<String,Object>> entrySet()

Specified by:
  `entrySet` in interface `Map<String,Object>`

#### toString

    public String toString()

Overrides:
  `toString` in class `Object`