eu.semaine.util
Class XMLTool

java.lang.Object
  extended by eu.semaine.util.XMLTool

public class XMLTool
extends java.lang.Object

Utility methods for working wth XML documents.

Author:
marc

Constructor Summary
XMLTool()
           
 
Method Summary
static org.w3c.dom.Element appendChildElement(org.w3c.dom.Node node, java.lang.String childName)
          Create a child element with the given name and append it below node.
static org.w3c.dom.Element appendChildElement(org.w3c.dom.Node node, java.lang.String childName, java.lang.String childNamespace)
          Create a child element with the given name and namespace, and append it below node.
static org.w3c.dom.Element createElement(org.w3c.dom.Document doc, java.lang.String elementName)
          In the given document, create a new element of the given name, with the same namespace as the document element.
static org.w3c.dom.Element createElement(org.w3c.dom.Document doc, java.lang.String elementName, java.lang.String namespace)
          In the given document, create a new element with the given name and the given namespace.
static java.lang.String document2String(org.w3c.dom.Document document)
          Document type to String format conversion
static java.lang.String getAttributeIfAvailable(org.w3c.dom.Element e, java.lang.String attributeName)
          For the given element, return the value of the given attribute if it exists, or null if it doesn't exist.
static org.w3c.dom.Element getChildElementByTagNameNS(org.w3c.dom.Node node, java.lang.String childName, java.lang.String childNamespace)
          Get the direct child of node that is an element with the given tag name and namespace.
static java.util.List<org.w3c.dom.Element> getChildrenByTagNameNS(org.w3c.dom.Node node, java.lang.String childName, java.lang.String childNamespace)
          Get a list of all direct children with the given tag name and namespace.
static boolean isSameNamespace(java.lang.String namespaceA, java.lang.String namespaceB)
          Determine whether the two namespaces are the same.
static java.lang.String mergeTwoXMLFiles(java.lang.String xmlFileContent1, java.lang.String xmlFileContent2, java.io.InputStream xmlStyleSheet, java.lang.String refCodeName)
          Merge two XML files using XSLT
static java.lang.String needAttribute(org.w3c.dom.Element e, java.lang.String attributeName)
          For the given element, return the value of the given attribute if it exists, or complain with a MessageFormatException if it doesn't exist.
static org.w3c.dom.Element needChildElementByTagNameNS(org.w3c.dom.Node node, java.lang.String childName, java.lang.String childNamespace)
          Same as getChildElementByTagNameNS(Node, String, String), but throw a MessageFormatException if there is no such child element.
static org.w3c.dom.Document newDocument(java.lang.String rootTagname, java.lang.String namespace)
          Create a new document with the given name and namespace for the root element.
static org.w3c.dom.Document newDocument(java.lang.String rootTagname, java.lang.String namespace, java.lang.String version)
          Create a new document with the given name and namespace for the root element, and set the 'version' attribute of the root element to the given value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLTool

public XMLTool()
Method Detail

newDocument

public static org.w3c.dom.Document newDocument(java.lang.String rootTagname,
                                               java.lang.String namespace)
Create a new document with the given name and namespace for the root element.

Parameters:
rootTagname -
namespace - the namespace URI, e.g. http://www.w3.org/2003/04/emma, or null if no namespace is to be associated with the new element.
Returns:

newDocument

public static org.w3c.dom.Document newDocument(java.lang.String rootTagname,
                                               java.lang.String namespace,
                                               java.lang.String version)
Create a new document with the given name and namespace for the root element, and set the 'version' attribute of the root element to the given value.

Parameters:
rootTagname -
namespace - the namespace URI, e.g. http://www.w3.org/2003/04/emma, or null if no namespace is to be associated with the new element.
version - a value to add to the 'version' attribute of the root element
Returns:

createElement

public static org.w3c.dom.Element createElement(org.w3c.dom.Document doc,
                                                java.lang.String elementName)
In the given document, create a new element of the given name, with the same namespace as the document element.

Parameters:
doc - a document
elementName - the name of the new element
Returns:
an element which is not yet included in the tree hierarchy of the document.
Throws:
java.lang.NullPointerException - if doc or elementName is null.

createElement

public static org.w3c.dom.Element createElement(org.w3c.dom.Document doc,
                                                java.lang.String elementName,
                                                java.lang.String namespace)
In the given document, create a new element with the given name and the given namespace.

