org.jboss.soa.esb.dom
Class YADOMUtil

java.lang.Object
  extended by org.jboss.soa.esb.dom.YADOMUtil

public class YADOMUtil
extends java.lang.Object

Yet another set of DOM utility methods.

PLEASE DO NOT USE THIS CLASS OUTSIDE THIS PACKAGE. It will be removed as soon as we find equivalent DOM support methods somewhere else, or find a more appropriate home for these.

Author:
tom.fennelly@jboss.com

Constructor Summary
YADOMUtil()
           
 
Method Summary
static org.w3c.dom.Element addElement(org.w3c.dom.Node parent, java.lang.String elementName)
          Add an Element node to the supplied parent name.
static void copyChildNodes(org.w3c.dom.Node source, org.w3c.dom.Node target)
          Copy child node references from source to target.
static java.util.List<org.w3c.dom.Node> copyNodeList(org.w3c.dom.NodeList nodeList)
          Copy the nodes of a NodeList into the supplied list.
static int countElementsBefore(org.w3c.dom.Node node, java.lang.String tagName)
          Count the DOM element nodes before the supplied node, having the specified tag name, not including the node itself.
static org.w3c.dom.Document createDocument()
          Create a new W3C Document.
static java.lang.String getAttribute(org.w3c.dom.Element element, java.lang.String name, java.lang.String defaultVal)
           
static org.w3c.dom.Comment getCommentBefore(org.w3c.dom.Element element)
          Get the comment node before the supplied element.
static org.w3c.dom.Node getFirstChildByType(org.w3c.dom.Element element, int nodeType)
           
static org.w3c.dom.Element getFirstChildElement(org.w3c.dom.Element element)
          Get the first child element for the supplied element.
static org.w3c.dom.Element getFirstChildElementByName(org.w3c.dom.Element element, java.lang.String name)
          Get the first child of the supplied element that matches a given tag name.
static org.w3c.dom.Element getLastChildElement(org.w3c.dom.Element element)
          Get the last child element for the supplied element.
static java.lang.String getName(org.w3c.dom.Element element)
          Get the name from the supplied element.
static org.w3c.dom.Element getNextSiblingElement(org.w3c.dom.Node node)
           
static org.w3c.dom.Node getNode(org.w3c.dom.Node node, java.lang.String xpath)
          Get the W3C Node instance associated with the XPath selection supplied.
static org.w3c.dom.NodeList getNodeList(org.w3c.dom.Node node, java.lang.String xpath)
          Get the W3C NodeList instance associated with the XPath selection supplied.
static org.w3c.dom.Document parse(java.lang.String xml)
          Parse the supplied XML String and return the associated W3C Document object.
static org.w3c.dom.Document parseStream(java.io.InputStream stream, boolean validate, boolean expandEntityRefs)
          Parse the XML stream and return the associated W3C Document object.
static org.w3c.dom.Document parseStream(java.io.InputStream stream, boolean validate, boolean expandEntityRefs, boolean namespaceAware)
          Parse the XML stream and return the associated W3C Document object.
static void removeEmptyAttributes(org.w3c.dom.Element element)
          Remove all attributes having an empty value.
static void serialize(org.w3c.dom.Node node, java.io.File outdir, java.lang.String fileName)
          Serialize the supplied DOM node to the specified file in the specified output directory.
static void serialize(org.w3c.dom.Node node, java.io.OutputStream out)
           
static void serialize(org.w3c.dom.Node node, javax.xml.transform.stream.StreamResult streamRes)
          Serialize the supplied DOM node to the supplied DOM StreamResult instance.
static void serialize(org.w3c.dom.Node node, javax.xml.transform.stream.StreamResult streamRes, boolean omitXmlDecl)
          Serialize the supplied DOM node to the supplied DOM StreamResult instance.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

YADOMUtil

public YADOMUtil()
Method Detail

createDocument

public static org.w3c.dom.Document createDocument()
                                           throws ConfigurationException
Create a new W3C Document.

Handles exceptions etc.

Returns:
The new Document instance.
Throws:
ConfigurationException

parse

public static org.w3c.dom.Document parse(java.lang.String xml)
                                  throws org.xml.sax.SAXException,
                                         java.io.IOException
Parse the supplied XML String and return the associated W3C Document object.

Parameters:
xml - XML String.
Returns:
The W3C Document object associated with the input stream.
Throws:
org.xml.sax.SAXException
java.io.IOException

parseStream

public static org.w3c.dom.Document parseStream(java.io.InputStream stream,
                                               boolean validate,
                                               boolean expandEntityRefs)
                                        throws org.xml.sax.SAXException,
                                               java.io.IOException
Parse the XML stream and return the associated W3C Document object.

Performs a namespace unaware parse.

Parameters:
stream - The stream to be parsed.
validate - True if the document is to be validated, otherwise false.
expandEntityRefs - Expand entity References as per DocumentBuilderFactory.setExpandEntityReferences(boolean).
Returns:
The W3C Document object associated with the input stream.
Throws:
org.xml.sax.SAXException
java.io.IOException

parseStream

public static org.w3c.dom.Document parseStream(java.io.InputStream stream,
                                               boolean validate,
                                               boolean expandEntityRefs,
                                               boolean namespaceAware)
                                        throws org.xml.sax.SAXException,
                                               java.io.IOException
Parse the XML stream and return the associated W3C Document object.

Parameters:
stream - The stream to be parsed.
validate - True if the document is to be validated, otherwise false.
expandEntityRefs - Expand entity References as per DocumentBuilderFactory.setExpandEntityReferences(boolean).
namespaceAware - True if the document parse is to be namespace aware, otherwise false.
Returns:
The W3C Document object associated with the input stream.
Throws:
org.xml.sax.SAXException
java.io.IOException

