Class CmsParameterConfiguration
- All Implemented Interfaces:
Serializable
,Map<String,
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:
-
Each parameter in the file has the syntax
key = value
- The key may use any character but the equal sign '='.
- value may be separated on different lines if a backslash is placed at the end of the line that continues below.
- If value is a list of strings, each token is separated by a comma ','.
- Commas in each token are escaped placing a backslash right before the comma.
- Backslashes are escaped by using two consecutive backslashes i.e. \\. Note: Unlike in regular Java properties files, you don't need to escape Backslashes.
- If a key is used more than once, the values are appended as if they were on the same line separated with commas.
- Blank lines and lines starting with character '#' are skipped.
# 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?
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected static class
Used to read parameter lines from a property file.protected static class
This class divides property value into tokens separated by ",".Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,
V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object, V extends Object> -
Field Summary
Modifier and TypeFieldDescriptionstatic final CmsParameterConfiguration
An empty, immutable parameter configuration. -
Constructor Summary
ConstructorDescriptionCreates an empty parameter configuration.Creates a parameter configuration from an input stream.Creates a parameter wrapper by loading the parameters from the specified property file.CmsParameterConfiguration
(Map<String, String> configuration) Creates a parameter configuration from a Map of Strings. -
Method Summary
Modifier and TypeMethodDescriptionvoid
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, List<String> parametersToIgnore) Serializes this parameter configuration for the OpenCms XML configuration.void
clear()
boolean
containsKey
(Object key) boolean
containsValue
(Object value) protected static int
countPreceding
(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
(String line) Checks if the line ends with odd number of backslashes.entrySet()
Returns the String associated with the given parameter.boolean
getBoolean
(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
(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.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.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.Returns the raw Object associated with the given parameter, ornull
in case there is no Object for this parameter.getPrefixedProperties
(String keyPrefix) Creates a new Properties object from the existing configuration extracting all key-value pars whose key are prefixed with keyPrefix.Returns the String associated with the given parameter, or the given default value in case there is no value for this parameter.int
hashCode()
keySet()
void
load
(InputStream input) Load the parameters from the given input stream, which must be in property file format.Set a parameter for this configuration.void
Merges this parameter configuration with the provided other parameter configuration.Removes a parameter from this configuration.toString()
protected static String
Replaces escaped char sequences in the input value.static CmsParameterConfiguration
unmodifiableVersion
(CmsParameterConfiguration original) Returns an unmodifiable version of this parameter configuration.values()
Methods inherited from class java.util.AbstractMap
clone, equals, isEmpty, size
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Field Details
-
EMPTY_PARAMETERS
An empty, immutable parameter configuration.
-
-
Constructor Details
-
CmsParameterConfiguration
public CmsParameterConfiguration()Creates an empty parameter configuration. -
CmsParameterConfiguration
Creates a parameter configuration from an input stream.- Parameters:
in
- the input stream to create the parameter configuration from- Throws:
IOException
- in case of errors loading the parameters from the input stream
-
CmsParameterConfiguration
Creates a parameter configuration from a Map of Strings.- Parameters:
configuration
- the map of Strings to create the parameter configuration from
-
CmsParameterConfiguration
Creates a parameter wrapper by loading the parameters from the specified property file.- Parameters:
file
- the path of the file to load- Throws:
IOException
- in case of errors loading the parameters from the specified property file
-
-
Method Details
-
unmodifiableVersion
Returns an unmodifiable version of this parameter configuration.- Parameters:
original
- the configuration to make unmodifiable- Returns:
- an unmodifiable version of this parameter configuration
-
countPreceding
Counts the number of successive times 'ch' appears in the 'line' before the position indicated by the 'index'.- Parameters:
line
- the line to countindex
- the index position to startch
- the character to count- Returns:
- the number of successive times 'ch' appears in the 'line' before the position indicated by the 'index'
-
endsWithSlash
Checks if the line ends with odd number of backslashes.- Parameters:
line
- the line to check- Returns:
true
if the line ends with odd number of backslashes
-
unescape
Replaces escaped char sequences in the input value.- Parameters:
value
- the value to unescape- Returns:
- the unescaped String
-
add
Add a parameter to this configuration.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.
- Parameters:
key
- the parameter to addvalue
- the value to add
-
appendToXml
Serializes this parameter configuration for the OpenCms XML configuration.For each parameter, a XML node like this
<param name="theName">theValue</param>
is generated and appended to the provided parent node.- Parameters:
parentNode
- the parent node where the parameter nodes are appended to- Returns:
- the parent node
-
appendToXml
Serializes this parameter configuration for the OpenCms XML configuration.For each parameter, a XML node like this
<param name="theName">theValue</param>
is generated and appended to the provided parent node.- Parameters:
parentNode
- the parent node where the parameter nodes are appended toparametersToIgnore
- if notnull
, all parameters in this list are not written to the XML- Returns:
- the parent node
-
clear
-
containsKey
- Specified by:
containsKey
in interfaceMap<String,
String> - Overrides:
containsKey
in classAbstractMap<String,
String> - See Also:
-
containsValue
- Specified by:
containsValue
in interfaceMap<String,
String> - Overrides:
containsValue
in classAbstractMap<String,
String> - See Also:
-
entrySet
-
get
Returns the String associated with the given parameter. -
getBoolean
Returns the boolean associated with the given parameter, or the default value in case there is no boolean value for this parameter.- Parameters:
key
- the parameter to look up the value fordefaultValue
- the default value- Returns:
- the boolean associated with the given parameter, or the default value in case there is no boolean value for this parameter
-
getInteger
Returns the integer associated with the given parameter, or the default value in case there is no integer value for this parameter.- Parameters:
key
- the parameter to look up the value fordefaultValue
- the default value- Returns:
- the integer associated with the given parameter, or the default value in case there is no integer value for this parameter
-
getList
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.The list returned is a copy of the internal data of this object, and as such you may alter it freely.
- Parameters:
key
- the parameter to look up the value for- 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
-
getList
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.The list returned is a copy of the internal data of this object, and as such you may alter it freely.
- Parameters:
key
- the parameter to look up the value fordefaultValue
- the default value- 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
-
getObject
Returns the raw Object associated with the given parameter, ornull
in case there is no Object for this parameter.- Parameters:
key
- the parameter to look up the value for- Returns:
- the raw Object associated with the given parameter,
or
null
in case there is no Object for this parameter.
-
getPrefixedProperties
Creates a new Properties object from the existing configuration extracting all key-value pars whose key are prefixed with keyPrefix.For this example config:
# lines starting with # are comments db.pool.default.jdbcDriver=net.bull.javamelody.JdbcDriver db.pool.default.connectionProperties.driver=com.mysql.cj.jdbc.Driver
getPrefixedProperties("db.pool.default.connectionProperties") will return a Properties object with one single entry:key:"driver", value:"com.mysql.cj.jdbc.Driver"
- Parameters:
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- Returns:
- a new Properties object with all the entries from this configuration whose keys math the prefix
-
getString
Returns the String associated with the given parameter, or the given default value in case there is no value for this parameter.- Parameters:
key
- the parameter to look up the value fordefaultValue
- the default value- Returns:
- the String associated with the given parameter, or the given default value in case there is no value for this parameter.
-
hashCode
-
keySet
-
load
Load the parameters from the given input stream, which must be in property file format.- Parameters:
input
- the stream to load the input from- Throws:
IOException
- in case of IO errors reading from the stream
-
put
Set a parameter for this configuration.If the parameter already exists then the existing value will be replaced.
-
putAll
Merges this parameter configuration with the provided other parameter configuration.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. -
remove
Removes a parameter from this configuration. -
toString
- Overrides:
toString
in classAbstractMap<String,
String> - See Also:
-
java.util.Map#toString()
-
values
-