no.uio.edd.utils.xmlutils
Class DomUtils

java.lang.Object
  extended by no.uio.edd.utils.xmlutils.DomUtils

public class DomUtils
extends java.lang.Object

A class of static methods for XML DOM tasks.

Author:
oeide

Constructor Summary
DomUtils()
           
 
Method Summary
static java.lang.String convertToString(org.w3c.dom.Document doc)
          Convert from XML document to String
static java.lang.StringBuilder convertToStringBuffer(org.w3c.dom.Document doc)
          Convert from XML document to StringBuffer
static org.w3c.dom.Element findNamedElementBefore(org.w3c.dom.Node inNode, java.lang.String nodeName)
          Only finds on same sibling level for the time being.
static org.w3c.dom.Element findNamedElementBeforeOrUnder(org.w3c.dom.Node inNode)
          Only look on same sibling level for the time being - dangerous.
static java.lang.String findTextBetween(org.w3c.dom.Node firstNode, org.w3c.dom.Node lastNode)
           
static org.w3c.dom.Node findXmlAttrValue(org.w3c.dom.Node inNode, java.lang.String inAttrName, java.lang.String inAttrValue)
          Searches a subtree for a node with a specific attribute name and value pair.
static ExtendableNodeList getElementsByTagNames(org.w3c.dom.Document doc, java.lang.String[] elementNames)
          Creates a list of nodes based on one or more element names.
static java.lang.String getGmlFooter()
          Creates a GML footer.
static java.lang.String getGmlHeader()
          Create a GML header, with all values set to default.
static java.lang.String getSvgFooter()
          Created a SVG footer.
static java.lang.String getSvgHeader(java.lang.String title)
          Created a SVG header, with some standard values set.
static java.lang.String getSvgHeader(java.lang.String title, int sizeX, int sizeY)
          Created a SVG header, with some standard values set.
static java.lang.String getXhtmlFooter()
          Creates an xhtml footer, from the point where the body element is open.
static java.lang.String getXhtmlHeader(java.lang.String title)
          Creates a xhtml1-strict header, up to the point where the body element is open.
static java.lang.String getXmlHeader()
           
static java.lang.String isErrorReturnMessage(java.lang.String xmlIn)
          Checks if an XML is a error message.
static java.lang.String isErrorReturnXml(java.lang.String xmlIn)
          Checks if an XML is a error message.
static org.w3c.dom.Document parseStringAsXML(java.lang.String inString)
          Parses a string and return an XML DOM tree.
static org.w3c.dom.Document parseStringAsXML(java.lang.String inString, java.lang.String inSchemaFileUrl)
          Parses a string and return an XML DOM tree.
static org.w3c.dom.Document parseXMLFile(java.lang.String inFilePath)
          Parses a file and return an XML DOM tree.
static org.w3c.dom.Document parseXMLFile(java.lang.String inFilePath, java.lang.String inSchemaFilePath)
          Parses a file using an XML schema and return an XML DOM tree.
static org.w3c.dom.Document parseXMLUrl(java.lang.String inFileUrl)
          Parses a file and return an XML DOM tree.
static org.w3c.dom.Document parseXMLUrl(java.lang.String inFileUrl, java.lang.String inSchemaFileUrl)
          Parses a file using an XML schema and return an XML DOM tree.
static void removeAllChildren(org.w3c.dom.Element parent)
          Removes all children of all types from this element.
static org.w3c.dom.Node removeNamedNode(org.w3c.dom.Node rootNode, java.lang.String elementName)
          Removes all nodes in the subtree with the specified element name.
static java.lang.String xmlFragment(org.w3c.dom.Node def)
          Creates an xml string including the input node and all siblings.
static java.lang.String xmlNodeContents(org.w3c.dom.Node encapsule)
          Returns all the contents in one specific node, including tags.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DomUtils

public DomUtils()
Method Detail

convertToStringBuffer

