uk.org.ogsadai.common
Class XMLUtilities

java.lang.Object
  |
  +--uk.org.ogsadai.common.XMLUtilities

public class XMLUtilities
extends java.lang.Object

ogsadai class/interface uk.org.ogsadai.common.XMLUtilities A collection of XML helper methods


Constructor Summary
XMLUtilities()
           
 
Method Summary
static org.w3c.dom.Document createEmptyDocument()
          Creates a new empty Document.
static java.util.Iterator getChildElementIterator(org.w3c.dom.Node parent)
          Returns an iterator over the child elements of the specified node.
static java.lang.String getTextContent(org.w3c.dom.Node node)
          Gets the text content below of the specified node.
static java.lang.String nodeToString(org.w3c.dom.Node node)
          Converts anything that implements Node into a String.
static java.lang.String removeComments(java.lang.String xml)
          Removes any XML comments from the specified XML string.
static java.lang.String removePrologue(java.lang.String xml)
          Removes the XML prologue from the beginning of an XML string.
static void xmlDOMToFile(org.w3c.dom.Document doc, java.lang.String fileName)
          Puts a DOM Document object into a new file
static java.io.Reader xmlDOMToStream(org.w3c.dom.Document doc)
          Returns a stream to the XML document This method is provided for completeness - it is preferable to use the xmlDOMToStream(Document doc, Writer stream) method as it includes no caching.
static void xmlDOMToStream(org.w3c.dom.DocumentFragment doc, java.io.OutputStream stream)
          Writes the document to the stream
static void xmlDOMToStream(org.w3c.dom.DocumentFragment doc, java.io.Writer stream)
          Writes the document to the stream
static void xmlDOMToStream(org.w3c.dom.Document doc, java.io.OutputStream stream)
          Writes the document to the stream
static void xmlDOMToStream(org.w3c.dom.Document doc, java.io.Writer stream)
          Writes the document to the stream
static void xmlDOMToStream(org.w3c.dom.Element doc, java.io.OutputStream stream)
          Writes the document to the stream
static void xmlDOMToStream(org.w3c.dom.Element doc, java.io.Writer stream)
          Writes the document to the stream
static java.lang.String xmlDOMToString(org.w3c.dom.Document doc)
          Converts a DOM Document object into a string
static java.lang.String xmlDOMToString(org.w3c.dom.DocumentFragment doc)
          Converts a DOM DocumentFragment object into a string
static java.lang.String xmlDOMToString(org.w3c.dom.Element element)
          Converts a DOM Element object into a string
static org.w3c.dom.Document xmlFileToDOM(java.lang.String fileName)
          Converts the XML contents of a file into a DOM Document object
static org.w3c.dom.Document xmlFileToDOM(java.lang.String fileName, boolean validate)
          Converts the XML contents of a file into a DOM Document object
static org.w3c.dom.Document xmlStreamToDOM(java.io.InputStream stream)
          Converts the XML contents of a stream into a DOM Document object
static org.w3c.dom.Document xmlStreamToDOM(java.io.InputStream stream, boolean validate)
          Converts the XML contents of a stream into a DOM Document object
static org.w3c.dom.Document xmlStreamToDOM(java.io.InputStream stream, java.lang.String schemaPath)
          Parses the XML contents of an InputStream into a DOM Document and validates the document against the specified schema.
static org.w3c.dom.Document xmlStringToDOM(java.lang.String xmlString)
          Parses xml in string format into a DOM Document object
static org.w3c.dom.Document xmlStringToDOM(java.lang.String xmlString, boolean validate)
          Parses xml in string format into a DOM Document object
static org.w3c.dom.Document xmlStringToDOM(java.lang.String xml, java.lang.String schemaLocation)
          Parses an XML String into a DOM Document object and validates the document against the specified schema.
static org.w3c.dom.Document xmlURLToDOM(java.net.URL url)
          Converts the XML contents of a file that is retrieved via a URL into a DOM Document object
static org.w3c.dom.Document xmlURLToDOM(java.net.URL url, boolean validate)
          Converts the XML contents of a file that is retrieved via a URL into a DOM Document object
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLUtilities

public XMLUtilities()
Method Detail

xmlURLToDOM

