org.codehaus.dna.impl
Class DefaultConfiguration

java.lang.Object
  extended byorg.codehaus.dna.impl.AbstractFreezable
      extended byorg.codehaus.dna.impl.DefaultConfiguration
All Implemented Interfaces:
Configuration, Freezable

public class DefaultConfiguration
extends org.codehaus.dna.impl.AbstractFreezable
implements Configuration

In memory Configuration implementation. The developer should create the DefaultConfiguration, associate value, attributes and/or child elements configuration and then invoke makeReadOnly() before passing the Configuration to the client component.

Version:
$Revision: 1.2 $ $Date: 2004/05/01 09:51:48 $

Constructor Summary
DefaultConfiguration(java.lang.String name, java.lang.String location, java.lang.String path)
          Create a DefaultConfiguration instance.
 
Method Summary
 void addChild(Configuration configuration)
          Add a child configuration element.
protected  void checkWriteable()
          Check if the resource has been "frozen" and thus is read only.
protected  java.lang.String generateLocation()
          Generate a location string that postfixes autogenerated marker.
 java.lang.String getAttribute(java.lang.String name)
          Return attribute value with specified name.
 java.lang.String getAttribute(java.lang.String name, java.lang.String defaultValue)
          Return attribute value with specified name.
 boolean getAttributeAsBoolean(java.lang.String name)
          Return attribute value with specified name as a boolean.
 boolean getAttributeAsBoolean(java.lang.String name, boolean defaultValue)
          Return attribute value with specified name as a boolean.
 float getAttributeAsFloat(java.lang.String name)
          Return attribute value with specified name as afloat.
 float getAttributeAsFloat(java.lang.String name, float defaultValue)
          Return attribute value with specified name as a float.
 int getAttributeAsInteger(java.lang.String name)
          Return attribute value with specified name as an integer.
 int getAttributeAsInteger(java.lang.String name, int defaultValue)
          Return attribute value with specified name as an integer.
 long getAttributeAsLong(java.lang.String name)
          Return attribute value with specified name as a long.
 long getAttributeAsLong(java.lang.String name, long defaultValue)
          Return attribute value with specified name as a long.
protected  java.util.Map getAttributeMap()
          Return the backing map for attributes.
 java.lang.String[] getAttributeNames()
          Return an array of all the attribute names.
 Configuration getChild(java.lang.String name)
          Return a child Configuration element with specified name.
 Configuration getChild(java.lang.String name, boolean createChild)
          Return a child Configuration element with specified name.
protected  java.util.List getChildList()
          Return the list of child configuration objects.
 Configuration[] getChildren()
          Return an array of all the child elements.
 Configuration[] getChildren(java.lang.String name)
          Return an array of all the child elements with specified name.
 java.lang.String getLocation()
          Return the location of configuration element.
 java.lang.String getName()
          Return the name of the configuration element.
 java.lang.String getPath()
          Return the path to the configuration element.
 java.lang.String getValue()
          Return text value of element.
 java.lang.String getValue(java.lang.String defaultValue)
          Return text value of element.
 boolean getValueAsBoolean()
          Return text value of element as a boolean.
 boolean getValueAsBoolean(boolean defaultValue)
          Return text value of element as a boolean.
 float getValueAsFloat()
          Return text value of element as a float.
 float getValueAsFloat(float defaultValue)
          Return text value of element as a float.
 int getValueAsInteger()
          Return text value of element as an integer.
 int getValueAsInteger(int defaultValue)
          Return text value of element as an integer.
 long getValueAsLong()
          Return text value of element as a long.
 long getValueAsLong(long defaultValue)
          Return text value of element as a long.
protected  boolean isReadOnly()
          Return true if resource has been made read-only or frozen.
 void makeReadOnly()
          Mark the configuration and child configurations as read only.
 void setAttribute(java.lang.String key, java.lang.String value)
          Set an attribute of configuration.
 void setValue(java.lang.String value)
          Set the value of the configuration element.
protected  void throwMixedContentException()
          Throw an IllegalStateException warning about mixed content.
 java.lang.String toString()
          Overide toString to improve ability to debug implementation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DefaultConfiguration

public DefaultConfiguration(java.lang.String name,
                            java.lang.String location,
                            java.lang.String path)
Create a DefaultConfiguration instance.

Parameters:
name - the name of configuration element
location - the location of configuration element in source
path - the path of configuration element in document
Method Detail

getName

public java.lang.String getName()
Return the name of the configuration element.

Specified by:
getName in interface Configuration
Returns:
the name of the configuration element.

getPath

public final java.lang.String getPath()
Return the path to the configuration element. The path should be in the xpath form but may be the empty string if unabel to determine path.

