my-lib 20041108

my.config
Class Configuration

java.lang.Object
  extended bymy.config.Configuration

public class Configuration
extends java.lang.Object

Configuration is a Data Object use to store/retrieve hierarchical configuration values.

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 XmlConfigurationBuilder class, but could be built by program.

Configuration config = new Configuration("foo");
  config.setAttribute("attA", "valueA", false);
  config.setAttribute("attB", "32", false);
  ...

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:
D.Bernard (dwayne@java-fan.com)
Version:
20040820
Source :
my.config.Configuration
Diagrams :
Based on :
org.apache.avalon.framework.configuration

Field Summary
protected static Configuration[] EMPTY_ARRAY
           
 
Constructor Summary
Configuration(Configuration other)
           
Configuration(java.lang.String name)
           
Configuration(java.lang.String name, Configuration other)
           
 
Method Summary
 Configuration addChild(Configuration configuration)
          Add a child Configuration to this configuration element.
 java.lang.String getAttribute(java.lang.String name)
          Returns the value of a configuration attribute as a String.
 java.lang.String getAttribute(java.lang.String name, java.lang.String defaultValue)
          Returns the value of a configuration attribute as a String.
 boolean getAttributeAsBoolean(java.lang.String name)
          Returns the value of a configuration attribute as a boolean.
 boolean getAttributeAsBoolean(java.lang.String name, boolean defaultValue)
          Returns the value of a configuration attribute as a boolean.
 float getAttributeAsFloat(java.lang.String name)
          Returns the value of a configuration attribute as a float.
 float getAttributeAsFloat(java.lang.String name, float defaultValue)
          Returns the value of a configuration attribute as a float.
 int getAttributeAsInteger(java.lang.String name)
          Returns the value of a configuration attribute as an int.
 int getAttributeAsInteger(java.lang.String name, int defaultValue)
          Returns the value of a configuration attribute as an int.
 long getAttributeAsLong(java.lang.String name)
          Returns the value of a configuration attribute as a long.
 long getAttributeAsLong(java.lang.String name, long defaultValue)
          Returns the value of a configuration attribute as a long.
 java.lang.String[] getAttributes(java.lang.String childrenName, java.lang.String name)
          Returns the values of the children 's configuration attribute as a String.
 Configuration getChild(java.lang.String name, boolean createNew)
          Return the first Configuration object child of this associated with the given name.
 Configuration[] getChildren()
          Return an array of Configuration elements containing all node children.
 Configuration[] getChildren(java.lang.String name)
          Return an array of Configuration objects children of this associated with the given name.
 java.lang.String getName()
           
 java.lang.String getValue()
          Returns the value of the configuration element as a String.
 java.lang.String getValue(java.lang.String defaultValue)
          Returns the value of the configuration element as a String.
 boolean getValueAsBoolean()
          Returns the value of the configuration element as a boolean.
 boolean getValueAsBoolean(boolean defaultValue)
          Returns the value of the configuration element as a boolean.
 float getValueAsFloat()
          Returns the value of the configuration element as a float.
 float getValueAsFloat(float defaultValue)
          Returns the value of the configuration element as a float.
 int getValueAsInteger()
          Returns the value of the configuration element as an int.
 int getValueAsInteger(int defaultValue)
          Returns the value of the configuration element as an int.
 long getValueAsLong()
          Returns the value of the configuration element as a long.
 long getValueAsLong(long defaultValue)
          Returns the value of the configuration element as a long.
 java.lang.String[] getValues(java.lang.String childrenName)
          Returns the values of the children 's configuration element as a String.
 Configuration removeChild(Configuration configuration)
           
 Configuration setAttribute(java.lang.String name, java.lang.String newValue, boolean overwrite)
          Set the value of the configuration attribute.
 Configuration setValue(java.lang.String newValue, boolean overwrite)
          Set the value of the configuration element.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

EMPTY_ARRAY

protected static final Configuration[] EMPTY_ARRAY
Constructor Detail

Configuration

public Configuration(java.lang.String name)
              throws java.lang.Exception

Configuration

public Configuration(java.lang.String name,
                     Configuration other)
              throws java.lang.Exception

Configuration

public Configuration(Configuration other)
              throws java.lang.Exception
Method Detail

getName

public java.lang.String getName()
                         throws java.lang.Exception
Throws:
java.lang.Exception

setValue

public Configuration setValue(java.lang.String newValue,
                              boolean overwrite)
                       throws java.lang.Exception
Set the value of the configuration element. If null or length is 0, then the value is clear (set empty).

Parameters:
newValue - the value to set.
overwrite - if previous value (if not empty) must be overwrote.
Returns:
the updated Configuration.
Throws:
java.lang.Exception

