public class XML extends Object implements Iterable<XMLCursor>
XML DOM wrapper facilitating node querying and automatically creating,
validating, and populating classes from/to XML, with support
for IXMLConfigurable
and JAXB
.
Some white spaces in element text may be removed when parsed.
To keep them, add the XML standard attribute
xml:space="preserve"
to your element. For instance, the
following ensures the four spaces are kept when parsed:
<example xml:space="preserve"> </example>
Empty tags are interpreted as having an empty strings while
self-closing tags have their value interpreted as null
.
Non-existing tags have no effect (when loading over an object, that
object current value should remain unchanged).
Checked exceptions are wrapped into an XMLException
.
Modifier and Type | Class and Description |
---|---|
static class |
XML.Builder |
Constructor and Description |
---|
XML(File file)
Parse an XML file into an XML document, without consideration
for namespaces.
|
XML(Node node)
Creates an XML with the given node.
|
XML(Path file)
Parse an XML file into an XML document, without consideration
for namespaces.
|
XML(Reader reader)
Parse an XML stream into an XML document, without consideration
for namespaces.
|
XML(String xml)
Parse an XML string into an XML document, without consideration
for namespaces.
|
XML(String rootElement,
Object obj) |
Modifier and Type | Method and Description |
---|---|
XML |
addDelimitedElementList(String name,
List<?> values)
Adds a list of values as a new element after joining them with
a comma (CSV).
|
XML |
addDelimitedElementList(String name,
String delim,
List<?> values)
Adds a list of values as a new element after joining them with
the given delimiter.
|
XML |
addElement(String tagName)
Adds an empty child element to this XML root element.
|
XML |
addElement(String tagName,
Object value)
Adds a child element to this XML root element.
|
List<XML> |
addElementList(String tagName,
List<?> values) |
XML |
addElementList(String parentTagName,
String tagName,
List<?> values) |
List<XML> |
addElementMap(String tagName,
String attributeName,
Map<?,?> map)
Adds a
Map as a series of elements without a parent element
wrapping that group. |
XML |
addElementMap(String parentTagName,
String tagName,
String attributeName,
Map<?,?> map)
Adds a
Map as a series of elements with a parent element
wrapping that group. |
XML |
addXML(Reader xml) |
XML |
addXML(String xml) |
XML |
addXML(XML xml) |
static void |
assertWriteRead(IXMLConfigurable xmlConfigurable,
String elementName)
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) . |
void |
checkDeprecated(String deprecatedXPath,
boolean throwException)
Checks whether a deprecated configuration entry (without replacement)
was specified and log a warning or throw an
XMLException . |
void |
checkDeprecated(String deprecatedXPath,
String replacement,
boolean throwException)
Checks whether a deprecated configuration entry was specified
and log a warning or throw an
XMLException . |
XML |
clear()
Clears this XML by removing all its attributes and elements
(i.e., making it an empty tag).
|
boolean |
contains(String xpathExpression) |
void |
forEach(String xpathExpression,
Consumer<XML> action)
If the given expression matches one or more elements, consume those
element one by one.
|
<T> T |
get(String xpathExpression,
Class<T> type)
Gets the matching element/attribute, converted from
string to the given type.
|
<T> T |
get(String xpathExpression,
Class<T> type,
T defaultValue)
Gets the matching element/attribute, converted from
string to the given type.
|
Boolean |
getBoolean(String xpathExpression) |
Boolean |
getBoolean(String xpathExpression,
Boolean defaultValue) |
Charset |
getCharset(String xpathExpression) |
Charset |
getCharset(String xpathExpression,
Charset defaultValue) |
Class<?> |
getClass(String xpathExpression) |
<T> Class<T> |
getClass(String xpathExpression,
Class<T> defaultValue) |
<T> List<Class<T>> |
getClassList(String xpathExpression)
Gets values as a list of files.
|
<T> List<Class<? extends T>> |
getClassList(String xpathExpression,
List<Class<? extends T>> defaultValue)
Gets values as a list of files.
|
Long |
getDataSize(String xpathExpression)
Gets the size of a data expression, in bytes (e.g., 2KB, 1GiB,
3 megabytes, etc).
|
Long |
getDataSize(String xpathExpression,
DataUnit targetUnit,
Long defaultValue)
Gets the size of a data expression, in the specified target unit
(e.g., 2KB, 1GiB, 3 megabytes, etc).
|
Long |
getDataSize(String xpathExpression,
Long defaultValue)
Gets the size of a data expression, in bytes (e.g., 2KB, 1GiB,
3 megabytes, etc).
|
<E extends Enum<E>> |
getDelimitedEnumList(String xpathExpression,
Class<E> enumClass,
List<E> defaultValues)
Gets a list of enum constants after splitting the matching node value(s)
on commas (CSV).
|
<E extends Enum<E>> |
getDelimitedEnumList(String xpathExpression,
Class<E> enumClass,
String delimRegex,
List<E> defaultValues)
Gets a list of enum constants after splitting the matching node
value(s) with the given delimiter regular expression.
|
<T> List<T> |
getDelimitedList(String xpathExpression,
Class<T> type)
Gets a list of the given type after splitting the matching node value(s)
on commas (CSV).
|
<T> List<T> |
getDelimitedList(String xpathExpression,
Class<T> type,
List<T> defaultValues)
Gets a list of given type after splitting the matching node value(s)
on commas (CSV).
|
<T> List<T> |
getDelimitedList(String xpathExpression,
Class<T> type,
String delimRegex)
Gets a list of given type after splitting the matching node value(s) with
the given delimiter regular expression.
|
<T> List<T> |
getDelimitedList(String xpathExpression,
Class<T> type,
String delimRegex,
List<? extends T> defaultValues)
Gets a list of given type after splitting the matching node value(s) with
the given delimiter regular expression.
|
List<String> |
getDelimitedStringList(String xpathExpression)
Gets a list of strings after splitting the matching node value(s)
on commas (CSV).
|
List<String> |
getDelimitedStringList(String xpathExpression,
List<String> defaultValues)
Gets a list of strings after splitting the matching node value(s)
on commas (CSV).
|
List<String> |
getDelimitedStringList(String xpathExpression,
String delimRegex)
Gets a list of strings after splitting the matching node value(s) with
the given delimiter regular expression.
|
List<String> |
getDelimitedStringList(String xpathExpression,
String delimRegex,
List<String> defaultValues)
Gets a list of strings after splitting the matching node value(s) with
the given delimiter regular expression.
|
Dimension |
getDimension(String xpathExpression) |
Dimension |
getDimension(String xpathExpression,
Dimension defaultValue) |
DocumentBuilderFactory |
getDocumentBuilderFactory() |
Double |
getDouble(String xpathExpression) |
Double |
getDouble(String xpathExpression,
Double defaultValue) |
Duration |
getDuration(String xpathExpression)
Gets a duration which can exists as a numerical
value or a textual
representation of a duration as per
DurationParser . |
Duration |
getDuration(String xpathExpression,
Duration defaultValue)
Gets a duration which can exists as a numerical
value or a textual
representation of a duration as per
DurationParser . |
Long |
getDurationMillis(String xpathExpression)
Gets a duration in milliseconds which can exists as a numerical
value or a textual
representation of a duration as per
DurationParser . |
Long |
getDurationMillis(String xpathExpression,
Long defaultValue)
Gets a duration in milliseconds which can exists as a numerical
value or a textual
representation of a duration as per
DurationParser . |
<E extends Enum<E>> |
getEnum(String xpathExpression,
Class<E> enumClass)
Gets an Enum constant matching one of the constants in the provided
Enum class, ignoring case.
|
<E extends Enum<E>> |
getEnum(String xpathExpression,
Class<E> enumClass,
E defaultValue)
Gets an Enum constant matching one of the constants in the provided
Enum class, ignoring case.
|
<E extends Enum<E>> |
getEnumList(String xpathExpression,
Class<E> enumClass,
List<E> defaultValues)
Gets a list of enum constants.
|
ErrorHandler |
getErrorHandler() |
File |
getFile(String xpathExpression)
Gets a file, assuming the node value is a file system path.
|
File |
getFile(String xpathExpression,
File defaultValue)
Gets a file, assuming the node value is a file system path.
|
List<File> |
getFileList(String xpathExpression)
Gets values as a list of files.
|
List<File> |
getFileList(String xpathExpression,
List<File> defaultValue)
Gets values as a list of files.
|
Float |
getFloat(String xpathExpression) |
Float |
getFloat(String xpathExpression,
Float defaultValue) |
Integer |
getInteger(String xpathExpression) |
Integer |
getInteger(String xpathExpression,
Integer defaultValue) |
<T> List<? extends T> |
getList(String xpathExpression,
Class<T> type)
Gets the matching list of elements/attributes, converted from
string to the given type.
|
<T> List<? extends T> |
getList(String xpathExpression,
Class<T> type,
List<? extends T> defaultValues)
Gets the matching list of elements/attributes, converted from
string to the given type.
|
Locale |
getLocale(String xpathExpression) |
Locale |
getLocale(String xpathExpression,
Locale defaultValue) |
Long |
getLong(String xpathExpression) |
Long |
getLong(String xpathExpression,
Long defaultValue) |
String |
getName() |
Node |
getNode() |
Node |
getNode(String xpathExpression) |
<T> T |
getObject(String xpathExpression)
Creates a new instance of the class represented by the "class"
attribute on the node matching the expression.
|
<T> T |
getObject(String xpathExpression,
T defaultObject)
Creates a new instance of the class represented by the "class"
attribute on the node matching the expression.
|
<T> T |
getObjectImpl(Class<?> type,
String xpathExpression)
Creates a new instance of the class represented by the "class"
attribute on the node matching the expression.
|
<T> T |
getObjectImpl(Class<?> type,
String xpathExpression,
T defaultObject)
Creates a new instance of the class represented by the "class"
attribute on the node matching the expression.
|
<T> List<T> |
getObjectList(String xpathExpression)
Creates an instance list from classes represented by the "class"
attribute on the nodes matching the expression.
|
<T> List<T> |
getObjectList(String xpathExpression,
List<T> defaultObjects)
Creates an instance list from classes represented by the "class"
attribute on the nodes matching the expression.
|
<T> List<T> |
getObjectListImpl(Class<?> type,
String xpathExpression)
Creates an instance list from classes represented by the "class"
attribute on the nodes matching the expression.
|
<T> List<T> |
getObjectListImpl(Class<?> type,
String xpathExpression,
List<T> defaultObjects)
Creates an instance list from classes represented by the "class"
attribute on the nodes matching the expression.
|
Path |
getPath(String xpathExpression)
Gets a path, assuming the node value is a file system path.
|
Path |
getPath(String xpathExpression,
Path defaultValue)
Gets a path, assuming the node value is a file system path.
|
List<Path> |
getPathList(String xpathExpression)
Gets values as a list of paths.
|
List<Path> |
getPathList(String xpathExpression,
List<Path> defaultValue)
Gets values as a list of paths.
|
String |
getString(String xpathExpression) |
String |
getString(String xpathExpression,
String defaultValue) |
List<String> |
getStringList(String xpathExpression)
Gets the matching list of elements/attributes as strings.
|
List<String> |
getStringList(String xpathExpression,
List<String> defaultValues)
Gets the matching list of elements/attributes as strings.
|
Map<String,String> |
getStringMap(String xpathList,
String xpathKey,
String xpathValue)
Gets the matching map of elements/attributes as strings.
|
Map<String,String> |
getStringMap(String xpathList,
String xpathKey,
String xpathValue,
Map<String,String> defaultValues)
Gets the matching map of elements/attributes as strings.
|
URL |
getURL(String xpathExpression) |
URL |
getURL(String xpathExpression,
URL defaultValue) |
List<URL> |
getURLList(String xpathExpression) |
List<URL> |
getURLList(String xpathExpression,
List<URL> defaultValue) |
XML |
getXML(String xpathExpression)
Gets the xml subset matching the xpath expression.
|
List<XML> |
getXMLList(String xpathExpression)
Gets the XML subsets matching the xpath expression.
|
EnhancedXMLStreamWriter |
getXMLStreamWriter() |
Writer |
getXMLWriter() |
void |
ifXML(String xpathExpression,
Consumer<XML> then)
If the given expression matches an element, consume that
element.
|
XML |
insertAfter(XML newXML)
Inserts a new XML node after this one, as a sibling of a shared parent.
|
XML |
insertBefore(XML newXML)
Inserts a new XML node before this one, as a sibling of a shared parent.
|
boolean |
isDisabled() |
boolean |
isEnabled() |
static boolean |
isJAXB(Object obj) |
static boolean |
isXMLConfigurable(Object obj) |
Iterator<XMLCursor> |
iterator()
|
static Iterator<XMLCursor> |
iterator(Object obj)
|
String |
join(String delim,
List<?> values) |
static XPath |
newXPath() |
static XPathExpression |
newXPathExpression(String expression) |
static XML.Builder |
of(File file) |
static XML.Builder |
of(InputStream is) |
static XML.Builder |
of(Node node) |
static XML.Builder |
of(Path path) |
static XML.Builder |
of(Reader reader) |
static XML.Builder |
of(String xml) |
static XML.Builder |
of(String rootElementName,
Object object) |
<T> T |
parseXML(String xpathExpression,
Function<XML,T> parser) |
<T> T |
parseXML(String xpathExpression,
Function<XML,T> parser,
T defaultValue) |
<T> List<T> |
parseXMLList(String xpathExpression,
Function<XML,T> parser) |
<T> List<T> |
parseXMLList(String xpathExpression,
Function<XML,T> parser,
List<T> defaultValue) |
<K,V> Map<K,V> |
parseXMLMap(String xpathExpression,
Function<XML,Map.Entry<K,V>> parser) |
<K,V> Map<K,V> |
parseXMLMap(String xpathExpression,
Function<XML,Map.Entry<K,V>> parser,
Map<K,V> defaultValue) |
List<XMLValidationError> |
populate(Object targetObject)
Populates supplied object with this XML.
|
List<XMLValidationError> |
populate(Object targetObject,
String xpathExpression)
Populates supplied object with the XML matching the given expression.
|
XML |
remove()
Removes itself from its XML parent (if any).
|
XML |
removeAttribute(String name)
Removes an attribute on this XML element.
|
XML |
removeElement(String tagName)
Removes an element from this XML.
|
XML |
rename(String newName)
Rename this XML (element tag name).
|
XML |
replace(XML replacement)
Replaces the current XML with the provided one.
|
XML |
setAttribute(String name,
Object value)
Sets an attribute on this XML element, converting the supplied object
to a string (enums are also converted to lowercase).
|
XML |
setAttributes(Map<String,?> attribs)
Sets attributes on this XML element.
|
XML |
setDelimitedAttributeList(String name,
List<?> values)
Sets a list of values as an attribute after joining them with
a comma (CSV).
|
XML |
setDelimitedAttributeList(String name,
String delim,
List<?> values)
Sets a list of values as an attribute after joining them with
the given delimiter.
|
XML |
setTextContent(Object textContent)
Sets the text content of an XML element.
|
Stream<XMLCursor> |
stream()
|
static Stream<XMLCursor> |
stream(Object obj)
|
Node |
toNode() |
<T> T |
toObject()
Creates a new instance of the class represented by the "class" attribute
on this XML root node.
|
<T> T |
toObject(T defaultObject)
Creates a new instance of the class represented by the "class" attribute
on the given node.
|
<T> T |
toObjectImpl(Class<?> type)
Creates a new instance of the class represented by the "class" attribute
on this XML root node.
|
<T> T |
toObjectImpl(Class<?> type,
T defaultObject)
Creates a new instance of the class represented by the "class" attribute
on the given node.
|
Reader |
toReader()
|
String |
toString()
Gets a string representation of this XML.
|
String |
toString(int indent)
Gets a string representation of this XML.
|
XML |
unwrap()
Unwraps this XML by removing the root tag and keeping its child element
(and its nested element).
|
List<XMLValidationError> |
validate()
Validates this XML against an XSD schema attached to the class
represented in this XML root tag "class" attribute.
|
List<XMLValidationError> |
validate(Class<?> clazz)
Validates this XML for classes having an XSD schema attached,
and logs any error/warnings.
|
List<XMLValidationError> |
validate(Object obj)
Validates this XML for objects having an XSD schema attached,
and logs any error/warnings.
|
XML |
wrap(String parentName)
Wraps this XML by adding a parent element around it.
|
void |
write(File file) |
void |
write(File file,
int indent) |
void |
write(Writer writer) |
void |
write(Writer writer,
int indent) |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
forEach, spliterator
public XML(Path file)
Parse an XML file into an XML document, without consideration for namespaces.
file
- the XML file to parseof(Path)
public XML(File file)
Parse an XML file into an XML document, without consideration for namespaces.
file
- the XML file to parseof(File)
public XML(Reader reader)
Parse an XML stream into an XML document, without consideration for namespaces.
reader
- the XML stream to parseof(Reader)
public XML(String xml)
Parse an XML string into an XML document, without consideration for namespaces.
The supplied "xml" string can either be a well-formed XML or a string without angle brackets. When the later is supplied, it is assumed to be the XML root element name (for a fresh XML).
xml
- the XML string to parseof(String)
public XML(Node node)
Creates an XML with the given node.
node
- the node representing the XMLpublic boolean isEnabled()
public boolean isDisabled()
public Node toNode()
public <T> T toObject()
Creates a new instance of the class represented by the "class" attribute
on this XML root 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(XML)
method,
passing it the node XML.
If the class is annotated with an
XmlRootElement
, it will use JAXB to unmarshall it to an object.
Performs XML validation if the target object has an associated schema.
T
- the type of the return valueXMLValidationException
- if the XML has validation errorsXMLException
- if something prevented object creationpublic <T> T toObject(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(XML)
method,
passing it the node XML.
If the class is annotated with an
XmlRootElement
, it will use JAXB to unmarshall it to an object.
Performs XML validation if the target object has an associated schema.
T
- the type of the return valuedefaultObject
- if returned object is null or undefined,
returns this default object.XMLException
- if something prevented object creationpublic <T> T toObjectImpl(Class<?> type)
Creates a new instance of the class represented by the "class" attribute
on this XML root 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(XML)
method,
passing it the node XML.
If the class is annotated with an
XmlRootElement
, it will use JAXB to unmarshall it to an object.
Other than making sure the class is a subtype of the specified
super class, the main difference between method this and
toObject()
is the support for partial class names.
That is, this method will scan the current class loader for a class
with its name ending with the value of the "class" attribute. If
more than one is found, an XMLException
will be thrown.
If you are expecting fully qualified class names, use the
toObject()
method, which is faster.
T
- the type of the return valuetype
- the expected class (sub)type to returnnull
.XMLValidationException
- if the XML has validation errorsXMLException
- if something prevented object creationpublic <T> T toObjectImpl(Class<?> type, 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(XML)
method,
passing it the node XML.
If the class is annotated with an
XmlRootElement
, it will use JAXB to unmarshall it to an object.
Other than making sure the class is a subtype of the specified
super class, the main difference between method this and
toObject(Object)
is the support for partial class names.
That is, this method will scan the current class loader for a class
with its name ending with the value of the "class" attribute. If
more than one is found, an XMLException
will be thrown.
If you are expecting fully qualified class names, use the
toObject(Object)
method, which is faster.
T
- the type of the return valuetype
- the expected class (sub)type to returndefaultObject
- if returned object is null or undefined,
returns this default object.null
, else the default object.XMLException
- if something prevented object creationpublic List<XMLValidationError> populate(Object targetObject, String xpathExpression)
Populates supplied object with the XML matching the given expression.
If there is no match, the object does not get populated.
Takes into consideration whether the target object implements
IXMLConfigurable
or JAXB.
Performs XML validation if the target object has an associated schema.
xpathExpression
- XPath expressiontargetObject
- object to populate with this XMLnull
)public List<XMLValidationError> populate(Object targetObject)
Populates supplied object with this XML. Takes into consideration
whether the target object implements IXMLConfigurable
or
JAXB.
Performs XML validation if the target object has an associated schema.
Invoking this method with a null
target has no effect
(returns an empty list).
targetObject
- object to populate with this XMLnull
)public <T> T getObject(String xpathExpression)
Creates a new instance of the class represented by the "class"
attribute on the node matching the expression.
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(XML)
method,
passing it the node XML.
This method should throw a
XMLException
upon error. Use a method
with a default value argument to avoid throwing exceptions.
T
- the type of the return valuexpathExpression
- xpath expressionpublic <T> T getObject(String xpathExpression, T defaultObject)
Creates a new instance of the class represented by the "class"
attribute on the node matching the expression.
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(XML)
method,
passing it the node XML.
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 exceptions on errors.
T
- the type of the return valuedefaultObject
- if returned object is null or undefined,
returns this default object.xpathExpression
- xpath expressionpublic <T> List<T> getObjectList(String xpathExpression, List<T> defaultObjects)
Creates an instance list from classes represented by the "class"
attribute on the nodes matching the expression.
The classes must have an empty constructor.
If a class is an instance of IXMLConfigurable
, the object
created will be automatically populated by invoking the
IXMLConfigurable.loadFromXML(XML)
method,
passing it the node XML.
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 exceptions on errors.
T
- the type of the return valuexpathExpression
- xpath expressiondefaultObjects
- if returned list is empty,
returns this default list.XMLException
- if instance cannot be created/populatedpublic <T> List<T> getObjectList(String xpathExpression)
Creates an instance list from classes represented by the "class"
attribute on the nodes matching the expression.
The classes must have an empty constructor.
If a class is an instance of IXMLConfigurable
, the object
created will be automatically populated by invoking the
IXMLConfigurable.loadFromXML(XML)
method,
passing it the node XML.
This method should throw a
XMLException
upon error. Use a method
with a default value argument to avoid throwing exceptions.
T
- the type of the return valuexpathExpression
- xpath expressionXMLException
- if instance cannot be created/populatedpublic <T> T getObjectImpl(Class<?> type, String xpathExpression)
Creates a new instance of the class represented by the "class"
attribute on the node matching the expression.
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(XML)
method,
passing it the node XML.
This method should throw a
XMLException
upon error. Use a method
with a default value argument to avoid throwing exceptions.
Other than making sure the class is a subtype of the specified
super class, the main difference between method this and
getObject(String)
is the support for partial class names.
That is, this method will scan the current class loader for a class
with its name ending with the value of the "class" attribute. If
more than one is found, an XMLException
will be thrown.
If you are expecting fully qualified class names, use the
getObject(String)
method, which is faster.
T
- the type of the return valuetype
- the expected class (sub)type to returnxpathExpression
- xpath expressionpublic <T> T getObjectImpl(Class<?> type, String xpathExpression, T defaultObject)
Creates a new instance of the class represented by the "class"
attribute on the node matching the expression.
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(XML)
method,
passing it the node XML.
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 exceptions on errors.
Other than making sure the class is a subtype of the specified
super class, the main difference between method this and
getObject(String, Object)
is the support for partial class names.
That is, this method will scan the current class loader for a class
with its name ending with the value of the "class" attribute. If
more than one is found, an XMLException
will be thrown.
If you are expecting fully qualified class names, use the
getObject(String, Object)
method, which is faster.
T
- the type of the return valuetype
- the expected class (sub)type to returndefaultObject
- if returned object is null or undefined,
returns this default object.xpathExpression
- xpath expressionpublic <T> List<T> getObjectListImpl(Class<?> type, String xpathExpression, List<T> defaultObjects)
Creates an instance list from classes represented by the "class"
attribute on the nodes matching the expression.
The classes must have an empty constructor.
If a class is an instance of IXMLConfigurable
, the object
created will be automatically populated by invoking the
IXMLConfigurable.loadFromXML(XML)
method,
passing it the node XML.
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 exceptions on errors.
Other than making sure the class is a subtype of the specified
super class, the main difference between method this and
getObjectList(String, List)
is the support for partial class names.
That is, this method will scan the current class loader for a class
with its name ending with the value of the "class" attribute. If
more than one is found, an XMLException
will be thrown.
If you are expecting fully qualified class names, use the
getObjectList(String, List)
method, which is faster.
T
- the type of the return valuetype
- the expected class (sub)type to returnxpathExpression
- xpath expressiondefaultObjects
- if returned list is empty,
returns this default list.XMLException
- if instance cannot be created/populatedpublic <T> List<T> getObjectListImpl(Class<?> type, String xpathExpression)
Creates an instance list from classes represented by the "class"
attribute on the nodes matching the expression.
The classes must have an empty constructor.
If a class is an instance of IXMLConfigurable
, the object
created will be automatically populated by invoking the
IXMLConfigurable.loadFromXML(XML)
method,
passing it the node XML.
This method should throw a
XMLException
upon error. Use a method
with a default value argument to avoid throwing exceptions.
Other than making sure the class is a subtype of the specified
super class, the main difference between method this and
getObjectList(String)
is the support for partial class names.
That is, this method will scan the current class loader for a class
with its name ending with the value of the "class" attribute. If
more than one is found, an XMLException
will be thrown.
If you are expecting fully qualified class names, use the
getObjectList(String)
method, which is faster.
T
- the type of the return valuetype
- the expected class (sub)type to returnxpathExpression
- xpath expressionXMLException
- if instance cannot be created/populatedpublic XML getXML(String xpathExpression)
xpathExpression
- expression to matchnull
is xpath has no matchpublic void ifXML(String xpathExpression, Consumer<XML> then)
xpathExpression
- expressionthen
- XML consumerpublic void forEach(String xpathExpression, Consumer<XML> action)
xpathExpression
- expressionaction
- The action to be performed for each elementpublic List<XML> getXMLList(String xpathExpression)
xpathExpression
- expression to matchnull
public Reader toReader()
Reader
from a Node
.
Do not forget to close the reader instance when you are done with it.XMLException
- cannot read configurationpublic String toString()
toString
in class Object
XMLException
- cannot read configurationpublic String toString(int indent)
indent
- whether to indent the XMLXMLException
- cannot read configurationpublic List<XMLValidationError> validate()
Validates this XML against an XSD schema attached to the class represented in this XML root tag "class" attribute. In addition to being returned, some validation errors/warnings may be logged. The schema is expected to be found at the same classpath location and have the same name as the object class, but with the ".xsd" extension.
This method is the same as invoking
validate(getClass("@class"))
null
)public List<XMLValidationError> validate(Object obj)
Validates this XML for objects having an XSD schema attached, and logs any error/warnings. The schema expected to be found at the same classpath location and have the same name as the object class, but with the ".xsd" extension.
This method is the same as invoking validate(obj.getClass())
obj
- the object to validate the XML fornull
)public List<XMLValidationError> validate(Class<?> clazz)
clazz
- the class to validate the XML fornull
)public static void assertWriteRead(IXMLConfigurable xmlConfigurable, String elementName)
IXMLConfigurable
instance
can be written, and read into an new instance that is equal as per
Object.equals(Object)
.xmlConfigurable
- the instance to test if it writes/read properlyelementName
- the tag name of the root element being writtenXMLException
- Cannot save/load configurationpublic boolean contains(String xpathExpression)
public List<String> getDelimitedStringList(String xpathExpression)
xpathExpression
- XPath expression to the node value(s) to splitnull
public List<String> getDelimitedStringList(String xpathExpression, List<String> defaultValues)
xpathExpression
- XPath expression to the node value(s) to splitdefaultValues
- default values if the split returns
null
or an empty listpublic List<String> getDelimitedStringList(String xpathExpression, String delimRegex)
xpathExpression
- XPath expression to the node value(s) to splitdelimRegex
- regular expression matching split delimiternull
public List<String> getDelimitedStringList(String xpathExpression, String delimRegex, List<String> defaultValues)
xpathExpression
- XPath expression to the node value(s) to splitdelimRegex
- regular expression matching split delimiterdefaultValues
- default values if the split returns
null
or an empty listpublic <T> List<T> getDelimitedList(String xpathExpression, Class<T> type)
T
- returned list typexpathExpression
- XPath expression to the node value(s) to splittype
- target list typenull
public <T> List<T> getDelimitedList(String xpathExpression, Class<T> type, List<T> defaultValues)
T
- returned list typexpathExpression
- XPath expression to the node value(s) to splittype
- target list typedefaultValues
- default values if the split returns
null
or an empty listpublic <T> List<T> getDelimitedList(String xpathExpression, Class<T> type, String delimRegex)
T
- returned list typexpathExpression
- XPath expression to the node value(s) to splittype
- target list typedelimRegex
- regular expression matching split delimiternull
public <T> List<T> getDelimitedList(String xpathExpression, Class<T> type, String delimRegex, List<? extends T> defaultValues)
T
- returned list typexpathExpression
- XPath expression to the node value(s) to splittype
- target list typedelimRegex
- regular expression matching split delimiterdefaultValues
- default values if the split returns
null
or an empty listpublic static XPath newXPath()
public static XPathExpression newXPathExpression(String expression)
public Node getNode()
public List<String> getStringList(String xpathExpression)
xpathExpression
- XPath expression to the node valuesnull
public List<String> getStringList(String xpathExpression, List<String> defaultValues)
xpathExpression
- XPath expression to the node valuesdefaultValues
- default values if the expression does not match
anything.public <T> T get(String xpathExpression, Class<T> type)
T
- target typexpathExpression
- XPath expression to the node valuetype
- target class type of returned valuepublic <T> T get(String xpathExpression, Class<T> type, T defaultValue)
T
- target typexpathExpression
- XPath expression to the node valuetype
- target class type of returned valuedefaultValue
- default value if the expression returns
null
public <T> List<? extends T> getList(String xpathExpression, Class<T> type)
T
- returned list typexpathExpression
- XPath expression to the node valuestype
- target class type of returned listnull
public <T> List<? extends T> getList(String xpathExpression, Class<T> type, List<? extends T> defaultValues)
T
- returned list typexpathExpression
- XPath expression to the node valuestype
- target class type of returned listdefaultValues
- default values if the expression returns
null
or an empty listpublic Map<String,String> getStringMap(String xpathList, String xpathKey, String xpathValue)
xpathList
- XPath expression to the node list representing the mapxpathKey
- XPath expression to a node keyxpathValue
- XPath expression to a node valuenull
public Map<String,String> getStringMap(String xpathList, String xpathKey, String xpathValue, Map<String,String> defaultValues)
xpathList
- XPath expression to the node list representing the mapxpathKey
- XPath expression to a node keyxpathValue
- XPath expression to a node valuedefaultValues
- default values if the expressions return
null
or an empty mapnull
unless default value
is returned and is null
public Long getDataSize(String xpathExpression)
xpathExpression
- xpath to the element/attribute with the sizepublic Long getDataSize(String xpathExpression, Long defaultValue)
xpathExpression
- xpath to the element/attribute with the sizedefaultValue
- default valuenull
public Long getDataSize(String xpathExpression, DataUnit targetUnit, Long defaultValue)
xpathExpression
- xpath to the element/attribute with the sizetargetUnit
- the unit to convert the value intodefaultValue
- default valuenull
public Long getDurationMillis(String xpathExpression)
DurationParser
.
If the key value is found but there are parsing errors, a
DurationParserException
will be thrown.xpathExpression
- xpath to the element/attribute containing the
durationpublic Long getDurationMillis(String xpathExpression, Long defaultValue)
DurationParser
.
If the key value is found but there are parsing errors, a
DurationParserException
will be thrown.xpathExpression
- xpath to the element/attribute containing the
durationdefaultValue
- default durationpublic Duration getDuration(String xpathExpression)
DurationParser
.
If the duration does not exists for the given key or is blank,
null
is returned.
If the key value is found but there are parsing errors, a
DurationParserException
will be thrown.xpathExpression
- xpath to the element/attribute containing the
durationpublic Duration getDuration(String xpathExpression, Duration 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.xpathExpression
- xpath to the element/attribute containing the
durationdefaultValue
- default durationpublic String getName()
public XML addElement(String tagName)
tagName
- element namepublic XML addElement(String tagName, Object value)
Adds a child element to this XML root element.
If the element value is blank, and empty element is created.
Otherwise, the value is handled as
of(String, Object)
tagName
- element namevalue
- element valuenull
if value is
null
public XML addDelimitedElementList(String name, List<?> values)
name
- attribute namevalues
- attribute valuespublic XML addDelimitedElementList(String name, String delim, List<?> values)
name
- attribute namedelim
- delimitervalues
- attribute valuespublic List<XML> addElementMap(String tagName, String attributeName, Map<?,?> map)
Adds a Map
as a series of elements without a parent element
wrapping that group. Map keys are defined as element attributes
and the map value is the element content. The structure can be
visualized like this:
<tagName attributeName="(key)">(value)</tagName> <tagName attributeName="(key)">(value)</tagName> ...
Map keys are assumed to be strings or single objects with supported
conversion to string (see Converter
).
Map values can be single values or multi-values. Arrays or collections
will have their values be treated as individual elements with the same
key name. In any case, single or multiple values are otherwise converted
to strings just like keys.
tagName
- name of tags for each map entriesattributeName
- name of the tag attribute holding the map entry keymap
- map to addnull
if map is null
.public XML addElementMap(String parentTagName, String tagName, String attributeName, Map<?,?> map)
Adds a Map
as a series of elements with a parent element
wrapping that group. Map keys are defined as element attributes
and the map value is the element content. The structure can be
visualized like this:
<parentTagName> <tagName attributeName="(key)">(value)</tagName> <tagName attributeName="(key)">(value)</tagName> ... </parentTagName>
Map keys are assumed to be strings or single objects with supported
conversion to string (see Converter
).
Map values can be single values or multi-values. Arrays or collections
will have their values be treated as individual elements with the same
key name. In any case, single or multiple values are otherwise converted
to strings just like keys.
parentTagName
- required name of map elements wrapper tagtagName
- name of tags for each map entriesattributeName
- name of the tag attribute holding the map entry keymap
- map to addnull
if map is null
.public XML removeElement(String tagName)
tagName
- element namepublic XML remove()
public XML insertBefore(XML newXML)
newXML
- the XML to insertpublic XML insertAfter(XML newXML)
newXML
- the XML to insertpublic XML setAttribute(String name, Object value)
null
value is equivalent to not
adding or removing that attribute.name
- attribute namevalue
- attribute valuepublic XML setAttributes(Map<String,?> attribs)
attribs
- attributespublic XML setDelimitedAttributeList(String name, List<?> values)
name
- attribute namevalues
- attribute valuespublic XML setDelimitedAttributeList(String name, String delim, List<?> values)
name
- attribute namedelim
- delimitervalues
- attribute valuespublic XML removeAttribute(String name)
name
- attribute namepublic XML setTextContent(Object textContent)
textContent
- text contentpublic Writer getXMLWriter()
public EnhancedXMLStreamWriter getXMLStreamWriter()
public void write(Writer writer)
public void write(Writer writer, int indent)
public void write(File file)
public void write(File file, int indent)
public XML unwrap()
XMLException
.public XML rename(String newName)
newName
- new name for this XMLpublic XML wrap(String parentName)
parentName
- name of wrapping elementpublic XML clear()
public XML replace(XML replacement)
replacement
- replacing XMLpublic final <E extends Enum<E>> E getEnum(String xpathExpression, Class<E> enumClass)
E
- enum typexpathExpression
- XPath expression to the enum value.enumClass
- target enum classnull
if no values are matching.public final <E extends Enum<E>> E getEnum(String xpathExpression, Class<E> enumClass, E defaultValue)
E
- enum typexpathExpression
- XPath expression to the enum value.enumClass
- target enum classdefaultValue
- defaultValuepublic <E extends Enum<E>> List<E> getEnumList(String xpathExpression, Class<E> enumClass, List<E> defaultValues)
E
- enum typexpathExpression
- XPath expressionenumClass
- target enum classdefaultValues
- default valuespublic <E extends Enum<E>> List<E> getDelimitedEnumList(String xpathExpression, Class<E> enumClass, List<E> defaultValues)
E
- enum typexpathExpression
- XPath expression to the node value(s) to splitenumClass
- target enum classdefaultValues
- default values if the split returns
null
or an empty listpublic <E extends Enum<E>> List<E> getDelimitedEnumList(String xpathExpression, Class<E> enumClass, String delimRegex, List<E> defaultValues)
E
- enum typexpathExpression
- XPath expression to the node value(s) to splitenumClass
- target enum classdelimRegex
- regular expression matching split delimiterdefaultValues
- default values if the split returns
null
or an empty listpublic final Path getPath(String xpathExpression)
xpathExpression
- XPath expression to the node containing the pathpublic final Path getPath(String xpathExpression, Path defaultValue)
xpathExpression
- XPath expression to the node containing the pathdefaultValue
- default path being returned if no path has been
defined for the given expression.public final List<Path> getPathList(String xpathExpression)
xpathExpression
- XPath expressionpublic final List<Path> getPathList(String xpathExpression, List<Path> defaultValue)
xpathExpression
- XPath expressiondefaultValue
- default valuepublic final File getFile(String xpathExpression)
xpathExpression
- XPath expression to the node containing the pathpublic final File getFile(String xpathExpression, File defaultValue)
xpathExpression
- XPath expression to the node containing the pathdefaultValue
- default file being returned if no file has been
defined for the given expression.public final List<File> getFileList(String xpathExpression)
xpathExpression
- XPath expressionpublic final List<File> getFileList(String xpathExpression, List<File> defaultValue)
xpathExpression
- XPath expressiondefaultValue
- default valuepublic final <T> List<Class<T>> getClassList(String xpathExpression)
T
- returned list typexpathExpression
- XPath expressionpublic final <T> List<Class<? extends T>> getClassList(String xpathExpression, List<Class<? extends T>> defaultValue)
T
- returned list typexpathExpression
- XPath expressiondefaultValue
- default valuepublic <T> List<T> parseXMLList(String xpathExpression, Function<XML,T> parser, List<T> defaultValue)
public <K,V> Map<K,V> parseXMLMap(String xpathExpression, Function<XML,Map.Entry<K,V>> parser)
public <K,V> Map<K,V> parseXMLMap(String xpathExpression, Function<XML,Map.Entry<K,V>> parser, Map<K,V> defaultValue)
public void checkDeprecated(String deprecatedXPath, String replacement, boolean throwException)
XMLException
.deprecatedXPath
- xpath to the invalid entryreplacement
- new xpath or instructions to replacethrowException
- true
to throw exception, else log
a warningpublic void checkDeprecated(String deprecatedXPath, boolean throwException)
XMLException
.deprecatedXPath
- xpath to the invalid entrythrowException
- true
to throw exception, else log
a warningpublic ErrorHandler getErrorHandler()
public DocumentBuilderFactory getDocumentBuilderFactory()
public static boolean isXMLConfigurable(Object obj)
public static boolean isJAXB(Object obj)
public Stream<XMLCursor> stream()
Stream
of XMLCursor
from this XML,
in sequential order.
Invoking a "read" methods on XMLCursor
which reads child
elements will result in the stream skipping those already read
elements.public static Iterator<XMLCursor> iterator(Object obj)
Returns an Iterator
of XMLCursor
from the supplied XML
object, in sequential order.
Invoking a "read" methods on XMLCursor
which reads child
elements will result in the iterator skipping those already read
elements.
The object argument type must be one of the following:
obj
- the XML to iterate overpublic static Stream<XMLCursor> stream(Object obj)
Returns a Stream
of XMLCursor
from the supplied XML
object, in sequential order.
Invoking a "read" methods on XMLCursor
which reads child
elements will result in the stream skipping those already read
elements.
The object argument type must be one of the following:
obj
- the XML to streampublic static XML.Builder of(File file)
public static XML.Builder of(Path path)
public static XML.Builder of(Node node)
public static XML.Builder of(InputStream is)
public static XML.Builder of(Reader reader)
public static XML.Builder of(String xml)
public static XML.Builder of(String rootElementName, Object object)
Copyright © 2008–2023 Norconex Inc.. All rights reserved.