public static java.lang.StringBuilder convertToStringBuffer(org.w3c.dom.Document doc)
Convert from XML document to StringBuffer

Parameters:
doc - An xmlParser document
Returns:
a StringBuffer version of the XML document. Null on failure.

convertToString

public static java.lang.String convertToString(org.w3c.dom.Document doc)
Convert from XML document to String

Parameters:
doc - An xmlParser document
Returns:
a String version of the XML document

parseStringAsXML

public static org.w3c.dom.Document parseStringAsXML(java.lang.String inString)
Parses a string and return an XML DOM tree. No error messages on failure, well-formdness checking should be done before this function is calles, if necessary.

Parameters:
inString - an XML document expresses as a string.
Returns:
The DOM Document if successfull. If something went wrong, null is returned.

parseStringAsXML

public static org.w3c.dom.Document parseStringAsXML(java.lang.String inString,
                                                    java.lang.String inSchemaFileUrl)
Parses a string and return an XML DOM tree. No error messages on failure, well-formdness checking should be done before this function is calles, if necessary.

Parameters:
inString - an XML document expresses as a string.
inSchemaFileUrl - Path to an XML schema used in the parsing.
Returns:
The DOM Document if successfull. If something went wrong, null is returned.

parseXMLFile

public static org.w3c.dom.Document parseXMLFile(java.lang.String inFilePath)
Parses a file and return an XML DOM tree. No error messages on failure, well-formedness checking should be done before this function is called, if necessary.

Parameters:
inFilePath - Path to an XML document expresses as a string.
Returns:
The DOM Document if successful. If something went wrong, null is returned.

parseXMLUrl

public static org.w3c.dom.Document parseXMLUrl(java.lang.String inFileUrl,
                                               java.lang.String inSchemaFileUrl)
Parses a file using an XML schema and return an XML DOM tree. No error messages on failure, validation should be done before this function is called, if detailed output is necessary.

Parameters:
inFileUrl - URL to an XML document expresses as a string.
inSchemaFileUrl - Path to an XML schema used in the parsing.
Returns:
The DOM Document if successfull. If something went wrong, null is returned.

parseXMLUrl

public static org.w3c.dom.Document parseXMLUrl(java.lang.String inFileUrl)
Parses a file and return an XML DOM tree. No error messages on failure, validation should be done before this function is called, if detailed output is necessary.

Parameters:
inFileUrl - URL to an XML document expresses as a string.
Returns:
The DOM Document if successful. If something went wrong, null is returned.

parseXMLFile

public static org.w3c.dom.Document parseXMLFile(java.lang.String inFilePath,
                                                java.lang.String inSchemaFilePath)
Parses a file using an XML schema and return an XML DOM tree. No error messages on failure, validation should be done before this function is called, if detailed output is necessary.

Parameters:
inFilePath - Path to an XML document expresses as a string.
inSchemaFilePath - Path to an XML schema used in the parsing.
Returns:
The DOM Document if successfull. If something went wrong, null is returned.

xmlNodeContents

public static java.lang.String xmlNodeContents(org.w3c.dom.Node encapsule)
                                        throws java.io.IOException
Returns all the contents in one specific node, including tags.

Parameters:
encapsule - The root node
Returns:
XML-fragment The XML fragment string.
Throws:
java.io.IOException

xmlFragment

public static java.lang.String xmlFragment(org.w3c.dom.Node def)
                                    throws java.io.IOException
Creates an xml string including the input node and all siblings. To get all contents of a node, send in the first child.

Parameters:
def - The input node
Returns:
XML-fragment The xml fragment string.
Throws:
java.io.IOException

findXmlAttrValue

public static org.w3c.dom.Node findXmlAttrValue(org.w3c.dom.Node inNode,
                                                java.lang.String inAttrName,
                                                java.lang.String inAttrValue)
Searches a subtree for a node with a specific attribute name and value pair.

