Class Config
- All Implemented Interfaces:
Cloneable
Config
class manages the configuration of parameters and lists.
It is capable of being loaded from and stored into XML files or the JDK Preferences
mechanism.
The XML schema includes additional tags that can be used when manually writing config XML files. These are all the supported XML tags:
<param name="param.name">value</param>
sets a parameter to the provided value, optionally takes afinal
attribute<list name="list.name"></list>
creates a named list withitem
tags, optionally takes afinal
attribute<item>value</item>
adds an item to thelist
it's declared in<property name="prop.name"/>
will be replaced with the property value from the hierarchical properties<value name="param.name"/>
will be replaced with the previously declared parameter value of that name<selector class="package.name"/>
will be replaced with result of the providedNameSelector
class<include>config/name.xml</include>
includes another configuration that's looked up through the same resource finder
Here's an example of a complete config XML file:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE config SYSTEM "/dtd/config.dtd"> <config> <list name="list1"> <item>item1</item> <item>item2:<property name="prop.name"/></item> </list> <list name="list2" final="true"> <item>item3</item> <item>item4</item> </list> <param name="param1">5133</param> <param name="param2">astring<value name="param1"/></param> <param name="param3" final="true">value</param> <include>xml/config-<selector class="NameSelectorHostname"/>.xml</include> </config>
Two special configuration parameters can be used in the XML to indicate where config
should be stored to Preferences
.
If both of these are specified, then the user one will take precedence:
config.preferences.user
config.preferences.system
For example:
<config> <param name="config.preferences.user">/myapplication</param> <param name="default.language">en</param> </config>
When loading this config file from a resource that's not writable, changes can then still
be saved and stored in Preferences
using the storeToPreferences()
method.
As long as the special preferences parameter is part of the configuration, parameters and lists will be retrieved from the preferences node instead of from the static configuration file. If the preferences node doesn't exist or the parameter or list doesn't exist in the preferences node, then the static config value will be returned instead.
- Since:
- 1.6.0
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Clears all the items from a list.clone()
int
Returns the number of lists that are present.int
Returns the number of parameters that are present.Sets whether a list is final or not.finalParameter
(String parameter, boolean isFinal) Sets whether a parameter is final or not.static Config
fromXmlFile
(File file) Creates a newConfig
instance that is parsed from an XML file.static Config
fromXmlFile
(File file, HierarchicalProperties properties) Creates a newConfig
instance that is parsed from an XML file.static Config
fromXmlResource
(String resourceName, HierarchicalProperties properties) Creates a newConfig
instance that is parsed from an XML resource.static Config
fromXmlResource
(String resourceName, ResourceFinder resourceFinder) Creates a newConfig
instance that is parsed from an XML resource.static Config
fromXmlResource
(String resourceName, ResourceFinder resourceFinder, HierarchicalProperties properties) Creates a newConfig
instance that is parsed from an XML resource.boolean
Retrieves the boolean value of a parameter.boolean
Retrieves the boolean value of a parameter.getBoolItems
(String list) Retrieves a list as boolean items.char
Retrieves the char value of a parameter.char
Retrieves the char value of a parameter.getCharItems
(String list) Retrieves a list as char items.double
Retrieves the double value of a parameter.double
Retrieves the double value of a parameter.getDoubleItems
(String list) Retrieves a list as double items.float
Retrieves the float value of a parameter.float
Retrieves the float value of a parameter.getFloatItems
(String list) Retrieves a list as float items.int
Retrieves the int value of a parameter.int
Retrieves the int value of a parameter.getIntItems
(String list) Retrieves a list as int items.long
Retrieves the long value of a parameter.long
Retrieves the long value of a parameter.getLongItems
(String list) Retrieves a list as long items.Retrieves thePreferences
node that is used by this config instance.<TargetType extends Serializable>
TargetTypegetSerializable
(String parameter) Retrieves a serialized instance of a parameter.<TargetType extends Serializable>
TargetTypegetSerializable
(String parameter, TargetType defaultValue) Retrieves a serialized instance of a parameter.<TargetType extends Serializable>
List<TargetType>getSerializableItems
(String list) Retrieves a list as serializable items.Retrieves the string value of a parameter.Retrieves the string value of a parameter.getStringItems
(String list) Retrieves a list as string items.Retrieves the XML file that is used for this config instance.boolean
Returns whether a list exists.boolean
hasParameter
(String parameter) Returns whether a parameter exists.boolean
Indicates whether this config instance is using aPreferences
node.boolean
isFinalList
(String list) Indicates whether a list is final.boolean
isFinalParameter
(String parameter) Indicates whether a parameter is final.preferencesNode
(Preferences node) Sets thePreferences
node that should be used for storing this config data.Sets the boolean value of a parameter.Sets the char value of a parameter.Sets the double value of a parameter.Sets the float value of a parameter.Sets the int value of a parameter.Sets the long value of a parameter.put
(String parameter, Serializable value) Sets the serializable value of a parameter.Sets the string value of a parameter.Adds a new boolean item to a list.Adds a new char item to a list.Adds a new double item to a list.Adds a new float item to a list.Adds a new int item to a list.Adds a new long item to a list.putItem
(String list, Serializable item) Adds a new serializable item to a list.Adds a new string item to a list.void
Removes a parameter.void
removeList
(String list) Removes a list.void
Store this config instance to its assignedPreferences
node.void
storeToPreferences
(Preferences preferences) Store this config instance to a specificPreferences
node.void
Store this config instance to its XML file.void
storeToXml
(File destination) Store this config instance to a specific XML file.toXml()
Generates an XML presentation of the current state of this config instance.Set the XML file that should be used for this config instance.
-
Field Details
-
PARAMETER_PREFERENCES_USER
- See Also:
-
PARAMETER_PREFERENCES_SYSTEM
- See Also:
-
-
Constructor Details
-
Config
public Config()Creates a newConfig
instance.- Since:
- 1.6.0
-
-
Method Details
-
fromXmlFile
Creates a newConfig
instance that is parsed from an XML file. it will be treated as such regardless.The
system hierarchical properties
will be used.- Parameters:
file
- the XML file to parse- Returns:
- the resulting config instance
- Throws:
ConfigErrorException
- when an error occurred during parsing the XML file- Since:
- 1.6.0
-
fromXmlFile
public static Config fromXmlFile(File file, HierarchicalProperties properties) throws ConfigErrorException Creates a newConfig
instance that is parsed from an XML file.- Parameters:
file
- the XML file to parseproperties
- the hierarchical properties to use- Returns:
- the resulting config instance
- Throws:
ConfigErrorException
- when an error occurred during parsing the XML file- Since:
- 1.6.0
-
fromXmlResource
public static Config fromXmlResource(String resourceName, HierarchicalProperties properties) throws ConfigErrorException Creates a newConfig
instance that is parsed from an XML resource.The
class path resource finder
will be used.- Parameters:
resourceName
- the name of the resource to parseproperties
- the hierarchical properties to use- Returns:
- the resulting config instance
- Throws:
ConfigErrorException
- when an error occurred during parsing the XML resource- Since:
- 1.6.0
-
fromXmlResource
public static Config fromXmlResource(String resourceName, ResourceFinder resourceFinder) throws ConfigErrorException Creates a newConfig
instance that is parsed from an XML resource.The
system hierarchical properties
will be used.- Parameters:
resourceName
- the name of the resource to parseresourceFinder
- the resource finder to use- Returns:
- the resulting config instance
- Throws:
ConfigErrorException
- when an error occurred during parsing the XML resource- Since:
- 1.6.0
-
fromXmlResource
public static Config fromXmlResource(String resourceName, ResourceFinder resourceFinder, HierarchicalProperties properties) throws ConfigErrorException Creates a newConfig
instance that is parsed from an XML resource.- Parameters:
resourceName
- the name of the resource to parseproperties
- the hierarchical properties to useresourceFinder
- the resource finder to use- Returns:
- the resulting config instance
- Throws:
ConfigErrorException
- when an error occurred during parsing the XML resource- Since:
- 1.6.0
-
getXmlFile
Retrieves the XML file that is used for this config instance.This can be
null
if the config instance wasn't parsed from an XML file and the file wasn't explicitly set.- Returns:
- the XML file used for this config instance; or
null
if no XML file is used - Since:
- 1.6.0
-
xmlFile
Set the XML file that should be used for this config instance.This method simply changes the XML file, it doesn't perform storage or any other file-related operations.
- Parameters:
file
- the XML file to use- Returns:
- this config instance
- Since:
- 1.6.0
-
preferencesNode
Sets thePreferences
node that should be used for storing this config data.- Parameters:
node
- the preferences node to use- Returns:
- this config instance
- Since:
- 1.6.0
-
hasPreferencesNode
public boolean hasPreferencesNode()Indicates whether this config instance is using aPreferences
node.- Returns:
true
if this config instance is using a preferences node; orfalse
otherwise- Since:
- 1.6.0
-
getPreferencesNode
Retrieves thePreferences
node that is used by this config instance.- Returns:
- the used preferences node; or
null
if no preferences node is used - Since:
- 1.6.0
-
hasParameter
Returns whether a parameter exists.- Parameters:
parameter
- the name of the parameter- Returns:
true
of the parameter exists; orfalse
otherwise- Since:
- 1.6.0
-
isFinalParameter
Indicates whether a parameter is final.A final parameter can not be changed.
- Parameters:
parameter
- the name of the parameter- Returns:
true
if the parameter is final; orfalse
otherwise- Since:
- 1.6.0
-
countParameters
public int countParameters()Returns the number of parameters that are present.- Returns:
- the number of parameters
- Since:
- 1.6.0
-
getString
Retrieves the string value of a parameter.- Parameters:
parameter
- the name of the parameter- Returns:
- the string value; or
null
if the parameter doesn't exist - Since:
- 1.6.0
-
getString
Retrieves the string value of a parameter.- Parameters:
parameter
- the name of the parameterdefaultValue
- the default value to use when the parameter doesn't exist- Returns:
- the string value; or the provided default value if the parameter doesn't exist
- Since:
- 1.6.0
-
getBool
Retrieves the boolean value of a parameter.- Parameters:
parameter
- the name of the parameter- Returns:
- the boolean value; or
false
if the parameter doesn't exist - Since:
- 1.6.0
-
getBool
Retrieves the boolean value of a parameter.- Parameters:
parameter
- the name of the parameterdefaultValue
- the default value to use when the parameter doesn't exist- Returns:
- the boolean value; or the provided default value if the parameter doesn't exist
- Since:
- 1.6.0
-
getChar
Retrieves the char value of a parameter.- Parameters:
parameter
- the name of the parameter- Returns:
- the char value; or
'0'
if the parameter doesn't exist - Since:
- 1.6.0
-
getChar
Retrieves the char value of a parameter.- Parameters:
parameter
- the name of the parameterdefaultValue
- the default value to use when the parameter doesn't exist- Returns:
- the char value; or the provided default value if the parameter doesn't exist
- Since:
- 1.6.0
-
getInt
Retrieves the int value of a parameter.- Parameters:
parameter
- the name of the parameter- Returns:
- the int value; or
0
if the parameter doesn't exist - Since:
- 1.6.0
-
getInt
Retrieves the int value of a parameter.- Parameters:
parameter
- the name of the parameterdefaultValue
- the default value to use when the parameter doesn't exist- Returns:
- the int value; or the provided default value if the parameter doesn't exist
- Since:
- 1.6.0
-
getLong
Retrieves the long value of a parameter.- Parameters:
parameter
- the name of the parameter- Returns:
- the long value; or
0L
if the parameter doesn't exist - Since:
- 1.6.0
-
getLong
Retrieves the long value of a parameter.- Parameters:
parameter
- the name of the parameterdefaultValue
- the default value to use when the parameter doesn't exist- Returns:
- the long value; or the provided default value if the parameter doesn't exist
- Since:
- 1.6.0
-
getFloat
Retrieves the float value of a parameter.- Parameters:
parameter
- the name of the parameter- Returns:
- the float value; or
0F
if the parameter doesn't exist - Since:
- 1.6.0
-
getFloat
Retrieves the float value of a parameter.- Parameters:
parameter
- the name of the parameterdefaultValue
- the default value to use when the parameter doesn't exist- Returns:
- the float value; or the provided default value if the parameter doesn't exist
- Since:
- 1.6.0
-
getDouble
Retrieves the double value of a parameter.- Parameters:
parameter
- the name of the parameter- Returns:
- the double value; or
0D
if the parameter doesn't exist - Since:
- 1.6.0
-
getDouble
Retrieves the double value of a parameter.- Parameters:
parameter
- the name of the parameterdefaultValue
- the default value to use when the parameter doesn't exist- Returns:
- the double value; or the provided default value if the parameter doesn't exist
- Since:
- 1.6.0
-
getSerializable
Retrieves a serialized instance of a parameter.- Parameters:
parameter
- the name of the parameter- Returns:
- the instance of the serialized value; or
null
if the parameter doesn't exist or can't be deserialized - Since:
- 1.6.0
-
getSerializable
public <TargetType extends Serializable> TargetType getSerializable(String parameter, TargetType defaultValue) Retrieves a serialized instance of a parameter.- Parameters:
parameter
- the name of the parameterdefaultValue
- the default value to use when the parameter doesn't exist- Returns:
- the double value; or the provided default value if the parameter doesn't exist
- Since:
- 1.6.0
-
finalParameter
Sets whether a parameter is final or not.- Parameters:
parameter
- the name of the parameterisFinal
-true
to make the parameter final; orfalse
otherwise- Returns:
- this config instance
- Since:
- 1.6.0
-
put
Sets the string value of a parameter.- Parameters:
parameter
- the name of the parametervalue
- the value of the parameter- Returns:
- this config instance
- Since:
- 1.6.0
-
put
Sets the boolean value of a parameter.- Parameters:
parameter
- the name of the parametervalue
- the value of the parameter- Returns:
- this config instance
- Since:
- 1.6.0
-
put
Sets the char value of a parameter.- Parameters:
parameter
- the name of the parametervalue
- the value of the parameter- Returns:
- this config instance
- Since:
- 1.6.0
-
put
Sets the int value of a parameter.- Parameters:
parameter
- the name of the parametervalue
- the value of the parameter- Returns:
- this config instance
- Since:
- 1.6.0
-
put
Sets the long value of a parameter.- Parameters:
parameter
- the name of the parametervalue
- the value of the parameter- Returns:
- this config instance
- Since:
- 1.6.0
-
put
Sets the float value of a parameter.- Parameters:
parameter
- the name of the parametervalue
- the value of the parameter- Returns:
- this config instance
- Since:
- 1.6.0
-
put
Sets the double value of a parameter.- Parameters:
parameter
- the name of the parametervalue
- the value of the parameter- Returns:
- this config instance
- Since:
- 1.6.0
-
put
Sets the serializable value of a parameter.- Parameters:
parameter
- the name of the parametervalue
- the value of the parameter- Returns:
- this config instance
- Throws:
ConfigErrorException
- Since:
- 1.6.0
-
remove
Removes a parameter.- Parameters:
parameter
- the parameter name- Since:
- 1.6.0
-
isFinalList
Indicates whether a list is final.A final list can not be changed.
- Parameters:
list
- the name of the list- Returns:
true
if the list is final; orfalse
otherwise- Since:
- 1.6.0
-
getStringItems
Retrieves a list as string items.- Parameters:
list
- the name of the list- Returns:
- the requested list; or
null
of the list couldn't be found - Since:
- 1.6.0
-
getBoolItems
Retrieves a list as boolean items.- Parameters:
list
- the name of the list- Returns:
- the requested list; or
null
of the list couldn't be found - Since:
- 1.6.0
-
getCharItems
Retrieves a list as char items.Any item that couldn't be converted to a char will be excluded.
- Parameters:
list
- the name of the list- Returns:
- the requested list; or
null
of the list couldn't be found - Since:
- 1.6.0
-
getIntItems
Retrieves a list as int items.Any item that couldn't be converted to an int will be excluded.
- Parameters:
list
- the name of the list- Returns:
- the requested list; or
null
of the list couldn't be found - Since:
- 1.6.0
-
getLongItems
Retrieves a list as long items.Any item that couldn't be converted to a long will be excluded.
- Parameters:
list
- the name of the list- Returns:
- the requested list; or
null
of the list couldn't be found - Since:
- 1.6.0
-
getFloatItems
Retrieves a list as float items.Any item that couldn't be converted to a float will be excluded.
- Parameters:
list
- the name of the list- Returns:
- the requested list; or
null
of the list couldn't be found - Since:
- 1.6.0
-
getDoubleItems
Retrieves a list as double items.Any item that couldn't be converted to a double will be excluded.
- Parameters:
list
- the name of the list- Returns:
- the requested list; or
null
of the list couldn't be found - Since:
- 1.6.0
-
getSerializableItems
Retrieves a list as serializable items.Any item that couldn't be deserialized will be excluded.
- Parameters:
list
- the name of the list- Returns:
- the requested list; or
null
of the list couldn't be found - Since:
- 1.6.0
-
hasList
Returns whether a list exists.- Parameters:
list
- the name of the list- Returns:
true
if the list exists; orfalse
otherwise- Since:
- 1.6.0
-
countLists
public int countLists()Returns the number of lists that are present.- Returns:
- the number of lists
- Since:
- 1.6.0
-
putItem
Adds a new string item to a list.- Parameters:
list
- the name of the listitem
- the value of the item- Returns:
- this config instance
- Since:
- 1.6.0
-
putItem
Adds a new boolean item to a list.- Parameters:
list
- the name of the listitem
- the value of the item- Returns:
- this config instance
- Since:
- 1.6.0
-
putItem
Adds a new char item to a list.- Parameters:
list
- the name of the listitem
- the value of the item- Returns:
- this config instance
- Since:
- 1.6.0
-
putItem
Adds a new int item to a list.- Parameters:
list
- the name of the listitem
- the value of the item- Returns:
- this config instance
- Since:
- 1.6.0
-
putItem
Adds a new long item to a list.- Parameters:
list
- the name of the listitem
- the value of the item- Returns:
- this config instance
- Since:
- 1.6.0
-
putItem
Adds a new float item to a list.- Parameters:
list
- the name of the listitem
- the value of the item- Returns:
- this config instance
- Since:
- 1.6.0
-
putItem
Adds a new double item to a list.- Parameters:
list
- the name of the listitem
- the value of the item- Returns:
- this config instance
- Since:
- 1.6.0
-
putItem
Adds a new serializable item to a list.- Parameters:
list
- the name of the listitem
- the value of the item- Returns:
- this config instance
- Throws:
ConfigErrorException
- Since:
- 1.6.0
-
clearList
Clears all the items from a list.If the list doesn't exist, nothing will happen.
- Parameters:
list
- the name of the list- Since:
- 1.6.0
-
removeList
Removes a list.If the list doesn't exist, nothing will happen.
- Parameters:
list
- the name of the list- Since:
- 1.6.0
-
finalList
Sets whether a list is final or not.- Parameters:
list
- the name of the parameterisFinal
-true
to make the list final; orfalse
otherwise- Returns:
- this config instance
- Since:
- 1.6.0
-
toXml
Generates an XML presentation of the current state of this config instance.- Returns:
- the generates XML
- Since:
- 1.6.0
-
storeToXml
Store this config instance to its XML file.- Throws:
ConfigErrorException
- when an error occurred during the storage of the XML file or when no XML file was set- Since:
- 1.6.0
- See Also:
-
storeToXml
Store this config instance to a specific XML file.- Parameters:
destination
- the file to store this config instance to- Throws:
ConfigErrorException
- when an error occurred during the storage of the XML file- Since:
- 1.6.0
-
storeToPreferences
Store this config instance to its assignedPreferences
node.- Throws:
ConfigErrorException
- when an error occurred during the storage or when no preferences node was assigned- Since:
- 1.6.0
-
storeToPreferences
Store this config instance to a specificPreferences
node.- Throws:
ConfigErrorException
- when an error occurred during the storage- Since:
- 1.6.0
-
clone
-