org.springframework.shell.support.util
Class DomUtils

java.lang.Object
  extended by org.springframework.shell.support.util.DomUtils

public final class DomUtils
extends Object

Convenience methods for working with the DOM API, in particular for working with DOM Nodes and DOM Elements.

Since:
1.2.0
See Also:
Node, Element

Method Summary
static Element createChildElement(String tagName, Node parent, Document document)
          Creates a child element with the given name and parent.
static Element createChildIfNotExists(String tagName, Node parent, Document document)
          Returns the child node with the given tag name, creating it if it does not exist.
static Element findFirstElementByName(String name, Element root)
          Checks in under a given root element whether it can find a child element which matches the name supplied.
static Element getChildElementByTagName(Element element, String childElementName)
          Returns the first child element identified by its name.
static List<Element> getChildElementsByTagName(Element element, String childEleName)
          Retrieve all child elements of the given DOM element that match the given element name.
static List<Element> getChildElementsByTagName(Element element, String[] childElementNames)
          Retrieve all child elements of the given DOM element that match any of the given element names.
static String getChildElementValueByTagName(Element element, String childElementName)
          Returns the first child element value identified by its name.
static String getChildTextContent(Element parent, String child)
          Returns the text content of the first child of the given parent that has the given tag name, if any.
static String getTextContent(Node node, String defaultValue)
          Returns the text content of the given Node, null safe.
static String getTextValue(Element valueElement)
          Extract the text value from the given DOM element, ignoring XML comments.
static boolean nodeNameEquals(Node node, String desiredName)
          Namespace-aware equals comparison.
static void removeElements(String xPath, Element searchBase)
          Removes any elements matching the given XPath expression, relative to the given Element
static void removeTextNodes(Node node)
          Removes empty text nodes from the specified node.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getChildElementsByTagName

public static List<Element> getChildElementsByTagName(Element element,
                                                      String[] childElementNames)
Retrieve all child elements of the given DOM element that match any of the given element names. Only look at the direct child level of the given element; do not go into further depth (in contrast to the DOM API's getElementsByTagName method).

Parameters:
element - the DOM element to analyze
childElementNames - the child element names to look for
Returns:
a List of child org.w3c.dom.Element instances
See Also:
Element, Element.getElementsByTagName(java.lang.String)

getChildElementsByTagName

public static List<Element> getChildElementsByTagName(Element element,
                                                      String childEleName)
Retrieve all child elements of the given DOM element that match the given element name. Only look at the direct child level of the given element; do not go into further depth (in contrast to the DOM API's getElementsByTagName method).

Parameters:
element - the DOM element to analyze
childEleName - the child element name to look for
Returns:
a List of child org.w3c.dom.Element instances
See Also:
Element, Element.getElementsByTagName(java.lang.String)

getChildElementByTagName

public static Element getChildElementByTagName(Element element,
                                               String childElementName)
Returns the first child element identified by its name.

Parameters:
element - the DOM element to analyze
childElementName - the child element name to look for
Returns:
the org.w3c.dom.Element instance, or null if none found

getChildElementValueByTagName

public static String getChildElementValueByTagName(Element element,
                                                   String childElementName)
Returns the first child element value identified by its name.

Parameters:
element - the DOM element to analyze
childElementName - the child element name to look for
Returns:
the extracted text value, or null if no child element found

getTextValue

public static String getTextValue(Element valueElement)
Extract the text value from the given DOM element, ignoring XML comments.

Appends all CharacterData nodes and EntityReference nodes into a single String value, excluding Comment nodes.

See Also:
CharacterData, EntityReference, Comment

nodeNameEquals

public static boolean nodeNameEquals(Node node,
                                     String desiredName)
Namespace-aware equals comparison. Returns true if either Node.getLocalName() or Node.getNodeName() equals desiredName, otherwise returns false.

Parameters:
node - (required)
desiredName - (required)
Returns:

removeTextNodes

public static void removeTextNodes(Node node)
Removes empty text nodes from the specified node.

Parameters:
node - the element where empty text nodes will be removed

getTextContent

public static String getTextContent(Node node,
                                    String defaultValue)
Returns the text content of the given Node, null safe.

Parameters:
node - can be null
defaultValue - the value to return if the node is null
Returns:
the given default value if the node is null
Since:
1.2.0
See Also:
Node.getTextContent()

createChildElement

public static Element createChildElement(String tagName,
                                         Node parent,
                                         Document document)
Creates a child element with the given name and parent. Avoids the type of bug whereby the developer calls Document.createElement(String) but forgets to append it to the relevant parent.

Parameters:
tagName - the name of the new child (required)
parent - the parent node (required)
document - the document to which the parent and child belong (required)
Returns:
the created element
Since:
1.2.0

createChildIfNotExists

public static Element createChildIfNotExists(String tagName,
                                             Node parent,
                                             Document document)
Returns the child node with the given tag name, creating it if it does not exist.

Parameters:
tagName - the child tag to look for and possibly create (required)
parent - the parent in which to look for the child (required)
document - the document containing the parent (required)
Returns:
the existing or created child (never null)
Since:
1.2.0

getChildTextContent

public static String getChildTextContent(Element parent,
                                         String child)
Returns the text content of the first child of the given parent that has the given tag name, if any.

Parameters:
parent - the parent in which to search (required)
child - the child name for which to search (required)
Returns:
null if there is no such child, otherwise the first such child's text content

findFirstElementByName

public static Element findFirstElementByName(String name,
                                             Element root)
Checks in under a given root element whether it can find a child element which matches the name supplied. Returns Element if exists.

Parameters:
name - the Element name (required)
root - the parent DOM element (required)
Returns:
the Element if discovered

removeElements

public static void removeElements(String xPath,
                                  Element searchBase)
Removes any elements matching the given XPath expression, relative to the given Element

Parameters:
xPath - the XPath of the element(s) to remove (can be blank)
searchBase - the element to which the XPath expression is relative