getAttribute

public static java.lang.String getAttribute(org.w3c.dom.Element element,
                                            java.lang.String name,
                                            java.lang.String defaultVal)

addElement

public static org.w3c.dom.Element addElement(org.w3c.dom.Node parent,
                                             java.lang.String elementName)
Add an Element node to the supplied parent name.

Parameters:
parent - The parent to to which the new Element node is to be added.
elementName - The name of the Element to be added.
Returns:
The new Element.

removeEmptyAttributes

public static void removeEmptyAttributes(org.w3c.dom.Element element)
Remove all attributes having an empty value.

Parameters:
element - The element to be processed.

serialize

public static void serialize(org.w3c.dom.Node node,
                             java.io.File outdir,
                             java.lang.String fileName)
                      throws ConfigurationException
Serialize the supplied DOM node to the specified file in the specified output directory.

Parameters:
node - The DOM node to be serialised.
outdir - The directory into which the file is to be serialised.
fileName - The name of the file.
Throws:
ConfigurationException - Unable to serialise the node.

serialize

public static void serialize(org.w3c.dom.Node node,
                             java.io.OutputStream out)
                      throws ConfigurationException
Throws:
ConfigurationException

serialize

public static void serialize(org.w3c.dom.Node node,
                             javax.xml.transform.stream.StreamResult streamRes)
                      throws ConfigurationException
Serialize the supplied DOM node to the supplied DOM StreamResult instance.

Parameters:
node - The DOM node to be serialised.
streamRes - The StreamResult into which the node is to be serialised.
Throws:
ConfigurationException - Unable to serialise the node.

serialize

public static void serialize(org.w3c.dom.Node node,
                             javax.xml.transform.stream.StreamResult streamRes,
                             boolean omitXmlDecl)
                      throws ConfigurationException
Serialize the supplied DOM node to the supplied DOM StreamResult instance.

Parameters:
node - The DOM node to be serialised.
streamRes - The StreamResult into which the node is to be serialised.
omitXmlDecl - Omit the XML declaration.
Throws:
ConfigurationException - Unable to serialise the node.

countElementsBefore

public static int countElementsBefore(org.w3c.dom.Node node,
                                      java.lang.String tagName)
Count the DOM element nodes before the supplied node, having the specified tag name, not including the node itself.

Counts the sibling nodes.

Parameters:
node - Node whose element siblings are to be counted.
tagName - The tag name of the sibling elements to be counted.
Returns:
The number of siblings elements before the supplied node with the specified tag name.

copyNodeList

public static java.util.List<org.w3c.dom.Node> copyNodeList(org.w3c.dom.NodeList nodeList)
Copy the nodes of a NodeList into the supplied list.

This is not a cloneCollectionTemplateElement. It's just a copy of the node references.

Allows iteration over the Nodelist using the copy in the knowledge that the list will remain the same length, even if we modify the underlying NodeList. Using the NodeList can result in problems because elements can get removed from the list while we're iterating over it.

This code was acquired donated by the Milyn Smooks project.

Parameters:
nodeList - Nodelist to copy.
Returns:
List copy.

getNextSiblingElement

public static org.w3c.dom.Element getNextSiblingElement(org.w3c.dom.Node node)

getFirstChildByType

public static org.w3c.dom.Node getFirstChildByType(org.w3c.dom.Element element,
                                                   int nodeType)

getNodeList

public static org.w3c.dom.NodeList getNodeList(org.w3c.dom.Node node,
                                               java.lang.String xpath)
Get the W3C NodeList instance associated with the XPath selection supplied.

NOTE: Taken from Milyn Commons.

Parameters:
node - The document node to be searched.
xpath - The XPath String to be used in the selection.
Returns:
The W3C NodeList instance at the specified location in the document, or null.

getNode

public static org.w3c.dom.Node getNode(org.w3c.dom.Node node,
                                       java.lang.String xpath)
Get the W3C Node instance associated with the XPath selection supplied.

NOTE: Taken from Milyn Commons.

Parameters:
node - The document node to be searched.
xpath - The XPath String to be used in the selection.
Returns:
The W3C Node instance at the specified location in the document, or null.

getName

public static java.lang.String getName(org.w3c.dom.Element element)
Get the name from the supplied element.

Returns the localName of the element if set (namespaced element), otherwise the element's tagName is returned.

NOTE: Taken from Milyn Smooks.

Parameters:
element - The element.
Returns:
The element name.

copyChildNodes

public static void copyChildNodes(org.w3c.dom.Node source,
                                  org.w3c.dom.Node target)
Copy child node references from source to target.

Parameters:
source - Source Node.
target - Target Node.

getCommentBefore

public static org.w3c.dom.Comment getCommentBefore(org.w3c.dom.Element element)
Get the comment node before the supplied element.

Parameters:
element - The element.
Returns:
The Comment node.

getFirstChildElement

public static org.w3c.dom.Element getFirstChildElement(org.w3c.dom.Element element)
Get the first child element for the supplied element.

Parameters:
element - The element.
Returns:
The child element.

getLastChildElement

public static org.w3c.dom.Element getLastChildElement(org.w3c.dom.Element element)
Get the last child element for the supplied element.

Parameters:
element - The element.
Returns:
The child element.

getFirstChildElementByName

public static org.w3c.dom.Element getFirstChildElementByName(org.w3c.dom.Element element,
                                                             java.lang.String name)
Get the first child of the supplied element that matches a given tag name.

Parameters:
element - The element.
name - The name of the child element to search for.
Returns:
The first child element with the matching tag name.