org.yawlfoundation.yawl.util
Class DOMUtil

java.lang.Object
  extended by org.yawlfoundation.yawl.util.DOMUtil

public class DOMUtil
extends Object

This class provides helper methods to perform common DOM related tasks.

Author:
Mike Fowler Date: Oct 25, 2005

Constructor Summary
DOMUtil()
           
 
Method Summary
static Node alphabetiseChildNodes(Node root)
          Alphabetises the top level children of the node root.
static Document createDocumentInstance()
          Creates a new, namespace aware, Document node
static Document createNamespacelessDocumentInstance()
          Creates a new, namespace aware, Document node
static InputSource createUTF8InputSource(Node node)
          Converts a Document dom into an Input source with UTF-8 encoding
static InputSource createUTF8InputSource(String xml)
          Converts a XML String into an Input source with UTF-8 encoding
static String formatXMLStringForDisplay(String xml)
          Formats a string of XML suitable for standard display (ie.
static String formatXMLStringForDisplay(String xml, boolean omitDeclaration)
          Formats a string of XML suitable for standard display (ie.
static Document getDocumentFromString(String xml)
          Converts a xml String to a DOM Document
static Document getNamespacelessDocumentFromDocument(Document dom)
           
static Document getNamespacelessDocumentFromString(String xml)
           
static String getNodeText(Node node)
          Extracts to text from the supplied node and it's children.
static String getXMLStringFragmentFromNode(Node node)
          Takes the supplied node as the root of an xml document and converts it to a String representation.
static String getXMLStringFragmentFromNode(Node node, boolean omitDeclaration)
          Takes the supplied node as the root of an xml document and converts it to a String representation.
static String getXMLStringFragmentFromNode(Node node, boolean omitDeclaration, boolean collapseEmptyTags)
          Takes the supplied node as the root of an xml document and converts it to a String representation.
static String getXMLStringFragmentFromNode(Node node, boolean omitDeclaration, String encoding)
          Takes the supplied node as the root of an xml document and converts it to a String representation.
static String getXMLStringFragmentFromNode(Node node, boolean omitDeclaration, String encoding, boolean collapseEmptyTags)
          Takes the supplied node as the root of an xml document and converts it to a String representation.
static String getXMLStringFragmentFromNode(Node node, String encoding)
          Takes the supplied node as the root of an xml document and converts it to a String representation.
static void removeAllAttributes(Element element)
           
static void removeAllChildNodes(Node node)
          Removes all child nodes from the context Node node.
static Node removeEmptyElements(Node node)
          Deprecated.  
static Document removeEmptyNodes(Node node)
           
static NodeList selectNodeList(Node node, String expression)
           
static String selectNodeText(Node node, String expression)
           
static Node selectSingleNode(Node node, String expression)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DOMUtil

public DOMUtil()
Method Detail

getDocumentFromString

public static Document getDocumentFromString(String xml)
                                      throws ParserConfigurationException,
                                             SAXException,
                                             IOException
Converts a xml String to a DOM Document

Parameters:
xml - to be parsed
Returns:
DOM Document representation of XML.
Throws:
ParserConfigurationException
SAXException
IOException

createDocumentInstance

public static Document createDocumentInstance()
                                       throws ParserConfigurationException
Creates a new, namespace aware, Document node

Returns:
the created DOM Document, otherwise return null
Throws:
ParserConfigurationException

createNamespacelessDocumentInstance

public static Document createNamespacelessDocumentInstance()
                                                    throws ParserConfigurationException
Creates a new, namespace aware, Document node

Returns:
the created DOM Document, otherwise return null
Throws:
ParserConfigurationException

getNamespacelessDocumentFromDocument

public static Document getNamespacelessDocumentFromDocument(Document dom)
                                                     throws TransformerException,
                                                            IOException,
                                                            ParserConfigurationException,
                                                            SAXException
Throws:
TransformerException
IOException
ParserConfigurationException
SAXException

getNamespacelessDocumentFromString

public static Document getNamespacelessDocumentFromString(String xml)
                                                   throws ParserConfigurationException,
                                                          SAXException,
                                                          IOException
Throws:
ParserConfigurationException
SAXException
IOException

getNodeText

public static String getNodeText(Node node)
Extracts to text from the supplied node and it's children.

Parameters:
node - to extract text from.
Returns:
String representation of the node text.

getXMLStringFragmentFromNode

public static String getXMLStringFragmentFromNode(Node node)
                                           throws TransformerException
Takes the supplied node as the root of an xml document and converts it to a String representation. The xml header is omitted.

Parameters:
node - to convert to a String
Returns:
String XML fragment.
Throws:
TransformerException

getXMLStringFragmentFromNode

public static String getXMLStringFragmentFromNode(Node node,
                                                  String encoding)
                                           throws TransformerException
Takes the supplied node as the root of an xml document and converts it to a String representation. The xml header is omitted.

Parameters:
node - to convert to a String
encoding - Target encoding of output XML
Returns:
String XML fragment.
Throws:
TransformerException

getXMLStringFragmentFromNode

public static String getXMLStringFragmentFromNode(Node node,
                                                  boolean omitDeclaration)
                                           throws TransformerException
Takes the supplied node as the root of an xml document and converts it to a String representation.

Parameters:
node - to convert to a String
omitDeclaration - set to false to include the <? xml ?> declaration
Returns:
String XML fragment.
Throws:
TransformerException

getXMLStringFragmentFromNode

public static String getXMLStringFragmentFromNode(Node node,
                                                  boolean omitDeclaration,
                                                  String encoding)
                                           throws TransformerException
Takes the supplied node as the root of an xml document and converts it to a String representation.

Parameters:
node - to convert to a String
omitDeclaration - set to false to include the <? xml ?> declaration
encoding - Target encoding of output XML
Returns:
String XML fragment.
Throws:
TransformerException

getXMLStringFragmentFromNode

public static String getXMLStringFragmentFromNode(Node node,
                                                  boolean omitDeclaration,
                                                  boolean collapseEmptyTags)
                                           throws TransformerException
Takes the supplied node as the root of an xml document and converts it to a String representation.

Parameters:
node - to convert to a String
omitDeclaration - set to false to include the <? xml ?> declaration
collapseEmptyTags - set to false to use the long form of xml tags (ie. <a></a>)
Returns:
String XML fragment.
Throws:
TransformerException

getXMLStringFragmentFromNode

public static String getXMLStringFragmentFromNode(Node node,
                                                  boolean omitDeclaration,
                                                  String encoding,
                                                  boolean collapseEmptyTags)
                                           throws TransformerException
Takes the supplied node as the root of an xml document and converts it to a String representation.

Parameters:
node - to convert to a String
omitDeclaration - set to false to include the <? xml ?> declaration
encoding - Target encoding of output XML
collapseEmptyTags - set to false to use the long form of xml tags (ie. <a></a>)
Returns:
String XML fragment.
Throws:
TransformerException

removeEmptyNodes

public static Document removeEmptyNodes(Node node)
                                 throws IOException,
                                        TransformerException,
                                        SAXException,
                                        ParserConfigurationException
Throws:
IOException
TransformerException
SAXException
ParserConfigurationException

removeEmptyElements

public static Node removeEmptyElements(Node node)
                                throws XPathExpressionException
Deprecated. 

Takes the supplied node and recursively removes empty (no content/child nodes) elements

Parameters:
node - to remove all empty elements from
Returns:
Trimmed node
Throws:
XPathExpressionException

selectSingleNode

public static Node selectSingleNode(Node node,
                                    String expression)
                             throws XPathExpressionException
Throws:
XPathExpressionException

selectNodeText

public static String selectNodeText(Node node,
                                    String expression)
                             throws XPathExpressionException
Throws:
XPathExpressionException

selectNodeList

public static NodeList selectNodeList(Node node,
                                      String expression)
                               throws XPathExpressionException
Throws:
XPathExpressionException

formatXMLStringForDisplay

public static String formatXMLStringForDisplay(String xml)
Formats a string of XML suitable for standard display (ie. no xml directive and idented). If the formatting fails, the original XML string is returned unaltered.

Parameters:
xml - to format for display
Returns:
formatted xml if no exceptions occured, otherwise original string.

formatXMLStringForDisplay

public static String formatXMLStringForDisplay(String xml,
                                               boolean omitDeclaration)
Formats a string of XML suitable for standard display (ie. no xml directive and idented). If the formatting fails, the original XML string is returned unaltered.

Parameters:
xml - to format for display
Returns:
formatted xml if no exceptions occured, otherwise original string.

removeAllChildNodes

public static void removeAllChildNodes(Node node)
Removes all child nodes from the context Node node.

Parameters:
node - to remove all children from

removeAllAttributes

public static void removeAllAttributes(Element element)

createUTF8InputSource

public static InputSource createUTF8InputSource(String xml)
                                         throws UnsupportedEncodingException
Converts a XML String into an Input source with UTF-8 encoding

Parameters:
xml -
Returns:
an instantiated InputSource
Throws:
UnsupportedEncodingException

createUTF8InputSource

public static InputSource createUTF8InputSource(Node node)
                                         throws UnsupportedEncodingException,
                                                TransformerException
Converts a Document dom into an Input source with UTF-8 encoding

Parameters:
node -
Returns:
an instantiated InputSource
Throws:
UnsupportedEncodingException
TransformerException

alphabetiseChildNodes

public static Node alphabetiseChildNodes(Node root)
                                  throws XPathExpressionException
Alphabetises the top level children of the node root.

Parameters:
root -
Returns:
The alphabetised root node.
Throws:
XPathExpressionException


Copyright © 2004-2012 The YAWL Foundation.