org.ow2.dragon.util
Class XMLUtil

java.lang.Object
  extended by org.ow2.dragon.util.XMLUtil

public final class XMLUtil
extends java.lang.Object

Contains utilities methods for XML operations.

Since:
1.0
Author:
alouis - EBMWebsourcing, ddesjardins - EBMWebsourcing, gblondelle - EBMWebsourcing

Method Summary
static int compareXML(java.io.InputStream xml1, java.io.InputStream xml2)
           
static org.w3c.dom.Node createAttribute(org.w3c.dom.Document document, java.lang.String att, java.lang.String value)
          Create an attribute node with the specified value
static org.w3c.dom.Document createDocument()
          Get a new document
static org.w3c.dom.Document createDocument(org.xml.sax.InputSource is)
          Get a new document from an InputSource
static org.w3c.dom.Document createDocument(java.io.InputStream is)
          Get a new document from an InputStream
static org.w3c.dom.Document createDocumentFromString(java.lang.String xml)
          Create a document from a String
static org.w3c.dom.Node createNode(org.w3c.dom.Document document, java.lang.String nodeName, java.lang.String... attVal)
          Create a node with the specified name, attributes and values. attVal is a suite : att1,val1,att2,val2,...
static java.lang.String createStringFromDOMDocument(org.w3c.dom.Node document)
          Create a String result from a DOM document
static java.lang.String createStringFromDOMNode(org.w3c.dom.Node node)
          Create a String result from a DOM Node
static java.lang.String createStringFromDOMNode(org.w3c.dom.Node node, boolean omitDeclaration)
          Create a String result from a DOM Node
static java.lang.String createStringFromDOMNodeList(org.w3c.dom.NodeList list)
          Create a String from a Node list
static javax.xml.namespace.QName extractRequiredXmlAttributeQName(org.w3c.dom.Node node, java.lang.String attrName)
           
static javax.xml.namespace.QName extractXmlAttributeQName(org.w3c.dom.Node node, java.lang.String attrName)
          Return the QName extracted from the targeted attribute of the given Node
static java.lang.String extractXmlEncoding(java.lang.String msg)
          Extract the xml header charset encoding from the given xml String
static org.w3c.dom.Node findChild(org.w3c.dom.Node node, java.lang.String nodeName, boolean recursive)
          Deprecated. Use findChild(Node, String, String, boolean) with a namespace set to null.
static org.w3c.dom.Node findChild(org.w3c.dom.Node node, java.lang.String nodeName, java.lang.String namespaceURI, boolean recursive)
          Search for a child with the given nodeName.
static java.lang.String getAttributeValue(org.w3c.dom.Node n, java.lang.String attName)
          Return the value of the attribute in the node
static org.w3c.dom.Node getFirstChild(org.w3c.dom.Node node)
          Return the first child of a node, regardless text node and CDATA sections
static org.w3c.dom.Node getNextSibling(org.w3c.dom.Node node)
          Return the next sibling of a node, regardless text node and CDATA sections
static org.w3c.dom.Node getNode(org.w3c.dom.Document document, java.lang.String elementName)
           
static java.util.List<org.w3c.dom.Node> getNodeChildren(org.w3c.dom.Node node)
          A List of Node that contains all children of this node; nodes must be of type Node.ELEMENT_NODE to be returned.
static org.w3c.dom.Node getNodeWithAttribute(org.w3c.dom.Document document, java.lang.String elementName, java.lang.String attributeName, java.lang.String attributeValue)
           
static java.lang.String getRequiredAttributeValue(org.w3c.dom.Node n, java.lang.String attName)
          Return the value of the attribute in the node.
static java.lang.String getTextContent(org.w3c.dom.Node node)
          Return the text element of a node, even if it is contained in a CDATA section
static java.util.List<java.lang.String> getTextContents(org.w3c.dom.NodeList list)
          Return an array of String representing each Text element of the nodes which are in the list.
static java.lang.String getXMLSHA1Sign(java.io.InputStream inputStream, boolean ommitComments)
           
static org.w3c.dom.Document loadDocument(java.io.InputStream inputStream)
          Load a document from an input stream.
static java.lang.String parseToString(org.w3c.dom.Node node)
          Transform an XML Node into a String
static void writeDocument(org.w3c.dom.Document document, java.io.OutputStream outputStream)
          Write a document to an output stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

createAttribute

