ch.javasoft.xml.config
Class XmlUtil

java.lang.Object
  extended by ch.javasoft.xml.config.XmlUtil

public class XmlUtil
extends Object


Method Summary
static void checkExpectedAttributeValue(Element element, XmlNode attribute, String expectedValue)
          Check whether the given element has the desired value for a specific attribute.
static void checkExpectedElementName(Element element, XmlNode expected)
          Check whether the given element has the desired name.
static Element getChildElementByAttributeValue(Element element, XmlNode child, XmlNode attribute, String attributeValue, boolean throwExceptionIfNull)
          Returns the child element of the given type which has the desired attribute value in the given attribute.
static Iterator<Element> getChildElements(Element element, XmlNode childType)
          Returns a typed iterator for child elements of a certain type
static String getElementPath(Element elem, boolean recurseParents)
          Returns an xpath like string for the given xml element
static String getNodePath(Node node, boolean recurseParents)
          Returns an xpath like string for the given xml node
static String getOptionalAttributeValue(Element element, XmlNode attribute, String defaultValue)
          Returns the desired attribute value, or the given default value if no such attribute exists.
static Element getOptionalSingleChildElement(Element element, XmlNode child)
          Returns the desired single child element, or throws an exception if none or multiple are found.
static String getRequiredAttributeValue(Element element, XmlNode attribute)
          Returns the desired attribute value, or throws an exception no such attribute exists.
static Element getRequiredSingleChildElement(Element element, XmlNode child)
          Returns the desired single child element, or throws an exception if none or multiple are found.
static boolean isExpectedElementName(Element element, XmlNode expected)
          Returns true if the given element has the desired name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getChildElementByAttributeValue

public static Element getChildElementByAttributeValue(Element element,
                                                      XmlNode child,
                                                      XmlNode attribute,
                                                      String attributeValue,
                                                      boolean throwExceptionIfNull)
                                               throws XmlConfigException
Returns the child element of the given type which has the desired attribute value in the given attribute. In xpath, this would be somewhat like /child[@attribute=$attributeValue]

If multiple children match the criteria, an exception is thrown. If no child matching the criteria is found, then either null is returned (if throwExceptionIfNull==false), or an exception is thrown (otherwise).

Parameters:
element - the parent element containing the desired child element
child - specifies the name of the child element
attribute - specifies the name of the identifying attribute
attributeValue - the desired attribute value
throwExceptionIfNull - if true, an exception is thrown if no child matches the criteria
Returns:
the desired child element, or null if throwExceptionIfNull==false and no child element matched the criteria
Throws:
XmlConfigException - If multiple matches occur, or if no match was found and exception throwing is desired for this case (see throwExceptionIfNull)

getChildElements

public static Iterator<Element> getChildElements(Element element,
                                                 XmlNode childType)
                                          throws XmlConfigException
Returns a typed iterator for child elements of a certain type

Parameters:
element - parent of which certain child elements are desired
childType - node defining the name of the desired child elements
Throws:
XmlConfigException

getRequiredSingleChildElement

public static Element getRequiredSingleChildElement(Element element,
                                                    XmlNode child)
                                             throws XmlConfigException
Returns the desired single child element, or throws an exception if none or multiple are found.

Parameters:
element - the parent of the desired child
child - specifies the desired child element name
Returns:
the child element
Throws:
XmlConfigException - if no or multiple children match the criteria

getOptionalSingleChildElement

public static Element getOptionalSingleChildElement(Element element,
                                                    XmlNode child)
                                             throws XmlConfigException
Returns the desired single child element, or throws an exception if none or multiple are found.

Parameters:
element - the parent of the desired child
child - specifies the desired child element name
Returns:
the child element
Throws:
XmlConfigException - if no or multiple children match the criteria

getRequiredAttributeValue

public static String getRequiredAttributeValue(Element element,
                                               XmlNode attribute)
                                        throws XmlConfigException
Returns the desired attribute value, or throws an exception no such attribute exists.

Parameters:
element - the element to get the attribute value from
attribute - specifies the desired attribute name
Returns:
the attribute value
Throws:
XmlConfigException - if no such attribute value is defined

getOptionalAttributeValue

public static String getOptionalAttributeValue(Element element,
                                               XmlNode attribute,
                                               String defaultValue)
                                        throws XmlConfigException
Returns the desired attribute value, or the given default value if no such attribute exists.

Parameters:
element - the element to get the attribute value from
attribute - specifies the desired attribute name
defaultValue - the default value to use if no such attribute exists
Returns:
the attribute value, or the default if no such attribute exists
Throws:
XmlConfigException

checkExpectedElementName

public static void checkExpectedElementName(Element element,
                                            XmlNode expected)
                                     throws XmlConfigException
Check whether the given element has the desired name. Throws an exception otherwise.

Parameters:
element - the element to check
expected - specifies the expected element name
Throws:
XmlConfigException - if the check fails

isExpectedElementName

public static boolean isExpectedElementName(Element element,
                                            XmlNode expected)
Returns true if the given element has the desired name.

Parameters:
element - the element to check
expected - specifies the expected element name
Returns:
true if the check is successful

checkExpectedAttributeValue

public static void checkExpectedAttributeValue(Element element,
                                               XmlNode attribute,
                                               String expectedValue)
                                        throws XmlConfigException
Check whether the given element has the desired value for a specific attribute. Throws an exception otherwise.

Parameters:
element - the element to check
attribute - specifies the attribute name
expectedValue - the desired attribute value
Throws:
XmlConfigException - if the check fails

getNodePath

public static String getNodePath(Node node,
                                 boolean recurseParents)
Returns an xpath like string for the given xml node

Parameters:
node - the node to convert to a string
recurseParents - true if parents should be included
Returns:
an xpath like string for the given node

getElementPath

public static String getElementPath(Element elem,
                                    boolean recurseParents)
Returns an xpath like string for the given xml element

Parameters:
elem - the element to convert to a string
recurseParents - true if parents should be included
Returns:
an xpath like string for the given element