uk.org.ogsadai.activity.xmldb
Class ElementParser

java.lang.Object
  |
  +--uk.org.ogsadai.activity.xmldb.ElementParser

public class ElementParser
extends java.lang.Object

This class contains helper methods for parsing activity elements and throwing useful UserExceptions if they are invalid.

Author:
The OGSA-DAI Project Team

Constructor Summary
ElementParser()
           
 
Method Summary
static org.w3c.dom.Element getChildElement(org.w3c.dom.Element parent, java.lang.String childName)
          Gets the first named child element of the specified parent.
static org.w3c.dom.Element getChildElement(org.w3c.dom.Element parent, java.lang.String namespaceURI, java.lang.String childName)
          Gets the named child element of the specified parent.
static java.lang.String getChildElementAttribute(org.w3c.dom.Element parent, java.lang.String childName, java.lang.String attributeName)
          Gets the named attribute of the named child element of the specified parent.
static java.util.List getChildElements(org.w3c.dom.Element parent)
          Gets a list of all the child elements of the specified parent.
static java.lang.String getChildText(org.w3c.dom.Element parent, java.lang.String childName)
          Gets the concatenated text children, trimmed of surrounding whitespace, of the named child element of the specified parent.
static java.lang.String getChildText(org.w3c.dom.Node parent)
          Gets a text representation of the children of the specified parent Node trimmed of any surrounding whitespace.
static java.lang.String parseAttribute(org.w3c.dom.Element element, java.lang.String attributeName, boolean nonEmpty)
          Parses the value of an attribute of an element.
static java.lang.String parseChildElementAttribute(org.w3c.dom.Element parent, java.lang.String childName, java.lang.String attributeName, boolean required, boolean nonEmpty)
          Parses the value of an attribute of a child element.
static java.util.List parseChildElements(org.w3c.dom.Element parent, int number)
          Parses the child elements of the specified parent.
static java.lang.String parseChildElementValue(org.w3c.dom.Element parent, java.lang.String childName, boolean required, boolean nonEmpty)
          Parses the value of a child element of the specified parent.
static java.lang.String parseElementValue(org.w3c.dom.Element element, boolean nonEmpty)
          Parses the specified element and returns its value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ElementParser

public ElementParser()
Method Detail

parseElementValue

public static java.lang.String parseElementValue(org.w3c.dom.Element element,
                                                 boolean nonEmpty)
                                          throws CommonUserException,
                                                 CommonSystemException
Parses the specified element and returns its value. The nonEmpty flag can be used to control the parsing validation.

Parameters:
element - The Element containing the value.
nonEmpty - Set to true if element must contain a value, otherwise false.
Returns:
The String value. If nonEmpty is set to false then this may be null.
Throws:
CommonUserException - If the value should not be empty but is.
CommonSystemException - If there is a system problem parsing the value.

parseChildElementValue

public static java.lang.String parseChildElementValue(org.w3c.dom.Element parent,
                                                      java.lang.String childName,
                                                      boolean required,
                                                      boolean nonEmpty)
                                               throws CommonUserException,
                                                      CommonSystemException
Parses the value of a child element of the specified parent. The required and nonEmpty flags should be set to control the validation during the parsing.

Parameters:
parent - The parent Element that may contain the child.
childName - The name of the child element.
required - Set to true if the child element is required, false otherwise.
nonEmpty - Set to true if the value of the child element (if it exists) can not be empty, false otherwise.
Throws:
CommonUserException - If the parsing validation is not satisfied.
CommonSystemException - If there is a system problem while parsing.

parseAttribute

public static java.lang.String parseAttribute(org.w3c.dom.Element element,
                                              java.lang.String attributeName,
                                              boolean nonEmpty)
                                       throws CommonUserException
Parses the value of an attribute of an element.

Parameters:
element - The Element containing the attribute.
attributeName - The name of the attribute.
nonEmpty - Set to true if the attribute requires a value, false otherwise.
Throws:
CommonUserException - If the parsing validation is not satisfied.

parseChildElementAttribute

public static java.lang.String parseChildElementAttribute(org.w3c.dom.Element parent,
                                                          java.lang.String childName,
                                                          java.lang.String attributeName,
                                                          boolean required,
                                                          boolean nonEmpty)
                                                   throws CommonUserException
Parses the value of an attribute of a child element.

Parameters:
parent - The parent Element that may contain the child.
childName - The name of the child Element.
attributeName - The name of the attribute of the child.
required - Set to true if this child element is required, false otherwise.
nonEmpty - Set to true if the value of the attribute of the child element (if the child element exists) can not be empty, false otherwise.
Throws:
CommonUserException - If the parsing validation is not satisfied.

parseChildElements

public static java.util.List parseChildElements(org.w3c.dom.Element parent,
                                                int number)
                                         throws CommonUserException
Parses the child elements of the specified parent.

Parameters:
parent - The parent Element containing the children.
number - The number of child elements that there must be.
Throws:
CommonUserException - If the parsing validation is not satisfied.

getChildElements

public static java.util.List getChildElements(org.w3c.dom.Element parent)
Gets a list of all the child elements of the specified parent.

Parameters:
parent - The Element containing the children.
Returns:
a List of Element objects.

getChildElement

public static org.w3c.dom.Element getChildElement(org.w3c.dom.Element parent,
                                                  java.lang.String childName)
Gets the first named child element of the specified parent. If there is more than one child element with this name, the first one is returned.

Parameters:
parent - The parent Element.
childName - The name of the child element.
Returns:
An Element or null if the child does not exist.

getChildElement

public static org.w3c.dom.Element getChildElement(org.w3c.dom.Element parent,
                                                  java.lang.String namespaceURI,
                                                  java.lang.String childName)
Gets the named child element of the specified parent. If there is more than one child element with this name, then the first one is returned.

Parameters:
parent - The parent Element.
namespaceURI - The URI for the namespace of the chile element.
childName - The name of the child element.
Returns:
An Element or null if the child does not exist.

getChildElementAttribute

public static java.lang.String getChildElementAttribute(org.w3c.dom.Element parent,
                                                        java.lang.String childName,
                                                        java.lang.String attributeName)
Gets the named attribute of the named child element of the specified parent. If there is more than one child element with this name, then the attribute value of the first one is returned.

Parameters:
parent - The parent Element.
childName - The name of the child element.
attributeName - The name of the attribute of the child.
Returns:
The attribute value of the child or null if the child does not exist.

getChildText

public static java.lang.String getChildText(org.w3c.dom.Element parent,
                                            java.lang.String childName)
                                     throws CommonSystemException
Gets the concatenated text children, trimmed of surrounding whitespace, of the named child element of the specified parent.

Parameters:
parent - The parent Element.
childName - The name of the child element.
Returns:
A String containing the concatenated text children, trimmed of surrounding white space, or null if the parent does not contain the specified child.
CommonSystemException

getChildText

public static java.lang.String getChildText(org.w3c.dom.Node parent)
                                     throws CommonSystemException
Gets a text representation of the children of the specified parent Node trimmed of any surrounding whitespace. This is formed by converting any child elements into strings and concatenating them with any additional text or CDATA children.

Parameters:
parent - The parent containing the text or CDATA children.
Returns:
A String representation of the children. This could be an empty String.
Throws:
CommonSystemException - If there is a system problem parsing the parent node.