edu.rice.cs.cunit.util
Class XMLConfig

java.lang.Object
  extended by edu.rice.cs.cunit.util.XMLConfig

public class XMLConfig
extends java.lang.Object

XML configuration management.

This class uses DOM paths of a specific form to refer to nodes in the XML document. Consider this XML structure: abc def The path "foo/bar" refers to the value "abc". The path "foo/fum" refers to the value "def". If this form is used, there may be only #text or #comment nodes in the node. All #text nodes will be concatenated and then stripped of whitespace at the beginning and the end. The path "foo/fum.fee" refers to the value "xyz". The path "foo.a" refers to the value "foo.a". When using getMultiple, any node or attribute name can be substituted with "*" to get all elements: The path "foo/*" returns both the value "abc" and "def".

Author:
Mathias Ricken

Nested Class Summary
static class XMLConfig.XMLConfigException
          Exception in XMLConfig methods.
static class XMLConfig.XMLConfigTest
          Unit tests.
 
Field Summary
private  org.w3c.dom.Document _document
          XML document.
static java.lang.String NL
          Newline string.
 
Constructor Summary
XMLConfig()
          Creates an empty configuration.
XMLConfig(java.io.File f)
          Creates a configuration from a file.
XMLConfig(java.io.InputStream is)
          Creates a configuration from an input stream.
XMLConfig(java.io.Reader r)
          Creates a configuration from a reader.
XMLConfig(java.lang.String filename)
          Creates a configuration from a file name.
 
Method Summary
 org.w3c.dom.Node createNode(java.lang.String path)
          Create the node specified by the DOM path.
 org.w3c.dom.Node createNode(java.lang.String path, org.w3c.dom.Node n)
          Create the node specified by the DOM path.
 org.w3c.dom.Node createNode(java.lang.String path, org.w3c.dom.Node n, boolean overwrite)
          Create the node specified by the DOM path.
 java.lang.String get(java.lang.String path)
          Returns the value as specified by the DOM path.
 java.lang.String get(java.lang.String path, java.lang.String defaultVal)
          Returns the value as specified by the DOM path, or the default value if the value could not be found.
 java.util.List<java.lang.String> getMultiple(java.lang.String path)
          Returns the value as specified by the DOM path.
 java.util.List<java.lang.String> getMultiple(java.lang.String path, org.w3c.dom.Node root)
          Returns the value as specified by the DOM path.
private  void getMultipleAddAttributesHelper(java.lang.String path, org.w3c.dom.Node n, java.util.List<org.w3c.dom.Node> accum)
           
private  void getMultipleHelper(java.lang.String path, org.w3c.dom.Node n, java.util.List<org.w3c.dom.Node> accum, boolean dotRead)
          Returns the value as specified by the DOM path.
static java.lang.String getNodePath(org.w3c.dom.Node n)
          Return the path of a node as it is used in XMLConfig.
 java.util.List<org.w3c.dom.Node> getNodes(java.lang.String path)
          Returns the nodes as specified by the DOM path.
 java.util.List<org.w3c.dom.Node> getNodes(java.lang.String path, org.w3c.dom.Node root)
          Returns the nodes as specified by the DOM path.
private  void init(org.xml.sax.InputSource is)
          Initialize this XML configuration.
 void save(java.io.File f)
          Saves configuration to a file.
 void save(java.io.OutputStream os)
          Saves configuration to an output stream
 void save(java.lang.String filename)
          Saves configuration to a file specified by a file name.
 org.w3c.dom.Node set(java.lang.String path, java.lang.String value)
          Set the value of the node or attribute specified by the DOM path.
 org.w3c.dom.Node set(java.lang.String path, java.lang.String value, boolean overwrite)
          Set the value of the node or attribute specified by the DOM path.
 org.w3c.dom.Node set(java.lang.String path, java.lang.String value, org.w3c.dom.Node n, boolean overwrite)
          Set the value of the node or attribute specified by the DOM path.
 java.lang.String toString()
          Returns a string representation of the object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

NL

public static final java.lang.String NL
Newline string.


_document

private org.w3c.dom.Document _document
XML document.

Constructor Detail

XMLConfig

public XMLConfig()
Creates an empty configuration.


XMLConfig

public XMLConfig(java.io.InputStream is)
Creates a configuration from an input stream.

Parameters:
is - input stream

XMLConfig

public XMLConfig(java.io.Reader r)
Creates a configuration from a reader.

Parameters:
r - reader

XMLConfig

public XMLConfig(java.io.File f)
Creates a configuration from a file.

Parameters:
f - file

XMLConfig

public XMLConfig(java.lang.String filename)
Creates a configuration from a file name.

Parameters:
filename - file name
Method Detail

init

private void init(org.xml.sax.InputSource is)
Initialize this XML configuration.

Parameters:
is - the XML input source

save

public void save(java.io.OutputStream os)
Saves configuration to an output stream

Parameters:
os - output stream

save

public void save(java.io.File f)
Saves configuration to a file.

Parameters:
f - file

save

public void save(java.lang.String filename)
Saves configuration to a file specified by a file name.

Parameters:
filename - file name

get

public java.lang.String get(java.lang.String path)
Returns the value as specified by the DOM path.

Parameters:
path - DOM path
Returns:
value.

getMultiple

public java.util.List<java.lang.String> getMultiple(java.lang.String path)
Returns the value as specified by the DOM path.

Parameters:
path - DOM path
Returns:
list of values.

getMultiple

public java.util.List<java.lang.String> getMultiple(java.lang.String path,
                                                    org.w3c.dom.Node root)
Returns the value as specified by the DOM path.

Parameters:
path - DOM path
root - node where the search should start
Returns:
list of values.

getNodes

public java.util.List<org.w3c.dom.Node> getNodes(java.lang.String path)
Returns the nodes as specified by the DOM path.

Parameters:
path - DOM path
Returns:
list of nodes.

getNodes

public java.util.List<org.w3c.dom.Node> getNodes(java.lang.String path,
                                                 org.w3c.dom.Node root)
Returns the nodes as specified by the DOM path.

Parameters:
path - DOM path
root - node where the search should start
Returns:
list of nodes.

getMultipleHelper

private void getMultipleHelper(java.lang.String path,
                               org.w3c.dom.Node n,
                               java.util.List<org.w3c.dom.Node> accum,
                               boolean dotRead)
Returns the value as specified by the DOM path.

Parameters:
path - DOM path
n - node where the search begins
accum - accumulator
dotRead - whether a dot has been read

getMultipleAddAttributesHelper

private void getMultipleAddAttributesHelper(java.lang.String path,
                                            org.w3c.dom.Node n,
                                            java.util.List<org.w3c.dom.Node> accum)

get

public java.lang.String get(java.lang.String path,
                            java.lang.String defaultVal)
Returns the value as specified by the DOM path, or the default value if the value could not be found.

Parameters:
path - DOM path
defaultVal - default value in case value is not in DOM
Returns:
value.

set

public org.w3c.dom.Node set(java.lang.String path,
                            java.lang.String value)
Set the value of the node or attribute specified by the DOM path.

Parameters:
path - DOM path
value - node or attribute value
Returns:
the node that was created, or the parent node of the attribute if it was an attribute

set

public org.w3c.dom.Node set(java.lang.String path,
                            java.lang.String value,
                            boolean overwrite)
Set the value of the node or attribute specified by the DOM path.

Parameters:
path - DOM path
value - node or attribute value
overwrite - whether to overwrite (true) or add (false)
Returns:
the node that was created, or the parent node of the attribute if it was an attribute

set

public org.w3c.dom.Node set(java.lang.String path,
                            java.lang.String value,
                            org.w3c.dom.Node n,
                            boolean overwrite)
Set the value of the node or attribute specified by the DOM path.

Parameters:
path - DOM path
value - node or attribute value
n - node where the search should start
overwrite - whether to overwrite (true) or add (false) -- only applies for last node!
Returns:
the node that was created, or the parent node of the attribute if it was an attribute

createNode

public org.w3c.dom.Node createNode(java.lang.String path)
Create the node specified by the DOM path.

Parameters:
path - DOM path
Returns:
the node that was created, or the parent node of the attribute if it was an attribute

createNode

public org.w3c.dom.Node createNode(java.lang.String path,
                                   org.w3c.dom.Node n)
Create the node specified by the DOM path.

Parameters:
path - DOM path
n - node where the search should start, or null for the root
Returns:
the node that was created, or the parent node of the attribute if it was an attribute

createNode

public org.w3c.dom.Node createNode(java.lang.String path,
                                   org.w3c.dom.Node n,
                                   boolean overwrite)
Create the node specified by the DOM path.

Parameters:
path - DOM path
n - node where the search should start, or null for the root
overwrite - whether to overwrite (true) or add (false) -- only applies for last node!
Returns:
the node that was created, or the parent node of the attribute if it was an attribute

toString

public java.lang.String toString()
Returns a string representation of the object.

Overrides:
toString in class java.lang.Object
Returns:
a string representation of the object.

getNodePath

public static java.lang.String getNodePath(org.w3c.dom.Node n)
Return the path of a node as it is used in XMLConfig.

Parameters:
n - node
Returns:
path