public class

DomKit

extends Object
java.lang.Object
   ↳ com.atlassian.jira.functest.framework.util.dom.DomKit

Class Overview

This is some methods to manipulate the DOM and text in it

Summary

Public Constructors
DomKit()
Public Methods
static Node betterNode(Node node)
This returns a org.w3c.dom.Node implementation that has better toString() attached to it.
static Node copyDOM(Node srcNode, boolean convertTagsToLowerCase)
This will copy the Node' s from srcNode into a new org.w3c.dom.DocumentFragment
static Element getBodyElement(Document document)
Returns the first BODY element of the Document or null if there isnt one
static String getCollapsedText(Node node)
Returns the collapsed text of the specified Node.
static Element getFirstParentByTag(Element element, String parentTagName)
static String getHTML(Node node)
This will return an "canonical" version of the HTML that makes up the given org.w3c.dom.Element, including itself.
static Element getHtmlElement(Document document)
Returns the first HTML element of the Document or null if there isnt one
static String getInnerHTML(Node node)
This will return an "canonical" version of the HTML that makes up the given org.w3c.dom.Node, exclusing itself.
static DocumentBuilder getNonValidatingDocumentBuilder()
Retrieves a thread-specific non validating DocumentBuilder.
static String getRawText(Node node)
This returns a deep search of the raw text of a specified node.
static Text[] getTextNodes(Element element)
Gets all the text nodes of an Element.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public DomKit ()

Public Methods

public static Node betterNode (Node node)

This returns a org.w3c.dom.Node implementation that has better toString() attached to it. Its useful inside a debugger so you can easily see what content a node has. Each org.w3c.dom.Node that is linked to from this node will also get the better toString() methods applied to it.

You can call this function safely on already wrapped Node objects since it can detect if they have already been wrapped

Parameters
node the Node to make into a better one
Returns
  • a proxied implementation of org.w3c.dom.Node that has the new functionality

public static Node copyDOM (Node srcNode, boolean convertTagsToLowerCase)

This will copy the Node' s from srcNode into a new org.w3c.dom.DocumentFragment

Parameters
srcNode the source element to copy from
convertTagsToLowerCase whether to copy the element tags to lower case
Returns
  • the copied Node
Throws
IllegalArgumentException if the srcNode is not a org.w3c.dom.Document or org.w3c.dom.Element

public static Element getBodyElement (Document document)

Returns the first BODY element of the Document or null if there isnt one

Parameters
document the Document to search
Returns
  • an Element of tag name body

public static String getCollapsedText (Node node)

Returns the collapsed text of the specified Node. The collapsing of text text is done in the following way :

  • First find all the child text nodes of the Node (deeply)
  • If they begin with white space, collapse that into one space
  • If they end with white space, collapse that into one space
  • Append them together as one string, collapsing any white space into a single white while appending.
  • Any white space at the front and back of the final string is removed.

So if ^ represent spaces then the string "^^^^some^bold%^text^and^someother^text" will become "some bold text and someother text".

Parameters
node the Node to search
Returns
  • the collapsed text as a String

public static Element getFirstParentByTag (Element element, String parentTagName)

Parameters
element the element to start searching from
parentTagName the tag name of the parent to retrieve
Returns
  • the first parent element of this tag name, or null if not found

public static String getHTML (Node node)

This will return an "canonical" version of the HTML that makes up the given org.w3c.dom.Element, including itself.

HTML tags will be converted to lower case if they are not already. This is a suitabe default in JIRA even if the object model of browsers and HttpUnit use uppercase for tags. It matches the JSP code we use in lower case.

Parameters
node the Node in play
Returns
  • a text representation of the innards of the Element
Throws
RuntimeException if the parsing is unsuccessful

public static Element getHtmlElement (Document document)

Returns the first HTML element of the Document or null if there isnt one

Parameters
document the Document to search
Returns
  • an Element of tag name body

public static String getInnerHTML (Node node)

This will return an "canonical" version of the HTML that makes up the given org.w3c.dom.Node, exclusing itself.

HTML tags will be converted to lower case if they are not already.

Parameters
node the Node in play
Returns
  • a text representation of the innards of the Node
Throws
RuntimeException if the parsing is unsuccessful

public static DocumentBuilder getNonValidatingDocumentBuilder ()

Retrieves a thread-specific non validating DocumentBuilder. We do this because they are expensive to build.

Returns
  • the DocumentBuilder serving the current thread.

public static String getRawText (Node node)

This returns a deep search of the raw text of a specified node.

Parameters
node the org.w3c.dom.Node in question
Returns
  • the raw text of the noed with no trim processing done.

public static Text[] getTextNodes (Element element)

Gets all the text nodes of an Element. Does a deep walk of the node tree.

Parameters
element the Element in play
Returns
  • a non null array of TextNodes