org.apache.commons.configuration
Class DOM4JConfiguration

java.lang.Object
  extended byorg.apache.commons.configuration.AbstractConfiguration
      extended byorg.apache.commons.configuration.BaseConfiguration
          extended byorg.apache.commons.configuration.BasePathConfiguration
              extended byorg.apache.commons.configuration.XMLConfiguration
                  extended byorg.apache.commons.configuration.DOM4JConfiguration
All Implemented Interfaces:
BasePathLoader, Configuration

public class DOM4JConfiguration
extends XMLConfiguration

Reads a XML configuration file. To retrieve the value of an attribute of an element, use X.Y.Z[@attribute]. The '@' symbol was chosen for consistency with XPath. Setting property values will NOT automatically persist changes to disk, unless autoSave=true.

Since:
0.8.1
Author:
Kelvin Tan, Daniel Rall

Nested Class Summary
 
Nested classes inherited from class org.apache.commons.configuration.AbstractConfiguration
AbstractConfiguration.Container, AbstractConfiguration.PropertiesTokenizer
 
Field Summary
private static char ATTRIB_MARKER
           
private static java.lang.String ATTRIB_START_MARKER
           
private  boolean autoSave
          If true, modifications are immediately persisted.
private  org.dom4j.Document document
          The XML document from our data source.
private  java.lang.String fileName
          A handle to our data source.
private static java.lang.String NODE_DELIMITER
          For consistency with properties files.
 
Fields inherited from class org.apache.commons.configuration.BasePathConfiguration
 
Fields inherited from class org.apache.commons.configuration.BaseConfiguration
 
Fields inherited from class org.apache.commons.configuration.AbstractConfiguration
defaults, END_TOKEN, START_TOKEN
 
Constructor Summary
DOM4JConfiguration()
          Empty construtor.
DOM4JConfiguration(java.io.File file)
          Attempts to load the XML file.
DOM4JConfiguration(java.lang.String resource)
          Attempts to load the XML file as a resource from the classpath.
 
Method Summary
 void addProperty(java.lang.String name, java.lang.Object value)
          Calls super method, and also ensures the underlying Document is modified so changes are persisted when saved.
 void clearProperty(java.lang.String name)
          Calls super method, and also ensures the underlying Document is modified so changes are persisted when saved.
private  void clearXmlProperty(java.lang.String name)
           
 java.io.File getFile()
          Returns the file.
 java.lang.String getFileName()
          Returns the fileName.
private  void initProperties(org.dom4j.Element element, java.lang.StringBuffer hierarchy)
          Loads and initializes from the XML file.
 void load()
           
private  void possiblySave()
           
private static java.io.File resourceURLToFile(java.lang.String resource)
           
 void save()
           
 void setAutoSave(boolean autoSave)
          If true, changes are automatically persisted.
 void setFile(java.io.File file)
          Sets the file.
 void setFileName(java.lang.String fileName)
           
 void setProperty(java.lang.String name, java.lang.Object value)
          Calls super method, and also ensures the underlying Document is modified so changes are persisted when saved.
private  void setXmlProperty(java.lang.String name, java.lang.Object value)
          Sets the property value in our document tree, auto-saving if appropriate.
 
Methods inherited from class org.apache.commons.configuration.BasePathConfiguration
getBasePath, setBasePath
 
Methods inherited from class org.apache.commons.configuration.BaseConfiguration
addPropertyDirect, containsKey, getKeys, getPropertyDirect, isEmpty
 
Methods inherited from class org.apache.commons.configuration.AbstractConfiguration
getBoolean, getBoolean, getBoolean, getByte, getByte, getByte, getDouble, getDouble, getDouble, getFloat, getFloat, getFloat, getInt, getInt, getInteger, getKeys, getList, getList, getLong, getLong, getLong, getProperties, getProperties, getProperty, getShort, getShort, getShort, getString, getString, getStringArray, interpolate, interpolateHelper, processString, subset, testBoolean
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ATTRIB_MARKER

private static final char ATTRIB_MARKER
See Also:
Constant Field Values

ATTRIB_START_MARKER

private static final java.lang.String ATTRIB_START_MARKER
See Also:
Constant Field Values

NODE_DELIMITER

private static final java.lang.String NODE_DELIMITER
For consistency with properties files. Access nodes via an "A.B.C" notation.

See Also:
Constant Field Values

fileName

private java.lang.String fileName
A handle to our data source.


document

private org.dom4j.Document document
The XML document from our data source.


autoSave

private boolean autoSave
If true, modifications are immediately persisted.

Constructor Detail

DOM4JConfiguration

public DOM4JConfiguration()
Empty construtor. You must provide a file/fileName and call the load method


DOM4JConfiguration

public DOM4JConfiguration(java.lang.String resource)
                   throws java.lang.Exception
Attempts to load the XML file as a resource from the classpath. The XML file must be located somewhere in the classpath.

Parameters:
resource - Name of the resource
Throws:
java.lang.Exception - If error reading data source.
See Also:
DOM4JConfiguration(File)

DOM4JConfiguration

public DOM4JConfiguration(java.io.File file)
                   throws java.lang.Exception
Attempts to load the XML file.

Parameters:
file - File object representing the XML file.
Throws:
java.lang.Exception - If error reading data source.
Method Detail

load

public void load()
          throws java.lang.Exception
Throws:
java.lang.Exception

resourceURLToFile

private static java.io.File resourceURLToFile(java.lang.String resource)

initProperties

private void initProperties(org.dom4j.Element element,
                            java.lang.StringBuffer hierarchy)
Loads and initializes from the XML file.

Parameters:
element - The element to start processing from. Callers should supply the root element of the document.
hierarchy -

addProperty

public void addProperty(java.lang.String name,
                        java.lang.Object value)
Calls super method, and also ensures the underlying Document is modified so changes are persisted when saved.

Specified by:
addProperty in interface Configuration
Overrides:
addProperty in class AbstractConfiguration
Parameters:
name -
value -

setProperty

public void setProperty(java.lang.String name,
                        java.lang.Object value)
Calls super method, and also ensures the underlying Document is modified so changes are persisted when saved.

Specified by:
setProperty in interface Configuration
Overrides:
setProperty in class AbstractConfiguration
Parameters:
name -
value -

setXmlProperty

private void setXmlProperty(java.lang.String name,
                            java.lang.Object value)
Sets the property value in our document tree, auto-saving if appropriate.

Parameters:
name - The name of the element to set a value for.
value - The value to set.

clearProperty

public void clearProperty(java.lang.String name)
Calls super method, and also ensures the underlying Document is modified so changes are persisted when saved.

Specified by:
clearProperty in interface Configuration
Overrides:
clearProperty in class BaseConfiguration
Parameters:
name - The name of the property to clear.

clearXmlProperty

private void clearXmlProperty(java.lang.String name)

possiblySave

private void possiblySave()
Throws:

setAutoSave

public void setAutoSave(boolean autoSave)
If true, changes are automatically persisted.

Parameters:
autoSave -

save

public void save()
          throws java.io.IOException
Throws:
java.io.IOException

getFile

public java.io.File getFile()
Returns the file.

Returns:
File

setFile

public void setFile(java.io.File file)
Sets the file.

Parameters:
file - The file to set

setFileName

public void setFileName(java.lang.String fileName)

getFileName

public java.lang.String getFileName()
Returns the fileName.

Returns:
String