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 afinalattribute<list name="list.name"></list>
creates a named list withitemtags, optionally takes afinalattribute<item>value</item>
adds an item to thelistit'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 providedNameSelectorclass<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.userconfig.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
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidClears all the items from a list.clone()intReturns the number of lists that are present.intReturns 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 ConfigfromXmlFile(File file) Creates a newConfiginstance that is parsed from an XML file.static ConfigfromXmlFile(File file, HierarchicalProperties properties) Creates a newConfiginstance that is parsed from an XML file.static ConfigfromXmlResource(String resourceName, HierarchicalProperties properties) Creates a newConfiginstance that is parsed from an XML resource.static ConfigfromXmlResource(String resourceName, ResourceFinder resourceFinder) Creates a newConfiginstance that is parsed from an XML resource.static ConfigfromXmlResource(String resourceName, ResourceFinder resourceFinder, HierarchicalProperties properties) Creates a newConfiginstance that is parsed from an XML resource.booleanRetrieves the boolean value of a parameter.booleanRetrieves the boolean value of a parameter.getBoolItems(String list) Retrieves a list as boolean items.charRetrieves the char value of a parameter.charRetrieves the char value of a parameter.getCharItems(String list) Retrieves a list as char items.doubleRetrieves the double value of a parameter.doubleRetrieves the double value of a parameter.getDoubleItems(String list) Retrieves a list as double items.floatRetrieves the float value of a parameter.floatRetrieves the float value of a parameter.getFloatItems(String list) Retrieves a list as float items.intRetrieves the int value of a parameter.intRetrieves the int value of a parameter.getIntItems(String list) Retrieves a list as int items.longRetrieves the long value of a parameter.longRetrieves the long value of a parameter.getLongItems(String list) Retrieves a list as long items.Retrieves thePreferencesnode 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.booleanReturns whether a list exists.booleanhasParameter(String parameter) Returns whether a parameter exists.booleanIndicates whether this config instance is using aPreferencesnode.booleanisFinalList(String list) Indicates whether a list is final.booleanisFinalParameter(String parameter) Indicates whether a parameter is final.preferencesNode(Preferences node) Sets thePreferencesnode 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.voidRemoves a parameter.voidremoveList(String list) Removes a list.voidStore this config instance to its assignedPreferencesnode.voidstoreToPreferences(Preferences preferences) Store this config instance to a specificPreferencesnode.voidStore this config instance to its XML file.voidstoreToXml(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 newConfiginstance.- Since:
- 1.6.0
-
-
Method Details
-
fromXmlFile
Creates a newConfiginstance that is parsed from an XML file. it will be treated as such regardless.The
system hierarchical propertieswill 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 newConfiginstance 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 newConfiginstance that is parsed from an XML resource.The
class path resource finderwill 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 newConfiginstance that is parsed from an XML resource.The
system hierarchical propertieswill 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 newConfiginstance 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
nullif 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
nullif 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 thePreferencesnode 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 aPreferencesnode.- Returns:
trueif this config instance is using a preferences node; orfalseotherwise- Since:
- 1.6.0
-
getPreferencesNode
Retrieves thePreferencesnode that is used by this config instance.- Returns:
- the used preferences node; or
nullif no preferences node is used - Since:
- 1.6.0
-
hasParameter
Returns whether a parameter exists.- Parameters:
parameter- the name of the parameter- Returns:
trueof the parameter exists; orfalseotherwise- 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:
trueif the parameter is final; orfalseotherwise- 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
nullif 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
falseif 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
0if 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
0Lif 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
0Fif 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
0Dif 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
nullif 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-trueto make the parameter final; orfalseotherwise- 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:
trueif the list is final; orfalseotherwise- Since:
- 1.6.0
-
getStringItems
Retrieves a list as string items.- Parameters:
list- the name of the list- Returns:
- the requested list; or
nullof 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
nullof 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
nullof 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
nullof 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
nullof 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
nullof 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
nullof 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
nullof the list couldn't be found - Since:
- 1.6.0
-
hasList
Returns whether a list exists.- Parameters:
list- the name of the list- Returns:
trueif the list exists; orfalseotherwise- 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-trueto make the list final; orfalseotherwise- 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 assignedPreferencesnode.- 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 specificPreferencesnode.- Throws:
ConfigErrorException- when an error occurred during the storage- Since:
- 1.6.0
-
clone
-