Interface Configuration
-
public interface ConfigurationConfigurationis 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,
Configurations 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
Configurationnode has a namespace associated with it, in the form of a string, accessible throughgetNamespace(). If no namespace is present,getNamespacewill return blank (""). See DefaultConfigurationBuilder for details on how XML namespaces are mapped toConfigurationnamespaces.Example
As an example, consider two
Configurations (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 DefaultConfigurationBuilder#DefaultConfigurationBuilder(boolean) new DefaultConfigurationBuilder(true)), then the
xmlns:docelement will not translate into a Configuration attribute, and thedoc:descelement will become aConfigurationnode with name "desc" and namespace "http://myco.com/documentation". Thewidgetelement will have namespace "".If namespace support is disabled (the default for DefaultConfigurationBuilder), the above XML will translate directly to
Configurationnodes. Themy-systemnode will have an attribute named "xmlns:doc", and a child called "doc:desc".Assuming the
Configurationobject is namedconf, here is how the data could be retrieved:Code No namespaces With namespaces conf.getName()my-system conf.getAttributeNames().length2 1 conf.getChildren().length2 conf.getAttributeAsFloat("version")1.3 conf.getChild("widget").getAttribute("name")fooWidget conf.getChild("widget") .getAttributeAsBoolean("threadsafe")trueconf.getChild("widget").getLocation()file:///home/jeff/tmp/java/avalon/src/java/new.xconf:4:60 conf.getChild("desc").getName()desc (see getChild(String))desc conf.getChild("doc:desc").getName()doc:desc doc:desc (see getChild(String))conf.getChild("desc").getValue()ConfigurationExceptionThis is a highly fictitious config file conf.getChild("doc:desc").getValue()This is a highly fictitious config file ConfigurationExceptionconf.getChild("desc").getNamespace()http://myco.com/documentation" Type-safe utility methods are provided for retrieving attribute and element values as
String,int,long,floatandboolean.Miscellanea
Currently, the configuration tree can only be traversed one node at a time, eg., through
getChild("foo")orgetChildren(). 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:
- $Id: Configuration.java 156533 2005-03-08 08:51:40 -0600 (Tue, 08 Mar 2005) leif $
- Author:
- Avalon Development Team
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description java.lang.StringgetAttribute(java.lang.String paramName)Return the value of specified attribute.java.lang.StringgetAttribute(java.lang.String name, java.lang.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.booleangetAttributeAsBoolean(java.lang.String paramName)Return thebooleanvalue of the specified parameter contained in this node.booleangetAttributeAsBoolean(java.lang.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.doublegetAttributeAsDouble(java.lang.String paramName)Return thedoublevalue of the specified parameter contained in this node.doublegetAttributeAsDouble(java.lang.String name, double defaultValue)Returns the value of the attribute specified by its name as adouble, or the default value if no attribute by that name exists or is empty.floatgetAttributeAsFloat(java.lang.String paramName)Return thefloatvalue of the specified parameter contained in this node.floatgetAttributeAsFloat(java.lang.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.intgetAttributeAsInteger(java.lang.String paramName)Return theintvalue of the specified attribute contained in this node.intgetAttributeAsInteger(java.lang.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.longgetAttributeAsLong(java.lang.String name)Returns the value of the attribute specified by its name as along.longgetAttributeAsLong(java.lang.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.java.lang.String[]getAttributeNames()Return an array of all attribute names.ConfigurationgetChild(java.lang.String child)Return a newConfigurationinstance encapsulating the specified child node.ConfigurationgetChild(java.lang.String child, boolean createNew)Return aConfigurationinstance encapsulating the specified child node.Configuration[]getChildren()Return anArrayofConfigurationelements containing all node children.Configuration[]getChildren(java.lang.String name)Return anArrayofConfigurationelements containing all node children with the specified name.java.lang.StringgetLocation()Return a string describing location of Configuration.java.lang.StringgetName()Return the name of the node.java.lang.StringgetNamespace()Returns a string indicating which namespace this Configuration node belongs to.java.lang.StringgetValue()Return theStringvalue of the node.java.lang.StringgetValue(java.lang.String defaultValue)Returns the value of the configuration element as aString.booleangetValueAsBoolean()Return thebooleanvalue of the node.booleangetValueAsBoolean(boolean defaultValue)Returns the value of the configuration element as aboolean.doublegetValueAsDouble()Return thedoublevalue of the node.doublegetValueAsDouble(double defaultValue)Returns the value of the configuration element as adouble.floatgetValueAsFloat()Return thefloatvalue of the node.floatgetValueAsFloat(float defaultValue)Returns the value of the configuration element as afloat.intgetValueAsInteger()Return theintvalue of the node.intgetValueAsInteger(int defaultValue)Returns the value of the configuration element as anint.longgetValueAsLong()Return thelongvalue of the node.longgetValueAsLong(long defaultValue)Returns the value of the configuration element as along.
-
-
-
Method Detail
-
getName
java.lang.String getName()
Return the name of the node.- Returns:
- name of the
Configurationnode.
-
getLocation
java.lang.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
java.lang.String getNamespace() throws ConfigurationExceptionReturns 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
foowill be "", and the namespace ofbarwill be "http://blah.com".- Returns:
- a String identifying the namespace of this Configuration.
- Throws:
ConfigurationException- if an error occurs- Since:
- 4.1
-
getChild
Configuration getChild(java.lang.String child)
Return a newConfigurationinstance encapsulating the specified child node.If no such child node exists, an empty
Configurationwill be returned, allowing constructs such asconf.getChild("foo").getChild("bar").getChild("baz").getValue("default");If you wish to get a
nullreturn when no element is present, usegetChild("foo", false).- Parameters:
child- The name of the child node.- Returns:
- Configuration
-
getChild
Configuration getChild(java.lang.String child, boolean createNew)
Return aConfigurationinstance encapsulating the specified child node.- Parameters:
child- The name of the child node.createNew- Iftrue, a newConfigurationwill be created and returned if the specified child does not exist. Iffalse,nullwill be returned when the specified child doesn't exist.- Returns:
- Configuration
-
getChildren
Configuration[] getChildren()
Return anArrayofConfigurationelements containing all node children. The array order will reflect the order in the source config file.- Returns:
- All child nodes
-
getChildren
Configuration[] getChildren(java.lang.String name)
Return anArrayofConfigurationelements 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
java.lang.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
java.lang.String getAttribute(java.lang.String paramName) throws ConfigurationExceptionReturn 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
int getAttributeAsInteger(java.lang.String paramName) throws ConfigurationExceptionReturn theintvalue 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 tointfails.
-
getAttributeAsLong
long getAttributeAsLong(java.lang.String name) throws ConfigurationExceptionReturns 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 tolongfails.
-
getAttributeAsFloat
float getAttributeAsFloat(java.lang.String paramName) throws ConfigurationExceptionReturn thefloatvalue 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 tofloatfails.
-
getAttributeAsDouble
double getAttributeAsDouble(java.lang.String paramName) throws ConfigurationExceptionReturn thedoublevalue of the specified parameter contained in this node.- Parameters:
paramName- The name of the parameter you ask the value of.- Returns:
- double value of attribute
- Throws:
ConfigurationException- If no parameter with that name exists. or if conversion todoublefails.
-
getAttributeAsBoolean
boolean getAttributeAsBoolean(java.lang.String paramName) throws ConfigurationExceptionReturn thebooleanvalue 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 tobooleanfails.
-
getValue
java.lang.String getValue() throws ConfigurationExceptionReturn theStringvalue of the node.- Returns:
- the value of the node.
- Throws:
ConfigurationException- if an error occurs
-
getValueAsInteger
int getValueAsInteger() throws ConfigurationExceptionReturn theintvalue of the node.- Returns:
- the value of the node.
- Throws:
ConfigurationException- If conversion tointfails.
-
getValueAsFloat
float getValueAsFloat() throws ConfigurationExceptionReturn thefloatvalue of the node.- Returns:
- the value of the node.
- Throws:
ConfigurationException- If conversion tofloatfails.
-
getValueAsDouble
double getValueAsDouble() throws ConfigurationExceptionReturn thedoublevalue of the node.- Returns:
- the value of the node.
- Throws:
ConfigurationException- If conversion todoublefails.
-
getValueAsBoolean
boolean getValueAsBoolean() throws ConfigurationExceptionReturn thebooleanvalue of the node.- Returns:
- the value of the node.
- Throws:
ConfigurationException- If conversion tobooleanfails.
-
getValueAsLong
long getValueAsLong() throws ConfigurationExceptionReturn thelongvalue of the node.- Returns:
- the value of the node.
- Throws:
ConfigurationException- If conversion tolongfails.
-
getValue
java.lang.String getValue(java.lang.String defaultValue)
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.
-
getValueAsDouble
double getValueAsDouble(double defaultValue)
Returns the value of the configuration element as adouble. 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
java.lang.String getAttribute(java.lang.String name, java.lang.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.- 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
int getAttributeAsInteger(java.lang.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.- 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
long getAttributeAsLong(java.lang.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.- 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
float getAttributeAsFloat(java.lang.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.- 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.
-
getAttributeAsDouble
double getAttributeAsDouble(java.lang.String name, double defaultValue)Returns the value of the attribute specified by its name as adouble, 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
boolean getAttributeAsBoolean(java.lang.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.- 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.
-
-