Parameters:
inNode - the root node of the subtree to be searched.
inAttrName - the name of the attribute.
inAttrValue - the value of the attribute.
Returns:
the node in which the attribute name-value pair was found. If not found, returns null. On multiple hits, returns one of them; which one is undetermined.

isErrorReturnXml

public static java.lang.String isErrorReturnXml(java.lang.String xmlIn)
Checks if an XML is a error message.

Parameters:
xmlIn - the XML
Returns:
null if not an error, otherwise the first error message XML fragment.

isErrorReturnMessage

public static java.lang.String isErrorReturnMessage(java.lang.String xmlIn)
Checks if an XML is a error message.

Parameters:
xmlIn - the XML
Returns:
null if not an error, otherwise the first error message as a string based on the expl element, if it exist. Otherwise, return the full XML.

removeNamedNode

public static org.w3c.dom.Node removeNamedNode(org.w3c.dom.Node rootNode,
                                               java.lang.String elementName)
Removes all nodes in the subtree with the specified element name.

Parameters:
rootNode - the root node of the subtree.
elementName - the element name for the elements to be removed.
Returns:
the reduced subtree.

removeAllChildren

public static void removeAllChildren(org.w3c.dom.Element parent)
Removes all children of all types from this element.

Parameters:
parent - The partent from which all children will be removed.

findNamedElementBefore

public static org.w3c.dom.Element findNamedElementBefore(org.w3c.dom.Node inNode,
                                                         java.lang.String nodeName)
Only finds on same sibling level for the time being.

Parameters:
inNode -
nodeName -
Returns:

findNamedElementBeforeOrUnder

public static org.w3c.dom.Element findNamedElementBeforeOrUnder(org.w3c.dom.Node inNode)
Only look on same sibling level for the time being - dangerous.

Parameters:
inNode -
Returns:

findTextBetween

public static java.lang.String findTextBetween(org.w3c.dom.Node firstNode,
                                               org.w3c.dom.Node lastNode)

getXhtmlHeader

public static java.lang.String getXhtmlHeader(java.lang.String title)
Creates a xhtml1-strict header, up to the point where the body element is open.

Parameters:
title - The title to be set in the head element.
Returns:
The xhtml header.

getXhtmlFooter

public static java.lang.String getXhtmlFooter()
Creates an xhtml footer, from the point where the body element is open.

Returns:
The xhtml footer.

getSvgHeader

public static java.lang.String getSvgHeader(java.lang.String title,
                                            int sizeX,
                                            int sizeY)
Created a SVG header, with some standard values set.

Parameters:
title - The title of the SVG document.
sizeX - Size of canvas.
sizeY - Size of canvas.
Returns:
The new SVG header.

getSvgHeader

public static java.lang.String getSvgHeader(java.lang.String title)
Created a SVG header, with some standard values set.

Parameters:
title - The title of the SVG document.
Returns:
The new SVG header.

getSvgFooter

public static java.lang.String getSvgFooter()
Created a SVG footer.

Returns:
The SVG footer.

getGmlHeader

public static java.lang.String getGmlHeader()
Create a GML header, with all values set to default.

Returns:
The new GML header.

getGmlFooter

public static java.lang.String getGmlFooter()
Creates a GML footer.

Returns:
The GML footer.

getXmlHeader

public static java.lang.String getXmlHeader()
Returns:
A standard XML 1.0 header for UTF-8 encoded documents.

getElementsByTagNames

public static ExtendableNodeList getElementsByTagNames(org.w3c.dom.Document doc,
                                                       java.lang.String[] elementNames)
Creates a list of nodes based on one or more element names. Thus, all Nodes in the list are actually Elements. The output is expressed as an ExtendableObjectList in which all elements are Elements (and also, of course, Nodes).

Parameters:
doc - The DOM document to look for nodes in.
elementNames - The list of names to look for
Returns:
All the found Elements.