net.sf.xolite.dom
Class DomUtils

java.lang.Object
  extended by net.sf.xolite.dom.DomUtils

public abstract class DomUtils
extends Object

Utilities for DOM.


Field Summary
static int EMPTY_EQUALS_NULL
          Flag for comparison: empty elements (like <el/> or <el></el>) are equivalent to no-existent elements.
static int IGNORE_CASE
          Flag for comparison: ignore case for string values.
static int IGNORE_SEQUENCE_ORDER
          Flag for comparison: ignore order of elements in sequences.
static int NORMALIZE_STRING_VALUES
          Flag for comparison: normalize whitespaces in string values .
static int TRIM_STRING_VALUES
          Flag for comparison: trim string values.
 
Method Summary
static boolean checkObjectSerialisation(XMLSerializable src, InputStream is)
          Check that result of the given XMLSerializable serialisation is the same XML as the given inputStream content.
static boolean checkObjectSerialisation(XMLSerializable src, InputStream is, int flags)
           
static boolean documentsAreEquals(Document doc1, Document doc2)
           
static boolean documentsAreEquals(Document doc1, Document doc2, int flags)
           
static String domToString(Document doc)
          Serialize a DOM document to a String with multiple lines and 4 indentation spaces.
static String domToString(Document doc, int nbrIndentationSpace, boolean multiLine)
          Serialize a DOM document to a String.
static boolean elementsAreEquals(Element elem1, Element elem2, int flags)
           
static String elementToString(Element elem)
          Serialize a DOM document to a String with multiple lines and 4 indentation spaces.