public static org.w3c.dom.Document xmlURLToDOM(java.net.URL url)
                                        throws CommonSystemException,
                                               CommonUserException
Converts the XML contents of a file that is retrieved via a URL into a DOM Document object

Parameters:
url - The URL of the file to convert
Returns:
Document
CommonSystemException
CommonUserException

xmlURLToDOM

public static org.w3c.dom.Document xmlURLToDOM(java.net.URL url,
                                               boolean validate)
                                        throws CommonSystemException,
                                               CommonUserException
Converts the XML contents of a file that is retrieved via a URL into a DOM Document object

Parameters:
url - The URL of the file to convert
validate - A boolean flag to indicate if schema validation is required
Returns:
Document
CommonSystemException
CommonUserException

xmlFileToDOM

public static org.w3c.dom.Document xmlFileToDOM(java.lang.String fileName)
                                         throws CommonSystemException,
                                                CommonUserException
Converts the XML contents of a file into a DOM Document object

Parameters:
fileName - The file to convert
Returns:
Document
CommonSystemException
CommonUserException

xmlFileToDOM

public static org.w3c.dom.Document xmlFileToDOM(java.lang.String fileName,
                                                boolean validate)
                                         throws CommonSystemException,
                                                CommonUserException
Converts the XML contents of a file into a DOM Document object

Parameters:
fileName - The name of the file to convert
validate - A boolean flag to indicate if schema validation is required
Returns:
Document
CommonSystemException
CommonUserException

xmlStringToDOM

public static org.w3c.dom.Document xmlStringToDOM(java.lang.String xmlString)
                                           throws CommonUserException
Parses xml in string format into a DOM Document object

Parameters:
xmlString - java.lang.String The xml in a String object
CommonUserException

xmlStringToDOM

public static org.w3c.dom.Document xmlStringToDOM(java.lang.String xmlString,
                                                  boolean validate)
                                           throws CommonUserException
Parses xml in string format into a DOM Document object

Parameters:
xmlString - java.lang.String The xml in a String object
validate - boolean Whether or not the XML should be validated
CommonUserException

xmlStringToDOM

public static org.w3c.dom.Document xmlStringToDOM(java.lang.String xml,
                                                  java.lang.String schemaLocation)
                                           throws CommonUserException
Parses an XML String into a DOM Document object and validates the document against the specified schema.

Parameters:
xml - a String containing XML data
schemaLocation - the namespace and location of the schema, in the same format as the standard XML xsi:schemaLocation attribute. For example: "http://ogsadai.org.uk/namespaces/2004/11/core/supported_activities schema/ogsadai/xsd/supported_activities.xsd"
CommonUserException

xmlStreamToDOM

public static org.w3c.dom.Document xmlStreamToDOM(java.io.InputStream stream)
                                           throws CommonUserException
Converts the XML contents of a stream into a DOM Document object

Parameters:
stream - The stream to convert
Returns:
Document
CommonUserException

xmlStreamToDOM

public static org.w3c.dom.Document xmlStreamToDOM(java.io.InputStream stream,
                                                  boolean validate)
                                           throws CommonUserException
Converts the XML contents of a stream into a DOM Document object

Parameters:
stream - The stream to convert
validate - A boolean flag to indicate if schema validation is required
Returns:
Document
CommonUserException

xmlStreamToDOM

public static org.w3c.dom.Document xmlStreamToDOM(java.io.InputStream stream,
                                                  java.lang.String schemaPath)
                                           throws CommonUserException
Parses the XML contents of an InputStream into a DOM Document and validates the document against the specified schema.

Parameters:
stream - an InputStream containing XML data
schemaPath - the namespace and location of the schema, in the same format as the standard XML xsi:schemaLocation attribute. For example: "http://ogsadai.org.uk/namespaces/2004/11/core/supported_activities schema/ogsadai/xsd/supported_activities.xsd"
CommonUserException

nodeToString

public static java.lang.String nodeToString(org.w3c.dom.Node node)
                                     throws CommonSystemException
Converts anything that implements Node into a String.

Parameters:
node - The Node to convert.
Returns:
A String representation of the node.
Throws:
CommonSystemException - If there is a problem converting the node.

xmlDOMToString

public static java.lang.String xmlDOMToString(org.w3c.dom.Document doc)
                                       throws CommonSystemException
