|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object sk.baka.ikslibs.DOMUtils
public final class DOMUtils
Utility methods for DOM object model.
Warning: getNodeIndex(Node)
uses DOM implementation-dependent
methods which may be implemented incorrectly in some DOM implementation. For
details please see DOMImplSelector
.
Field Summary | |
---|---|
static Comparator<Node> |
DOCUMENT_ORDER_COMPARATOR
Comparator that orders nodes by the document order. |
Method Summary | |
---|---|
static boolean |
belongsTo(Node node,
Document doc)
Checks the node if it is from given document. |
static boolean |
canHaveNamespace(Node node)
Checks if this node can have a namespace. |
static boolean |
canHaveNamespace(short nodeType)
Checks if this node can have a namespace. |
static void |
checkNode(Node node,
Document doc)
Checks the node if it is from given document. |
static void |
checkPointer(DomPointer ptr,
Document doc)
Checks the pointer if it is from given document. |
static boolean |
containsElements(Node node)
Checks, whether given node contains some elements also, or it contains text/comments/pi only. |
static boolean |
equalsNodeLists(Collection<Node> c1,
Collection<Node> c2)
Compares two collections if they contain same nodes. |
static boolean |
equalsNodeNamespace(Node node1,
Node node2)
Returns true if the two Nodes are from same namespace. |
static boolean |
equalsNodeQName(Node node1,
Node node2)
Returns true if the two Nodes have same qname (namespace
and localname). |
static boolean |
equalsNodeQName(Node node,
QName name)
Returns true if the node is from given namespace and has
name localName. |
static boolean |
equalsNodeQName(Node node,
String namespaceURI,
String localName)
Returns true if the node is from provided namespace and has name localName. |
static boolean |
equalsQName(QName qname,
String namespace,
String localName)
Returns true if given qname represents given namespace and
local name. |
static boolean |
equalsURI(String uri1,
String uri2)
Returns true if the two uri are equal. |
static Attr |
getAttribute(Node element,
QName attrName)
Retrieves attribute with given qname from given element. |
static int |
getAttributeCount(Node node)
Retrieves the attribute count for given node. |
static List<Attr> |
getAttributes(Node n,
boolean descendants,
org.w3c.dom.traversal.NodeFilter filter)
Appends all attribute nodes from given element to given list. |
static void |
getAttributes(Node n,
List<Attr> list,
boolean descendants,
org.w3c.dom.traversal.NodeFilter filter)
Appends all attribute nodes from given element to given list. |
static int |
getChildCount(Node node)
Returns count of a child nodes. |
static List<Element> |
getChildElements(Node node)
Returns all elements, that are children of given node. |
static String |
getContainerText(Node node)
Computes text value of element or entity by concatenating all textual values of descendant text/cdata nodes. |
static String |
getData(Node node)
Returns data, associated with given node. |
static Comparator<Node> |
getDocumentOrderComparator()
Returns comparator that orders nodes by the document order. |
static org.w3c.dom.traversal.DocumentTraversal |
getDocumentTraversal(Document doc)
Returns the DocumentTraversal instance for given document. |
static byte |
getDOMLevel(Node node)
Returns DOM level that given implementation supports. |
static Entity |
getEntity(Document doc,
String entityName)
Returns entity node representing requested entity. |
static String |
getErrorMessage(DOMError err)
Distills the detailed error message from the error object. |
static Element |
getFirstChildElement(Node node)
Returns first element contained in the node. |
static CharacterData |
getFirstDescendantOrSelfTextualNode(Node node)
Returns first textual node encountered on descendant-or-self axis of given node. |
static int |
getHeight(Node node)
Computes the height of the node: number of nodes in the ascendant axis. |
static Node |
getLastDescendantOrSelfIdentifiableNode(Node node)
Returns last identifiable node encountered on descendant-or-self axis of given node. |
static CharacterData |
getLastDescendantOrSelfTextualNode(Node node)
Returns last textual node encountered. |
static String |
getLocalName(Node n)
Retrieves the localname of the node. |
static Element |
getNameRoot(Node node)
Returns element that is the nameroot of tree containing given node. |
static Set<String> |
getNamespaces(Node node)
Returns set of all namespaces present in the node and its descendants and their attributes. |
static Node |
getNearestCommonAscendant(Node node1,
Node node2)
Computes and returns common ascendant for both nodes. |
static int |
getNodeIndex(Node node)
Returns zero-based index of node (i.e. |
static List<Node> |
getNodePathToRoot(Node node)
Returns path from given node to the root node. |
static String |
getNodeType(Node node)
Returns the name of the node type. |
static String |
getNodeType(short nodeType)
Returns the name of the node type. |
static Notation |
getNotation(Document doc,
String notationName)
Returns notation node representing requested notation. |
static Document |
getOwnerDoc(Node n1)
Retrieves the owner document. |
static Node |
getParentContainer(Node node)
Returns the parent container of the node. |
static Element |
getParentElement(Node node)
Returns the parent element of the node. |
static String |
getPathToRoot(Node node)
Returns human-readable path from given element to root of document, in the pathlike form /root/.../element . |
static QName |
getQName(Node node)
Returns QName object containing qualified name of given node. |
static QName |
getQName(String uri,
String localName,
String qName)
Converts SAX-style qname to the QName instance. |
static Element |
getRootElement(DOMSource source)
Returns root element of given source. |
static SAXResult |
getSAXSerializer(Result os,
String encoding,
XMLSerializeBean serializeBean)
Returns the SAX serializer. |
static String |
getTextContents(Node node,
org.w3c.dom.traversal.NodeFilter filter)
Retrieves textual value of the node: all nodes except element, text, cdata and entity are ignored. |
static Attr |
getXmlnsNode(String prefix,
Element e)
Returns xmlns attribute that defines given prefix in given element e. |
static String |
getXmlnsPrefix(Attr attr)
Returns prefix being bound in this xmlns attribute. |
static boolean |
hasData(Node node)
Checks if given node has associated character data (which can be retrieved using the getData(Node) function. |
static boolean |
hasData(short type)
Checks if given node has associated character data (which can be retrieved using the getData(Node) function. |
static boolean |
hasEmptyContent(Node node)
Checks if node has empty content: empty text nodes, empty entities, entities that contains empty text nodes or empty entities etc. |
static Node |
importNode(Document newOwner,
Node nodeToImport,
boolean deep)
Calls Document.importNode(Node, boolean) on given node. |
static boolean |
isCharacterContainer(Node node)
Checks if given node can contain character data. |
static boolean |
isCharacterContainer(short nodeType)
Checks if given node can contain character data. |
static boolean |
isContainer(Node node)
Checks if given node is able to contain other nodes as its children. |
static boolean |
isContainer(short nodeType)
Checks if given node is able to contain other nodes as its children. |
static boolean |
isContentNode(Node node)
Returns true if the node is a content node: if it can be a
part of DOM document content. |
static boolean |
isDocumentRoot(Node node)
Checks if the node is a root node. |
static boolean |
isEmptyEntity(Node node)
Check if given node is an empty entity. |
static boolean |
isEntity(Node node)
Checks if given node is entity or entity reference node. |
static boolean |
isEntity(short nodeType)
Checks if given node is entity or entity reference node. |
static boolean |
isFromSameDoc(Node n1,
Node n2)
Checks if given nodes are from same document (or they both does not belong to any document). |
static boolean |
isInEntity(Node node)
Checks if given node is a descendant of an EntityReference node. |
static boolean |
isInTree(Node node)
Checks if given node is part of a document tree. |
static boolean |
isNodeContentEmpty(Node node)
Returns true if node has empty text contents (the Text or
CDATASection node), or contains whitespaces only ( Text ). |
static boolean |
isRangeBPCharacterContainer(Node node)
Checks if given node can serve as a range text boundary-point. |
static boolean |
isRangeBPCharacterContainer(short nodeType)
Checks if given node can serve as a range text boundary-point. |
static boolean |
isRootNode(Node node)
Checks if this node is a root node in the node hierarchy. |
static boolean |
isSameNode(Node n1,
Node n2)
Checks if two nodes are same: they are both null or
Node.isSameNode(Node) is true . |
static boolean |
isText(Node node)
Checks if the node is a textual node - text or cdata node. |
static boolean |
isText(short nodeType)
Checks if the node is a textual node - text or cdata node. |
static boolean |
isTextualEntity(Node node)
Checks if given node is an entity reference that contains text/cdata nodes only. |
static boolean |
isTextualNode(Node node)
Returns true if the node represents text only - itself is textual or it is a pure-textual entity. |
static boolean |
isXmlns(Attr attr)
Checks if given attribute is an xmlns namespace definition attribute. |
static List<Node> |
nodeListToList(NodeList nodeList)
Converts nodeList to List. |
static String |
prettyPrintChildCount(int childCount)
Pretty prints given children count. |
static String |
prettyPrintEvent(Event evt)
Pretty-prints given DOM Level 2 event. |
static String |
prettyPrintNode(Node node)
Shows an informative, pretty printed short informative node description. |
static String |
printQName(Node node)
Prints given node qname as a prefix:localname pair. |
static String |
printQName(QName qname)
Prints given qname as a prefix:localname pair. |
static String |
printQName(String prefix,
String local)
Prints given qname as a prefix:localname pair. |
static void |
serializeDOM(OutputStream os,
String encoding,
XMLSerializeBean serializeBean,
Node node)
Serializes the document object model. |
static void |
sortByDocumentOrder(List<Node> list)
Sorts list of nodes by the document ordering. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final Comparator<Node> DOCUMENT_ORDER_COMPARATOR
Method Detail |
---|
public static org.w3c.dom.traversal.DocumentTraversal getDocumentTraversal(Document doc)
DocumentTraversal
instance for given document.
doc
- the document instance.
null
.public static boolean isNodeContentEmpty(Node node)
Text
or
CDATASection
node), or contains whitespaces only ( Text
).
node
- node to check.
true
if CharacterData.getData()
/CharacterData.getData()
returns null
or empty string.public static String getData(Node node)
node
- the node.
null
.public static boolean hasData(Node node)
getData(Node)
function.
node
- node to check
true
if node is comment, text, cdata, PI or
attribute, false
otherwise.public static boolean hasData(short type)
getData(Node)
function.
type
- node type to check
true
if node is comment, text, cdata, PI or
attribute, false
otherwise.public static String getContainerText(Node node)
node
- node to query.
null
.public static Element getNameRoot(Node node)
node
- the node.
null
if there is no parent
element.public static boolean equalsURI(String uri1, String uri2)
uri1
- first uriuri2
- second uri
uri1 == null ? uri2 == null : uri1.equals(uri2)
public static boolean equalsNodeQName(Node node, String namespaceURI, String localName)
node
- Node to check.namespaceURI
- namespace that the node must be from.localName
- name that must node have.
namespaceURI:localName
public static boolean equalsNodeQName(Node node, QName name)
true
if the node is from given namespace and has
name localName.
node
- Node to check.name
- qualified name.
true
if name of node is from
namespaceURI:localName
public static boolean equalsNodeNamespace(Node node1, Node node2)
true
if the two Nodes are from same namespace.
node1
- First Node to compare.node2
- Second Node to compare.
true
if the two Nodes are from same namespace.public static boolean equalsNodeQName(Node node1, Node node2)
true
if the two Nodes have same qname (namespace
and localname).
node1
- First Node to compare.node2
- Second Node to compare.
true
if the two Nodes are from same namespace and
have same localname.public static boolean equalsQName(QName qname, String namespace, String localName)
true
if given qname represents given namespace and
local name.
qname
- qualified name.namespace
- the namespace to check.localName
- local name to check.
true
if both namespace and localname are equal,
false
otherwise.public static int getNodeIndex(Node node)
Returns zero-based index of node (i.e. how many nodes are its previous siblings).
Warning: uses implementation-dependent DOMImplSelector
class.
node
- the node to test.
-1
if null
was
given.public static List<Element> getChildElements(Node node)
node
- the node to process
null
.public static boolean isText(Node node)
node
- the node to check.
true
if node is Text
or
CDATASection
node, false
otherwise.public static boolean isText(short nodeType)
nodeType
- the node type to check.
true
if node is Text
or
CDATASection
node, false
otherwise.public static boolean isInEntity(Node node)
EntityReference
node.
node
- node to check.
EntityReference
node. In
case of attribute the function checks if its owner element is in entity.
If the node itself is EntityReference
node but is not contained
in other entity reference node then the result is false
.public static Element getParentElement(Node node)
Node.getParentNode()
when the node is in an entity reference
node.
node
- the node to check.
null
if and only if
given node is the root node of some document, document or document
fragment.
IllegalArgumentException
- if the node does not have a parent.public static Node getParentContainer(Node node)
Node.getParentNode()
when the node is in an entity reference
node. A container is an ID-level
node holder; it
is never an entity reference, for example. It is equal to
getParentElement(Node)
if it is not null
. If
getParentElement(Node)
is null
then this
container is a Document or document fragment instance.
node
- the node to check.
null
if and only
if given node is disconnected from any element, document or document
fragment node.public static boolean containsElements(Node node)
node
- the node.
public static boolean isTextualEntity(Node node)
node
- node to check.
public static boolean hasEmptyContent(Node node)
node
- the nodes to check
public static boolean isEmptyEntity(Node node)
node
- node to check.
true
if the node is an empty entity and it has
empty content.public static boolean isEntity(Node node)
node
- the node to check.
true
if node is entity or entity reference node,
false
otherwise.public static boolean isEntity(short nodeType)
nodeType
- the node type to check.
true
if node is entity or entity reference node,
false
otherwise.public static boolean isTextualNode(Node node)
node
- node to check
public static Node getLastDescendantOrSelfIdentifiableNode(Node node)
node
- the node to check.
null
.public static CharacterData getLastDescendantOrSelfTextualNode(Node node)
node
- the node to check.
null
.public static CharacterData getFirstDescendantOrSelfTextualNode(Node node)
node
- the node to check.
public static boolean isDocumentRoot(Node node)
null
parent or its parent is document or document
fragment.
node
- the node to check.
true
if the node is a root node of some document.public static boolean isContainer(Node node)
node
- node to check
public static boolean isContainer(short nodeType)
nodeType
- node to check
public static boolean isCharacterContainer(Node node)
node
- node to check
true
if node can have character data,
false
otherwise.public static boolean isCharacterContainer(short nodeType)
nodeType
- node to check
true
if node can have character data,
false
otherwise.public static boolean isRangeBPCharacterContainer(Node node)
CharacterData
(text, cdata and comment nodes) and
Attr
are such nodes. See DOM
Ranges for details.
node
- node to check.
true
if the node is a CharacterData,
false
otherwise.public static boolean isRangeBPCharacterContainer(short nodeType)
CharacterData
(PI, text, cdata and comment nodes)
and Attr
are such nodes. See DOM
Ranges for details.
nodeType
- node to check.
true
if the node is a CharacterData,
false
otherwise.public static String printQName(String prefix, String local)
local
- the local part of the qname.prefix
- the prefix of the qname.
public static String printQName(Node node)
node
- the node
public static String prettyPrintChildCount(int childCount)
childCount
- the children count
0 children
, 1 child
etc.public static String prettyPrintNode(Node node)
node
- the node to print
public static String prettyPrintEvent(Event evt)
evt
- event to print.
public static int getChildCount(Node node)
node
- node to check.
public static String printQName(QName qname)
qname
- the qname
public static QName getQName(Node node)
node
- node to convert. It must be element, attribute node, entity,
entity reference, processing instruction, document type or notation node,
otherwise null
is returned.
nodeName table
for
details. Only attribute and element can have non-null namespace.public static String getNodeType(Node node)
node
- node type to print.
public static String getNodeType(short nodeType)
nodeType
- node type to print.
public static boolean canHaveNamespace(Node node)
node
- node to check
true
if the node is element or attribute.public static boolean canHaveNamespace(short nodeType)
nodeType
- node type to check
true
if the node is element or attribute.public static QName getQName(String uri, String localName, String qName)
QName
instance.
uri
- the Namespace URI, or the empty string if the element has no
Namespace URI or if Namespace processing is not being performedlocalName
- the local name (without prefix), or the empty string if
Namespace processing is not being performedqName
- the qualified XML name (with prefix), or the empty string if
qualified names are not available
public static boolean isContentNode(Node node)
true
if the node is a content node: if it can be a
part of DOM document content.
node
- node to check.
true
if the node is an attribute, element, text,
cdata, pi, comment or entity reference node.public static Element getFirstChildElement(Node node)
node
- node to scan
null
if no such
element exists.public static String getPathToRoot(Node node)
/root/.../element
. Only local names are
displayed, no namespaces. Useful for printing element location.
node
- display path for this node.
public static Element getRootElement(DOMSource source)
source
- the source to analyze.
null
if no element node is
attached to source's node.public static List<Node> nodeListToList(NodeList nodeList)
nodeList
- the node list.
public static String getErrorMessage(DOMError err)
err
- error to analyze.
public static String getTextContents(Node node, org.w3c.dom.traversal.NodeFilter filter)
node
- node to retrieve textual value from. If attribute is
specified then its value is returned.filter
- filters out nodes. If null
then no nodes are
filtered. The function differentiates NodeFilter.FILTER_SKIP
and
NodeFilter.FILTER_REJECT
.
public static List<Attr> getAttributes(Node n, boolean descendants, org.w3c.dom.traversal.NodeFilter filter)
n
- append attributes from this element.descendants
- if true
then all descendant elements'
attributes are appended aswell.filter
- able to filter the attributes out. If null
no attributes are filtered out.
public static void getAttributes(Node n, List<Attr> list, boolean descendants, org.w3c.dom.traversal.NodeFilter filter)
n
may be an attribute.
n
- append attributes from this element. May be a single attribute
node.list
- attributes are appended to this list, in no specified order.descendants
- if true
then all descendant elements
attributes are appended aswell.filter
- able to filter the attributes out. If null
no attributes are filtered out.public static int getAttributeCount(Node node)
node
- node to check
public static List<Node> getNodePathToRoot(Node node)
node
- display path for this node.
root
item - its parent is
null
.public static int getHeight(Node node)
node
- the node to check
getNodePathToRoot(Node)
- 1.public static Node getNearestCommonAscendant(Node node1, Node node2)
node1
- first nodenode2
- second node.
null
.public static boolean isRootNode(Node node)
node
- the node to check.
true
if the parent of the node is
null
or is the node itself.public static void checkNode(Node node, Document doc)
node
- node to check.doc
- node should belong to this document.
IllegalArgumentException
- if node is not from the document.public static void checkPointer(DomPointer ptr, Document doc)
ptr
- pointer to check.doc
- pointer should belong to this document.
IllegalArgumentException
- if pointer does not point into the
document.public static boolean belongsTo(Node node, Document doc)
node
- node to check, must not be null
.doc
- node should belong to this document.
true
if the node was created by given document,
false
otherwise.public static void sortByDocumentOrder(List<Node> list)
list
- list to sort.public static Comparator<Node> getDocumentOrderComparator()
public static boolean equalsNodeLists(Collection<Node> c1, Collection<Node> c2)
Node.isSameNode(Node)
rather than on the
Object.equals(Object)
.
c1
- first collection of nodesc2
- second collection of nodes
true
if both collections contains same node using
SameNodeComparator.equals(Object)
and
SameNodeComparator.hashCode()
functions.public static final Attr getXmlnsNode(String prefix, Element e)
null
.
prefix
- prefix to check.e
- the element to look in.
null
if no such
attribute exists.public static boolean isXmlns(Attr attr)
attr
- attribute to check
true
if this attribute is a xmlns attribute,
false
otherwise.public static String getXmlnsPrefix(Attr attr)
attr
- the xmlns attribute to check
null
if this attribute is not an xmlns attribute.public static final boolean isSameNode(Node n1, Node n2)
null
or
Node.isSameNode(Node)
is true
.
n1
- first node to comparen2
- second node to compare.
true
if they are same.public static final boolean isFromSameDoc(Node n1, Node n2)
n1
- first node.n2
- second node.
true
if both nodes belongs to the same document.
Usespublic static final Document getOwnerDoc(Node n1)
null
only if given
node was null
or the node is a DocumentType
that
is not yet attached to a document.
n1
- the node to check
public static String getLocalName(Node n)
Node.getLocalName()
returns null
- this happens
when the DOM implementation thinks the attribute is a DOM level 1
attribute (xerces for example does this with a null-namespace attribute).
n
- the node.
null
.public static void serializeDOM(OutputStream os, String encoding, XMLSerializeBean serializeBean, Node node)
os
- send serialized document here.encoding
- the encoding of the document. If null
then
default is used.serializeBean
- output format parameters. May be null
-
in such case it is ignored.node
- node to serialize.public static SAXResult getSAXSerializer(Result os, String encoding, XMLSerializeBean serializeBean) throws TransformerConfigurationException
os
- send serialized document hereencoding
- the encoding of the documentserializeBean
- output format parameters
SAXResult.getLexicalHandler()
and
SAXResult.getHandler()
are guaranteed to be non-null
.
TransformerConfigurationException
- if serializer error occurs.public static Notation getNotation(Document doc, String notationName)
doc
- the document instance.notationName
- notation name.
null
if no such notation exists.public static Entity getEntity(Document doc, String entityName)
doc
- the document instance.entityName
- entity name.
null
if no such entity exists.public static Set<String> getNamespaces(Node node)
null
namespace, xml and
xmlns attributes are ignored.
node
- the node to check, must not be null
.
public static byte getDOMLevel(Node node)
node
- the node to check.
public static Node importNode(Document newOwner, Node nodeToImport, boolean deep)
Document.importNode(Node, boolean)
on given node. If given
node is a document it is imported as a document fragment.
newOwner
- the document that will perform the import.nodeToImport
- the node to import.deep
- If true
, recursively import the subtree under
the specified node; if false
, import only the node
itself, as explained above. This has no effect on nodes that cannot have
any children, and on Attr
, and
EntityReference
nodes.
null
if nodeToImport was
null
.public static Attr getAttribute(Node element, QName attrName)
element
- the element.attrName
- the name of the attribute.
null
if no such attribute can be
found.public static boolean isInTree(Node node)
node
- the node to check.
true
if node has a parent (or owner element in
case of attribute). Always true
for Document
node,
always false
for DocumentFragment
.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |