public class DomUtil extends Object
DomUtil
provides some common utility methods related to w3c-DOM.Constructor and Description |
---|
DomUtil() |
Modifier and Type | Method and Description |
---|---|
static Element |
addChildElement(Element parent,
String localName,
Namespace namespace)
Add a new child element with the given local name and namespace to the
specified parent.
|
static Element |
addChildElement(Element parent,
String localName,
Namespace namespace,
String text)
Add a new child element with the given local name and namespace to the
specified parent.
|
static Element |
addChildElement(Node parent,
String localName,
Namespace namespace)
Add a new child element with the given local name and namespace to the
specified parent.
|
static Document |
createDocument()
Creates and returns a new empty DOM document.
|
static Element |
createElement(Document factory,
QName elementName)
Create a new DOM element with the specified local name and namespace.
|
static Element |
createElement(Document factory,
String localName,
Namespace namespace)
Create a new DOM element with the specified local name and namespace.
|
static Element |
createElement(Document factory,
String localName,
Namespace namespace,
String text)
Create a new DOM element with the specified local name and namespace and
add the specified text as Text node to it.
|
static Element |
depthToXml(boolean isDeep,
Document factory)
Returns the Xml representation of a boolean isDeep, where false
presents a depth value of '0', true a depth value of 'infinity'.
|
static Element |
depthToXml(String depth,
Document factory)
Returns the Xml representation of a depth String.
|
static String |
getAttribute(Element parent,
String localName,
Namespace namespace)
Returns the value of the named attribute of the current element.
|
static Element |
getChildElement(Node parent,
QName childName)
Returns the first child element that matches the given
QName . |
static Element |
getChildElement(Node parent,
String childLocalName,
Namespace childNamespace)
Returns the first child element that matches the given local name and
namespace.
|
static ElementIterator |
getChildren(Element parent)
Return an
ElementIterator over all child elements. |
static ElementIterator |
getChildren(Element parent,
QName childName)
Returns a
ElementIterator containing all child elements of
the given parent node that match the given QName . |
static ElementIterator |
getChildren(Element parent,
String childLocalName,
Namespace childNamespace)
Returns a
ElementIterator containing all child elements of
the given parent node that match the given local name and namespace. |
static String |
getChildText(Element parent,
String childLocalName,
Namespace childNamespace)
Calls
getText(Element) on the first child element that matches
the given local name and namespace. |
static String |
getChildTextTrim(Element parent,
QName childName)
Calls
getTextTrim(Element) on the first child element that matches
the given name. |
static String |
getChildTextTrim(Element parent,
String childLocalName,
Namespace childNamespace)
Calls
getTextTrim(Element) on the first child element that matches
the given local name and namespace. |
static List<Node> |
getContent(Node parent)
Return a list of all child nodes that are either Element, Text or CDATA.
|
static String |
getExpandedName(String localName,
Namespace namespace)
Returns a string representation of the name of a DOM node consisting
of "{" + namespace uri + "}" + localName.
|
static Element |
getFirstChildElement(Node parent)
Return the first child element
|
static Namespace |
getNamespace(Element element)
Build a Namespace from the prefix and uri retrieved from the given element.
|
static Attr[] |
getNamespaceAttributes(Element element)
Returns the namespace attributes of the given element.
|
static String |
getPrefixedName(QName name)
Return the qualified name of a DOM node consisting of
namespace prefix + ":" + local name.
|
static String |
getPrefixedName(String localName,
Namespace namespace)
Return the qualified name of a DOM node consisting of
namespace prefix + ":" + local name.
|
static String |
getQualifiedName(String localName,
Namespace namespace)
Deprecated.
As of 2.0. Please use
getExpandedName(String, Namespace)
instead. This method was named according to usage of 'qualified name' in
JSR 170 that conflicts with the terminology used in XMLNS. As of JCR 2.0
the String consisting of "{" + namespace uri + "}" + localName
is called Expanded Name . |
static String |
getText(Element element)
Concatenates the values of all child nodes of type 'Text' or 'CDATA'/
|
static String |
getText(Element element,
String defaultValue)
Same as
getText(Element) except that 'defaultValue' is returned
instead of null , if the element does not contain any text. |
static String |
getTextTrim(Element element)
Removes leading and trailing whitespace after calling
getText(Element) . |
static boolean |
hasChildElement(Node parent,
String childLocalName,
Namespace childNamespace)
Returns true if the given parent node has a child element that matches
the specified local name and namespace.
|
static boolean |
hasContent(Node parent)
Return
true if the given parent contains any child that is
either an Element, Text or CDATA. |
static Element |
hrefToXml(String href,
Document factory)
Builds a 'DAV:href' Xml element from the given href.
|
static boolean |
matches(Node node,
QName requiredName)
Returns true if the specified node matches the required
QName . |
static boolean |
matches(Node node,
String requiredLocalName,
Namespace requiredNamespace)
Returns true if the specified node matches the required names.
|
static Document |
parseDocument(InputStream stream)
Parses the given input stream and returns the resulting DOM document.
|
static void |
setAttribute(Element element,
String attrLocalName,
Namespace attrNamespace,
String attrValue)
Add an attribute node to the given element.
|
static void |
setBuilderFactory(DocumentBuilderFactory documentBuilderFactory)
Support the replacement of
BUILDER_FACTORY . |
static void |
setNamespaceAttribute(Element element,
String prefix,
String uri)
Adds a namespace attribute on the given element.
|
static void |
setText(Element element,
String text)
Create a new text node and add it as child to the given element.
|
static Element |
timeoutToXml(long timeout,
Document factory)
Converts the given timeout (long value defining the number of milli-
second until timeout is reached) to its Xml representation as defined
by RFC 4918.
|
static void |
transformDocument(Document xmlDoc,
OutputStream out)
Uses a new Transformer instance to transform the specified xml document
to the specified writer output target.
|
static void |
transformDocument(Document xmlDoc,
Writer writer)
Uses a new Transformer instance to transform the specified xml document
to the specified writer output target.
|
public static void setBuilderFactory(DocumentBuilderFactory documentBuilderFactory)
BUILDER_FACTORY
. This is useful
for injecting a customized BuilderFactory, for example with one that
uses a local catalog resolver. This is one technique for addressing
this issue:
http://www.w3.org/blog/systeam/2008/02/08/w3c_s_excessive_dtd_trafficdocumentBuilderFactory
- public static Document createDocument() throws ParserConfigurationException
ParserConfigurationException
- if the document can not be createdpublic static Document parseDocument(InputStream stream) throws ParserConfigurationException, SAXException, IOException
stream
- XML input streamParserConfigurationException
- if the document can not be createdSAXException
- if the document can not be parsedIOException
- if the input stream can not be readpublic static String getAttribute(Element parent, String localName, Namespace namespace)
parent
- localName
- attribute local name or 'nodeName' if no namespace is
specified.namespace
- or null
null
if not foundpublic static Attr[] getNamespaceAttributes(Element element)
element
- public static String getText(Element element)
element
- null
if the length of the resulting String is 0.isText(Node)
public static String getText(Element element, String defaultValue)
getText(Element)
except that 'defaultValue' is returned
instead of null
, if the element does not contain any text.element
- defaultValue
- defaultValue
if the element does not contain any text.public static String getTextTrim(Element element)
getText(Element)
.element
- null
public static String getChildText(Element parent, String childLocalName, Namespace childNamespace)
getText(Element)
on the first child element that matches
the given local name and namespace.parent
- childLocalName
- childNamespace
- null
.getText(Element)
public static String getChildTextTrim(Element parent, String childLocalName, Namespace childNamespace)
getTextTrim(Element)
on the first child element that matches
the given local name and namespace.parent
- childLocalName
- childNamespace
- null
. Note, that leading and trailing whitespace
is removed from the text.getTextTrim(Element)
public static String getChildTextTrim(Element parent, QName childName)
getTextTrim(Element)
on the first child element that matches
the given name.parent
- childName
- null
. Note, that leading and trailing whitespace
is removed from the text.getTextTrim(Element)
public static boolean hasChildElement(Node parent, String childLocalName, Namespace childNamespace)
parent
- childLocalName
- childNamespace
- public static Element getChildElement(Node parent, String childLocalName, Namespace childNamespace)
null
is returned.parent
- childLocalName
- childNamespace
- null
.public static Element getChildElement(Node parent, QName childName)
QName
.
If no child element is present or no child element matches,
null
is returned.parent
- childName
- null
.public static ElementIterator getChildren(Element parent, String childLocalName, Namespace childNamespace)
ElementIterator
containing all child elements of
the given parent node that match the given local name and namespace.
If the namespace is null
only the localName is compared.parent
- the node the children elements should be retrieved fromchildLocalName
- childNamespace
- ElementIterator
giving access to all child elements
that match the specified localName and namespace.public static ElementIterator getChildren(Element parent, QName childName)
ElementIterator
containing all child elements of
the given parent node that match the given QName
.parent
- the node the children elements should be retrieved fromchildName
- ElementIterator
giving access to all child
elements that match the specified name.public static ElementIterator getChildren(Element parent)
ElementIterator
over all child elements.parent
- for a method that only
retrieves child elements that match a specific local name and namespace.
public static Element getFirstChildElement(Node parent)
null
if the given node has no
child elements.public static boolean hasContent(Node parent)
true
if the given parent contains any child that is
either an Element, Text or CDATA.parent
- true
if the given parent contains any child that is
either an Element, Text or CDATA.public static List<Node> getContent(Node parent)
parent
- public static Namespace getNamespace(Element element)
Namespace
of the given element.public static boolean matches(Node node, String requiredLocalName, Namespace requiredNamespace)
null
.node
- requiredLocalName
- requiredNamespace
- public static boolean matches(Node node, QName requiredName)
QName
.node
- requiredName
- public static Element createElement(Document factory, String localName, Namespace namespace)
factory
- localName
- namespace
- Document.createElement(String)
,
Document.createElementNS(String, String)
public static Element createElement(Document factory, QName elementName)
factory
- elementName
- Document.createElement(String)
,
Document.createElementNS(String, String)
public static Element createElement(Document factory, String localName, Namespace namespace, String text)
factory
- localName
- namespace
- text
- Document.createElement(String)
,
Document.createElementNS(String, String)
,
Document.createTextNode(String)
,
Node.appendChild(org.w3c.dom.Node)
public static Element addChildElement(Element parent, String localName, Namespace namespace)
parent
- localName
- namespace
- public static Element addChildElement(Node parent, String localName, Namespace namespace)
parent
- localName
- namespace
- public static Element addChildElement(Element parent, String localName, Namespace namespace, String text)
parent
- localName
- namespace
- text
- Document.createElement(String)
,
Document.createElementNS(String, String)
,
Document.createTextNode(String)
,
Node.appendChild(org.w3c.dom.Node)
public static void setText(Element element, String text)
element
- text
- public static void setAttribute(Element element, String attrLocalName, Namespace attrNamespace, String attrValue)
element
- attrLocalName
- attrNamespace
- attrValue
- public static void setNamespaceAttribute(Element element, String prefix, String uri)
element
- prefix
- uri
- public static Element timeoutToXml(long timeout, Document factory)
timeout
- number of milli-seconds until timeout is reached.public static Element depthToXml(boolean isDeep, Document factory)
isDeep
- public static Element depthToXml(String depth, Document factory)
depth
- public static Element hrefToXml(String href, Document factory)
Note that the path present needs to be a valid URI or URI reference.
href
- String representing the text of the 'href' Xml elementfactory
- the Document used as factorypublic static String getQualifiedName(String localName, Namespace namespace)
getExpandedName(String, Namespace)
instead. This method was named according to usage of 'qualified name' in
JSR 170 that conflicts with the terminology used in XMLNS. As of JCR 2.0
the String consisting of "{" + namespace uri + "}" + localName
is called Expanded Name
.getExpandedName(String, Namespace)
.localName
- namespace
- null
or represents
the empty namespace, the local name is returned.public static String getExpandedName(String localName, Namespace namespace)
null
or represents the empty namespace, the local name is
returned.localName
- namespace
- null
or represents
the empty namespace, the local name is returned.getQualifiedName(String, Namespace)
.public static String getPrefixedName(String localName, Namespace namespace)
null
or contains an empty prefix, the local name is returned.localName
- namespace
- Document.createAttributeNS(String, String)
,
Document.createElementNS(String, String)
public static String getPrefixedName(QName name)
null
or contains an empty prefix, the local name is returned.name
- Document.createAttributeNS(String, String)
,
Document.createElementNS(String, String)
public static void transformDocument(Document xmlDoc, Writer writer) throws TransformerException, SAXException
xmlDoc
- XML document to create the transformation
Source
for.writer
- The writer used to create a new transformation
Result
for.TransformerException
SAXException
public static void transformDocument(Document xmlDoc, OutputStream out) throws TransformerException, SAXException
xmlDoc
- XML document to create the transformation
Source
for.out
- The stream used to create a new transformation
Result
for.TransformerException
SAXException
Copyright © 2004–2024 The Apache Software Foundation. All rights reserved.