static String elementToString(Element elem, int nbrIndentationSpace, boolean multiLine)
          Serialize a DOM element (and all it's sub-elements) to a String.
static String getDefinedPerfix(Node startNode, String uri)
          Get the prefix already defined in the DOM at the node location for the given namespace URI.
static List<String> getDefinedPerfixes(Node startNode, String uri)
          Get the list of prefixs already defined in the DOM at the node location for the given namespace URI.
static String getDefinedUri(Node startNode, String prefix)
          Get the URI already defined in the DOM and associated to the given prefix.
static Element getDirectChild(Element parent, String namespaceURI, String localName, int index)
          Get the direct child of an element matching the given namespace+tag+index.
static List<Node> getDirectChildElementsByTagNameNS(Element parent, String namespaceURI, String localName)
          Get the list of direct child of an element matching the given namespace+tag.
static String getEncoding(String xml)
          Get the encoding declared in the given XML or "UTF-8" by default.
static String getTextValue(Node nod)
          Get the text value of an element or attribute node.
static boolean isEmpty(Element el)
          An empty element means an element without attributes and without text or with white text.
static boolean matchElement(Node nod, String uri, String tag)
          Check if the given node is an element matching the given namespace+tag.
static Document resourceToDom(Class<?> resolver, String resourcePath)
           
static void setTextValue(Node nod, String text)
          Set the text value of an element or attribute node.
static Document streamToDom(InputStream is)
           
static Document stringToDom(String xml)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

IGNORE_CASE

public static final int IGNORE_CASE
Flag for comparison: ignore case for string values.

See Also:
Constant Field Values

TRIM_STRING_VALUES

public static final int TRIM_STRING_VALUES
Flag for comparison: trim string values.

See Also:
Constant Field Values

NORMALIZE_STRING_VALUES

public static final int NORMALIZE_STRING_VALUES
Flag for comparison: normalize whitespaces in string values .

See Also:
Constant Field Values

IGNORE_SEQUENCE_ORDER

public static final int IGNORE_SEQUENCE_ORDER
Flag for comparison: ignore order of elements in sequences.

See Also:
Constant Field Values

EMPTY_EQUALS_NULL

public static final int EMPTY_EQUALS_NULL
Flag for comparison: empty elements (like <el/> or <el></el>) are equivalent to no-existent elements.

See Also:
Constant Field Values
Method Detail

getDirectChildElementsByTagNameNS

public static List<Node> getDirectChildElementsByTagNameNS(Element parent,
                                                           String namespaceURI,
                                                           String localName)
Get the list of direct child of an element matching the given namespace+tag.

Parameters:
parent - parent node when the child are searched.
namespaceURI - requested element namespace URI.
localName - requested element local name (tag).
Returns:
List a list with all the direct children matching the given namespace+localName.

getDirectChild

public static Element getDirectChild(Element parent,
                                     String namespaceURI,
                                     String localName,
                                     int index)
Get the direct child of an element matching the given namespace+tag+index.

Parameters:
parent - parent node when the child are searched.
namespaceURI - requested element namespace URI.
localName - requested element local name (tag).
index - the child element index (among the elements matching the namespace+localName). Note: Unlike XPath standard, this index is zero-based.
Returns:
List a list with all the direct childs matching the given namespace+localName.

getDefinedPerfix

public static String getDefinedPerfix(Node startNode,
                                      String uri)
Get the prefix already defined in the DOM at the node location for the given namespace URI. Unlike the DOM implementation this method returns "" to denote that the default prefix is used (with a xmlns="...." declaration). This method returns null when no prefix is found in the given context for the URI.

Parameters:
startNode - the context node where we search for prefix.
uri - the namespace URI to search.
Returns:
the prefix already used to define the URI or null if the URI is not yet defined in this context.

getDefinedPerfixes

public static List<String> getDefinedPerfixes(Node startNode,
                                              String uri)
Get the list of prefixs already defined in the DOM at the node location for the given namespace URI. Unlike the DOM implementation this method returns "" to denote that the default prefix is used (with a xmlns="...." declaration). This method returns an empty list when no prefix is found in the given context for the URI.

Parameters:
startNode - the context node where we search for prefix.
uri - the namespace URI to search.
Returns:
the list of all prefix already used to define the URI or empty list if the URI is not yet defined in this context.

getDefinedUri

public static String getDefinedUri(Node startNode,
                                   String prefix)
Get the URI already defined in the DOM and associated to the given prefix.

Parameters:
startNode - the context node where we search for prefix.
prefix - the prefix to search.
Returns:
the URI already defined in the DOM or null if no URI is mapped to the prefix in this context.

matchElement

public static boolean matchElement(Node nod,
                                   String uri,
                                   String tag)
Check if the given node is an element matching the given namespace+tag.

Parameters:
nod - the node
uri - namespace for the element
tag - local name for the element
Returns:
boolean true iff the given element match the given namespace+tag.

getTextValue

public static String getTextValue(Node nod)
Get the text value of an element or attribute node. The value of all the text nodes of an element are concatenated.

Parameters:
nod - The element or attribute node.
Returns:
String it's text value.

setTextValue

public static void setTextValue(Node nod,
                                String text)
Set the text value of an element or attribute node. For elements, the value of the first the text node is replaced and any other text node are removed. If the element did not contain text node previously, a new text node is added with given text value.

Parameters:
nod - The element or attribute node.
text - it's new text value.

checkObjectSerialisation

public static boolean checkObjectSerialisation(XMLSerializable src,
                                               InputStream is)
Check that result of the given XMLSerializable serialisation is the same XML as the given inputStream content.
Both are transformed to DOM documents and then compared, this way ignorable whitespaces, prefixes, attribute order, ... are ignored.

Parameters:
src - a XML serializable object
is - a stream containing the expected serialisation result.
Returns:
true if the serialisation match the expected result.

checkObjectSerialisation

public static boolean checkObjectSerialisation(XMLSerializable src,
                                               InputStream is,
                                               int flags)

documentsAreEquals

public static boolean documentsAreEquals(Document doc1,
                                         Document doc2)

documentsAreEquals

public static boolean documentsAreEquals(Document doc1,
                                         Document doc2,
                                         int flags)

elementsAreEquals

public static boolean elementsAreEquals(Element elem1,
                                        Element elem2,
                                        int flags)

isEmpty

public static boolean isEmpty(Element el)
An empty element means an element without attributes and without text or with white text.


domToString

public static String domToString(Document doc)
Serialize a DOM document to a String with multiple lines and 4 indentation spaces. Note: Don't support mixed content.


domToString

public static String domToString(Document doc,
                                 int nbrIndentationSpace,
                                 boolean multiLine)
Serialize a DOM document to a String. Note: Don't support mixed content.


elementToString

public static String elementToString(Element elem)
Serialize a DOM document to a String with multiple lines and 4 indentation spaces. Note: Don't support mixed content.


elementToString

public static String elementToString(Element elem,
                                     int nbrIndentationSpace,
                                     boolean multiLine)
Serialize a DOM element (and all it's sub-elements) to a String. Note: Don't support mixed content.


stringToDom

public static Document stringToDom(String xml)

getEncoding

public static String getEncoding(String xml)
Get the encoding declared in the given XML or "UTF-8" by default.


resourceToDom

public static Document resourceToDom(Class<?> resolver,
                                     String resourcePath)

streamToDom

public static Document streamToDom(InputStream is)


Copyright © 2012. All Rights Reserved.