com.norconex.commons.lang.config
Class ConfigurationUtil

java.lang.Object
  extended by com.norconex.commons.lang.config.ConfigurationUtil

public final class ConfigurationUtil
extends Object

Utility methods when dealing with configuration files.

Author:
Pascal Essiembre

Method Summary
static void assertWriteRead(IXMLConfigurable xmlConfiurable)
          Convenience class for testing that a IXMLConfigurable instance can be written, and read into an new instance that is equal as per Object.equals(Object).
static XMLConfiguration getXmlAt(HierarchicalConfiguration node, String key)
          This method is the same as HierarchicalConfiguration.configurationAt(String), except that it first checks if the key exists before attempting to retrieve it, and returns null on missing keys instead of an IllegalArgumentException
static
<T> T
newInstance(HierarchicalConfiguration node)
          Creates a new instance of the class represented by the "class" attribute on the given node.
static
<T> T
newInstance(HierarchicalConfiguration node, boolean supportXMLConfigurable)
          Creates a new instance of the class represented by the "class" attribute on the given node.
static
<T> T
newInstance(HierarchicalConfiguration node, String key)
          Creates a new instance of the class represented by the "class" attribute on the sub-node of the node argument, matching the key provided.
static
<T> T
newInstance(HierarchicalConfiguration node, String key, boolean supportXMLConfigurable)
          Creates a new instance of the class represented by the "class" attribute on the sub-node of the node argument, matching the key provided.
static
<T> T
newInstance(HierarchicalConfiguration node, String key, T defaultObject)
          Creates a new instance of the class represented by the "class" attribute on the sub-node of the node argument, matching the key provided.
static
<T> T
newInstance(HierarchicalConfiguration node, String key, T defaultObject, boolean supportXMLConfigurable)
          Creates a new instance of the class represented by the "class" attribute on the sub-node of the node argument, matching the key provided.
static
<T> T
newInstance(HierarchicalConfiguration node, T defaultObject)
          Creates a new instance of the class represented by the "class" attribute on the given node.
static
<T> T
newInstance(HierarchicalConfiguration node, T defaultObject, boolean supportXMLConfigurable)
          Creates a new instance of the class represented by the "class" attribute on the given node.
static Reader newReader(HierarchicalConfiguration node)
          Creates a new Reader from a XMLConfiguration.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

newInstance

public static <T> T newInstance(HierarchicalConfiguration node)
Creates a new instance of the class represented by the "class" attribute on the given node. The class must have an empty constructor. If the class is an instance of IXMLConfigurable, the object created will be automatically populated by invoking the IXMLConfigurable.loadFromXML(Reader) method, passing it the node XML automatically populated.

Parameters:
node - the node representing the class to instantiate.
Returns:
a new object.
Throws:
ConfigurationException - if instance cannot be created/populated

newInstance

public static <T> T newInstance(HierarchicalConfiguration node,
                                boolean supportXMLConfigurable)
Creates a new instance of the class represented by the "class" attribute on the given node. The class must have an empty constructor. If the class is an instance of IXMLConfigurable and supportXMLConfigurable is true, the object created will be automatically populated by invoking the IXMLConfigurable.loadFromXML(Reader) method, passing it the node XML automatically populated.

Parameters:
node - the node representing the class to instantiate.
supportXMLConfigurable - automatically populates the object from XML if it is implementing IXMLConfigurable.
Returns:
a new object.
Throws:
ConfigurationException - if instance cannot be created/populated

newInstance

public static <T> T newInstance(HierarchicalConfiguration node,
                                T defaultObject)
Creates a new instance of the class represented by the "class" attribute on the given node. The class must have an empty constructor. If the class is an instance of IXMLConfigurable, the object created will be automatically populated by invoking the IXMLConfigurable.loadFromXML(Reader) method, passing it the node XML automatically populated.

Parameters:
node - the node representing the class to instantiate.
defaultObject - if returned object is null or undefined, returns this default object.
Returns:
a new object.
Throws:
ConfigurationException - if instance cannot be created/populated

newInstance

public static <T> T newInstance(HierarchicalConfiguration node,
                                T defaultObject,
                                boolean supportXMLConfigurable)
Creates a new instance of the class represented by the "class" attribute on the given node. The class must have an empty constructor. If the class is an instance of IXMLConfigurable and supportXMLConfigurable is true, the object created will be automatically populated by invoking the IXMLConfigurable.loadFromXML(Reader) method, passing it the node XML automatically populated.

