com.novell.xml.dom
Class DOMUtil

java.lang.Object
  extended bycom.novell.xml.dom.DOMUtil

public final class DOMUtil
extends Object

Utility class implementing, among others, things we need for DOM that aren't defined by the 1.0 DOM spec (namespaces, for example)


Method Summary
static boolean addNamespaceDecls(Node node)
          Adds namespace declarations as needed to match DOM Level 2 namespace attributes.
static boolean checkNamespacesAgree(Node node)
          Check's whether or not namespace declarations match DOM Level 2 namespace attributes
static Document cloneDocument(Document srcDoc)
          Creates a clone of the input document.
static int compareNodes(Node node1, Node node2)
          Compare first node with second node for equivalence and some semblance of order
static int deepCompareNodes(Node node1, Node node2)
          Compare first node with second node and all of their children and attribute for equality and some semblance of order.
static String describeNode(Node node)
          Returns a brief description of the specified node, appropriate for error reporting and debugging.
static Comparator getDeepNodeComparator()
          get a Comparator that compares Node hierarchies
static Element getElementByID(Document document, String idValue)
          Return the element in the passed document that has the passed ID.
static ExpandedQName getExpandedName(Node node)
          Return the expanded name for the node.
static String getLocalName(Node node)
          Returns the local name of the specified node.
static NamespaceName getNamespaceName(Node node)
          Returns the namespace name (the namespace URI) of the specified node.
static Comparator getNodeComparator()
          get a Comparator that compares Nodes
static String getPrefixforNSURI(String namespaceURI, Node node)
          Return the prefix string (if any) given a namespace URI and a node as context.
static StylesheetAssociation[] getStylesheetAssociations(Document document, String baseURI)
          Returns all stylesheet associations found in the passed document's prologue that are specified with the xml-stylesheet processing instruction.
static String getURIforNSPrefix(String prefix, Node node)
          Return the URI string given a prefix string and a node as context.
static void printDocument(Writer writer, Document document)
          output a generic DOM document as XML
static void printDocumentWithFormat(Writer writer, Document document)
          output a generic DOM document as XML with whitespace formatting
static void printFragment(Writer writer, Node fragment)
          Output a DOM subtree, serialized as XML
static int stringCompare(String str1, String str2)
          String order comparison that won't blow up if either or both of the strings are null a non-empty value
static boolean stringsEqual(String str1, String str2)
          String equality comparison that won't blow up if either or both of the strings are null a non-empty value
static void stripWhitespace(Node root)
          Strip a DOM subtree of Text nodes that are pure whitespace, unless an ancestor element has an xml:space attribute equal to "preserve" and no closer ancestor element has an xml:space attribute equal to "default".
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

printDocument

public static void printDocument(Writer writer,
                                 Document document)
                          throws IOException
output a generic DOM document as XML

Parameters:
document - instance of DOM document to output
writer - output stream
Throws:
IOException

printDocumentWithFormat

public static void printDocumentWithFormat(Writer writer,
                                           Document document)
                                    throws IOException
output a generic DOM document as XML with whitespace formatting

Parameters:
document - instance of DOM document to output
writer - output stream
Throws:
IOException

printFragment

public static void printFragment(Writer writer,
                                 Node fragment)
                          throws IOException
Output a DOM subtree, serialized as XML

Parameters:
writer - The writer object to use as the serializer encoder.
fragment - The root of the subtree to output
Throws:
IOException

stripWhitespace

public static void stripWhitespace(Node root)
Strip a DOM subtree of Text nodes that are pure whitespace, unless an ancestor element has an xml:space attribute equal to "preserve" and no closer ancestor element has an xml:space attribute equal to "default".

This operation will combine any adjacent text nodes in the subtree.

Parameters:
root - The root of the DOM subtree to strip.

describeNode

public static String describeNode(Node node)
Returns a brief description of the specified node, appropriate for error reporting and debugging.

Parameters:
node - the node to be described
Returns:
a brief description of the node

getExpandedName

public static ExpandedQName getExpandedName(Node node)
Return the expanded name for the node.

Parameters:
node - The node whose expanded name should be returned.
Returns:
The expanded node name

getLocalName

public static String getLocalName(Node node)
Returns the local name of the specified node.

This method is needed because DOM Level 1 does not support namespaces.

Parameters:
node - the node whose local name is to be returned
Returns:
the node's local name