Specified by:
getPath in interface Configuration
Returns:
the path to the configuration element.

getLocation

public java.lang.String getLocation()
Return the location of configuration element. Usually of the form "uri[:line number[:column number]]" if possible. ie "file:myFile.xml:80:2". However the line number and column number may be elided if unavailable.

Specified by:
getLocation in interface Configuration
Returns:
the location of configuration element.

getChildren

public Configuration[] getChildren()
Return an array of all the child elements.

Specified by:
getChildren in interface Configuration
Returns:
an array of all the child elements.

getChildren

public Configuration[] getChildren(java.lang.String name)
Return an array of all the child elements with specified name.

Specified by:
getChildren in interface Configuration
Parameters:
name - the name of child configuration objects
Returns:
an array of all the child elements with specified name.

getChild

public Configuration getChild(java.lang.String name)
Return a child Configuration element with specified name. If no such element exists an element will be autocreated.

Specified by:
getChild in interface Configuration
Parameters:
name - the name of child configuration object
Returns:
a child Configuration element with specified name.

getChild

public Configuration getChild(java.lang.String name,
                              boolean createChild)
Return a child Configuration element with specified name. If no such element exists and createChild is true then an element will be autocreated otherwise null will be returned.

Specified by:
getChild in interface Configuration
Parameters:
name - the name of child configuration object
createChild - true if child should be created if it does not exist
Returns:
a child Configuration element with specified name.

getValue

public java.lang.String getValue()
                          throws ConfigurationException
Return text value of element.

Specified by:
getValue in interface Configuration
Returns:
the value
Throws:
ConfigurationException - if no value in element

getValue

public java.lang.String getValue(java.lang.String defaultValue)
Return text value of element. Use specified default if no value in element.

Specified by:
getValue in interface Configuration
Parameters:
defaultValue - the default value
Returns:
the value

getValueAsBoolean

public boolean getValueAsBoolean()
                          throws ConfigurationException
Return text value of element as a boolean.

Specified by:
getValueAsBoolean in interface Configuration
Returns:
the value
Throws:
ConfigurationException - if no value in element or value can not be converted to correct type

getValueAsBoolean

public boolean getValueAsBoolean(boolean defaultValue)
Return text value of element as a boolean. Use specified default if no value in element or value can not be converted to correct type.

Specified by:
getValueAsBoolean in interface Configuration
Parameters:
defaultValue - the default value
Returns:
the value

getValueAsInteger

public int getValueAsInteger()
                      throws ConfigurationException
Return text value of element as an integer.

Specified by:
getValueAsInteger in interface Configuration
Returns:
the value
Throws:
ConfigurationException - if no value in element or value can not be converted to correct type

getValueAsInteger

public int getValueAsInteger(int defaultValue)
Return text value of element as an integer. Use specified default if no value in element or value can not be converted to correct type.

Specified by:
getValueAsInteger in interface Configuration
Parameters:
defaultValue - the default value
Returns:
the value

getValueAsLong

public long getValueAsLong()
                    throws ConfigurationException
Return text value of element as a long.

Specified by:
getValueAsLong in interface Configuration
Returns:
the value
Throws:
ConfigurationException - if no value in element or value can not be converted to correct type

getValueAsLong

public long getValueAsLong(long defaultValue)
Return text value of element as a long. Use specified default if no value in element or value can not be converted to correct type.

Specified by:
getValueAsLong in interface Configuration
Parameters:
defaultValue - the default value
Returns:
the value

getValueAsFloat

public float getValueAsFloat()
                      throws ConfigurationException
Return text value of element as a float.

Specified by:
getValueAsFloat in interface Configuration
Returns:
the value
Throws:
ConfigurationException - if no value in element or value can not be converted to correct type

getValueAsFloat

public float getValueAsFloat(float defaultValue)
Return text value of element as a float. Use specified default if no value in element or value can not be converted to correct type.

Specified by:
getValueAsFloat in interface Configuration
Parameters:
defaultValue - the default value
Returns:
the value

getAttributeNames

public java.lang.String[] getAttributeNames()
Return an array of all the attribute names.

Specified by:
getAttributeNames in interface Configuration
Returns:
an array of all the attribute names.

getAttribute

public java.lang.String getAttribute(java.lang.String name)
                              throws ConfigurationException
Return attribute value with specified name.

Specified by:
getAttribute in interface Configuration
Parameters:
name - the attribute name
Returns:
the attribute value
Throws:
ConfigurationException - if no attribute with specified name

getAttribute

public java.lang.String getAttribute(java.lang.String name,
                                     java.lang.String defaultValue)
Return attribute value with specified name. If no attribute with specified name then return default value.