Converts a DOM Document object into a string

Parameters:
doc - The document to convert
Returns:
String
CommonSystemException

xmlDOMToString

public static java.lang.String xmlDOMToString(org.w3c.dom.DocumentFragment doc)
                                       throws CommonSystemException
Converts a DOM DocumentFragment object into a string

Parameters:
doc - The document to convert
Returns:
String
CommonSystemException

xmlDOMToString

public static java.lang.String xmlDOMToString(org.w3c.dom.Element element)
                                       throws CommonSystemException
Converts a DOM Element object into a string

Parameters:
element - The document to convert
Returns:
String
CommonSystemException

xmlDOMToFile

public static void xmlDOMToFile(org.w3c.dom.Document doc,
                                java.lang.String fileName)
                         throws CommonUserException,
                                CommonSystemException
Puts a DOM Document object into a new file

Parameters:
doc - The document to convert
Returns:
void
CommonUserException
CommonSystemException

xmlDOMToStream

public static java.io.Reader xmlDOMToStream(org.w3c.dom.Document doc)
                                     throws CommonSystemException
Returns a stream to the XML document This method is provided for completeness - it is preferable to use the xmlDOMToStream(Document doc, Writer stream) method as it includes no caching.

Parameters:
doc - The document to convert
Returns:
String
CommonSystemException

xmlDOMToStream

public static void xmlDOMToStream(org.w3c.dom.Document doc,
                                  java.io.Writer stream)
                           throws CommonSystemException
Writes the document to the stream

Parameters:
doc - The document to write
stream - The stream to write to
CommonSystemException

xmlDOMToStream

public static void xmlDOMToStream(org.w3c.dom.DocumentFragment doc,
                                  java.io.Writer stream)
                           throws CommonSystemException
Writes the document to the stream

Parameters:
doc - The document to write
stream - The stream to write to
CommonSystemException

xmlDOMToStream

public static void xmlDOMToStream(org.w3c.dom.Element doc,
                                  java.io.Writer stream)
                           throws CommonSystemException
Writes the document to the stream

Parameters:
doc - The document to write
stream - The stream to write to
CommonSystemException

xmlDOMToStream

public static void xmlDOMToStream(org.w3c.dom.Document doc,
                                  java.io.OutputStream stream)
                           throws CommonSystemException
Writes the document to the stream

Parameters:
doc - The document to write
stream - The stream to write to
CommonSystemException

xmlDOMToStream

public static void xmlDOMToStream(org.w3c.dom.DocumentFragment doc,
                                  java.io.OutputStream stream)
                           throws CommonSystemException
Writes the document to the stream

Parameters:
doc - The document to write
stream - The stream to write to
CommonSystemException

xmlDOMToStream

public static void xmlDOMToStream(org.w3c.dom.Element doc,
                                  java.io.OutputStream stream)
                           throws CommonSystemException
Writes the document to the stream

Parameters:
doc - The document to write
stream - The stream to write to
CommonSystemException

removePrologue

public static java.lang.String removePrologue(java.lang.String xml)
Removes the XML prologue from the beginning of an XML string.

Parameters:
xml - a String containing XML
Returns:
a String containing the same XML data with the XML prologue removed, or unchanged if there is no prologue

removeComments

public static java.lang.String removeComments(java.lang.String xml)
                                       throws CommonSystemException,
                                              CommonUserException
Removes any XML comments from the specified XML string.

Parameters:
xml - a String containing XML
Returns:
a String containing the same XML data with the comments removed
Throws:
CommonUserException
CommonSystemException

getTextContent

public static java.lang.String getTextContent(org.w3c.dom.Node node)
Gets the text content below of the specified node. This is achieved by appending the character data contained in any text or CDATA child nodes. Surrounding whitespace is trimmed and duplicate whitespace removed.

Parameters:
node - the Node
Returns:
a String containing a concatenation of the text children of the specified node, or an empty string if there are no child nodes

getChildElementIterator

public static java.util.Iterator getChildElementIterator(org.w3c.dom.Node parent)
Returns an iterator over the child elements of the specified node.

Parameters:
parent - the parent node
Returns:
java.util.Iterator containing Element objects

createEmptyDocument

public static org.w3c.dom.Document createEmptyDocument()
Creates a new empty Document.