Package org.opengis.cite.cat30.util
Class XMLUtils
java.lang.Object
org.opengis.cite.cat30.util.XMLUtils
Provides various utility methods for accessing or manipulating XML representations.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Element
createElement
(QName qName) Creates a new Element having the specified qualified name.static Object
Evaluates an XPath expression using the given context item and returns the result as the specified type.static NodeList
Evaluates an XPath 1.0 expression using the given context and returns the result as a node set.static net.sf.saxon.s9api.XdmValue
Evaluates an XPath 2.0 expression using the Saxon s9api interfaces.static net.sf.saxon.s9api.XdmValue
Evaluates an XQuery 1.0 expression using the Saxon s9api interfaces.static String
expandReferencesInText
(String value) Expands character entity ( &name;) and numeric references (&#xhhhh; or &dddd;) that occur within a given string value.getElementsByNamespaceURI
(Node node, String namespaceURI) Returns a List of all descendant Element nodes having the specified [namespace name] property.getNodeListAsList
(NodeList nodeList) Returns a List view of the specified NodeList collection.getNodeValues
(NodeList nodeList) Returns the text content of the nodes in the given list.static boolean
isXML
(jakarta.ws.rs.core.MediaType mediaType) Determines if the given media type is an XML-based media type.static QName
nameOfDocumentElement
(Source source) Returns the name of the document element in the given XML resource.static Document
Parses the content of the given Source and returns a DOM Document node.static StreamSource
toStreamSource
(DOMSource domSource) Converts a DOMSource object to a StreamSource representing an XML data source.static Document
Transforms the content of a DOM Node using a specified XSLT stylesheet.static void
writeNode
(Node node, OutputStream outputStream) Writes the content of a DOM Node to a byte stream.static String
writeNodeToString
(Node node) Writes the content of a DOM Node to a string.static String
writeXdmValueToString
(net.sf.saxon.s9api.XdmValue value) Writes the content of an XdmValue sequence to a string.
-
Constructor Details
-
XMLUtils
public XMLUtils()
-
-
Method Details
-
writeNodeToString
Writes the content of a DOM Node to a string. The XML declaration is omitted and the character encoding is set to "US-ASCII" (any character outside of this set is serialized as a numeric character reference).- Parameters:
node
- The DOM Node to be serialized.- Returns:
- A String representing the content of the given node.
-
writeNode
Writes the content of a DOM Node to a byte stream. An XML declaration is always omitted.- Parameters:
node
- The DOM Node to be serialized.outputStream
- The destination OutputStream reference.
-
evaluateXPath
public static NodeList evaluateXPath(Node context, String expr, Map<String, String> namespaceBindings) throws XPathExpressionExceptionEvaluates an XPath 1.0 expression using the given context and returns the result as a node set.- Parameters:
context
- The context node.expr
- An XPath expression.namespaceBindings
- A collection of namespace bindings for the XPath expression, where each entry maps a namespace URI (key) to a prefix (value). Standard bindings do not need to be declared (seeNamespaceBindings.withStandardBindings()
.- Returns:
- A NodeList containing nodes that satisfy the expression (it may be empty).
- Throws:
XPathExpressionException
- If the expression cannot be evaluated for any reason.
-
evaluateXPath
public static Object evaluateXPath(Source context, String expr, Map<String, String> namespaceBindings, QName returnType) throws XPathExpressionExceptionEvaluates an XPath expression using the given context item and returns the result as the specified type.Note: The Saxon implementation supports XPath 2.0 expressions when using the JAXP XPath APIs (the default implementation will throw an exception).
- Parameters:
context
- The context item.expr
- An XPath expression.namespaceBindings
- A collection of namespace bindings for the XPath expression, where each entry maps a namespace URI (key) to a prefix (value). Standard bindings do not need to be declared (seeNamespaceBindings.withStandardBindings()
.returnType
- The desired return type (as declared inXPathConstants
).- Returns:
- The result converted to the desired returnType.
- Throws:
XPathExpressionException
- If the expression cannot be evaluated for any reason.
-
evaluateXPath2
public static net.sf.saxon.s9api.XdmValue evaluateXPath2(Source xmlSource, String expr, Map<String, String> nsBindings) throws net.sf.saxon.s9api.SaxonApiExceptionEvaluates an XPath 2.0 expression using the Saxon s9api interfaces.- Parameters:
xmlSource
- The XML Source.expr
- The XPath expression to be evaluated.nsBindings
- A collection of namespace bindings required to evaluate the XPath expression, where each entry maps a namespace URI (key) to a prefix (value); this may benull
if not needed.- Returns:
- An XdmValue object representing a value in the XDM data model; this is a sequence of zero or more items, where each item is either an atomic value or a node.
- Throws:
net.sf.saxon.s9api.SaxonApiException
- If an error occurs while evaluating the expression; this always wraps some other underlying exception.
-
evaluateXQuery
public static net.sf.saxon.s9api.XdmValue evaluateXQuery(Source source, String query, Map<String, String> nsBindings) throws net.sf.saxon.s9api.SaxonApiExceptionEvaluates an XQuery 1.0 expression using the Saxon s9api interfaces.- Parameters:
source
- The XML Source.query
- The query expression.nsBindings
- A collection of namespace bindings required to evaluate the query, where each entry maps a namespace URI (key) to a prefix (value).- Returns:
- An XdmValue object representing a value in the XDM data model.
- Throws:
net.sf.saxon.s9api.SaxonApiException
- If an error occurs while evaluating the query (this always wraps some other underlying exception).
-
createElement
Creates a new Element having the specified qualified name. The element must beadopted
when inserted into another Document.- Parameters:
qName
- A QName object.- Returns:
- An Element node (with a Document owner but no parent).
-
getElementsByNamespaceURI
Returns a List of all descendant Element nodes having the specified [namespace name] property. The elements are listed in document order.- Parameters:
node
- The node to search from.namespaceURI
- An absolute URI denoting a namespace name.- Returns:
- A List containing elements in the specified namespace; the list is empty if there are no elements in the namespace.
-
transform
Transforms the content of a DOM Node using a specified XSLT stylesheet.- Parameters:
xslt
- A Source object representing a stylesheet (XSLT 1.0 or 2.0).source
- A Node representing the XML source. If it is an Element node it will be imported into a new DOM Document.- Returns:
- A DOM Document containing the result of the transformation.
-
expandReferencesInText
Expands character entity ( &name;) and numeric references (&#xhhhh; or &dddd;) that occur within a given string value. It may be necessary to do this before processing an XPath expression.- Parameters:
value
- A string representing text content.- Returns:
- A string with all included references expanded.
-
toStreamSource
Converts a DOMSource object to a StreamSource representing an XML data source. The system ID is preserved, allowing relative URIs to be processed.- Parameters:
domSource
- A DOMSource instance.- Returns:
- A StreamSource object for reading the content represented by the original DOM tree.
-
nameOfDocumentElement
Returns the name of the document element in the given XML resource.- Parameters:
source
- The Source to read the document from.- Returns:
- The qualified name of the document element, or
null
if the source is not an XML document or it cannot be read for some reason.
-
parse
Parses the content of the given Source and returns a DOM Document node.- Parameters:
source
- The Source to read the XML content from.- Returns:
- A Document node representing the XML content.
- Throws:
TransformerException
- If the source cannot be parsed for any reason.
-
getNodeListAsList
Returns a List view of the specified NodeList collection.- Parameters:
nodeList
- An ordered collection of DOM nodes.- Returns:
- A List containing the original sequence of Node objects.
-
writeXdmValueToString
Writes the content of an XdmValue sequence to a string. Each item in the sequence is either an atomic value or a node.- Parameters:
value
- A value in the XDM data model.- Returns:
- A String representing the content of the sequence.
- See Also:
-
isXML
public static boolean isXML(jakarta.ws.rs.core.MediaType mediaType) Determines if the given media type is an XML-based media type.- Parameters:
mediaType
- A MediaType object.- Returns:
- true if the type corresponds to an XML entity; false otherwise.
- See Also:
-
getNodeValues
Returns the text content of the nodes in the given list.- Parameters:
nodeList
- A sequence of DOM nodes.- Returns:
- A list of String values, each of which represents the content of a node (and its descendants, if any).
-