public final class XMLConfigurationUtil extends Object
Modifier and Type | Field and Description |
---|---|
static String |
W3C_XML_SCHEMA_NS_URI_1_1 |
Modifier and Type | Method and Description |
---|---|
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 void |
disableDelimiterParsing(XMLConfiguration xml)
Disables delimiter parsing for both attributes and elements.
|
static int[] |
getCSVIntArray(HierarchicalConfiguration xml,
String key)
Gets a comma-separated-value string as an int array, removing any
blank entries.
|
static int[] |
getCSVIntArray(HierarchicalConfiguration xml,
String key,
int[] defaultValues)
Gets a comma-separated-value string as an int array, removing any
blank entries.
|
static String[] |
getCSVStringArray(HierarchicalConfiguration xml,
String key)
Gets a comma-separated-value string as a String array, trimming values
and removing any blank entries.
|
static String[] |
getCSVStringArray(HierarchicalConfiguration xml,
String key,
String[] defaultValues)
Gets a comma-separated-value string as a String array, trimming values
and removing any blank entries.
|
static long |
getDuration(HierarchicalConfiguration xml,
String key,
long defaultValue)
Gets a duration which can be a numerical value or a textual
representation of a duration as per
DurationParser . |
static BigDecimal |
getNullableBigDecimal(HierarchicalConfiguration xml,
String key,
BigDecimal defaultValue)
Gets a BigDecimal from XML configuration.
|
static BigInteger |
getNullableBigInteger(HierarchicalConfiguration xml,
String key,
BigInteger defaultValue)
Gets a BigInteger from XML configuration.
|
static Boolean |
getNullableBoolean(HierarchicalConfiguration xml,
String key,
Boolean defaultValue)
Gets a Boolean from XML configuration.
|
static Byte |
getNullableByte(HierarchicalConfiguration xml,
String key,
Byte defaultValue)
Gets a Byte from XML configuration.
|
static Class<?> |
getNullableClass(HierarchicalConfiguration xml,
String key,
Class<?> defaultValue)
Gets a Class from XML configuration.
|
static Dimension |
getNullableDimension(HierarchicalConfiguration xml,
String key,
Dimension defaultValue)
Gets a Dimension from XML configuration (e.g., 400x500, or 200).
|
static Double |
getNullableDouble(HierarchicalConfiguration xml,
String key,
Double defaultValue)
Gets a Double from XML configuration.
|
static Float |
getNullableFloat(HierarchicalConfiguration xml,
String key,
Float defaultValue)
Gets a Float from XML configuration.
|
static Integer |
getNullableInteger(HierarchicalConfiguration xml,
String key,
Integer defaultValue)
Gets an Integer from XML configuration.
|
static Long |
getNullableLong(HierarchicalConfiguration xml,
String key,
Long defaultValue)
Gets a Long from XML configuration.
|
static Short |
getNullableShort(HierarchicalConfiguration xml,
String key,
Short defaultValue)
Gets a Short from XML configuration.
|
static String |
getNullableString(HierarchicalConfiguration xml,
String key,
String defaultValue)
Gets a String from XML configuration.
|
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 boolean |
keyExists(HierarchicalConfiguration xml,
String key) |
static void |
loadFromXML(IXMLConfigurable obj,
HierarchicalConfiguration node)
Loads XML into the given object, performing validation first.
|
static void |
loadFromXML(IXMLConfigurable obj,
Reader reader)
Loads XML into the given object, performing validation first.
|
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,
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,
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,
T defaultObject)
Creates a new instance of the class represented by the "class" attribute
on the given node.
|
static <T> T |
newInstance(Reader reader)
Creates a new instance of the class represented by the "class"
attribute on the supplied XML.
|
static Reader |
newReader(HierarchicalConfiguration node)
Creates a new
Reader from a XMLConfiguration . |
static XMLConfiguration |
newXMLConfiguration(HierarchicalConfiguration c)
This load method will return an Apache XML Configuration from
from a
HierarchicalConfiguration , with delimiter parsing
disabled. |
static XMLConfiguration |
newXMLConfiguration(Reader in)
This load method will return an Apache XML Configuration from
from a reader, with delimiter parsing disabled.
|
static int |
validate(Class<?> clazz,
HierarchicalConfiguration node)
For classes implementing
IXMLConfigurable , validates XML against
a class XSD schema and logs any error/warnings. |
static int |
validate(Class<?> clazz,
Reader xml)
For classes implementing
IXMLConfigurable , validates XML against
a class XSD schema and logs any error/warnings. |
public static final String W3C_XML_SCHEMA_NS_URI_1_1
public static void disableDelimiterParsing(XMLConfiguration xml)
xml
- XML configurationpublic static XMLConfiguration newXMLConfiguration(HierarchicalConfiguration c)
HierarchicalConfiguration
, with delimiter parsing
disabled.c
- hierarchical configurationpublic static XMLConfiguration newXMLConfiguration(Reader in)
This load method will return an Apache XML Configuration from from a reader, with delimiter parsing disabled.
Note: Leading and trailing white spaces are not preserved by
default.
To preserve them, add xml:space="preserve"
to your tag, like this:
<mytag xml:space="preserve"> </mytag>
The above example will preserve the white space in the tag's body.
in
- input streampublic static <T> T newInstance(Reader reader)
Creates a new instance of the class represented by the "class"
attribute on the supplied XML.
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.
The reader is expected to hold an XML, which will
first be converted to an XMLConfiguration
. This method has the
same effect as invoking:
XMLConfigurationUtil.newInstance( XMLConfigurationUtil.newXMLConfiguration(reader));
T
- the type of the return valuereader
- the XML representing the class to instantiate.ConfigurationException
- if instance cannot be created/populatedpublic static <T> T newInstance(HierarchicalConfiguration node)
IXMLConfigurable
, the object
created will be automatically populated by invoking the
IXMLConfigurable.loadFromXML(Reader)
method,
passing it the node XML automatically populated.T
- the type of the return valuenode
- the node representing the class to instantiate.ConfigurationException
- if instance cannot be created/populatedpublic static <T> T newInstance(HierarchicalConfiguration node, T defaultObject)
IXMLConfigurable
, the object
created will be automatically populated by invoking the
IXMLConfigurable.loadFromXML(Reader)
method,
passing it the node XML automatically populated.T
- the type of the return valuenode
- the node representing the class to instantiate.defaultObject
- if returned object is null or undefined,
returns this default object.ConfigurationException
- if instance cannot be created/populatedpublic 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.
Since 1.6.0, this method should throw a
ConfigurationException
upon error. Use a method
with a default value argument to avoid throwing exceptions.
T
- the type of the return valuenode
- the node representing the class to instantiate.key
- sub-node name/hierarchical pathConfigurationException
- if instance cannot be created/populatedpublic 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.
This method should not throw exception upon errors, but will return the default value instead (even if null). Use a method without a default value argument to get exception on errors.
T
- the type of the return valuenode
- the node representing the class to instantiate.defaultObject
- if returned object is null or undefined,
returns this default object.key
- sub-node name/hierarchical pathpublic static Reader newReader(HierarchicalConfiguration node) throws IOException
Reader
from a XMLConfiguration
.
Do not forget to close the reader instance when you are done with it.node
- the xml configuration to convert to a reader instance.ConfigurationException
- cannot read configurationIOException
- cannot read configurationpublic static int validate(Class<?> clazz, HierarchicalConfiguration node)
IXMLConfigurable
, validates XML against
a class XSD schema and logs any error/warnings.
The schema expected to be found at the same classpath location and have
the same name as the class, but with the ".xsd" extension.clazz
- the class to validate the XML fornode
- the XML to validatepublic static int validate(Class<?> clazz, Reader xml)
IXMLConfigurable
, validates XML against
a class XSD schema and logs any error/warnings.
The schema expected to be found at the same classpath location and have
the same name as the class, but with the ".xsd" extension.clazz
- the class to validate the XML forxml
- the XML to validatepublic static void loadFromXML(IXMLConfigurable obj, Reader reader)
IXMLConfigurable.loadFromXML(Reader)
on an object.obj
- object to have loadedreader
- xml readerpublic static void loadFromXML(IXMLConfigurable obj, HierarchicalConfiguration node)
obj
- object to have loadednode
- XML node to have loadedpublic static XMLConfiguration getXmlAt(HierarchicalConfiguration node, String key)
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
node
- the tree to extract a sub tree fromkey
- the key that selects the sub treepublic static void assertWriteRead(IXMLConfigurable xmlConfiurable) throws IOException
IXMLConfigurable
instance
can be written, and read into an new instance that is equal as per
Object.equals(Object)
.xmlConfiurable
- the instance to test if it writes/read properlyIOException
- Cannot read/writeConfigurationException
- Cannot load configurationpublic static BigDecimal getNullableBigDecimal(HierarchicalConfiguration xml, String key, BigDecimal defaultValue)
AbstractConfiguration.getBigDecimal(String, BigDecimal)
except that
the default value will only be returned if the key does not exist.
If it exists and it is empty, null
will be returned instead.xml
- xml configurationkey
- key to the element/attribute containing the valuedefaultValue
- value to be returned if the key does not exists.null
public static BigInteger getNullableBigInteger(HierarchicalConfiguration xml, String key, BigInteger defaultValue)
AbstractConfiguration.getBigInteger(String, BigInteger)
except that
the default value will only be returned if the key does not exist.
If it exists and it is empty, null
will be returned instead.xml
- xml configurationkey
- key to the element/attribute containing the valuedefaultValue
- value to be returned if the key does not exists.null
public static Boolean getNullableBoolean(HierarchicalConfiguration xml, String key, Boolean defaultValue)
AbstractConfiguration.getBoolean(String, Boolean)
except that
the default value will only be returned if the key does not exist.
If it exists and it is empty, null
will be returned instead.xml
- xml configurationkey
- key to the element/attribute containing the valuedefaultValue
- value to be returned if the key does not exists.null
public static Byte getNullableByte(HierarchicalConfiguration xml, String key, Byte defaultValue)
AbstractConfiguration.getByte(String, Byte)
except that
the default value will only be returned if the key does not exist.
If it exists and it is empty, null
will be returned instead.xml
- xml configurationkey
- key to the element/attribute containing the valuedefaultValue
- value to be returned if the key does not exists.null
public static Class<?> getNullableClass(HierarchicalConfiguration xml, String key, Class<?> defaultValue)
null
will be returned instead.xml
- xml configurationkey
- key to the element/attribute containing the valuedefaultValue
- value to be returned if the key does not exists.null
public static Double getNullableDouble(HierarchicalConfiguration xml, String key, Double defaultValue)
AbstractConfiguration.getDouble(String, Double)
except that
the default value will only be returned if the key does not exist.
If it exists and it is empty, null
will be returned instead.xml
- xml configurationkey
- key to the element/attribute containing the valuedefaultValue
- value to be returned if the key does not exists.null
public static Float getNullableFloat(HierarchicalConfiguration xml, String key, Float defaultValue)
AbstractConfiguration.getFloat(String, Float)
except that
the default value will only be returned if the key does not exist.
If it exists and it is empty, null
will be returned instead.xml
- xml configurationkey
- key to the element/attribute containing the valuedefaultValue
- value to be returned if the key does not exists.null
public static Integer getNullableInteger(HierarchicalConfiguration xml, String key, Integer defaultValue)
AbstractConfiguration.getInteger(String, Integer)
except that
the default value will only be returned if the key does not exist.
If it exists and it is empty, null
will be returned instead.xml
- xml configurationkey
- key to the element/attribute containing the valuedefaultValue
- value to be returned if the key does not exists.null
public static Long getNullableLong(HierarchicalConfiguration xml, String key, Long defaultValue)
AbstractConfiguration.getLong(String, Long)
except that
the default value will only be returned if the key does not exist.
If it exists and it is empty, null
will be returned instead.xml
- xml configurationkey
- key to the element/attribute containing the valuedefaultValue
- value to be returned if the key does not exists.null
public static Short getNullableShort(HierarchicalConfiguration xml, String key, Short defaultValue)
AbstractConfiguration.getShort(String, Short)
except that
the default value will only be returned if the key does not exist.
If it exists and it is empty, null
will be returned instead.xml
- xml configurationkey
- key to the element/attribute containing the valuedefaultValue
- value to be returned if the key does not exists.null
public static String getNullableString(HierarchicalConfiguration xml, String key, String defaultValue)
AbstractConfiguration.getString(String, String)
except that
the default value will only be returned if the key does not exist.
If it exists and it is empty, null
will be returned instead.xml
- xml configurationkey
- key to the element/attribute containing the valuedefaultValue
- value to be returned if the key does not exists.null
public static Dimension getNullableDimension(HierarchicalConfiguration xml, String key, Dimension defaultValue)
null
will be returned instead.xml
- xml configurationkey
- key to the element/attribute containing the valuedefaultValue
- value to be returned if the key does not exists.null
public static boolean keyExists(HierarchicalConfiguration xml, String key)
public static long getDuration(HierarchicalConfiguration xml, String key, long defaultValue)
DurationParser
.
If the duration does not exists for the given key or is blank,
the default value is returned.
If the key value is found but there are parsing errors, a
DurationParserException
will be thrown.xml
- xml configurationkey
- key to the element/attribute containing the durationdefaultValue
- default durationpublic static String[] getCSVStringArray(HierarchicalConfiguration xml, String key)
newXMLConfiguration(Reader)
disables delimiter parsing,
this method is an useful alternative to
AbstractConfiguration.getStringArray(String)
.xml
- xml configurationkey
- key to the element/attribute containing the CSV stringpublic static String[] getCSVStringArray(HierarchicalConfiguration xml, String key, String[] defaultValues)
newXMLConfiguration(Reader)
disables delimiter parsing,
this method is an useful alternative to
AbstractConfiguration.getStringArray(String)
.xml
- xml configurationkey
- key to the element/attribute containing the CSV stringdefaultValues
- default values if the split returns null
or an empty arraypublic static int[] getCSVIntArray(HierarchicalConfiguration xml, String key)
xml
- xml configurationkey
- key to the element/attribute containing the CSV stringpublic static int[] getCSVIntArray(HierarchicalConfiguration xml, String key, int[] defaultValues)
xml
- xml configurationkey
- key to the element/attribute containing the CSV stringdefaultValues
- default values if the split returns null
or an empty arrayCopyright © 2008–2021 Norconex Inc.. All rights reserved.