Interface Configuration
- All Known Subinterfaces:
MutableConfiguration
- All Known Implementing Classes:
AbstractConfiguration
,DefaultConfiguration
,DefaultImmutableConfiguration
Configuration
is a interface encapsulating a configuration node
used to retrieve configuration values.
This is a "read only" interface preventing applications from modifying their own configurations. Once it is created, the information never changes.
Data Model
The data model is a subset of XML's; a single-rooted hierarchical tree where each
node can contain multiple attributes, and leaf nodes can also
contain a value. Reflecting this, Configuration
s are
usually built from an XML file by the DefaultConfigurationBuilder
class, or directly by a SAX parser using a SAXConfigurationHandler
or
NamespacedSAXConfigurationHandler
event handler.
Namespace support
Since version 4.1, each Configuration
node has a namespace
associated with it, in the form of a string, accessible through getNamespace()
. If no namespace is present, getNamespace
will
return blank (""). See DefaultConfigurationBuilder
for details on how
XML namespaces are mapped to Configuration
namespaces.
Example
As an example, consider two Configuration
s (with and without
namespaces) built from this XML:
<my-system version="1.3" xmlns:doc="http://myco.com/documentation"> <doc:desc>This is a highly fictitious config file</doc:desc> <widget name="fooWidget" initOrder="1" threadsafe="true"/> </my-system>
If namespace support is enabled (eg through new
DefaultConfigurationBuilder(true)
), then the xmlns:doc
element
will not translate into a Configuration attribute, and the
doc:desc
element will become a Configuration
node
with name "desc" and namespace "http://myco.com/documentation". The
widget
element will have namespace "".
If namespace support is disabled (the default for DefaultConfigurationBuilder
), the above XML will translate directly to
Configuration
nodes. The my-system
node will have
an attribute named "xmlns:doc", and a child called "doc:desc".
Assuming the Configuration
object is named conf
,
here is how the data could be retrieved:
Code | No namespaces | With namespaces |
---|---|---|
conf. | my-system | |
conf.
| 2 | 1 |
conf.
| 2 | |
conf.
| 1.3 | |
conf.
| fooWidget | |
conf. |
true | |
conf.
| file:///home/jeff/tmp/java/avalon/src/java/new.xconf:4:60 | |
conf.
| desc (see getChild(String) ) | desc |
conf.
| doc:desc | doc:desc (see getChild(String) ) |
conf.
| ConfigurationException | This is a highly fictitious config file |
conf.
| This is a highly fictitious config file | ConfigurationException |
conf.
| http://myco.com/documentation" |
Type-safe utility methods are provided for retrieving attribute and element
values as String
, int
, long
,
float
and boolean
.
Miscellanea
Currently, the configuration tree can only be traversed one node at a time,
eg., through getChild("foo")
or getChildren()
. In
a future release, it may be possible to access child nodes with an XPath-like
syntax.
Checking for the existence of an attribute can be done as follows:
String value = conf.getAttribute( "myAttribute", null ); if ( null == value ) { // Do the processing applicable if the attribute isn't present. }
- Version:
- CVS $Revision: 1.29 $ $Date: 2004/02/11 14:34:24 $
- Author:
- Avalon Development Team
-
Method Summary
Modifier and TypeMethodDescriptiongetAttribute
(String paramName) Return the value of specified attribute.getAttribute
(String name, String defaultValue) Returns the value of the attribute specified by its name as aString
, or the default value if no attribute by that name exists or is empty.boolean
getAttributeAsBoolean
(String paramName) Return theboolean
value of the specified parameter contained in this node.boolean
getAttributeAsBoolean
(String name, boolean defaultValue) Returns the value of the attribute specified by its name as aboolean
, or the default value if no attribute by that name exists or is empty.float
getAttributeAsFloat
(String paramName) Return thefloat
value of the specified parameter contained in this node.float
getAttributeAsFloat
(String name, float defaultValue) Returns the value of the attribute specified by its name as afloat
, or the default value if no attribute by that name exists or is empty.int
getAttributeAsInteger
(String paramName) Return theint
value of the specified attribute contained in this node.int
getAttributeAsInteger
(String name, int defaultValue) Returns the value of the attribute specified by its name as aint
, or the default value if no attribute by that name exists or is empty.long
getAttributeAsLong
(String name) Returns the value of the attribute specified by its name as along
.long
getAttributeAsLong
(String name, long defaultValue) Returns the value of the attribute specified by its name as along
, or the default value if no attribute by that name exists or is empty.String[]
Return an array of all attribute names.Return a newConfiguration
instance encapsulating the specified child node.Return aConfiguration
instance encapsulating the specified child node.Return anArray
ofConfiguration
elements containing all node children.getChildren
(String name) Return anArray
ofConfiguration
elements containing all node children with the specified name.Return a string describing location of Configuration.getName()
Return the name of the node.Returns a string indicating which namespace this Configuration node belongs to.getValue()
Return theString
value of the node.Returns the value of the configuration element as aString
.boolean
Return theboolean
value of the node.boolean
getValueAsBoolean
(boolean defaultValue) Returns the value of the configuration element as aboolean
.float
Return thefloat
value of the node.float
getValueAsFloat
(float defaultValue) Returns the value of the configuration element as afloat
.int
Return theint
value of the node.int
getValueAsInteger
(int defaultValue) Returns the value of the configuration element as anint
.long
Return thelong
value of the node.long
getValueAsLong
(long defaultValue) Returns the value of the configuration element as along
.
-
Method Details
-
getName
String getName()Return the name of the node.- Returns:
- name of the
Configuration
node.
-
getLocation
String getLocation()Return a string describing location of Configuration. Location can be different for different mediums (ie "file:line" for normal XML files or "table:primary-key" for DB based configurations);- Returns:
- a string describing location of Configuration
-
getNamespace
Returns a string indicating which namespace this Configuration node belongs to.What this returns is dependent on the configuration file and the Configuration builder. If the Configuration builder does not support namespaces, this method will return a blank string.
In the case of
DefaultConfigurationBuilder
, the namespace will be the URI associated with the XML element. Eg.,:<foo xmlns:x="http://blah.com"> <x:bar/> </foo>
The namespace of
foo
will be "", and the namespace ofbar
will be "http://blah.com".- Returns:
- a String identifying the namespace of this Configuration.
- Throws:
ConfigurationException
- if an error occurs- Since:
- 4.1
-
getChild
Return a newConfiguration
instance encapsulating the specified child node.If no such child node exists, an empty
Configuration
will be returned, allowing constructs such asconf.getChild("foo").getChild("bar").getChild("baz").
getValue
("default");If you wish to get a
null
return when no element is present, usegetChild("foo", false)
.- Parameters:
child
- The name of the child node.- Returns:
- Configuration
-
getChild
Return aConfiguration
instance encapsulating the specified child node.- Parameters:
child
- The name of the child node.createNew
- Iftrue
, a newConfiguration
will be created and returned if the specified child does not exist. Iffalse
,null
will be returned when the specified child doesn't exist.- Returns:
- Configuration
-
getChildren
Configuration[] getChildren()Return anArray
ofConfiguration
elements containing all node children. The array order will reflect the order in the source config file.- Returns:
- All child nodes
-
getChildren
Return anArray
ofConfiguration
elements containing all node children with the specified name. The array order will reflect the order in the source config file.- Parameters:
name
- The name of the children to get.- Returns:
- The child nodes with name
name
-
getAttributeNames
String[] getAttributeNames()Return an array of all attribute names.The order of attributes in this array can not be relied on. As with XML, a
Configuration
's attributes are an unordered set. If your code relies on order, eg conf.getAttributeNames()[0], then it is liable to break if a different XML parser is used.- Returns:
- a
String[]
value
-
getAttribute
Return the value of specified attribute.- Parameters:
paramName
- The name of the parameter you ask the value of.- Returns:
- String value of attribute.
- Throws:
ConfigurationException
- If no attribute with that name exists.
-
getAttributeAsInteger
Return theint
value of the specified attribute contained in this node.- Parameters:
paramName
- The name of the parameter you ask the value of.- Returns:
- int value of attribute
- Throws:
ConfigurationException
- If no parameter with that name exists. or if conversion toint
fails.
-
getAttributeAsLong
Returns the value of the attribute specified by its name as along
.- Parameters:
name
- The name of the parameter you ask the value of.- Returns:
- long value of attribute
- Throws:
ConfigurationException
- If no parameter with that name exists. or if conversion tolong
fails.
-
getAttributeAsFloat
Return thefloat
value of the specified parameter contained in this node.- Parameters:
paramName
- The name of the parameter you ask the value of.- Returns:
- float value of attribute
- Throws:
ConfigurationException
- If no parameter with that name exists. or if conversion tofloat
fails.
-
getAttributeAsBoolean
Return theboolean
value of the specified parameter contained in this node.- Parameters:
paramName
- The name of the parameter you ask the value of.- Returns:
- boolean value of attribute
- Throws:
ConfigurationException
- If no parameter with that name exists. or if conversion toboolean
fails.
-
getValue
Return theString
value of the node.- Returns:
- the value of the node.
- Throws:
ConfigurationException
- if an error occurs
-
getValueAsInteger
Return theint
value of the node.- Returns:
- the value of the node.
- Throws:
ConfigurationException
- If conversion toint
fails.
-
getValueAsFloat
Return thefloat
value of the node.- Returns:
- the value of the node.
- Throws:
ConfigurationException
- If conversion tofloat
fails.
-
getValueAsBoolean
Return theboolean
value of the node.- Returns:
- the value of the node.
- Throws:
ConfigurationException
- If conversion toboolean
fails.
-
getValueAsLong
Return thelong
value of the node.- Returns:
- the value of the node.
- Throws:
ConfigurationException
- If conversion tolong
fails.
-
getValue
Returns the value of the configuration element as aString
. If the configuration value is not set, the default value will be used.- Parameters:
defaultValue
- The default value desired.- Returns:
- String value of the
Configuration
, or default if none specified.
-
getValueAsInteger
int getValueAsInteger(int defaultValue) Returns the value of the configuration element as anint
. If the configuration value is not set, the default value will be used.- Parameters:
defaultValue
- The default value desired.- Returns:
- int value of the
Configuration
, or default if none specified.
-
getValueAsLong
long getValueAsLong(long defaultValue) Returns the value of the configuration element as along
. If the configuration value is not set, the default value will be used.- Parameters:
defaultValue
- The default value desired.- Returns:
- long value of the
Configuration
, or default if none specified.
-
getValueAsFloat
float getValueAsFloat(float defaultValue) Returns the value of the configuration element as afloat
. If the configuration value is not set, the default value will be used.- Parameters:
defaultValue
- The default value desired.- Returns:
- float value of the
Configuration
, or default if none specified.
-
getValueAsBoolean
boolean getValueAsBoolean(boolean defaultValue) Returns the value of the configuration element as aboolean
. If the configuration value is not set, the default value will be used.- Parameters:
defaultValue
- The default value desired.- Returns:
- boolean value of the
Configuration
, or default if none specified.
-
getAttribute
Returns the value of the attribute specified by its name as aString
, or the default value if no attribute by that name exists or is empty.- Parameters:
name
- The name of the attribute you ask the value of.defaultValue
- The default value desired.- Returns:
- String value of attribute. It will return the default value if the named attribute does not exist, or if the value is not set.
-
getAttributeAsInteger
Returns the value of the attribute specified by its name as aint
, or the default value if no attribute by that name exists or is empty.- Parameters:
name
- The name of the attribute you ask the value of.defaultValue
- The default value desired.- Returns:
- int value of attribute. It will return the default value if the named attribute does not exist, or if the value is not set.
-
getAttributeAsLong
Returns the value of the attribute specified by its name as along
, or the default value if no attribute by that name exists or is empty.- Parameters:
name
- The name of the attribute you ask the value of.defaultValue
- The default value desired.- Returns:
- long value of attribute. It will return the default value if the named attribute does not exist, or if the value is not set.
-
getAttributeAsFloat
Returns the value of the attribute specified by its name as afloat
, or the default value if no attribute by that name exists or is empty.- Parameters:
name
- The name of the attribute you ask the value of.defaultValue
- The default value desired.- Returns:
- float value of attribute. It will return the default value if the named attribute does not exist, or if the value is not set.
-
getAttributeAsBoolean
Returns the value of the attribute specified by its name as aboolean
, or the default value if no attribute by that name exists or is empty.- Parameters:
name
- The name of the attribute you ask the value of.defaultValue
- The default value desired.- Returns:
- boolean value of attribute. It will return the default value if the named attribute does not exist, or if the value is not set.
-