org.ibisph.util
Class XMLLib

java.lang.Object
  extended by org.ibisph.util.XMLLib

public abstract class XMLLib
extends java.lang.Object

XML helper methods to create and manipulate XML Documents and Elements.

Author:
Garth Braithwaite, STG

Constructor Summary
XMLLib()
           
 
Method Summary
static void addElement(org.dom4j.Document document, java.lang.String parentElementXPath, org.dom4j.Element element)
          Adds the passed element to the container using an XPath expression to select the parent container of where to add the element to.
static boolean deleteNode(org.dom4j.Node node)
          Simple helper that provides a localized way to delete a single node.
static boolean deleteNodes(java.util.List nodeList)
          Simple helper that provides a localized way to delete a node list.
static org.dom4j.Node getNode(org.dom4j.Node currentNode, java.lang.String xPath)
          Simple helper that provides a localized way to get a node via an XPath expression.
static java.lang.String getText(org.dom4j.Document document, java.lang.String xPath)
          Simple overloaded helper that provides a localized way to get a document's text via an XPath expression.
static java.lang.String getText(org.dom4j.Document document, java.lang.String xPath, java.lang.String defaultValue)
          Simple overloaded helper that provides a localized way to get a document's text via an XPath expression with a default fall back value.
static java.lang.String getText(org.dom4j.Element element, java.lang.String xPath)
          Simple overloaded helper that provides a localized way to get an element's text via an XPath expression.
static java.lang.String getText(org.dom4j.Element element, java.lang.String xPath, java.lang.String defaultValue)
          Simple overloaded helper that provides a localized way to get an element's text via an XPath expression with a default fall back value.
static org.dom4j.Element newCDATAElement(java.lang.String elementName, java.lang.String text)
          Creates a new CDATA element and sets the text as CDATA.
static org.dom4j.Document newDocument()
           
static org.dom4j.Document newDocument(java.lang.String rootElementName)
          Creates a new document with a root element.
static org.dom4j.Element newElement(java.lang.String elementName)
          Creates a new element.
static org.dom4j.Element newElement(java.lang.String elementName, java.lang.Object object)
          Creates a new element and sets the element's text to the object.toString.
static org.dom4j.Element newElement(java.lang.String elementName, java.lang.String textValue)
          Creates a new element and sets the element's text to the textValue.
static void setElement(org.dom4j.Document document, java.lang.String parentElementXPath, org.dom4j.Element element)
          Selects a parent container element and sets the passed element to the container.
static void setElementCDATAText(org.dom4j.Document document, java.lang.String parentElementXPath, java.lang.String elementName, java.lang.String text)
          Sets/replaces the existing element with a new CDATA text element.
static void setElementText(org.dom4j.Document document, java.lang.String parentElementXPath, java.lang.String elementName, java.lang.String text)
          Sets an element's text with the new text.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLLib

public XMLLib()
Method Detail

newDocument

public static org.dom4j.Document newDocument()
Returns:
Creates a new blank document.

newDocument

public static org.dom4j.Document newDocument(java.lang.String rootElementName)
Creates a new document with a root element.

Parameters:
rootElementName - Name of the root element.
Returns:
New document with a root element named rootElementName.

newElement

public static org.dom4j.Element newElement(java.lang.String elementName)
Creates a new element.

Parameters:
elementName - Name of the element to be created.
Returns:
New element named elementName.

newElement

public static org.dom4j.Element newElement(java.lang.String elementName,
                                           java.lang.Object object)
Creates a new element and sets the element's text to the object.toString.

Parameters:
elementName - Name of the element to be created.
object - Object who's text will be added to the element's text.
Returns:
New element named elementName containing the object's text.

newElement

public static org.dom4j.Element newElement(java.lang.String elementName,
                                           java.lang.String textValue)
Creates a new element and sets the element's text to the textValue.

Parameters:
elementName - Name of the element to be created.
textValue - Text that will be added to the element's text.
Returns:
New element named elementName containing the textValue.

newCDATAElement

public static org.dom4j.Element newCDATAElement(java.lang.String elementName,
                                                java.lang.String text)
Creates a new CDATA element and sets the text as CDATA.

Parameters:
elementName - Name of the element to be created.
text - Text that will be added to the element's CDATA text.
Returns:
New CDATA element named elementName containing the text.