public static org.w3c.dom.Node createAttribute(org.w3c.dom.Document document,
                                               java.lang.String att,
                                               java.lang.String value)
Create an attribute node with the specified value

Parameters:
document - XML document
att - attribute name
value - attribute value
Returns:
an attribute, null if the document is null

createNode

public static org.w3c.dom.Node createNode(org.w3c.dom.Document document,
                                          java.lang.String nodeName,
                                          java.lang.String... attVal)
Create a node with the specified name, attributes and values. attVal is a suite : att1,val1,att2,val2,...

Parameters:
document - xlm document
nodeName - node name
attVal - attribute values
Returns:
the node with its attribute, null if the array is not correct or the document is null.

findChild

@Deprecated
public static org.w3c.dom.Node findChild(org.w3c.dom.Node node,
                                                    java.lang.String nodeName,
                                                    boolean recursive)
Deprecated. Use findChild(Node, String, String, boolean) with a namespace set to null.

Search for a child with the given nodeName. If recursive, search in all the child of first level, then if not found, search in the 2nd level of the first child, ...

Parameters:
node - parent node
nodeName - node name
recursive - boolean to know if we got through the xml tree
Returns:
a node

findChild

public static org.w3c.dom.Node findChild(org.w3c.dom.Node node,
                                         java.lang.String nodeName,
                                         java.lang.String namespaceURI,
                                         boolean recursive)
Search for a child with the given nodeName. If recursive, search in all the child of firdt level, then if not found, search in the 2nd level of the first child, ...

Parameters:
node - parent node
namespaceURI - The namespaceURI of the node. if null, the namespace is ignored
nodeName - node name
recursive - boolean to know if we got through the xml tree
Returns:
a node

parseToString

public static java.lang.String parseToString(org.w3c.dom.Node node)
                                      throws javax.xml.transform.TransformerException
Transform an XML Node into a String

Parameters:
node - the XML Node to parse
Returns:
the resulting String, null if node is null
Throws:
javax.xml.transform.TransformerException - if errors occured during transformation

getAttributeValue

public static java.lang.String getAttributeValue(org.w3c.dom.Node n,
                                                 java.lang.String attName)
Return the value of the attribute in the node

Parameters:
n - the node
attName - the name of the attribute
Returns:
the value of the attribute (can be empty), null if not found

getRequiredAttributeValue

public static java.lang.String getRequiredAttributeValue(org.w3c.dom.Node n,
                                                         java.lang.String attName)
Return the value of the attribute in the node. Throws an exception if missing attribute.

Parameters:
n - the node
attName - the name of the attribute, must be non null
Returns:
the value of the attribute (can be empty).
Throws:
java.lang.NullPointerException - if attribute not found in the node

getFirstChild

public static org.w3c.dom.Node getFirstChild(org.w3c.dom.Node node)
Return the first child of a node, regardless text node and CDATA sections

Parameters:
node -
Returns:

getNextSibling

public static org.w3c.dom.Node getNextSibling(org.w3c.dom.Node node)
Return the next sibling of a node, regardless text node and CDATA sections

Parameters:
node -
Returns:
The next sibling node, or null if node is null or no sibling node exists.

getTextContent

public static java.lang.String getTextContent(org.w3c.dom.Node node)
Return the text element of a node, even if it is contained in a CDATA section

Parameters:
node -
Returns:

createStringFromDOMDocument

public static java.lang.String createStringFromDOMDocument(org.w3c.dom.Node document)
                                                    throws javax.xml.transform.TransformerException
Create a String result from a DOM document

Parameters:
document - the DOM Document. Must not be null
Returns:
a String representation of the DOM Document
Throws:
javax.xml.transform.TransformerException

createStringFromDOMNode

public static java.lang.String createStringFromDOMNode(org.w3c.dom.Node node)
                                                throws javax.xml.transform.TransformerException
Create a String result from a DOM Node

Parameters:
node - the DOM Node. Must not be null
Returns:
a String representation of the DOM Document
Throws:
javax.xml.transform.TransformerException

createStringFromDOMNodeList

public static java.lang.String createStringFromDOMNodeList(org.w3c.dom.NodeList list)
                                                    throws javax.xml.transform.TransformerException
Create a String from a Node list

Parameters:
list -
Returns:
Throws:
javax.xml.transform.TransformerException

createStringFromDOMNode

public static java.lang.String createStringFromDOMNode(org.w3c.dom.Node node,
                                                       boolean omitDeclaration)
                                                throws javax.xml.transform.TransformerException