getValue

public java.lang.String getValue()
                          throws java.lang.Exception
Returns the value of the configuration element as a String.

Returns:
the value
Throws:
java.lang.NoSuchFieldException - if value undefined.
java.lang.Exception

getValue

public java.lang.String getValue(java.lang.String defaultValue)
                          throws java.lang.Exception
Returns the value of the configuration element as a String.

Parameters:
defaultValue - the default value to return if value malformed or empty
Returns:
a String value
Throws:
java.lang.Exception

getValues

public java.lang.String[] getValues(java.lang.String childrenName)
                             throws java.lang.Exception
Returns the values of the children 's configuration element as a String.

Parameters:
childrenName - the name of the children from wich extract Values
Returns:
a String[] of values, empty if no children found.
Throws:
java.lang.NoSuchFieldException - if value undefined.
java.lang.Exception

getValueAsInteger

public int getValueAsInteger()
                      throws java.lang.Exception
Returns the value of the configuration element as an int. Hexadecimal numbers begin with 0x, Octal numbers begin with 0o and binary numbers begin with 0b, all other values are assumed to be decimal.

Returns:
the value
Throws:
ConfigurationException - if an error occurs
java.lang.NoSuchFieldException - if value undefined.
java.lang.Exception

getValueAsInteger

public int getValueAsInteger(int defaultValue)
                      throws java.lang.Exception
Returns the value of the configuration element as an int. Hexadecimal numbers begin with 0x, Octal numbers begin with 0o and binary numbers begin with 0b, all other values are assumed to be decimal.

Parameters:
defaultValue - the default value to return if value malformed or empty
Returns:
the value
Throws:
java.lang.Exception

getValueAsLong

public long getValueAsLong()
                    throws java.lang.Exception
Returns the value of the configuration element as a long. Hexadecimal numbers begin with 0x, Octal numbers begin with 0o and binary numbers begin with 0b, all other values are assumed to be decimal.

Returns:
the value
Throws:
ConfigurationException - if an error occurs
java.lang.NoSuchFieldException - if value undefined.
java.lang.Exception

getValueAsLong

public long getValueAsLong(long defaultValue)
                    throws java.lang.Exception
Returns the value of the configuration element as a long. Hexadecimal numbers begin with 0x, Octal numbers begin with 0o and binary numbers begin with 0b, all other values are assumed to be decimal.

Parameters:
defaultValue - the default value to return if value malformed or empty
Returns:
the value
Throws:
java.lang.Exception

getValueAsFloat

public float getValueAsFloat()
                      throws java.lang.Exception
Returns the value of the configuration element as a float.

Returns:
the value
Throws:
java.lang.NoSuchFieldException - if value undefined.
java.lang.Exception

getValueAsFloat

public float getValueAsFloat(float defaultValue)
                      throws java.lang.Exception
Returns the value of the configuration element as a float.

Parameters:
defaultValue - the default value to return if value malformed or empty
Returns:
the value
Throws:
java.lang.Exception

getValueAsBoolean

public boolean getValueAsBoolean()
                          throws java.lang.Exception
Returns the value of the configuration element as a boolean.

Returns:
the value
Throws:
java.lang.NoSuchFieldException - if value undefined.
java.lang.Exception

getValueAsBoolean

public boolean getValueAsBoolean(boolean defaultValue)
                          throws java.lang.Exception
Returns the value of the configuration element as a boolean.

Parameters:
defaultValue - the default value to return if value malformed or empty
Returns:
the value
Throws:
java.lang.Exception

setAttribute

public Configuration setAttribute(java.lang.String name,
                                  java.lang.String newValue,
                                  boolean overwrite)
                           throws java.lang.Exception
Set the value of the configuration attribute. If null or length is 0, then the attribute is clear (set empty).

Parameters:
name - the attribute name.
newValue - the value to set.
overwrite - if previous value (if not empty) must be overwrote.
Returns:
the updated Configuration.
Throws:
java.lang.Exception

getAttribute

public java.lang.String getAttribute(java.lang.String name)
                              throws java.lang.Exception
Returns the value of a configuration attribute as a String.

Parameters:
name - the attribute name.
Returns:
the attribute value.
Throws:
java.lang.NoSuchFieldException - if attribute undefined.
java.lang.Exception

getAttribute

public java.lang.String getAttribute(java.lang.String name,
                                     java.lang.String defaultValue)
                              throws java.lang.Exception
Returns the value of a configuration attribute as a String.

Parameters:
name - the attribute name
defaultValue - the default value to return if empty
Returns:
the attribute value
Throws:
java.lang.Exception

getAttributes

public java.lang.String[] getAttributes(java.lang.String childrenName,
                                        java.lang.String name)
                                 throws java.lang.Exception