getNamespaceName

public static NamespaceName getNamespaceName(Node node)
Returns the namespace name (the namespace URI) of the specified node.

This method is needed because DOM Level 1 does not support namespaces.

Parameters:
node - the node whose namespace name is to be returned
Returns:
the node's namespace URI, or null if the node is not in a namespace

getURIforNSPrefix

public static String getURIforNSPrefix(String prefix,
                                       Node node)
Return the URI string given a prefix string and a node as context.

Parameters:
prefix - The prefix string for which to find the URI
node - The namespace context node.
Returns:
The prefix string if found, or null if not found. Note that the return may be the empty string.

getPrefixforNSURI

public static String getPrefixforNSURI(String namespaceURI,
                                       Node node)
Return the prefix string (if any) given a namespace URI and a node as context.

Parameters:
namespaceURI - The URI for which to find the prefix string
node - The namespace context node.
Returns:
The URI string if found, or null if not found. Note that the return may be the empty string.

getElementByID

public static Element getElementByID(Document document,
                                     String idValue)
Return the element in the passed document that has the passed ID.

Parameters:
document - The document in which to find the element.
idValue - The id value.
Returns:
The element matching the id, or null if no matching element exists.

getStylesheetAssociations

public static StylesheetAssociation[] getStylesheetAssociations(Document document,
                                                                String baseURI)
Returns all stylesheet associations found in the passed document's prologue that are specified with the xml-stylesheet processing instruction.

Parameters:
document - the document whose stylesheet URI is to be returned
baseURI - The baseURI for the passed document (may be null)
Returns:
An array of StylesheetAssociation objects, each corresponding to an xml-stylesheet processing instruction. The length may be 0 if no xml-stylesheet processing instructions are found in the document prologue. The order of the elements of the returned array corresponds to the order of the xml-stylesheet processing instructions in the document prologue.

cloneDocument

public static Document cloneDocument(Document srcDoc)
                              throws DOMException
Creates a clone of the input document.

Parameters:
srcDoc - the document to be cloned
Returns:
The cloned document
Throws:
DOMException

checkNamespacesAgree

public static boolean checkNamespacesAgree(Node node)
Check's whether or not namespace declarations match DOM Level 2 namespace attributes

Parameters:
node - root of subtree to check
Returns:
true if everything is in agreement, false otherwise

addNamespaceDecls

public static boolean addNamespaceDecls(Node node)
Adds namespace declarations as needed to match DOM Level 2 namespace attributes. This may change the prefix of an element or attribute if needed because of prefix conflicts or if another useable prefix for the same namespace is already in scope

Parameters:
node - root of subtree to process
Returns:
true if any modifications were made, false otherwise

stringsEqual

public static boolean stringsEqual(String str1,
                                   String str2)
String equality comparison that won't blow up if either or both of the strings are null a non-empty value

Parameters:
str1 - string 1
str2 - string 2
Returns:
true if the strings are equal or both null

stringCompare

public static int stringCompare(String str1,
                                String str2)
String order comparison that won't blow up if either or both of the strings are null a non-empty value

Parameters:
str1 - string 1
str2 - string 2
Returns:
== 0 if str1 == str2 > 0 if str1 != null and str2 == null < 0 if str1 == null and str2 != null str1.compareTo(str2) otherwise

compareNodes

public static int compareNodes(Node node1,
                               Node node2)
Compare first node with second node for equivalence and some semblance of order

Parameters:
node1 - first node
node2 - second node
Returns:
== 0 if same node or both nodes have same nodeType, namespaceURI, localName, and nodeValue; otherwise some order based on comparing each of those fields in that order

deepCompareNodes

public static int deepCompareNodes(Node node1,
                                   Node node2)
Compare first node with second node and all of their children and attribute for equality and some semblance of order.

This is the same as DOM Level 3 Node.isEqualNode() except that it adds a stable sort order in addition to equality

Parameters:
node1 - first node
node2 - second node
Returns:
== 0 if same node or both nodes and all of their children and attributes have same nodeType, namespaceURI, localName, and nodeValue; otherwise some order based on comparing each of those fields in that order

getNodeComparator

public static Comparator getNodeComparator()
get a Comparator that compares Nodes

Returns:
a Comparator

getDeepNodeComparator

public static Comparator getDeepNodeComparator()
get a Comparator that compares Node hierarchies

Returns:
a Comparator