Parameters:
node - the node representing the class to instantiate.
defaultObject - if returned object is null or undefined, returns this default object.
supportXMLConfigurable - automatically populates the object from XML if it is implementing IXMLConfigurable.
Returns:
a new object.
Throws:
ConfigurationException - if instance cannot be created/populated

newInstance

public static <T> T newInstance(HierarchicalConfiguration node,
                                String key)
Creates a new instance of the class represented by the "class" attribute on the sub-node of the node argument, matching the key provided. The class must have an empty constructor. If the class is an instance of IXMLConfigurable, the object created will be automatically populated by invoking the IXMLConfigurable.loadFromXML(Reader) method, passing it the node XML automatically populated.

Parameters:
node - the node representing the class to instantiate.
key - sub-node name/hierarchical path
Returns:
a new object.
Throws:
ConfigurationException - if instance cannot be created/populated

newInstance

public static <T> T newInstance(HierarchicalConfiguration node,
                                String key,
                                boolean supportXMLConfigurable)
Creates a new instance of the class represented by the "class" attribute on the sub-node of the node argument, matching the key provided. The class must have an empty constructor. If the class is an instance of IXMLConfigurable and supportXMLConfigurable is true, the object created will be automatically populated by invoking the IXMLConfigurable.loadFromXML(Reader) method, passing it the node XML automatically populated.

Parameters:
node - the node representing the class to instantiate.
key - sub-node name/hierarchical path
supportXMLConfigurable - automatically populates the object from XML if it is implementing IXMLConfigurable.
Returns:
a new object.
Throws:
ConfigurationException - if instance cannot be created/populated

newInstance

public static <T> T newInstance(HierarchicalConfiguration node,
                                String key,
                                T defaultObject)
Creates a new instance of the class represented by the "class" attribute on the sub-node of the node argument, matching the key provided. The class must have an empty constructor. If the class is an instance of IXMLConfigurable, the object created will be automatically populated by invoking the IXMLConfigurable.loadFromXML(Reader) method, passing it the node XML automatically populated.

Parameters:
node - the node representing the class to instantiate.
defaultObject - if returned object is null or undefined, returns this default object.
key - sub-node name/hierarchical path
Returns:
a new object.
Throws:
ConfigurationException - if instance cannot be created/populated

newInstance

public static <T> T newInstance(HierarchicalConfiguration node,
                                String key,
                                T defaultObject,
                                boolean supportXMLConfigurable)
Creates a new instance of the class represented by the "class" attribute on the sub-node of the node argument, matching the key provided. The class must have an empty constructor. If the class is an instance of IXMLConfigurable and supportXMLConfigurable is true, the object created will be automatically populated by invoking the IXMLConfigurable.loadFromXML(Reader) method, passing it the node XML automatically populated.

Parameters:
node - the node representing the class to instantiate.
defaultObject - if returned object is null or undefined, returns this default object.
key - sub-node name/hierarchical path
supportXMLConfigurable - automatically populates the object from XML if it is implementing IXMLConfigurable.
Returns:
a new object.
Throws:
ConfigurationException - if instance cannot be created/populated

newReader

public static Reader newReader(HierarchicalConfiguration node)
                        throws IOException
Creates a new Reader from a XMLConfiguration. Do not forget to close the reader instance when you are done with it.

Parameters:
node - the xml configuration to convert to a reader instance.
Returns:
reader
Throws:
ConfigurationException - cannot read configuration
IOException - cannot read configuration

getXmlAt

public static XMLConfiguration getXmlAt(HierarchicalConfiguration node,
                                        String key)
This method is the same as HierarchicalConfiguration.configurationAt(String), except that it first checks if the key exists before attempting to retrieve it, and returns null on missing keys instead of an IllegalArgumentException

Parameters:
node - the tree to extract a sub tree from
key - the key that selects the sub tree
Returns:
a XML configuration that contains this sub tree

assertWriteRead

public static void assertWriteRead(IXMLConfigurable xmlConfiurable)
                            throws IOException
Convenience class for testing that a IXMLConfigurable instance can be written, and read into an new instance that is equal as per Object.equals(Object).

Parameters:
xmlConfiurable - the instance to test if it writes/read properly
Throws:
IOException - Cannot read/write
ConfigurationException - Cannot load configuration


Copyright © 2008-2013 Norconex Inc.. All Rights Reserved.