Specified by:
getAttribute in interface Configuration
Parameters:
name - the attribute name
defaultValue - the default value
Returns:
the attribute value

getAttributeAsBoolean

public boolean getAttributeAsBoolean(java.lang.String name)
                              throws ConfigurationException
Return attribute value with specified name as a boolean.

Specified by:
getAttributeAsBoolean in interface Configuration
Parameters:
name - the attribute name
Returns:
the attribute value
Throws:
ConfigurationException - if no attribute with specified name or attribute can not be converted to correct type

getAttributeAsBoolean

public boolean getAttributeAsBoolean(java.lang.String name,
                                     boolean defaultValue)
Return attribute value with specified name as a boolean. If no attribute with specified name or attribute can not be converted to correct type then return default value.

Specified by:
getAttributeAsBoolean in interface Configuration
Parameters:
name - the attribute name
defaultValue - the default value
Returns:
the attribute value

getAttributeAsInteger

public int getAttributeAsInteger(java.lang.String name)
                          throws ConfigurationException
Return attribute value with specified name as an integer.

Specified by:
getAttributeAsInteger in interface Configuration
Parameters:
name - the attribute name
Returns:
the attribute value
Throws:
ConfigurationException - if no attribute with specified name or attribute can not be converted to correct type

getAttributeAsInteger

public int getAttributeAsInteger(java.lang.String name,
                                 int defaultValue)
Return attribute value with specified name as an integer. If no attribute with specified name or attribute can not be converted to correct type then return default value.

Specified by:
getAttributeAsInteger in interface Configuration
Parameters:
name - the attribute name
defaultValue - the default value
Returns:
the attribute value

getAttributeAsLong

public long getAttributeAsLong(java.lang.String name)
                        throws ConfigurationException
Return attribute value with specified name as a long.

Specified by:
getAttributeAsLong in interface Configuration
Parameters:
name - the attribute name
Returns:
the attribute value
Throws:
ConfigurationException - if no attribute with specified name or attribute can not be converted to correct type

getAttributeAsLong

public long getAttributeAsLong(java.lang.String name,
                               long defaultValue)
Return attribute value with specified name as a long. If no attribute with specified name or attribute can not be converted to correct type then return default value.

Specified by:
getAttributeAsLong in interface Configuration
Parameters:
name - the attribute name
defaultValue - the default value
Returns:
the attribute value

getAttributeAsFloat

public float getAttributeAsFloat(java.lang.String name)
                          throws ConfigurationException
Return attribute value with specified name as afloat.

Specified by:
getAttributeAsFloat in interface Configuration
Parameters:
name - the attribute name
Returns:
the attribute value
Throws:
ConfigurationException - if no attribute with specified name or attribute can not be converted to correct type

getAttributeAsFloat

public float getAttributeAsFloat(java.lang.String name,
                                 float defaultValue)
Return attribute value with specified name as a float. If no attribute with specified name or attribute can not be converted to correct type then return default value.

Specified by:
getAttributeAsFloat in interface Configuration
Parameters:
name - the attribute name
defaultValue - the default value
Returns:
the attribute value

makeReadOnly

public void makeReadOnly()
Mark the configuration and child configurations as read only.

Specified by:
makeReadOnly in interface Freezable

setAttribute

public void setAttribute(java.lang.String key,
                         java.lang.String value)
Set an attribute of configuration.

Parameters:
key - the attribute key
value - the attribute value

addChild

public void addChild(Configuration configuration)
Add a child configuration element.

Parameters:
configuration - the child configuration element.

setValue

public void setValue(java.lang.String value)
Set the value of the configuration element.

Parameters:
value - the value of the configuration element.

toString

public java.lang.String toString()
Overide toString to improve ability to debug implementation.

Returns:
string representation of object

getChildList

protected final java.util.List getChildList()
Return the list of child configuration objects.

Returns:
the list of child configuration objects.

getAttributeMap

protected final java.util.Map getAttributeMap()
Return the backing map for attributes.

Returns:
the backing map for attributes.

generateLocation

protected final java.lang.String generateLocation()
Generate a location string that postfixes autogenerated marker.

Returns:
a autogenerated location string

throwMixedContentException

protected final void throwMixedContentException()
Throw an IllegalStateException warning about mixed content.


checkWriteable

protected final void checkWriteable()
Check if the resource has been "frozen" and thus is read only. If read-only then throw an IllegalStateException.

Throws:
java.lang.IllegalStateException - if resource is read-only

isReadOnly

protected final boolean isReadOnly()
Return true if resource has been made read-only or frozen.

Returns:
true if resource has been made read-only or frozen, false otherwise.


Copyright © 2003-2004 Codehaus. All Rights Reserved.