sk.baka.ikslibs
Class DOMUtils

java.lang.Object
  extended by sk.baka.ikslibs.DOMUtils

public final class DOMUtils
extends Object

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.

Author:
Martin Vysny

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

DOCUMENT_ORDER_COMPARATOR

public static final Comparator<Node> DOCUMENT_ORDER_COMPARATOR
Comparator that orders nodes by the document order. In this ordering, parent precedes its children and a node precedes its next sibling.

Method Detail

getDocumentTraversal

public static org.w3c.dom.traversal.DocumentTraversal getDocumentTraversal(Document doc)
Returns the DocumentTraversal instance for given document.

Parameters:
doc - the document instance.
Returns:
the document traversal interface, never null.

isNodeContentEmpty

public static boolean isNodeContentEmpty(Node node)
Returns true if node has empty text contents (the Text or CDATASection node), or contains whitespaces only ( Text).

Parameters:
node - node to check.
Returns:
true if CharacterData.getData()/CharacterData.getData() returns null or empty string.

getData

public static String getData(Node node)
Returns data, associated with given node. Returns a text value.

Parameters:
node - the node.
Returns:
character data, that the node contains. Never null.

hasData

public static boolean hasData(Node node)
Checks if given node has associated character data (which can be retrieved using the getData(Node) function.

Parameters:
node - node to check
Returns:
true if node is comment, text, cdata, PI or attribute, false otherwise.

hasData

public static boolean hasData(short type)
Checks if given node has associated character data (which can be retrieved using the getData(Node) function.

Parameters:
type - node type to check
Returns:
true if node is comment, text, cdata, PI or attribute, false otherwise.

getContainerText

public static String getContainerText(Node node)
Computes text value of element or entity by concatenating all textual values of descendant text/cdata nodes.

Parameters:
node - node to query.
Returns:
textual value of element, never null.

getNameRoot

public static Element getNameRoot(Node node)
Returns element that is the nameroot of tree containing given node.

Parameters:
node - the node.
Returns:
nameroot (an element that belongs to same namespace as given node (if node is not element then its parent/owner is used instead), its parent is null or has different namespace and it is nearest such node to given node). May return null if there is no parent element.

equalsURI

public static boolean equalsURI(String uri1,
                                String uri2)
Returns true if the two uri are equal. "" and null are equal.

Parameters:
uri1 - first uri
uri2 - second uri
Returns:
uri1 == null ? uri2 == null : uri1.equals(uri2)

equalsNodeQName

public static boolean equalsNodeQName(Node node,
                                      String namespaceURI,
                                      String localName)
Returns true if the node is from provided namespace and has name localName.

Parameters:
node - Node to check.
namespaceURI - namespace that the node must be from.
localName - name that must node have.
Returns:
true if name of node is namespaceURI:localName

equalsNodeQName

public static boolean equalsNodeQName(Node node,
                                      QName name)
Returns true if the node is from given namespace and has name localName.

Parameters:
node - Node to check.
name - qualified name.
Returns:
true if name of node is from namespaceURI:localName

equalsNodeNamespace

public static boolean equalsNodeNamespace(Node node1,
                                          Node node2)
Returns true if the two Nodes are from same namespace.

Parameters:
node1 - First Node to compare.
node2 - Second Node to compare.
Returns:
true if the two Nodes are from same namespace.

equalsNodeQName

public static boolean equalsNodeQName(Node node1,
                                      Node node2)
Returns true if the two Nodes have same qname (namespace and localname).

Parameters:
node1 - First Node to compare.
node2 - Second Node to compare.
Returns:
true if the two Nodes are from same namespace and have same localname.

equalsQName

public static boolean equalsQName(QName qname,
                                  String namespace,
                                  String localName)
Returns true if given qname represents given namespace and local name.

Parameters:
qname - qualified name.
namespace - the namespace to check.
localName - local name to check.
Returns:
true if both namespace and localname are equal, false otherwise.

getNodeIndex

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.

Parameters:
node - the node to test.
Returns:
node index, or -1 if null was given.

getChildElements

public static List<Element> getChildElements(Node node)
Returns all elements, that are children of given node. The ordering is preserved.

Parameters:
node - the node to process
Returns:
modifiable ordered list of elements, never null.

isText

public static boolean isText(Node node)
Checks if the node is a textual node - text or cdata node.

Parameters:
node - the node to check.
Returns:
true if node is Text or CDATASection node, false otherwise.

isText

public static boolean isText(short nodeType)
Checks if the node is a textual node - text or cdata node.

Parameters:
nodeType - the node type to check.
Returns:
true if node is Text or CDATASection node, false otherwise.

isInEntity

public static boolean isInEntity(Node node)
Checks if given node is a descendant of an EntityReference node.

Parameters:
node - node to check.
Returns:
true if node is descendant of 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.

getParentElement

public static Element getParentElement(Node node)
Returns the parent element of the node. It may differ from Node.getParentNode() when the node is in an entity reference node.

Parameters:
node - the node to check.
Returns:
the parent element. It may be null if and only if given node is the root node of some document, document or document fragment.
Throws:
IllegalArgumentException - if the node does not have a parent.

getParentContainer

public static Node getParentContainer(Node node)
Returns the parent container of the node. It may differ from 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.

Parameters:
node - the node to check.
Returns:
the parent container. It may be null if and only if given node is disconnected from any element, document or document fragment node.

containsElements

public static boolean containsElements(Node node)
Checks, whether given node contains some elements also, or it contains text/comments/pi only. It checks also contents of entity reference nodes, contained in given node.

Parameters:
node - the node.
Returns:
true if node contains at least one regular element, false otherwise.

isTextualEntity

public static boolean isTextualEntity(Node node)
Checks if given node is an entity reference that contains text/cdata nodes only. Empty entity is textual.

Parameters:
node - node to check.
Returns:
true if it is an entity that contains text/cdata nodes or textual entities only, false if the node is not an entity or it contains comments/pi/elements.

hasEmptyContent

public 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.

Parameters:
node - the nodes to check
Returns:
true if the content of the node does not contain any element, character, comment etc.

isEmptyEntity

public static boolean isEmptyEntity(Node node)
Check if given node is an empty entity.

Parameters:
node - node to check.
Returns:
true if the node is an empty entity and it has empty content.

isEntity

public static boolean isEntity(Node node)
Checks if given node is entity or entity reference node.

Parameters:
node - the node to check.
Returns:
true if node is entity or entity reference node, false otherwise.

isEntity

public static boolean isEntity(short nodeType)
Checks if given node is entity or entity reference node.

Parameters:
nodeType - the node type to check.
Returns:
true if node is entity or entity reference node, false otherwise.

isTextualNode

public static boolean isTextualNode(Node node)
Returns true if the node represents text only - itself is textual or it is a pure-textual entity.

Parameters:
node - node to check
Returns:
true if it is textual or textual entity.

getLastDescendantOrSelfIdentifiableNode

public static Node getLastDescendantOrSelfIdentifiableNode(Node node)
Returns last identifiable node encountered on descendant-or-self axis of given node. It may return given node itself but this option is examined as a last option. Checks all descendants of given node first.

Parameters:
node - the node to check.
Returns:
last descendant-or-self of node in the document order. If no suitable text node is found then return null.

getLastDescendantOrSelfTextualNode

public static CharacterData getLastDescendantOrSelfTextualNode(Node node)
Returns last textual node encountered. It may return given node itself. Checks all descendants of given node.

Parameters:
node - the node to check.
Returns:
last descendant-or-self of node in the document order. If no suitable text node is found then return null.

getFirstDescendantOrSelfTextualNode

public static CharacterData getFirstDescendantOrSelfTextualNode(Node node)
Returns first textual node encountered on descendant-or-self axis of given node. It may return given node itself. Checks all descendants of given node.

Parameters:
node - the node to check.
Returns:
first descendant-or-self of node in the document order. If node itself is textual then it is returned.

isDocumentRoot

public static boolean isDocumentRoot(Node node)
Checks if the node is a root node. Root node is a node that has null parent or its parent is document or document fragment.

Parameters:
node - the node to check.
Returns:
true if the node is a root node of some document.

isContainer

public static boolean isContainer(Node node)
Checks if given node is able to contain other nodes as its children.

Parameters:
node - node to check
Returns:
true if node is element, entity, entity reference, document or document fragment node.

isContainer

public static boolean isContainer(short nodeType)
Checks if given node is able to contain other nodes as its children.

Parameters:
nodeType - node to check
Returns:
true if node is element, entity, entity reference, document or document fragment node.

isCharacterContainer

public static boolean isCharacterContainer(Node node)
Checks if given node can contain character data. Only attribute, text, cdata, PI and comment nodes are character containers.

Parameters:
node - node to check
Returns:
true if node can have character data, false otherwise.

isCharacterContainer

public static boolean isCharacterContainer(short nodeType)
Checks if given node can contain character data. Only attribute, text, cdata, PI and comment nodes are character containers.

Parameters:
nodeType - node to check
Returns:
true if node can have character data, false otherwise.

isRangeBPCharacterContainer

public static boolean isRangeBPCharacterContainer(Node node)
Checks if given node can serve as a range text boundary-point. Only instances of CharacterData (text, cdata and comment nodes) and Attr are such nodes. See DOM Ranges for details.

Parameters:
node - node to check.
Returns:
true if the node is a CharacterData, false otherwise.

isRangeBPCharacterContainer

public static boolean isRangeBPCharacterContainer(short nodeType)
Checks if given node can serve as a range text boundary-point. Only instances of CharacterData (PI, text, cdata and comment nodes) and Attr are such nodes. See DOM Ranges for details.

Parameters:
nodeType - node to check.
Returns:
true if the node is a CharacterData, false otherwise.

printQName

public static String printQName(String prefix,
                                String local)
Prints given qname as a prefix:localname pair.

Parameters:
local - the local part of the qname.
prefix - the prefix of the qname.
Returns:
name formatted in the prefix:localname format.

printQName

public static String printQName(Node node)
Prints given node qname as a prefix:localname pair.

Parameters:
node - the node
Returns:
name formatted in the prefix:localname format.

prettyPrintChildCount

public static String prettyPrintChildCount(int childCount)
Pretty prints given children count.

Parameters:
childCount - the children count
Returns:
0 children, 1 child etc.

prettyPrintNode

public static String prettyPrintNode(Node node)
Shows an informative, pretty printed short informative node description.

Parameters:
node - the node to print
Returns:
a XML-like notation.

prettyPrintEvent

public static String prettyPrintEvent(Event evt)
Pretty-prints given DOM Level 2 event.

Parameters:
evt - event to print.
Returns:
string representation of given event.

getChildCount

public static int getChildCount(Node node)
Returns count of a child nodes.

Parameters:
node - node to check.
Returns:
count of child nodes or zero if node does not have/can't have any children.

printQName

public static String printQName(QName qname)
Prints given qname as a prefix:localname pair.

Parameters:
qname - the qname
Returns:
name formatted in the prefix:localname format.

getQName

public static QName getQName(Node node)
Returns QName object containing qualified name of given node.

Parameters:
node - node to convert. It must be element, attribute node, entity, entity reference, processing instruction, document type or notation node, otherwise null is returned.
Returns:
QName of the node. See the nodeName table for details. Only attribute and element can have non-null namespace.

getNodeType

public static String getNodeType(Node node)
Returns the name of the node type.

Parameters:
node - node type to print.
Returns:
short name of the node type.

getNodeType

public static String getNodeType(short nodeType)
Returns the name of the node type.

Parameters:
nodeType - node type to print.
Returns:
short name of the node type.

canHaveNamespace

public static boolean canHaveNamespace(Node node)
Checks if this node can have a namespace. Only attribute and element nodes can have namespace.

Parameters:
node - node to check
Returns:
true if the node is element or attribute.

canHaveNamespace

public static boolean canHaveNamespace(short nodeType)
Checks if this node can have a namespace. Only attribute and element nodes can have namespace.

Parameters:
nodeType - node type to check
Returns:
true if the node is element or attribute.

getQName

public static QName getQName(String uri,
                             String localName,
                             String qName)
Converts SAX-style qname to the QName instance.

Parameters:
uri - the Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed
localName - the local name (without prefix), or the empty string if Namespace processing is not being performed
qName - the qualified XML name (with prefix), or the empty string if qualified names are not available
Returns:
qname instance.

isContentNode

public static boolean isContentNode(Node node)
Returns true if the node is a content node: if it can be a part of DOM document content.

Parameters:
node - node to check.
Returns:
true if the node is an attribute, element, text, cdata, pi, comment or entity reference node.

getFirstChildElement

public static Element getFirstChildElement(Node node)
Returns first element contained in the node. If the node is a Document instance then returns document's element.

Parameters:
node - node to scan
Returns:
element contained in the node, or null if no such element exists.

getPathToRoot

public static String getPathToRoot(Node node)
Returns human-readable path from given element to root of document, in the pathlike form /root/.../element. Only local names are displayed, no namespaces. Useful for printing element location.

Parameters:
node - display path for this node.
Returns:
path of element localnames.

getRootElement

public static Element getRootElement(DOMSource source)
Returns root element of given source.

Parameters:
source - the source to analyze.
Returns:
root element, or null if no element node is attached to source's node.

nodeListToList

public static List<Node> nodeListToList(NodeList nodeList)
Converts nodeList to List.

Parameters:
nodeList - the node list.
Returns:
list of nodes.

getErrorMessage

public static String getErrorMessage(DOMError err)
Distills the detailed error message from the error object.

Parameters:
err - error to analyze.
Returns:
detailed error message.

getTextContents

public 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.

Parameters:
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.
Returns:
textual value of given node.

getAttributes

public static List<Attr> getAttributes(Node n,
                                       boolean descendants,
                                       org.w3c.dom.traversal.NodeFilter filter)
Appends all attribute nodes from given element to given list.

Parameters:
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.
Returns:
list of attributes, in no specific order.

getAttributes

public 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. n may be an attribute.

Parameters:
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.

getAttributeCount

public static int getAttributeCount(Node node)
Retrieves the attribute count for given node.

Parameters:
node - node to check
Returns:
attribute count or zero if node does not have/can't have any attributes.

getNodePathToRoot

public static List<Node> getNodePathToRoot(Node node)
Returns path from given node to the root node.

Parameters:
node - display path for this node.
Returns:
path of element localnames. First item (index 0) is given node, last item is a root item - its parent is null.

getHeight

public static int getHeight(Node node)
Computes the height of the node: number of nodes in the ascendant axis. Root node has height of 0, all its children have height of 1 etc.

Parameters:
node - the node to check
Returns:
size of result of getNodePathToRoot(Node) - 1.

getNearestCommonAscendant

public static Node getNearestCommonAscendant(Node node1,
                                             Node node2)
Computes and returns common ascendant for both nodes.

Parameters:
node1 - first node
node2 - second node.
Returns:
a node that ascends both node1 and node2, and is nearest such node to node1 and node2. If nodes are disconnected, returns null.

isRootNode

public static boolean isRootNode(Node node)
Checks if this node is a root node in the node hierarchy.

Parameters:
node - the node to check.
Returns:
true if the parent of the node is null or is the node itself.

checkNode

public static void checkNode(Node node,
                             Document doc)
Checks the node if it is from given document.

Parameters:
node - node to check.
doc - node should belong to this document.
Throws:
IllegalArgumentException - if node is not from the document.

checkPointer

public static void checkPointer(DomPointer ptr,
                                Document doc)
Checks the pointer if it is from given document.

Parameters:
ptr - pointer to check.
doc - pointer should belong to this document.
Throws:
IllegalArgumentException - if pointer does not point into the document.

belongsTo

public static boolean belongsTo(Node node,
                                Document doc)
Checks the node if it is from given document.

Parameters:
node - node to check, must not be null.
doc - node should belong to this document.
Returns:
true if the node was created by given document, false otherwise.

sortByDocumentOrder

public static void sortByDocumentOrder(List<Node> list)
Sorts list of nodes by the document ordering.

Parameters:
list - list to sort.

getDocumentOrderComparator

public static Comparator<Node> getDocumentOrderComparator()
Returns comparator that orders nodes by the document order. In this ordering, parent precedes its children.

Returns:
comparator instance.

equalsNodeLists

public static boolean equalsNodeLists(Collection<Node> c1,
                                      Collection<Node> c2)
Compares two collections if they contain same nodes. The function relies on the Node.isSameNode(Node) rather than on the Object.equals(Object).

Parameters:
c1 - first collection of nodes
c2 - second collection of nodes
Returns:
true if both collections contains same node using SameNodeComparator.equals(Object) and SameNodeComparator.hashCode() functions.

getXmlnsNode

public static final Attr getXmlnsNode(String prefix,
                                      Element e)
Returns xmlns attribute that defines given prefix in given element e. If no such attribute exists in the element, function returns null.

Parameters:
prefix - prefix to check.
e - the element to look in.
Returns:
xmlns attribute instance or null if no such attribute exists.

isXmlns

public static boolean isXmlns(Attr attr)
Checks if given attribute is an xmlns namespace definition attribute.

Parameters:
attr - attribute to check
Returns:
true if this attribute is a xmlns attribute, false otherwise.

getXmlnsPrefix

public static String getXmlnsPrefix(Attr attr)
Returns prefix being bound in this xmlns attribute.

Parameters:
attr - the xmlns attribute to check
Returns:
prefix defined (or redefined) by this xmlns attribute, null if this attribute is not an xmlns attribute.

isSameNode

public static final boolean isSameNode(Node n1,
                                       Node n2)
Checks if two nodes are same: they are both null or Node.isSameNode(Node) is true.

Parameters:
n1 - first node to compare
n2 - second node to compare.
Returns:
true if they are same.

isFromSameDoc

public static final boolean isFromSameDoc(Node n1,
                                          Node n2)
Checks if given nodes are from same document (or they both does not belong to any document).

Parameters:
n1 - first node.
n2 - second node.
Returns:
true if both nodes belongs to the same document. Uses

getOwnerDoc

public static final Document getOwnerDoc(Node n1)
Retrieves the owner document. Returns null only if given node was null or the node is a DocumentType that is not yet attached to a document.

Parameters:
n1 - the node to check
Returns:
owner document. If given node was document then the node itself is returned.

getLocalName

public static String getLocalName(Node n)
Retrieves the localname of the node. Returns correct value even if the 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).

Parameters:
n - the node.
Returns:
localname of the node, will not be null.

serializeDOM

public static void serializeDOM(OutputStream os,
                                String encoding,
                                XMLSerializeBean serializeBean,
                                Node node)
Serializes the document object model. Uses the DOM Level 3 Load/Save functionality, for details please see the DOM Level3 LS documentation.

Parameters:
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.

getSAXSerializer

public static SAXResult getSAXSerializer(Result os,
                                         String encoding,
                                         XMLSerializeBean serializeBean)
                                  throws TransformerConfigurationException
Returns the SAX serializer.

Parameters:
os - send serialized document here
encoding - the encoding of the document
serializeBean - output format parameters
Returns:
SAX serializer in the form of content handler. Just post events to the handler. Both SAXResult.getLexicalHandler() and SAXResult.getHandler() are guaranteed to be non-null.
Throws:
TransformerConfigurationException - if serializer error occurs.

getNotation

public static Notation getNotation(Document doc,
                                   String notationName)
Returns notation node representing requested notation.

Parameters:
doc - the document instance.
notationName - notation name.
Returns:
notation node or null if no such notation exists.

getEntity

public static Entity getEntity(Document doc,
                               String entityName)
Returns entity node representing requested entity.

Parameters:
doc - the document instance.
entityName - entity name.
Returns:
entity node or null if no such entity exists.

getNamespaces

public static Set<String> getNamespaces(Node node)
Returns set of all namespaces present in the node and its descendants and their attributes. Attributes with null namespace, xml and xmlns attributes are ignored.

Parameters:
node - the node to check, must not be null.
Returns:
set of all namespaces present in the tree rooted in given node.

getDOMLevel

public static byte getDOMLevel(Node node)
Returns DOM level that given implementation supports.

Parameters:
node - the node to check.
Returns:
1 for DOM Level 1, 2 for DOM Level 2 etc.

importNode

public static Node importNode(Document newOwner,
                              Node nodeToImport,
                              boolean deep)
Calls Document.importNode(Node, boolean) on given node. If given node is a document it is imported as a document fragment.

Parameters:
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.
Returns:
imported node. Returns null if nodeToImport was null.

getAttribute

public static Attr getAttribute(Node element,
                                QName attrName)
Retrieves attribute with given qname from given element.

Parameters:
element - the element.
attrName - the name of the attribute.
Returns:
attribute node or null if no such attribute can be found.

isInTree

public static boolean isInTree(Node node)
Checks if given node is part of a document tree.

Parameters:
node - the node to check.
Returns:
true if node has a parent (or owner element in case of attribute). Always true for Document node, always false for DocumentFragment.


Copyright © 2006 Martin Vysny - baka. All Rights Reserved.