Top Description Methods
org.apache.avalon.framework.configuration

public Interface Configuration


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, 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 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 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: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:

CodeNo namespacesWith namespaces
conf.getName()my-system
conf.getAttributeNames().length 21
conf.getChildren().length 2
conf.getAttributeAsFloat("version") 1.3
conf.getChild("widget").getAttribute("name") fooWidget
conf.getChild("widget") .getAttributeAsBoolean("threadsafe") true
conf.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:descdoc: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 fileConfigurationException
conf.getChild("desc").getNamespace()  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.
 }
 
Author
Avalon Development Team
Version
$Id: Configuration.java 506231 2007-02-12 02:36:54Z crossley $

Method Summary

Modifier and TypeMethod and Description
public String

Returns:

String value of attribute.
getAttribute
(String
The name of the parameter you ask the value of.
paramName
)

Return the value of specified attribute.

public String

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.
getAttribute
(String
The name of the attribute you ask the value of.
name
,
String
The default value desired.
defaultValue
)

Returns the value of the attribute specified by its name as a String, or the default value if no attribute by that name exists or is empty.

public boolean

Returns:

boolean value of attribute
getAttributeAsBoolean
(String
The name of the parameter you ask the value of.
paramName
)

Return the boolean value of the specified parameter contained in this node.

public boolean

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.
getAttributeAsBoolean
(String
The name of the attribute you ask the value of.
name
,
boolean
The default value desired.
defaultValue
)

Returns the value of the attribute specified by its name as a boolean, or the default value if no attribute by that name exists or is empty.

public double

Returns:

double value of attribute
getAttributeAsDouble
(String
The name of the parameter you ask the value of.
paramName
)

Return the double value of the specified parameter contained in this node.

public double

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
(String
The name of the attribute you ask the value of.
name
,
double
The default value desired.
defaultValue
)

Returns the value of the attribute specified by its name as a double, or the default value if no attribute by that name exists or is empty.

public float

Returns:

float value of attribute
getAttributeAsFloat
(String
The name of the parameter you ask the value of.
paramName
)

Return the float value of the specified parameter contained in this node.

public float

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.
getAttributeAsFloat
(String
The name of the attribute you ask the value of.
name
,
float
The default value desired.
defaultValue
)

Returns the value of the attribute specified by its name as a float, or the default value if no attribute by that name exists or is empty.

public int

Returns:

int value of attribute
getAttributeAsInteger
(String
The name of the parameter you ask the value of.
paramName
)

Return the int value of the specified attribute contained in this node.

public int

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.
getAttributeAsInteger
(String
The name of the attribute you ask the value of.
name
,
int
The default value desired.
defaultValue
)

Returns the value of the attribute specified by its name as a int, or the default value if no attribute by that name exists or is empty.

public long

Returns:

long value of attribute
getAttributeAsLong
(String
The name of the parameter you ask the value of.
name
)

Returns the value of the attribute specified by its name as a long.

public long

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.
getAttributeAsLong
(String
The name of the attribute you ask the value of.
name
,
long
The default value desired.
defaultValue
)

Returns the value of the attribute specified by its name as a long, or the default value if no attribute by that name exists or is empty.

public String[]

Returns:

a String[] value
getAttributeNames
()

Return an array of all attribute names.

public Configuration

Returns:

Configuration
getChild
(String
The name of the child node.
child
)

Return a new Configuration instance encapsulating the specified child node.

public Configuration

Returns:

Configuration
getChild
(String
The name of the child node.
child
,
boolean
If true, a new Configuration will be created and returned if the specified child does not exist. If false, null will be returned when the specified child doesn't exist.
createNew
)

Return a Configuration instance encapsulating the specified child node.

public Configuration[]

Returns:

All child nodes
getChildren
()

Return an Array of Configuration elements containing all node children.

public Configuration[]

Returns:

The child nodes with name name
getChildren
(String
The name of the children to get.
name
)

Return an Array of Configuration elements containing all node children with the specified name.

public String

Returns:

a string describing location of Configuration
getLocation
()

Return a string describing location of Configuration.

public String

Returns:

name of the Configuration node.
getName
()

Return the name of the node.

public String

Returns:

a String identifying the namespace of this Configuration.
getNamespace
()

Returns a string indicating which namespace this Configuration node belongs to.

public String

Returns:

the value of the node.
getValue
()

Return the String value of the node.

public String

Returns:

String value of the Configuration, or default if none specified.
getValue
(String
The default value desired.
defaultValue
)

Returns the value of the configuration element as a String.

public boolean

Returns:

the value of the node.
getValueAsBoolean
()

Return the boolean value of the node.

public boolean

Returns:

boolean value of the Configuration, or default if none specified.
getValueAsBoolean
(boolean
The default value desired.
defaultValue
)

Returns the value of the configuration element as a boolean.

public double

Returns:

the value of the node.
getValueAsDouble
()

Return the double value of the node.

public double

Returns:

float value of the Configuration, or default if none specified.
getValueAsDouble
(double
The default value desired.
defaultValue
)

Returns the value of the configuration element as a double.

public float

Returns:

the value of the node.
getValueAsFloat
()

Return the float value of the node.

public float

Returns:

float value of the Configuration, or default if none specified.
getValueAsFloat
(float
The default value desired.
defaultValue
)

Returns the value of the configuration element as a float.

public int

Returns:

the value of the node.
getValueAsInteger
()

Return the int value of the node.

public int

Returns:

int value of the Configuration, or default if none specified.
getValueAsInteger
(int
The default value desired.
defaultValue
)

Returns the value of the configuration element as an int.

public long

Returns:

the value of the node.
getValueAsLong
()

Return the long value of the node.

public long

Returns:

long value of the Configuration, or default if none specified.
getValueAsLong
(long
The default value desired.
defaultValue
)

Returns the value of the configuration element as a long.

Method Detail

getAttributeback to summary
public String getAttribute(String paramName) throws ConfigurationException

Return the value of specified attribute.

Parameters
paramName:String

The name of the parameter you ask the value of.

Returns:String

String value of attribute.

Exceptions
ConfigurationException:
If no attribute with that name exists.
getAttributeback to summary
public String getAttribute(String name, String defaultValue)

Returns the value of the attribute specified by its name as a String, or the default value if no attribute by that name exists or is empty.

Parameters
name:String

The name of the attribute you ask the value of.

defaultValue:String

The default value desired.

Returns:String

String value of attribute. It will return the default value if the named attribute does not exist, or if the value is not set.

getAttributeAsBooleanback to summary
public boolean getAttributeAsBoolean(String paramName) throws ConfigurationException

Return the boolean value of the specified parameter contained in this node.

Parameters
paramName:String

The name of the parameter you ask the value of.

Returns:boolean

boolean value of attribute

Exceptions
ConfigurationException:
If no parameter with that name exists. or if conversion to boolean fails.
getAttributeAsBooleanback to summary
public boolean getAttributeAsBoolean(String name, boolean defaultValue)

Returns the value of the attribute specified by its name as a boolean, or the default value if no attribute by that name exists or is empty.

Parameters
name:String

The name of the attribute you ask the value of.

defaultValue:boolean

The default value desired.

Returns:boolean

boolean value of attribute. It will return the default value if the named attribute does not exist, or if the value is not set.

getAttributeAsDoubleback to summary
public double getAttributeAsDouble(String paramName) throws ConfigurationException

Return the double value of the specified parameter contained in this node.

Parameters
paramName:String

The name of the parameter you ask the value of.

Returns:double

double value of attribute

Exceptions
ConfigurationException:
If no parameter with that name exists. or if conversion to double fails.
getAttributeAsDoubleback to summary
public double getAttributeAsDouble(String name, double defaultValue)

Returns the value of the attribute specified by its name as a double, or the default value if no attribute by that name exists or is empty.

Parameters
name:String

The name of the attribute you ask the value of.

defaultValue:double

The default value desired.

Returns:double

float value of attribute. It will return the default value if the named attribute does not exist, or if the value is not set.

getAttributeAsFloatback to summary
public float getAttributeAsFloat(String paramName) throws ConfigurationException

Return the float value of the specified parameter contained in this node.

Parameters
paramName:String

The name of the parameter you ask the value of.

Returns:float

float value of attribute

Exceptions
ConfigurationException:
If no parameter with that name exists. or if conversion to float fails.
getAttributeAsFloatback to summary
public float getAttributeAsFloat(String name, float defaultValue)

Returns the value of the attribute specified by its name as a float, or the default value if no attribute by that name exists or is empty.

Parameters
name:String

The name of the attribute you ask the value of.

defaultValue:float

The default value desired.

Returns:float

float value of attribute. It will return the default value if the named attribute does not exist, or if the value is not set.

getAttributeAsIntegerback to summary
public int getAttributeAsInteger(String paramName) throws ConfigurationException

Return the int value of the specified attribute contained in this node.

Parameters
paramName:String

The name of the parameter you ask the value of.

Returns:int

int value of attribute

Exceptions
ConfigurationException:
If no parameter with that name exists. or if conversion to int fails.
getAttributeAsIntegerback to summary
public int getAttributeAsInteger(String name, int defaultValue)

Returns the value of the attribute specified by its name as a int, or the default value if no attribute by that name exists or is empty.

Parameters
name:String

The name of the attribute you ask the value of.

defaultValue:int

The default value desired.

Returns:int

int value of attribute. It will return the default value if the named attribute does not exist, or if the value is not set.

getAttributeAsLongback to summary
public long getAttributeAsLong(String name) throws ConfigurationException

Returns the value of the attribute specified by its name as a long.

Parameters
name:String

The name of the parameter you ask the value of.

Returns:long

long value of attribute

