public class CmsParameterConfiguration extends java.util.AbstractMap<java.lang.String,java.lang.String>
Usually the parameters are configured in some sort of String based file,
either in an XML configuration, or in a .property file.
This wrapper allows accessing such String values directly
as int
, boolean
or other data types, without
worrying about the type conversion.
It can also read a configuration from a special property file format, which is explained here:
key = value
# lines starting with # are comments # This is the simplest property key = value # A long property may be separated on multiple lines longvalue = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa # This is a property with many tokens tokens_on_a_line = first token, second token # This sequence generates exactly the same result tokens_on_multiple_lines = first token tokens_on_multiple_lines = second token # commas may be escaped in tokens commas.escaped = Hi\, what'up?
Modifier and Type | Class and Description |
---|---|
protected static class |
CmsParameterConfiguration.ParameterReader
Used to read parameter lines from a property file.
|
protected static class |
CmsParameterConfiguration.ParameterTokenizer
This class divides property value into tokens separated by ",".
|
Modifier and Type | Field and Description |
---|---|
static CmsParameterConfiguration |
EMPTY_PARAMETERS
An empty, immutable parameter configuration.
|
Constructor and Description |
---|
CmsParameterConfiguration()
Creates an empty parameter configuration.
|
CmsParameterConfiguration(java.io.InputStream in)
Creates a parameter configuration from an input stream.
|
CmsParameterConfiguration(java.util.Map<java.lang.String,java.lang.String> configuration)
Creates a parameter configuration from a Map of Strings.
|
CmsParameterConfiguration(java.lang.String file)
Creates a parameter wrapper by loading the parameters from the specified property file.
|
Modifier and Type | Method and Description |
---|---|
void |
add(java.lang.String key,
java.lang.String value)
Add a parameter to this configuration.
|
org.dom4j.Element |
appendToXml(org.dom4j.Element parentNode)
Serializes this parameter configuration for the OpenCms XML configuration.
|
org.dom4j.Element |
appendToXml(org.dom4j.Element parentNode,
java.util.List<java.lang.String> parametersToIgnore)
Serializes this parameter configuration for the OpenCms XML configuration.
|
void |
clear() |
boolean |
containsKey(java.lang.Object key) |
boolean |
containsValue(java.lang.Object value) |
protected static int |
countPreceding(java.lang.String line,
int index,
char ch)
Counts the number of successive times 'ch' appears in the
'line' before the position indicated by the 'index'.
|
protected static boolean |
endsWithSlash(java.lang.String line)
Checks if the line ends with odd number of backslashes.
|
java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.String>> |
entrySet() |
java.lang.String |
get(java.lang.Object key)
Returns the String associated with the given parameter.
|
boolean |
getBoolean(java.lang.String key,
boolean defaultValue)
Returns the boolean associated with the given parameter,
or the default value in case there is no boolean value for this parameter.
|
int |
getInteger(java.lang.String key,
int defaultValue)
Returns the integer associated with the given parameter,
or the default value in case there is no integer value for this parameter.
|
java.util.List<java.lang.String> |
getList(java.lang.String key)
Returns the List of Strings associated with the given parameter,
or an empty List in case there is no List of Strings for this parameter.
|
java.util.List<java.lang.String> |
getList(java.lang.String key,
java.util.List<java.lang.String> defaultValue)
Returns the List of Strings associated with the given parameter,
or the default value in case there is no List of Strings for this parameter.
|
java.lang.Object |
getObject(java.lang.String key)
Returns the raw Object associated with the given parameter,
or
null in case there is no Object for this parameter. |
java.util.Properties |
getPrefixedProperties(java.lang.String keyPrefix)
Creates a new Properties object from the existing configuration
extracting all key-value pars whose key are prefixed
with keyPrefix.
|
java.lang.String |
getString(java.lang.String key,
java.lang.String defaultValue)
Returns the String associated with the given parameter,
or the given default value in case there is no value for this parameter.
|
int |
hashCode() |
java.util.Set<java.lang.String> |
keySet() |
void |
load(java.io.InputStream input)
Load the parameters from the given input stream, which must be in property file format.
|
java.lang.String |
put(java.lang.String key,
java.lang.String value)
Set a parameter for this configuration.
|
void |
putAll(java.util.Map<? extends java.lang.String,? extends java.lang.String> other)
Merges this parameter configuration with the provided other parameter configuration.
|
java.lang.String |
remove(java.lang.Object key)
Removes a parameter from this configuration.
|
java.lang.String |
toString() |
protected static java.lang.String |
unescape(java.lang.String value)
Replaces escaped char sequences in the input value.
|
static CmsParameterConfiguration |
unmodifiableVersion(CmsParameterConfiguration original)
Returns an unmodifiable version of this parameter configuration.
|
java.util.Collection<java.lang.String> |
values() |
public static final CmsParameterConfiguration EMPTY_PARAMETERS
public CmsParameterConfiguration()
public CmsParameterConfiguration(java.io.InputStream in) throws java.io.IOException
in
- the input stream to create the parameter configuration fromjava.io.IOException
- in case of errors loading the parameters from the input streampublic CmsParameterConfiguration(java.util.Map<java.lang.String,java.lang.String> configuration)
configuration
- the map of Strings to create the parameter configuration frompublic CmsParameterConfiguration(java.lang.String file) throws java.io.IOException
file
- the path of the file to loadjava.io.IOException
- in case of errors loading the parameters from the specified property filepublic static CmsParameterConfiguration unmodifiableVersion(CmsParameterConfiguration original)
original
- the configuration to make unmodifiableprotected static int countPreceding(java.lang.String line, int index, char ch)
line
- the line to countindex
- the index position to startch
- the character to countprotected static boolean endsWithSlash(java.lang.String line)
line
- the line to checktrue
if the line ends with odd number of backslashesprotected static java.lang.String unescape(java.lang.String value)
value
- the value to unescapepublic void add(java.lang.String key, java.lang.String value)
If the parameter already exists then the value will be added to the existing configuration entry and a List will be created for the values.
String values separated by a comma "," will NOT be tokenized when this method is used. To create a List of String values for a parameter, call this method multiple times with the same parameter name.
key
- the parameter to addvalue
- the value to addpublic org.dom4j.Element appendToXml(org.dom4j.Element parentNode)
For each parameter, a XML node like this
<param name="theName">theValue</param>
is generated and appended to the provided parent node.
parentNode
- the parent node where the parameter nodes are appended topublic org.dom4j.Element appendToXml(org.dom4j.Element parentNode, java.util.List<java.lang.String> parametersToIgnore)
For each parameter, a XML node like this
<param name="theName">theValue</param>
is generated and appended to the provided parent node.
parentNode
- the parent node where the parameter nodes are appended toparametersToIgnore
- if not null
,
all parameters in this list are not written to the XMLpublic void clear()
clear
in interface java.util.Map<java.lang.String,java.lang.String>
clear
in class java.util.AbstractMap<java.lang.String,java.lang.String>
Map.clear()
public boolean containsKey(java.lang.Object key)
containsKey
in interface java.util.Map<java.lang.String,java.lang.String>
containsKey
in class java.util.AbstractMap<java.lang.String,java.lang.String>
Map.containsKey(java.lang.Object)
public boolean containsValue(java.lang.Object value)
containsValue
in interface java.util.Map<java.lang.String,java.lang.String>
containsValue
in class java.util.AbstractMap<java.lang.String,java.lang.String>
Map.containsValue(java.lang.Object)
public java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.String>> entrySet()
entrySet
in interface java.util.Map<java.lang.String,java.lang.String>
entrySet
in class java.util.AbstractMap<java.lang.String,java.lang.String>
Map.entrySet()
public java.lang.String get(java.lang.Object key)
get
in interface java.util.Map<java.lang.String,java.lang.String>
get
in class java.util.AbstractMap<java.lang.String,java.lang.String>
key
- the parameter to look up the value forpublic boolean getBoolean(java.lang.String key, boolean defaultValue)
key
- the parameter to look up the value fordefaultValue
- the default valuepublic int getInteger(java.lang.String key, int defaultValue)
key
- the parameter to look up the value fordefaultValue
- the default valuepublic java.util.List<java.lang.String> getList(java.lang.String key)
The list returned is a copy of the internal data of this object, and as such you may alter it freely.
key
- the parameter to look up the value forpublic java.util.List<java.lang.String> getList(java.lang.String key, java.util.List<java.lang.String> defaultValue)
The list returned is a copy of the internal data of this object, and as such you may alter it freely.
key
- the parameter to look up the value fordefaultValue
- the default valuepublic java.lang.Object getObject(java.lang.String key)
null
in case there is no Object for this parameter.key
- the parameter to look up the value fornull
in case there is no Object for this parameter.public java.lang.String getString(java.lang.String key, java.lang.String defaultValue)
key
- the parameter to look up the value fordefaultValue
- the default valuepublic int hashCode()
hashCode
in interface java.util.Map<java.lang.String,java.lang.String>
hashCode
in class java.util.AbstractMap<java.lang.String,java.lang.String>
Map.hashCode()
public java.util.Set<java.lang.String> keySet()
keySet
in interface java.util.Map<java.lang.String,java.lang.String>
keySet
in class java.util.AbstractMap<java.lang.String,java.lang.String>
Map.keySet()
public void load(java.io.InputStream input) throws java.io.IOException
input
- the stream to load the input fromjava.io.IOException
- in case of IO errors reading from the streampublic java.lang.String put(java.lang.String key, java.lang.String value)
If the parameter already exists then the existing value will be replaced.
put
in interface java.util.Map<java.lang.String,java.lang.String>
put
in class java.util.AbstractMap<java.lang.String,java.lang.String>
key
- the parameter to setvalue
- the value to setpublic void putAll(java.util.Map<? extends java.lang.String,? extends java.lang.String> other)
The difference form a simple Map<String, String>
is that for the parameter
configuration, the values of the keys in both maps are merged and kept in the Object store
as a List.
As result, this
configuration will be altered, the other configuration will
stay unchanged.
putAll
in interface java.util.Map<java.lang.String,java.lang.String>
putAll
in class java.util.AbstractMap<java.lang.String,java.lang.String>
other
- the other parameter configuration to merge this configuration withpublic java.lang.String remove(java.lang.Object key)
remove
in interface java.util.Map<java.lang.String,java.lang.String>
remove
in class java.util.AbstractMap<java.lang.String,java.lang.String>
key
- the parameter to removepublic java.lang.String toString()
toString
in class java.util.AbstractMap<java.lang.String,java.lang.String>
java.util.Map#toString()
public java.util.Collection<java.lang.String> values()
values
in interface java.util.Map<java.lang.String,java.lang.String>
values
in class java.util.AbstractMap<java.lang.String,java.lang.String>
Map.values()
public java.util.Properties getPrefixedProperties(java.lang.String keyPrefix)
For this example config:
# lines starting with # are comments db.pool.default.jdbcDriver=net.bull.javamelody.JdbcDriver db.pool.default.connectionProperties.driver=org.gjt.mm.mysql.DrivergetPrefixedProperties("db.pool.default.connectionProperties") will return a Properties object with one single entry:
key:"driver", value:"org.gjt.mm.mysql.Driver"
keyPrefix
- prefix to match. If it isn't already, it will be
terminated with a dot. If null, it will return
an empty Properties instance