org.ditchnet.xml.dom
Class DomUtils

java.lang.Object
  extended byorg.ditchnet.xml.dom.DomUtils

public class DomUtils
extends java.lang.Object

A Utility class that may not be instanciated (private constructor) that contains static utility methods that ease DOM traversal pain.

Author:
Todd Ditchendorf

Method Summary
static java.util.List getChildrenByClassName(org.w3c.dom.Node target, java.lang.String className)
          Returns all child elements of target with HTML class attribute values that contain className as an array of type List.
static java.util.List getDescendantsByClassName(org.w3c.dom.Node target, java.lang.String className)
          Returns all descendant elements of target with HTML class attribute values that contain className as an array of type List.
static org.w3c.dom.Element getFirstAncestorByClassName(org.w3c.dom.Node target, java.lang.String className)
          Search up the DOM tree for the first ancestor element of target with an HTML class attribute value of className.
static org.w3c.dom.Element getFirstAncestorOrSelfByClassName(org.w3c.dom.Node target, java.lang.String className)
          Search up the DOM tree for the first ancestor element of target with an HTML class attribute value of className.
static org.w3c.dom.Element getFirstChildByClassName(org.w3c.dom.Node target, java.lang.String className)
          Search target's children for the first element node with an HTML class attribute value of className.
static org.w3c.dom.Element getFirstDescendantByClassNameBreadthFirst(org.w3c.dom.Node target, java.lang.String className)
          Retreives target's first descendant element with an HTML class attribute value that includes className using a breadth-first algorithm.
static org.w3c.dom.Element getFirstDescendantByClassNameDepthFirst(org.w3c.dom.Node target, java.lang.String className)
          Retreives target's first descendant element with an HTML class attribute value that includes className using a depth-first algorithm.
static boolean hasClassName(org.w3c.dom.Element target, java.lang.String className)
          Test the target param to see if it's HTML class attribute contains className.
static boolean hasId(org.w3c.dom.Element target, java.lang.String id)
          Test Element param to see if it's HTML id attribute matches id.
static boolean isElementNode(org.w3c.dom.Node target)
          Tests to see if target's getNodeType() method returns Node.ELEMENT_NODE.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

hasId

public static boolean hasId(org.w3c.dom.Element target,
                            java.lang.String id)
Test Element param to see if it's HTML id attribute matches id.

Parameters:
target - The Element to test.
id - Check target for this value in it's HTML id attribute.

hasClassName

public static boolean hasClassName(org.w3c.dom.Element target,
                                   java.lang.String className)
Test the target param to see if it's HTML class attribute contains className. Keep in mind that the HTML 4.01 spec allows an element's HTML class attribute value to consist of multiple space-separated values. This method takes that fact into account, and tests target's HTML class attribute for the existence of className anywhere in the string.

So for example, say you had an org.w3c.dom.Element reference to the following HTML element in a variable named target:

<div class="ditch-tab-pane ditch-focused"></div>

note that this attribute value is taking advantage of the ability to list multiple space separated values.

DomUtils.hasClassName(target,"ditch-tab-pane");

Would return true.

DomUtils.hasClassName(target,"ditch-tab");

Would return false.

Parameters:
target - The Element to test.
className - Check target for this value in it's HTML class attribute.

isElementNode

public static boolean isElementNode(org.w3c.dom.Node target)
Tests to see if target's getNodeType() method returns Node.ELEMENT_NODE.


getFirstAncestorByClassName

public static org.w3c.dom.Element getFirstAncestorByClassName(org.w3c.dom.Node target,
                                                              java.lang.String className)
Search up the DOM tree for the first ancestor element of target with an HTML class attribute value of className.


getFirstAncestorOrSelfByClassName

public static org.w3c.dom.Element getFirstAncestorOrSelfByClassName(org.w3c.dom.Node target,
                                                                    java.lang.String className)
Search up the DOM tree for the first ancestor element of target with an HTML class attribute value of className.


getFirstChildByClassName

public static org.w3c.dom.Element getFirstChildByClassName(org.w3c.dom.Node target,
                                                           java.lang.String className)
Search target's children for the first element node with an HTML class attribute value of className.


getFirstDescendantByClassNameBreadthFirst

public static org.w3c.dom.Element getFirstDescendantByClassNameBreadthFirst(org.w3c.dom.Node target,
                                                                            java.lang.String className)
Retreives target's first descendant element with an HTML class attribute value that includes className using a breadth-first algorithm.


getFirstDescendantByClassNameDepthFirst

public static org.w3c.dom.Element getFirstDescendantByClassNameDepthFirst(org.w3c.dom.Node target,
                                                                          java.lang.String className)
Retreives target's first descendant element with an HTML class attribute value that includes className using a depth-first algorithm.


getChildrenByClassName

public static java.util.List getChildrenByClassName(org.w3c.dom.Node target,
                                                    java.lang.String className)
Returns all child elements of target with HTML class attribute values that contain className as an array of type List. NOTE that unlike the algorithms specified in the DOM spec, a NodeList is NOT returned. This method searches only one level deep... target's child nodes.


getDescendantsByClassName

public static java.util.List getDescendantsByClassName(org.w3c.dom.Node target,
                                                       java.lang.String className)
Returns all descendant elements of target with HTML class attribute values that contain className as an array of type List. NOTE that unlike the algorithms specified in the DOM spec, a NodeList is NOT returned. This method searched for all descendants of target meeting the criteria using a breadth-first algorithm.