getNode

public static org.dom4j.Node getNode(org.dom4j.Node currentNode,
                                     java.lang.String xPath)
Simple helper that provides a localized way to get a node via an XPath expression.

Parameters:
currentNode - Document/element/node to retrieve node from.
xPath - Text that contains an XPath expression to be used to select/specify the desired node.
Returns:
First, single node that matches the XPath expression.

getText

public static java.lang.String getText(org.dom4j.Document document,
                                       java.lang.String xPath,
                                       java.lang.String defaultValue)
Simple overloaded helper that provides a localized way to get a document's text via an XPath expression with a default fall back value.

Parameters:
document - Document to retrieve node's text from.
xPath - Text that contains an XPath expression to be used to select/specify the desired node's text.
defaultValue - Value returned if the XPath expression returns a null result.
Returns:
First, single node's text that matches the XPath expression.

getText

public static java.lang.String getText(org.dom4j.Document document,
                                       java.lang.String xPath)
Simple overloaded helper that provides a localized way to get a document's text via an XPath expression.

Parameters:
document - Document to retrieve node's text from.
xPath - Text that contains an XPath expression to be used to select/specify the desired node's text.
Returns:
First, single node's text that matches the XPath expression. If nothing found/matches then null is returned.

getText

public static java.lang.String getText(org.dom4j.Element element,
                                       java.lang.String xPath,
                                       java.lang.String defaultValue)
Simple overloaded helper that provides a localized way to get an element's text via an XPath expression with a default fall back value.

Parameters:
element - element to retrieve node's text from.
xPath - Text that contains an XPath expression to be used to select/specify the desired node's text.
defaultValue - Value returned if the XPath expression returns a null result.
Returns:
First, single node's text that matches the XPath expression.

getText

public static java.lang.String getText(org.dom4j.Element element,
                                       java.lang.String xPath)
Simple overloaded helper that provides a localized way to get an element's text via an XPath expression.

Parameters:
element - element to retrieve node's text from.
xPath - Text that contains an XPath expression to be used to select/specify the desired node's text.
Returns:
First, single node's text that matches the XPath expression. If nothing found/matches then null is returned.

deleteNode

public static boolean deleteNode(org.dom4j.Node node)
Simple helper that provides a localized way to delete a single node.

Parameters:
node - node to be deleted.
Returns:
true if node was successfully detached, otherwise false.

deleteNodes

public static boolean deleteNodes(java.util.List nodeList)
Simple helper that provides a localized way to delete a node list.

Parameters:
nodeList - List of nodes to be deleted.
Returns:
true if nodes were successfully detached, otherwise false.

addElement

public static void addElement(org.dom4j.Document document,
                              java.lang.String parentElementXPath,
                              org.dom4j.Element element)
Adds the passed element to the container using an XPath expression to select the parent container of where to add the element to.

Parameters:
document - Source to add the element to.
parentElementXPath - XPath that selects the parent container to have the element added to.
element - Actual element to add into the document.

setElement

public static void setElement(org.dom4j.Document document,
                              java.lang.String parentElementXPath,
                              org.dom4j.Element element)
Selects a parent container element and sets the passed element to the container. Any existing elements with the same name are automatically deleted first then the element is added.

Parameters:
document - Source to add the element to.
parentElementXPath - XPath that selects the parent container to have the element added to.
element - Actual element to add into the document.

setElementCDATAText

public static void setElementCDATAText(org.dom4j.Document document,
                                       java.lang.String parentElementXPath,
                                       java.lang.String elementName,
                                       java.lang.String text)
Sets/replaces the existing element with a new CDATA text element. If an element already exists with that name it is removed first. A new CDATA text element is then created and added to the parent container.

Parameters:
document - Source to add the element to.
parentElementXPath - XPath that selects the parent container to have the element added to.
elementName - Name of the element to be added.
text - Value to set within the element as CDATA.

setElementText

public static void setElementText(org.dom4j.Document document,
                                  java.lang.String parentElementXPath,
                                  java.lang.String elementName,
                                  java.lang.String text)
Sets an element's text with the new text. If element does not exist, then the element is created and added to the parent container.

Parameters:
document - Source to add the element to.
parentElementXPath - XPath that selects the parent container to have the element added to.
elementName - Name of the element to have its text set.
text - Value to set the element's text to.