Exceptions
ConfigurationException:
If no parameter with that name exists. or if conversion to long fails.
getAttributeAsLongback to summary
public long getAttributeAsLong(String name, long defaultValue)

Returns the value of the attribute specified by its name as a long, or the default value if no attribute by that name exists or is empty.

Parameters
name:String

The name of the attribute you ask the value of.

defaultValue:long

The default value desired.

Returns:long

long value of attribute. It will return the default value if the named attribute does not exist, or if the value is not set.

getAttributeNamesback to summary
public 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:String[]

a String[] value

getChildback to summary
public Configuration getChild(String child)

Return a new Configuration instance encapsulating the specified child node.

If no such child node exists, an empty Configuration will be returned, allowing constructs such as conf.getChild("foo").getChild("bar").getChild("baz").getValue("default");

If you wish to get a null return when no element is present, use getChild("foo", <b>false</b>).

Parameters
child:String

The name of the child node.

Returns:Configuration

Configuration

getChildback to summary
public Configuration getChild(String child, boolean createNew)

Return a Configuration instance encapsulating the specified child node.

Parameters
child:String

The name of the child node.

createNew:boolean

If true, a new Configuration will be created and returned if the specified child does not exist. If false, null will be returned when the specified child doesn't exist.

Returns:Configuration

Configuration

getChildrenback to summary
public Configuration[] getChildren()

Return an Array of Configuration elements containing all node children. The array order will reflect the order in the source config file.

Returns:Configuration[]

All child nodes

getChildrenback to summary
public Configuration[] getChildren(String name)

Return an Array of Configuration elements containing all node children with the specified name. The array order will reflect the order in the source config file.

Parameters
name:String

The name of the children to get.

Returns:Configuration[]

The child nodes with name name

getLocationback to summary
public 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:String

a string describing location of Configuration

getNameback to summary
public String getName()

Return the name of the node.

Returns:String

name of the Configuration node.

getNamespaceback to summary
public String getNamespace() throws ConfigurationException

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 of bar will be "http://blah.com".

Returns:String

a String identifying the namespace of this Configuration.

Exceptions
ConfigurationException:
if an error occurs
Since
4.1
getValueback to summary
public String getValue() throws ConfigurationException

Return the String value of the node.

Returns:String

the value of the node.

Exceptions
ConfigurationException:
if an error occurs
getValueback to summary
public String getValue(String defaultValue)

Returns the value of the configuration element as a String. If the configuration value is not set, the default value will be used.

Parameters
defaultValue:String

The default value desired.

Returns:String

String value of the Configuration, or default if none specified.

getValueAsBooleanback to summary
public boolean getValueAsBoolean() throws ConfigurationException

Return the boolean value of the node.

Returns:boolean

the value of the node.

Exceptions
ConfigurationException:
If conversion to boolean fails.
getValueAsBooleanback to summary
public boolean getValueAsBoolean(boolean defaultValue)

Returns the value of the configuration element as a boolean. If the configuration value is not set, the default value will be used.

Parameters
defaultValue:boolean

The default value desired.

Returns:boolean

boolean value of the Configuration, or default if none specified.

getValueAsDoubleback to summary
public double getValueAsDouble() throws ConfigurationException

Return the double value of the node.

Returns:double

the value of the node.

Exceptions
ConfigurationException:
If conversion to double fails.
getValueAsDoubleback to summary
public double getValueAsDouble(double defaultValue)

Returns the value of the configuration element as a double. If the configuration value is not set, the default value will be used.

Parameters
defaultValue:double

The default value desired.

Returns:double

float value of the Configuration, or default if none specified.

getValueAsFloatback to summary
public float getValueAsFloat() throws ConfigurationException

Return the float value of the node.

Returns:float

the value of the node.

Exceptions
ConfigurationException:
If conversion to float fails.
getValueAsFloatback to summary
public float getValueAsFloat(float defaultValue)

Returns the value of the configuration element as a float. If the configuration value is not set, the default value will be used.

Parameters
defaultValue:float

The default value desired.

Returns:float

float value of the Configuration, or default if none specified.

getValueAsIntegerback to summary
public int getValueAsInteger() throws ConfigurationException

Return the int value of the node.

Returns:int

the value of the node.

Exceptions
ConfigurationException:
If conversion to int fails.
getValueAsIntegerback to summary
public int getValueAsInteger(int defaultValue)

Returns the value of the configuration element as an int. If the configuration value is not set, the default value will be used.

Parameters
defaultValue:int

The default value desired.

Returns:int

int value of the Configuration, or default if none specified.

getValueAsLongback to summary
public long getValueAsLong() throws ConfigurationException

Return the long value of the node.

Returns:long

the value of the node.

Exceptions
ConfigurationException:
If conversion to long fails.
getValueAsLongback to summary
public long getValueAsLong(long defaultValue)

Returns the value of the configuration element as a long. If the configuration value is not set, the default value will be used.

Parameters
defaultValue:long

The default value desired.

Returns:long

long value of the Configuration, or default if none specified.