Returns the values of the children 's configuration attribute as a String.

Parameters:
childrenName - the name of the children from wich extract Values
name - the attribute name
Returns:
a String[] of values, empty if no children found.
Throws:
java.lang.NoSuchFieldException - if value undefined.
java.lang.Exception

getAttributeAsInteger

public int getAttributeAsInteger(java.lang.String name)
                          throws java.lang.Exception
Returns the value of a configuration attribute as an int. Hexadecimal numbers begin with 0x, Octal numbers begin with 0o and binary numbers begin with 0b, all other values are assumed to be decimal.

Parameters:
name - the attribute name
Returns:
the attribute value
Throws:
java.lang.NoSuchFieldException - if attribute undefined.
java.lang.Exception

getAttributeAsInteger

public int getAttributeAsInteger(java.lang.String name,
                                 int defaultValue)
                          throws java.lang.Exception
Returns the value of a configuration attribute as an int. Hexadecimal numbers begin with 0x, Octal numbers begin with 0o and binary numbers begin with 0b, all other values are assumed to be decimal.

Parameters:
name - the attribute name
defaultValue - the default value to return if empty
Returns:
the attribute value
Throws:
java.lang.Exception

getAttributeAsLong

public long getAttributeAsLong(java.lang.String name)
                        throws java.lang.Exception
Returns the value of a configuration attribute as a long. Hexadecimal numbers begin with 0x, Octal numbers begin with 0o and binary numbers begin with 0b, all other values are assumed to be decimal.

Parameters:
name - the attribute name
Returns:
the attribute value
Throws:
java.lang.NoSuchFieldException - if attribute undefined.
java.lang.Exception

getAttributeAsLong

public long getAttributeAsLong(java.lang.String name,
                               long defaultValue)
                        throws java.lang.Exception
Returns the value of a configuration attribute as a long. Hexadecimal numbers begin with 0x, Octal numbers begin with 0o and binary numbers begin with 0b, all other values are assumed to be decimal.

Parameters:
name - the attribute name
defaultValue - the default value to return if empty
Returns:
the attribute value
Throws:
java.lang.Exception

getAttributeAsFloat

public float getAttributeAsFloat(java.lang.String name)
                          throws java.lang.Exception
Returns the value of a configuration attribute as a float.

Parameters:
name - the attribute name
Returns:
the attribute value
Throws:
java.lang.NoSuchFieldException - if attribute undefined.
java.lang.Exception

getAttributeAsFloat

public float getAttributeAsFloat(java.lang.String name,
                                 float defaultValue)
                          throws java.lang.Exception
Returns the value of a configuration attribute as a float.

Parameters:
name - the attribute name
defaultValue - the default value to return if empty
Returns:
the attribute value
Throws:
java.lang.Exception

getAttributeAsBoolean

public boolean getAttributeAsBoolean(java.lang.String name)
                              throws java.lang.Exception
Returns the value of a configuration attribute as a boolean.

Parameters:
name - the attribute name
Returns:
the attribute value
Throws:
java.lang.NoSuchFieldException - if attribute undefined.
java.lang.Exception

getAttributeAsBoolean

public boolean getAttributeAsBoolean(java.lang.String name,
                                     boolean defaultValue)
                              throws java.lang.Exception
Returns the value of a configuration attribute as a boolean.

Parameters:
name - the attribute name
defaultValue - the default value to return if empty
Returns:
the attribute value
Throws:
java.lang.Exception

getChild

public Configuration getChild(java.lang.String name,
                              boolean createNew)
                       throws java.lang.Exception
Return the first Configuration object child of this associated with the given name.

Parameters:
name - the name of the child
createNew - true if you want to create a new Configuration object if none exists
Returns:
the child Configuration
Throws:
java.lang.Exception

getChildren

public Configuration[] getChildren()
                            throws java.lang.Exception
Return an array of Configuration elements containing all node children.

Returns:
The child nodes with name
Throws:
java.lang.Exception

getChildren

public Configuration[] getChildren(java.lang.String name)
                            throws java.lang.Exception
Return an array of Configuration objects children of this associated with the given name.
The returned array may be empty but is never null.

Parameters:
name - The name of the required children Configuration.
Returns:
a Configuration[] value
Throws:
java.lang.Exception

addChild

public Configuration addChild(Configuration configuration)
                       throws java.lang.Exception
Add a child Configuration to this configuration element.

Parameters:
configuration - a Configuration value
Returns:
the updated Configuration (this).
Throws:
java.lang.Exception

toString

public java.lang.String toString()

removeChild

public Configuration removeChild(Configuration configuration)

my-lib 20041108

Copyright ? 2004 D. Bernard. All Rights Reserved.