net.sf.doolin.util.xml
Class DOMUtils

java.lang.Object
  extended by net.sf.doolin.util.xml.DOMUtils

public class DOMUtils
extends Object

Methods for exploring a DOM tree. This class, for performances reasons, does not use any XPath expression.

Author:
Damien Coraboeuf

Method Summary
static String getAttribute(Element eNode, String attName, boolean mandatory, String defaultValue)
          Get the text of an attribute.
static String getAttribute(Element eNode, String attName, String defaultValue)
          Get the text of an attribute.
static boolean getBoolean(Element eParent, String childName, boolean mandatory, boolean defaultValue)
          Get the text of a child element as a boolean.
static boolean getBooleanAttribute(Element eNode, String attName, boolean mandatory, boolean defaultValue)
          Get the text of an attribute as a boolean.
static
<T> Class<T>
getClassAttribute(Element eNode, String attName, Class<T> defaultClass)
          Get a type attribute from an element.
static Element getElement(Element eParent, String childName)
          Get a child element.
static Element getElement(Element eParent, String namespaceURI, String childName)
          Get a child element.
static List<Element> getElements(Element eParent)
          List of all child elements
static List<Element> getElements(Element eParent, String name)
          List of childrens with a given tag name and namespace
static List<Element> getElements(Element eParent, String namespaceURI, String name)
          List of childrens with a given tag name.
static Element getFirstChildElement(Element eParent)
          Get the first element child of an element.
static int getInt(Element eParent, String childName, boolean mandatory, int defaultValue)
          Get the text of a child element as an integer.
static int getIntAttribute(Element eNode, String attName, boolean mandatory, int defaultValue)
          Get the text of an attribute as an integer.
static String getText(Element eNode)
          Get the text contained by an element.
static String getText(Element eParent, String childName)
          Get the text from an child under an element.
static String getText(Element eParent, String namespaceURI, String childName)
          Get the text from an child under an element.
static String getText(Node eNode)
          Get the text contained by an element.
static String getXPath(Node node)
          Gets the XPath for a node
static boolean isNameEqual(Element e, String name)
          This method compares the name (either local name or tag name) of an element
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getAttribute

public static String getAttribute(Element eNode,
                                  String attName,
                                  boolean mandatory,
                                  String defaultValue)
Get the text of an attribute.

Parameters:
eNode - Parent element
attName - Attribute name
mandatory - true if the value is required
defaultValue - Default value to use when the value is not defined
Returns:
Text of the attribute.

getAttribute

public static String getAttribute(Element eNode,
                                  String attName,
                                  String defaultValue)
Get the text of an attribute.

Parameters:
eNode - Parent element
attName - Attribute name
defaultValue - Default value to use when the value is not defined
Returns:
Text of the attribute.

getBoolean

public static boolean getBoolean(Element eParent,
                                 String childName,
                                 boolean mandatory,
                                 boolean defaultValue)
Get the text of a child element as a boolean.

Parameters:
eParent - Parent element
childName - Child element name
mandatory - true if the value is required
defaultValue - Default value to use when the value is not defined (and not mandatory).
Returns:
Text of a child element as a boolean.

getBooleanAttribute

public static boolean getBooleanAttribute(Element eNode,
                                          String attName,
                                          boolean mandatory,
                                          boolean defaultValue)
Get the text of an attribute as a boolean.

Parameters:
eNode - Parent element
attName - Attribute name
mandatory - true if the value is required
defaultValue - Default value to use when the value is not defined (and not mandatory).
Returns:
Text of the attribute as a boolean.

getClassAttribute

public static <T> Class<T> getClassAttribute(Element eNode,
                                             String attName,
                                             Class<T> defaultClass)
Get a type attribute from an element.

Type Parameters:
T - Expected type of the class
Parameters:
eNode - Element to get the attribute from
attName - Name of the attribute to get
defaultClass - Default class to return if the attribute is not defined
Returns:
A class

getElement

public static Element getElement(Element eParent,
                                 String childName)
Get a child element. If several elements exist with the same name, only the first one is returned.

Parameters:
eParent - Parent element
childName - Name of the child element to get (may be qualified)
Returns:
Child element or null if not found.

getElement

public static Element getElement(Element eParent,
                                 String namespaceURI,
                                 String childName)
Get a child element. If several elements exist with the same name, only the first one is returned.

Parameters:
eParent - Parent element
namespaceURI - Namespace URI for the child name
childName - Name of the child element to get (may be qualified)
Returns:
Child element or null if not found.

getElements

public static List<Element> getElements(Element eParent)
List of all child elements

Parameters:
eParent - Parent element
Returns:
List of children (not null but can be empty)

getElements

public static List<Element> getElements(Element eParent,
                                        String name)
List of childrens with a given tag name and namespace

Parameters:
eParent - Parent element
name - Name of the children to collect
Returns:
List of children (not null but can be empty)

getElements

public static List<Element> getElements(Element eParent,
                                        String namespaceURI,
                                        String name)
List of childrens with a given tag name.

Parameters:
eParent - Parent element
namespaceURI - Namespace URI for the child name
name - Name of the children to collect
Returns:
List of children (not null but can be empty)

getFirstChildElement

public static Element getFirstChildElement(Element eParent)
Get the first element child of an element.

Parameters:
eParent - Parent element
Returns:
Child element or null if there is no child element.

getInt

public static int getInt(Element eParent,
                         String childName,
                         boolean mandatory,
                         int defaultValue)
Get the text of a child element as an integer.

Parameters:
eParent - Parent element
childName - Child element name
mandatory - true if the value is required
defaultValue - Default value to use when the value is not defined (and not mandatory).
Returns:
Text of a child element as an integer.

getIntAttribute

public static int getIntAttribute(Element eNode,
                                  String attName,
                                  boolean mandatory,
                                  int defaultValue)
Get the text of an attribute as an integer.

Parameters:
eNode - Parent element
attName - Attribute name
mandatory - true if the value is required
defaultValue - Default value to use when the value is not defined (and not mandatory).
Returns:
Text of the attribute as an integer.

getText

public static String getText(Element eNode)
Get the text contained by an element.

Parameters:
eNode - Element to get the text from.
Returns:
Contained text. null if no text has been found.

getText

public static String getText(Element eParent,
                             String childName)
Get the text from an child under an element.

Parameters:
eParent - Parent element
childName - Name of the child element (may be qualified)
Returns:
Text of the child element or null if the child element does not exist or if it doesn't contain any text.

getText

public static String getText(Element eParent,
                             String namespaceURI,
                             String childName)
Get the text from an child under an element.

Parameters:
eParent - Parent element
namespaceURI - Namespace URI for the child name
childName - Name of the child element (not qualified)
Returns:
Text of the child element or null if the child element does not exist or if it doesn't contain any text.

getText

public static String getText(Node eNode)
Get the text contained by an element.

Parameters:
eNode - Element to get the text from.
Returns:
Contained text. null if no text has been found.

getXPath

public static String getXPath(Node node)
Gets the XPath for a node

Parameters:
node - Node to get the XPath to.
Returns:
XPath from the document root to the given node

isNameEqual

public static boolean isNameEqual(Element e,
                                  String name)
This method compares the name (either local name or tag name) of an element

Parameters:
e - Element
name - Name compared to
Returns:
Result of the comparison


Copyright © 2011. All Rights Reserved.