public class DOMCategory
extends Object
Category class which adds GPath style operations to Java's DOM classes. These helpers provide property access, traversal, mutation, and XPath support for W3C DOM nodes in a style similar to Groovy's XML slurper APIs.
| Type Params | Return Type | Name and description |
|---|---|---|
|
public static Element |
appendNode(Element self, Object name)Appends a child element with the given name. |
|
public static Element |
appendNode(Element self, Object name, Map attributes)Appends a child element with the given name and attributes. |
|
public static Element |
appendNode(Element self, Object name, String value)Appends a child element with the given name and optional text value. |
|
public static Element |
appendNode(Element self, Object name, Map attributes, String value)Appends a child element with attributes and optional text content. |
|
public static NamedNodeMap |
attributes(Element element)Returns the attributes belonging to the supplied element. |
|
public static NodeList |
breadthFirst(Element self)Returns a breadth-first traversal view containing the element and its descendants level by level. |
|
public static NodeList |
children(Element self)Returns the child nodes visible to DOMCategory navigation. |
|
public static NodeList |
depthFirst(Element self)Returns a depth-first traversal view containing the element itself followed by descendant elements. |
|
public static Object |
get(Element element, String elementName)Resolves a GPath-style property lookup against a DOM element. |
|
public static Object |
get(NodeList nodeList, String elementName)Resolves a GPath-style property lookup against every element in a node list. |
|
public static Object |
get(NamedNodeMap nodeMap, String elementName)Returns the value of a named attribute from a DOM attribute map. |
|
public static Node |
getAt(Node o, int i)Returns the indexed child visible through DOMCategory navigation. |
|
public static Node |
getAt(DOMCategory.NodeListsHolder o, int i)Returns the indexed node from an aggregated node-list view. |
|
public static Node |
getAt(DOMCategory.NodesHolder o, int i)Returns the indexed node from a simple node-list view. |
|
public static NodeList |
getAt(Node o, IntRange r)Returns a range of child nodes visible through DOMCategory navigation. |
|
public static NodeList |
getAt(DOMCategory.NodeListsHolder o, IntRange r)Returns a range of nodes from an aggregated node-list view. |
|
public static NodeList |
getAt(DOMCategory.NodesHolder o, IntRange r)Returns a range of nodes from a simple node-list view. |
|
public static boolean |
isEmpty(NodeList self)Determines whether the supplied node list is empty. |
|
public static boolean |
isGlobalKeepIgnorableWhitespace()
|
|
public static boolean |
isGlobalTrimWhitespace()
|
|
public static List<Node> |
list(NodeList self)Copies a NodeList into a mutable List. |
|
public static List<String> |
localText(Element self)Returns the list of any direct String nodes of this node. |
|
public static String |
name(Node node)Returns the DOM node name used for GPath name lookups. |
|
public static Node |
parent(Node node)Returns the parent DOM node. |
|
public static void |
plus(Element self, Closure c)Adds sibling nodes after the supplied element using the builder closure. |
|
public static void |
plus(NodeList self, Closure c)Adds sibling nodes after every element in the supplied node list. |
|
public static void |
putAt(Element self, String property, Object value)Performs a GPath-style property assignment on an element. |
|
public static Node |
replaceNode(DOMCategory.NodesHolder self, Closure c)Replaces a single selected node with the nodes produced by the supplied builder closure. |
|
public static Node |
replaceNode(Node self, Closure c)Replaces a node with the nodes produced by the supplied builder closure. |
|
public static void |
setGlobalKeepIgnorableWhitespace(boolean keepIgnorableWhitespace)Whether ignorable whitespace (e.g. whitespace between elements) is kept (default false). |
|
public static void |
setGlobalTrimWhitespace(boolean trimWhitespace)Whether text content is trimmed (removing leading and trailing whitespace); default false. |
|
public static void |
setValue(Element self, String value)Sets the value of the first child text node, creating one if the element has no children. |
|
public static int |
size(NamedNodeMap namedNodeMap)Returns the number of attributes in a DOM attribute map. |
|
public static int |
size(NodeList self)Returns the number of nodes in the supplied node list. |
|
public static String |
text(Node node)Returns the text visible from a DOM node. |
|
public static String |
text(NodeList nodeList)Concatenates the text visible from every node in the list. |
|
public static String |
toString(Object o)Renders a DOMCategory value in a GPath-friendly string form. |
|
public static Object |
xpath(Node self, String expression, QName returnType)Evaluates an XPath expression against the supplied node. |
|
public static String |
xpath(Node self, String expression)Evaluates an XPath expression against the supplied node and returns the string result. |
Appends a child element with the given name.
self - the parent elementname - the child element name, optionally a QNameAppends a child element with the given name and attributes.
self - the parent elementname - the child element name, optionally a QNameattributes - the attributes to apply to the new childAppends a child element with the given name and optional text value.
self - the parent elementname - the child element name, optionally a QNamevalue - the text value to append inside the new child, or nullAppends a child element with attributes and optional text content.
self - the parent elementname - the child element name, optionally a QNameattributes - the attributes to apply to the new childvalue - the text value to append inside the new child, or nullReturns the attributes belonging to the supplied element.
element - the element whose attributes should be returnedReturns a breadth-first traversal view containing the element and its descendants level by level.
self - the root element for traversalReturns the child nodes visible to DOMCategory navigation. Child elements are always included, and retained text nodes are included when they are not discarded as ignorable whitespace.
self - the parent elementReturns a depth-first traversal view containing the element itself followed by descendant elements.
self - the root element for traversal Resolves a GPath-style property lookup against a DOM element.
Supports child element lookup, attribute access using @name,
parent lookup using .., and depth-first traversal using **.
element - the element to queryelementName - the property or selector nameResolves a GPath-style property lookup against every element in a node list.
nodeList - the nodes to queryelementName - the property or selector nameReturns the value of a named attribute from a DOM attribute map.
nodeMap - the attributes to queryelementName - the attribute nameReturns the indexed child visible through DOMCategory navigation. Negative indices count backward from the end of the result.
o - the node or element to indexi - the zero-based indexnull if the index is out of rangeReturns the indexed node from an aggregated node-list view. Negative indices count backward from the end of the result.
o - the aggregated node-list viewi - the zero-based indexnull if the index is out of rangeReturns the indexed node from a simple node-list view. Negative indices count backward from the end of the result.
o - the node-list viewi - the zero-based indexnull if the index is out of rangeReturns a range of child nodes visible through DOMCategory navigation. Negative bounds count backward from the end of the result.
o - the node or element to slicer - the range of indices to selectReturns a range of nodes from an aggregated node-list view.
o - the aggregated node-list viewr - the range of indices to selectReturns a range of nodes from a simple node-list view.
o - the node-list viewr - the range of indices to selectDetermines whether the supplied node list is empty.
self - the node list to inspecttrue if the node list contains no nodes
Copies a NodeList into a mutable List.
self - the node list to copyReturns the list of any direct String nodes of this node.
Returns the DOM node name used for GPath name lookups.
node - the node to inspectReturns the parent DOM node.
node - the node whose parent should be returnednull for a root nodeAdds sibling nodes after the supplied element using the builder closure.
self is the document rootself - the element after which new siblings should be insertedc - the builder closure creating the sibling nodesAdds sibling nodes after every element in the supplied node list.
self - the elements after which new siblings should be insertedc - the builder closure creating the sibling nodes Performs a GPath-style property assignment on an element.
Attribute assignments use the @name form; all other properties are delegated to Groovy property handling.
self - the element to updateproperty - the property or attribute selectorvalue - the value to assignReplaces a single selected node with the nodes produced by the supplied builder closure.
self - the node selection to replace; it must contain exactly one nodec - the builder closure creating replacement nodesReplaces a node with the nodes produced by the supplied builder closure.
self is the document rootself - the node to replacec - the builder closure creating replacement nodesWhether ignorable whitespace (e.g. whitespace between elements) is kept (default false). WARNING: this is a global setting. Altering it will affect all DOMCategory usage within the current Java process.
keepIgnorableWhitespace - the new valueWhether text content is trimmed (removing leading and trailing whitespace); default false. WARNING: this is a global setting. Altering it will affect all DOMCategory usage within the current Java process. It is not recommended that this is altered; instead call the trim() method on the returned text, but the flag is available to support legacy Groovy behavior.
trimWhitespace - the new valueSets the value of the first child text node, creating one if the element has no children.
self - the element to updatevalue - the text value to setReturns the number of attributes in a DOM attribute map.
namedNodeMap - the attributes to inspectReturns the number of nodes in the supplied node list.
self - the node list to inspectReturns the text visible from a DOM node. Text and CDATA nodes return their value directly; other nodes concatenate descendant text.
node - the node to inspectConcatenates the text visible from every node in the list.
nodeList - the nodes to inspectRenders a DOMCategory value in a GPath-friendly string form.
o - the value to rendero.toString()Evaluates an XPath expression against the supplied node.
self - the context nodeexpression - the XPath expression to evaluatereturnType - the desired XPath return typeEvaluates an XPath expression against the supplied node and returns the string result.
self - the context nodeexpression - the XPath expression to evaluate