public class XmlTool
extends java.lang.Object
This provides methods for both SAX and DOM handling.
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
RELEASE_DATE
The current version of the library.
|
static java.lang.String |
XML_ATTR_VERSION
Version attribute which should be used in a lot of cases.
|
Modifier and Type | Method and Description |
---|---|
static int |
checkTagAndVersion(org.w3c.dom.Element element,
java.lang.String tag,
int maxVersion)
Check that the given element has a defined tag name and contains an integer
version attribute which is not higher than a given
number. |
static int |
checkTagAndVersion(org.w3c.dom.Element element,
java.lang.String tag,
int minVersion,
int maxVersion)
Check that the given element has a defined tag name and contains an integer
version attribute which is inside a given range. |
static boolean |
getBooleanValue(org.w3c.dom.Element element,
java.lang.String attributeName)
Get a boolean value from an attribute.
|
static boolean |
getBooleanValue(org.w3c.dom.Element element,
java.lang.String attributeName,
boolean fallback)
Get an optional boolean value from an attribute.
|
static boolean |
getBooleanValue(java.lang.String elementTag,
org.xml.sax.Attributes attributes,
java.lang.String attributeName)
Get a boolean value from an attribute.
|
static boolean |
getBooleanValue(java.lang.String elementTag,
org.xml.sax.Attributes attributes,
java.lang.String attributeName,
boolean fallback)
Get an optional boolean value from an attribute.
|
static java.util.List<org.w3c.dom.Element> |
getChildElements(org.w3c.dom.Element element,
java.lang.String tagName)
Get the child elements with a given name.
|
static Indexable<org.w3c.dom.Element> |
getChildren(org.w3c.dom.Element element,
java.lang.String subElementTag)
Get the direct children of an element with a given tag.
|
static Indexable<org.w3c.dom.Element> |
getChildren(org.w3c.dom.Element element,
java.lang.String subElementTag,
int requiredSize)
Get the direct children of an element with a given tag.
|
static java.lang.String |
getContent(org.w3c.dom.Element element,
boolean forceNonEmpty)
Get the text content of the given element.
|
static double |
getDoubleValue(org.w3c.dom.Element element,
java.lang.String attributeName)
Get a double value from an attribute.
|
static double |
getDoubleValue(java.lang.String elementTag,
org.xml.sax.Attributes attributes,
java.lang.String attributeName)
Get a double value from an attribute.
|
static double |
getDoubleValue(java.lang.String elementTag,
org.xml.sax.Attributes attributes,
java.lang.String attributeName,
double fallback)
Get an optional double value from an attribute.
|
static <E extends java.lang.Enum<E>> |
getEnumValue(org.w3c.dom.Element element,
java.lang.String attributeName,
java.lang.Class<E> enumClass)
Get an enum value from an attribute.
|
static <E extends java.lang.Enum<E>> |
getEnumValue(java.lang.String elementTag,
org.xml.sax.Attributes attributes,
java.lang.String attributeName,
java.lang.Class<E> enumClass)
Get an enum value from an attribute.
|
static int |
getIntValue(org.w3c.dom.Element element,
java.lang.String attributeName)
Get an integer value from an attribute.
|
static int |
getIntValue(java.lang.String elementTag,
org.xml.sax.Attributes attributes,
java.lang.String attributeName)
Get an integer value from an attribute.
|
static int |
getIntValue(java.lang.String elementTag,
org.xml.sax.Attributes attributes,
java.lang.String attributeName,
int fallback)
Get an optional integer value from an attribute.
|
static org.w3c.dom.Element |
getOptionalSingleChild(org.w3c.dom.Element element,
java.lang.String subElementTag)
Get a optional singular sub element from a given element.
|
static java.lang.String |
getOptionalValue(org.xml.sax.Attributes attributes,
java.lang.String attributeName)
Get the value of an optional attribute.
|
static <T> T |
getOptionalValue(org.xml.sax.Attributes attributes,
java.lang.String attributeName,
java.util.function.Function<java.lang.String,? extends T> extractor)
Get an unwrapped value from an attribute.
|
static java.lang.String |
getOptionalValue(org.w3c.dom.Element element,
java.lang.String attributeName)
Get the value of an optional attribute of the given element.
|
static <T> T |
getOptionalValue(org.w3c.dom.Element element,
java.lang.String attributeName,
java.util.function.Function<java.lang.String,? extends T> extractor)
Get a unwrapped value from an attribute.
|
static <T,E extends java.lang.Exception> |
getOptionalValueF(org.xml.sax.Attributes attributes,
java.lang.String attributeName,
FragileFunction1<? extends T,E,java.lang.String> extractor)
Get a unwrapped value from an attribute.
|
static <T,E extends java.lang.Exception> |
getOptionalValueF(org.w3c.dom.Element element,
java.lang.String attributeName,
FragileFunction1<? extends T,E,java.lang.String> extractor)
Get a unwrapped value from an attribute.
|
static org.w3c.dom.Element |
getSingleChild(org.w3c.dom.Element element)
Get the single child of an element, require that it is an element itself.
|
static org.w3c.dom.Element |
getSingleChild(org.w3c.dom.Element element,
java.lang.String subElementTag)
Get a singular sub element from a given element.
|
static java.lang.Boolean |
getTriStateValue(org.w3c.dom.Element element,
java.lang.String attributeName)
Get a tri-state boolean value from an attribute.
|
static java.lang.Boolean |
getTriStateValue(org.w3c.dom.Element element,
java.lang.String attributeName,
java.lang.Boolean fallback)
Get a tri-state optional boolean value from an attribute.
|
static java.lang.Boolean |
getTriStateValue(java.lang.String elementTag,
org.xml.sax.Attributes attributes,
java.lang.String attributeName)
Get a tri-state boolean value from an attribute.
|
static java.lang.Boolean |
getTriStateValue(java.lang.String elementTag,
org.xml.sax.Attributes attributes,
java.lang.String attributeName,
java.lang.Boolean fallback)
Get an optional tri-state boolean value from an attribute.
|
static java.lang.String |
getValue(org.xml.sax.Attributes attributes,
java.lang.String attributeName,
java.lang.String defaultValue)
Get the value of the attribute of the given element.
|
static java.lang.String |
getValue(org.w3c.dom.Element element,
java.lang.String attributeName)
Get the value of the attribute of the given element.
|
static <T> T |
getValue(org.w3c.dom.Element element,
java.lang.String attributeName,
java.util.function.Function<java.lang.String,? extends T> extractor)
Get a unwrapped value from an attribute.
|
static java.lang.String |
getValue(org.w3c.dom.Element element,
java.lang.String attributeName,
java.lang.String defaultValue)
Get the value of the attribute of the given element.
|
static java.lang.String |
getValue(java.lang.String elementTag,
org.xml.sax.Attributes attributes,
java.lang.String attributeName)
Get the value from attributes.
|
static <T> T |
getValue(java.lang.String elementTag,
org.xml.sax.Attributes attributes,
java.lang.String attributeName,
java.util.function.Function<java.lang.String,? extends T> extractor)
/**
Get a unwrapped value from an attribute.
|
static <T,E extends java.lang.Exception> |
getValueF(org.w3c.dom.Element element,
java.lang.String attributeName,
FragileFunction1<? extends T,E,java.lang.String> extractor)
Get a unwrapped value from an attribute.
|
static <T,E extends java.lang.Exception> |
getValueF(java.lang.String elementTag,
org.xml.sax.Attributes attributes,
java.lang.String attributeName,
FragileFunction1<? extends T,E,java.lang.String> extractor)
Get a unwrapped value from an attribute.
|
static org.w3c.dom.Document |
loadFromFile(java.lang.String path)
Load a document from a file or URL.
|
public static final java.lang.String RELEASE_DATE
public static final java.lang.String XML_ATTR_VERSION
1
which allows to usage of the shorter check method.@NotNull public static java.lang.String getValue(@NotNull org.w3c.dom.Element element, @NotNull java.lang.String attributeName) throws org.xml.sax.SAXException
element
- element containing attributeattributeName
- name of attributeorg.xml.sax.SAXException
- if attribute is non-existent@Nullable public static java.lang.String getOptionalValue(@NotNull org.w3c.dom.Element element, @NotNull java.lang.String attributeName)
element
- element containing attributeattributeName
- name of attributenull
if the attribute does not existpublic static <T> T getValue(@NotNull org.w3c.dom.Element element, @NotNull java.lang.String attributeName, @NotNull java.util.function.Function<java.lang.String,? extends T> extractor) throws org.xml.sax.SAXException
T
- result type of this methodelement
- element containing attributeattributeName
- name of attributeextractor
- converter from the textual value to the expected value typeorg.xml.sax.SAXException
- when the attribute does not exist, or when the extractor throwspublic static <T,E extends java.lang.Exception> T getValueF(@NotNull org.w3c.dom.Element element, @NotNull java.lang.String attributeName, @NotNull FragileFunction1<? extends T,E,java.lang.String> extractor) throws org.xml.sax.SAXException
T
- result type of this methodE
- exception type of extractorelement
- element containing attributeattributeName
- name of attributeextractor
- converter from the textual value to the expected value type which may throw an exceptionorg.xml.sax.SAXException
- when the attribute does not exist, or when the extractor throws an exception@Nullable public static <T> T getOptionalValue(@NotNull org.w3c.dom.Element element, @NotNull java.lang.String attributeName, @NotNull java.util.function.Function<java.lang.String,? extends T> extractor)
T
- result type of this methodelement
- element containing attributeattributeName
- name of attributeextractor
- converter from the textual value to the expected value typenull
if the value does not exist@Nullable public static <T,E extends java.lang.Exception> T getOptionalValueF(@NotNull org.w3c.dom.Element element, @NotNull java.lang.String attributeName, @NotNull FragileFunction1<? extends T,E,java.lang.String> extractor) throws E extends java.lang.Exception
T
- result type of this methodE
- exception type of extractorelement
- element containing attributeattributeName
- name of attributeextractor
- converter from the textual value to the expected value typenull
if the value does not existE
- when the extractor throws an exceptionE extends java.lang.Exception
@NotNull public static java.lang.String getValue(@NotNull org.w3c.dom.Element element, @NotNull java.lang.String attributeName, @NotNull java.lang.String defaultValue)
element
- element containing attributeattributeName
- name of attributedefaultValue
- default valuepublic static int getIntValue(@NotNull org.w3c.dom.Element element, @NotNull java.lang.String attributeName) throws org.xml.sax.SAXException
element
- element containing attributeattributeName
- name of attributeorg.xml.sax.SAXException
- if attribute is non-existent or not an integer valuepublic static double getDoubleValue(@NotNull org.w3c.dom.Element element, @NotNull java.lang.String attributeName) throws org.xml.sax.SAXException
element
- element containing attributeattributeName
- name of attributeorg.xml.sax.SAXException
- if attribute is non-existent or not a double valuepublic static boolean getBooleanValue(@NotNull org.w3c.dom.Element element, @NotNull java.lang.String attributeName) throws org.xml.sax.SAXException
element
- element containing attributeattributeName
- name of attributeorg.xml.sax.SAXException
- if attribute is non-existent or not a boolean valuepublic static boolean getBooleanValue(@NotNull org.w3c.dom.Element element, @NotNull java.lang.String attributeName, boolean fallback) throws org.xml.sax.SAXException
element
- element containing attributeattributeName
- name of attributefallback
- default value returned if attribute is not setorg.xml.sax.SAXException
- if attribute is not a boolean value@Nullable public static java.lang.Boolean getTriStateValue(@NotNull org.w3c.dom.Element element, @NotNull java.lang.String attributeName) throws org.xml.sax.SAXException
element
- element containing attributeattributeName
- name of attributenull
for undefinedorg.xml.sax.SAXException
- if attribute is non-existent or not a boolean value@Nullable public static java.lang.Boolean getTriStateValue(@NotNull org.w3c.dom.Element element, @NotNull java.lang.String attributeName, @Nullable java.lang.Boolean fallback) throws org.xml.sax.SAXException
element
- element containing attributeattributeName
- name of attributefallback
- default value returned if attribute is not setnull
for undefinedorg.xml.sax.SAXException
- if attribute is not a boolean value@NotNull public static <E extends java.lang.Enum<E>> E getEnumValue(@NotNull org.w3c.dom.Element element, @NotNull java.lang.String attributeName, @NotNull java.lang.Class<E> enumClass) throws org.xml.sax.SAXException
E
- enum typeelement
- element containing attributeattributeName
- name of attributeenumClass
- enum classorg.xml.sax.SAXException
- if attribute is non-existent or not an enum of the given type@NotNull public static java.lang.String getValue(@NotNull java.lang.String elementTag, @NotNull org.xml.sax.Attributes attributes, @NotNull java.lang.String attributeName) throws org.xml.sax.SAXException
elementTag
- element tag, used for errorsattributes
- attributes of XML elementattributeName
- name of attributeorg.xml.sax.SAXException
- if attribute is non-existent@NotNull public static java.lang.String getValue(@NotNull org.xml.sax.Attributes attributes, @NotNull java.lang.String attributeName, @NotNull java.lang.String defaultValue)
attributes
- attributes of XML elementattributeName
- name of attributedefaultValue
- default valuepublic static int getIntValue(@NotNull java.lang.String elementTag, @NotNull org.xml.sax.Attributes attributes, @NotNull java.lang.String attributeName) throws org.xml.sax.SAXException
elementTag
- element tag, used for errorsattributes
- attributes of XML elementattributeName
- name of attributeorg.xml.sax.SAXException
- if attribute is non-existent or not an integer valuepublic static int getIntValue(@NotNull java.lang.String elementTag, @NotNull org.xml.sax.Attributes attributes, @NotNull java.lang.String attributeName, int fallback) throws org.xml.sax.SAXException
elementTag
- element tag, used for errorsattributes
- attributes of XML elementattributeName
- name of attributefallback
- fallback value used if attribute is not definedorg.xml.sax.SAXException
- if attribute is not an integer valuepublic static double getDoubleValue(@NotNull java.lang.String elementTag, @NotNull org.xml.sax.Attributes attributes, @NotNull java.lang.String attributeName) throws org.xml.sax.SAXException
elementTag
- element tag, used for errorsattributes
- attributes of XML elementattributeName
- name of attributeorg.xml.sax.SAXException
- if attribute is non-existent or not a double valuepublic static double getDoubleValue(@NotNull java.lang.String elementTag, @NotNull org.xml.sax.Attributes attributes, @NotNull java.lang.String attributeName, double fallback) throws org.xml.sax.SAXException
elementTag
- element tag, used for errorsattributes
- attributes of XML elementattributeName
- name of attributefallback
- fallback value returned if attribute is not definedorg.xml.sax.SAXException
- if attribute is not a double valuepublic static boolean getBooleanValue(@NotNull java.lang.String elementTag, @NotNull org.xml.sax.Attributes attributes, @NotNull java.lang.String attributeName) throws org.xml.sax.SAXException
elementTag
- element tag, used for errorsattributes
- attributes of XML elementattributeName
- name of attributeorg.xml.sax.SAXException
- if attribute is non-existent or not a boolean valuepublic static boolean getBooleanValue(@NotNull java.lang.String elementTag, @NotNull org.xml.sax.Attributes attributes, @NotNull java.lang.String attributeName, boolean fallback) throws org.xml.sax.SAXException
elementTag
- element tag, used for errorsattributes
- attributes of XML elementattributeName
- name of attributefallback
- default value returned if attribute is not setorg.xml.sax.SAXException
- if attribute is not a boolean value@Nullable public static java.lang.Boolean getTriStateValue(@NotNull java.lang.String elementTag, @NotNull org.xml.sax.Attributes attributes, @NotNull java.lang.String attributeName) throws org.xml.sax.SAXException
elementTag
- element tag, used for errorsattributes
- attributes of XML elementattributeName
- name of attributenull
if undefinedorg.xml.sax.SAXException
- if attribute is non-existent or not a boolean value@Nullable public static java.lang.Boolean getTriStateValue(@NotNull java.lang.String elementTag, @NotNull org.xml.sax.Attributes attributes, @NotNull java.lang.String attributeName, @Nullable java.lang.Boolean fallback) throws org.xml.sax.SAXException
elementTag
- element tag, used for errorsattributes
- attributes of XML elementattributeName
- name of attributefallback
- default value returned if attribute is not setnull
if undefinedorg.xml.sax.SAXException
- if attribute is not a boolean value@NotNull public static <E extends java.lang.Enum<E>> E getEnumValue(@NotNull java.lang.String elementTag, @NotNull org.xml.sax.Attributes attributes, @NotNull java.lang.String attributeName, @NotNull java.lang.Class<E> enumClass) throws org.xml.sax.SAXException
E
- enum typeelementTag
- element tag, used for errorsattributes
- attributes of XML elementattributeName
- name of attributeenumClass
- enum classorg.xml.sax.SAXException
- if attribute is non-existent or not an enum of the given typepublic static <T> T getValue(@NotNull java.lang.String elementTag, @NotNull org.xml.sax.Attributes attributes, @NotNull java.lang.String attributeName, @NotNull java.util.function.Function<java.lang.String,? extends T> extractor) throws org.xml.sax.SAXException
T
- result type of this methodelementTag
- element tag, used for errorsattributes
- attributes of XML elementattributeName
- name of attributeextractor
- converter from the textual value to the expected value typeorg.xml.sax.SAXException
- when the attribute does not exist, or when the extractor throwspublic static <T,E extends java.lang.Exception> T getValueF(@NotNull java.lang.String elementTag, @NotNull org.xml.sax.Attributes attributes, @NotNull java.lang.String attributeName, @NotNull FragileFunction1<? extends T,E,java.lang.String> extractor) throws org.xml.sax.SAXException
T
- result type of this methodE
- exception type of extractorelementTag
- element tag, used for errorsattributes
- attributes of XML elementattributeName
- name of attributeextractor
- converter from the textual value to the expected value type which throws an exceptionorg.xml.sax.SAXException
- when the attribute does not exist, or when the extractor throws an exception@Nullable public static java.lang.String getOptionalValue(@NotNull org.xml.sax.Attributes attributes, @NotNull java.lang.String attributeName)
attributes
- attributes of XML elementattributeName
- name of requested attributenull
if attribute is not set@Nullable public static <T> T getOptionalValue(@NotNull org.xml.sax.Attributes attributes, @NotNull java.lang.String attributeName, @NotNull java.util.function.Function<java.lang.String,? extends T> extractor)
T
- result type of this methodattributes
- attributes of XML elementattributeName
- name of attributeextractor
- converter from the textual value to the expected value typenull
if the value does not exist@Nullable public static <T,E extends java.lang.Exception> T getOptionalValueF(@NotNull org.xml.sax.Attributes attributes, @NotNull java.lang.String attributeName, @NotNull FragileFunction1<? extends T,E,java.lang.String> extractor) throws E extends java.lang.Exception
T
- result type of this methodE
- exception type of extractorattributes
- attributes of XML elementattributeName
- name of attributeextractor
- converter from the textual value to the expected value typenull
if the value does not existE
- when the extractor throws an exceptionE extends java.lang.Exception
@NotNull public static org.w3c.dom.Element getSingleChild(@NotNull org.w3c.dom.Element element) throws org.xml.sax.SAXException
element
- parent elementorg.xml.sax.SAXException
- if there is none or more than one child, or the single child is not an element@NotNull public static org.w3c.dom.Element getSingleChild(@NotNull org.w3c.dom.Element element, @NotNull java.lang.String subElementTag) throws org.xml.sax.SAXException
element
- element containing sub elementsubElementTag
- name of sub elementorg.xml.sax.SAXException
- if sub element is missing or appearing more than once@Nullable public static org.w3c.dom.Element getOptionalSingleChild(@NotNull org.w3c.dom.Element element, @NotNull java.lang.String subElementTag) throws org.xml.sax.SAXException
element
- element containing sub elementsubElementTag
- name of sub elementnull
if there is noneorg.xml.sax.SAXException
- if sub element is missing or appearing more than once@NotNull public static Indexable<org.w3c.dom.Element> getChildren(@NotNull org.w3c.dom.Element element, @NotNull java.lang.String subElementTag) throws org.xml.sax.SAXException
element
- element for which children are requestedsubElementTag
- sub element tagorg.xml.sax.SAXException
- if the required size is non-negative and not matchedgetChildren(Element, String, int)
@NotNull public static Indexable<org.w3c.dom.Element> getChildren(@NotNull org.w3c.dom.Element element, @NotNull java.lang.String subElementTag, int requiredSize) throws org.xml.sax.SAXException
element
- element for which children are requestedsubElementTag
- sub element tagrequiredSize
- required and checked size if non-negative, ignored if negativeorg.xml.sax.SAXException
- if the required size is non-negative and not matched@NotNull public static java.lang.String getContent(@NotNull org.w3c.dom.Element element, boolean forceNonEmpty) throws org.xml.sax.SAXException
element
- element containing contentforceNonEmpty
- force that element tag is not empty, i.e. <tag/%gt;?org.xml.sax.SAXException
- if there are other nodes than text or attribute,
or if forceContent
is true
and
the element uses an empty element tag@NotNull public static java.util.List<org.w3c.dom.Element> getChildElements(@NotNull org.w3c.dom.Element element, @NotNull java.lang.String tagName)
element
- element for which child elements are requestedtagName
- tag name of requested children@NotNull public static org.w3c.dom.Document loadFromFile(@NotNull java.lang.String path) throws javax.xml.parsers.ParserConfigurationException, java.io.IOException, org.xml.sax.SAXException
path
- path of XML filejavax.xml.parsers.ParserConfigurationException
- on parser config errorsjava.io.IOException
- on read errorsorg.xml.sax.SAXException
- on format errorspublic static int checkTagAndVersion(@NotNull org.w3c.dom.Element element, @NotNull java.lang.String tag, int maxVersion) throws org.xml.sax.SAXException
version
attribute which is not higher than a given
number. This method assumes that the minimal version number is 1
.element
- element to checktag
- required tag name of the elementmaxVersion
- required maximal version of the elementorg.xml.sax.SAXException
- if element has another tag name, no integer version attribute, or
if the version is too highpublic static int checkTagAndVersion(@NotNull org.w3c.dom.Element element, @NotNull java.lang.String tag, int minVersion, int maxVersion) throws org.xml.sax.SAXException
version
attribute which is inside a given range.element
- element to checktag
- required tag name of the elementminVersion
- required minimal version of the elementmaxVersion
- required maximal version of the elementorg.xml.sax.SAXException
- if element has another tag name, no integer version attribute, or
if the version is too high