Parameters:
doc -
elementName -
namespace - the namespace URI, e.g. http://www.w3.org/2003/04/emma, or null if no namespace is to be associated with the new element.
Returns:
an element which is not yet included in the tree hierarchy of the document.
Throws:
java.lang.NullPointerException - if doc or elementName is null.

appendChildElement

public static org.w3c.dom.Element appendChildElement(org.w3c.dom.Node node,
                                                     java.lang.String childName)
Create a child element with the given name and append it below node. The new element will have the same namespace as node.

Parameters:
node -
childName -
Returns:
the child element

appendChildElement

public static org.w3c.dom.Element appendChildElement(org.w3c.dom.Node node,
                                                     java.lang.String childName,
                                                     java.lang.String childNamespace)
Create a child element with the given name and namespace, and append it below node.

Parameters:
node -
childName -
childNamespace - the namespace of the child, or null if no namespace is desired.
Returns:
the child element

getChildElementByTagNameNS

public static org.w3c.dom.Element getChildElementByTagNameNS(org.w3c.dom.Node node,
                                                             java.lang.String childName,
                                                             java.lang.String childNamespace)
Get the direct child of node that is an element with the given tag name and namespace.

Parameters:
node -
childName -
childNamespace -
Returns:
the child element, or null if there is no such child.

needChildElementByTagNameNS

public static org.w3c.dom.Element needChildElementByTagNameNS(org.w3c.dom.Node node,
                                                              java.lang.String childName,
                                                              java.lang.String childNamespace)
                                                       throws MessageFormatException
Same as getChildElementByTagNameNS(Node, String, String), but throw a MessageFormatException if there is no such child element.

Parameters:
node -
childName -
childNamespace -
Returns:
a non-null child element
Throws:
MessageFormatException - if there is no such child, i.e. when getChildElementByTagNameNS() would return null.

needAttribute

public static java.lang.String needAttribute(org.w3c.dom.Element e,
                                             java.lang.String attributeName)
                                      throws MessageFormatException
For the given element, return the value of the given attribute if it exists, or complain with a MessageFormatException if it doesn't exist.

Parameters:
e - the element whose attribute to return
attributeName - the name of the attribute to look up.
Returns:
the String value of the attribute if it exists
Throws:
MessageFormatException - if the attribute doesn't exist.

getAttributeIfAvailable

public static java.lang.String getAttributeIfAvailable(org.w3c.dom.Element e,
                                                       java.lang.String attributeName)
For the given element, return the value of the given attribute if it exists, or null if it doesn't exist. Note that this is different from calling e.getAttribute(), which will return the empty string both if the attribute doesn't exist and if it exists and has the empty value.

Parameters:
e -
attributeName -
Returns:
the String value of the attribute if it exists, or null

getChildrenByTagNameNS

public static java.util.List<org.w3c.dom.Element> getChildrenByTagNameNS(org.w3c.dom.Node node,
                                                                         java.lang.String childName,
                                                                         java.lang.String childNamespace)
Get a list of all direct children with the given tag name and namespace. Whereas getChildElementByTagNameNS() returns the single first child, this method returns all the children that match.

Parameters:
node -
childName -
childNamespace -
Returns:
a list containing the children that match, or an empty list if none match.
Throws:
MessageFormatException

isSameNamespace

public static boolean isSameNamespace(java.lang.String namespaceA,
                                      java.lang.String namespaceB)
Determine whether the two namespaces are the same.

Parameters:
namespaceA - a string representing a namespace, or null
namespaceB - a string representing a namespace, or null
Returns:
true if both are null or both are the same string, false otherwise.

mergeTwoXMLFiles

public static java.lang.String mergeTwoXMLFiles(java.lang.String xmlFileContent1,
                                                java.lang.String xmlFileContent2,
                                                java.io.InputStream xmlStyleSheet,
                                                java.lang.String refCodeName)
                                         throws java.lang.Exception,
                                                java.io.FileNotFoundException
Merge two XML files using XSLT

Parameters:
xmlFileContent1, - first XML file content
xmlFileContent2, - second XML file content
xmlStyleSheet, - XSL style sheet as a inputstream
refCodeName, - code name used in xsl sheet to refer xmlFile2 (example: semaine.mary.intonation )
Returns:
output of merged xml file
Throws:
java.lang.Exception
java.io.FileNotFoundException

document2String

public static java.lang.String document2String(org.w3c.dom.Document document)
                                        throws SystemConfigurationException
Document type to String format conversion

Parameters:
document -
Returns:
Throws:
java.lang.Exception
java.io.FileNotFoundException
SystemConfigurationException