Create a String result from a DOM Node

Parameters:
node - the DOM Node. Must not be null
Returns:
a String representation of the DOM Document
Throws:
javax.xml.transform.TransformerException

createDocumentFromString

public static org.w3c.dom.Document createDocumentFromString(java.lang.String xml)
Create a document from a String

Parameters:
xml - an xml string
Returns:
a Document representing the document, null if an error occured

getTextContents

public static java.util.List<java.lang.String> getTextContents(org.w3c.dom.NodeList list)
Return an array of String representing each Text element of the nodes which are in the list.

Parameters:
list -
Returns:

extractXmlAttributeQName

public static javax.xml.namespace.QName extractXmlAttributeQName(org.w3c.dom.Node node,
                                                                 java.lang.String attrName)
Return the QName extracted from the targeted attribute of the given Node

Parameters:
node - the node from which attribute must be extracted
attrName - name of the targeted attribute
Returns:
the QName value of the targeted Node attribute, null if not found
Throws:
java.lang.IllegalArgumentException - If the String does not conform to one of the following pattern : "localPart", "{ns}localPart" or "ns:localPart".

extractXmlEncoding

public static java.lang.String extractXmlEncoding(java.lang.String msg)
Extract the xml header charset encoding from the given xml String

Parameters:
msg - the xml String
Returns:
the xml header charset encoding extrated from the given String or default encoding (UTF-8) if not found

extractRequiredXmlAttributeQName

public static javax.xml.namespace.QName extractRequiredXmlAttributeQName(org.w3c.dom.Node node,
                                                                         java.lang.String attrName)
Parameters:
node -
attrName -
Returns:

getNodeChildren

public static java.util.List<org.w3c.dom.Node> getNodeChildren(org.w3c.dom.Node node)
A List of Node that contains all children of this node; nodes must be of type Node.ELEMENT_NODE to be returned. If there are no children, this is a NodeList containing no nodes.

Parameters:
node - the node from which children will be extracted
Returns:
a List of child Node of type Node.ELEMENT_NODE

getNode

public static org.w3c.dom.Node getNode(org.w3c.dom.Document document,
                                       java.lang.String elementName)
Parameters:
document -
elementName -
Returns:
the first matching node, null otherwise, or if document/path is null

getNodeWithAttribute

public static org.w3c.dom.Node getNodeWithAttribute(org.w3c.dom.Document document,
                                                    java.lang.String elementName,
                                                    java.lang.String attributeName,
                                                    java.lang.String attributeValue)
Parameters:
document -
elementName -
attributeName -
attributeValue -
Returns:
the first matching node, null otherwise, or if document/path is null

writeDocument

public static void writeDocument(org.w3c.dom.Document document,
                                 java.io.OutputStream outputStream)
                          throws java.lang.Exception
Write a document to an output stream. The output stream and the document must not be null.

Parameters:
document -
outputFile -
Throws:
java.lang.Exception

loadDocument

public static org.w3c.dom.Document loadDocument(java.io.InputStream inputStream)
                                         throws java.io.IOException,
                                                org.xml.sax.SAXException
Load a document from an input stream.

Parameters:
inputStream -
Returns:
Throws:
java.io.IOException
org.xml.sax.SAXException

createDocument

public static org.w3c.dom.Document createDocument()
Get a new document

Returns:

createDocument

public static org.w3c.dom.Document createDocument(java.io.InputStream is)
                                           throws org.xml.sax.SAXException,
                                                  java.io.IOException
Get a new document from an InputStream

Returns:
Throws:
java.io.IOException
org.xml.sax.SAXException

createDocument

public static org.w3c.dom.Document createDocument(org.xml.sax.InputSource is)
                                           throws org.xml.sax.SAXException,
                                                  java.io.IOException
Get a new document from an InputSource

Returns:
Throws:
java.io.IOException
org.xml.sax.SAXException

getXMLSHA1Sign

public static java.lang.String getXMLSHA1Sign(java.io.InputStream inputStream,
                                              boolean ommitComments)
                                       throws java.lang.Exception
Throws:
java.lang.Exception

compareXML

public static int compareXML(java.io.InputStream xml1,
                             java.io.InputStream xml2)
                      throws java.lang.Exception
Throws:
java.lang.Exception


Copyright © 2008-2009 eBMWebsourcing. All Rights Reserved.