Package groovy.xml.dom
Class DOMCategory
java.lang.Object
groovy.xml.dom.DOMCategory
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.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ElementappendNode(Element self, Object name) Appends a child element with the given name.static ElementappendNode(Element self, Object name, String value) Appends a child element with the given name and optional text value.static ElementappendNode(Element self, Object name, Map attributes) Appends a child element with the given name and attributes.static ElementappendNode(Element self, Object name, Map attributes, String value) Appends a child element with attributes and optional text content.static NamedNodeMapattributes(Element element) Returns the attributes belonging to the supplied element.static NodeListbreadthFirst(Element self) Returns a breadth-first traversal view containing the element and its descendants level by level.static NodeListReturns the child nodes visible to DOMCategory navigation.static NodeListdepthFirst(Element self) Returns a depth-first traversal view containing the element itself followed by descendant elements.static ObjectResolves a GPath-style property lookup against a DOM element.static Objectget(NamedNodeMap nodeMap, String elementName) Returns the value of a named attribute from a DOM attribute map.static ObjectResolves a GPath-style property lookup against every element in a node list.static NodegetAt(groovy.xml.dom.DOMCategory.NodeListsHolder o, int i) Returns the indexed node from an aggregated node-list view.static NodeListReturns a range of nodes from an aggregated node-list view.static NodegetAt(groovy.xml.dom.DOMCategory.NodesHolder o, int i) Returns the indexed node from a simple node-list view.static NodeListReturns a range of nodes from a simple node-list view.static NodeReturns the indexed child visible through DOMCategory navigation.static NodeListReturns a range of child nodes visible through DOMCategory navigation.static booleanDetermines whether the supplied node list is empty.static booleanstatic booleanReturns the list of any direct String nodes of this node.static StringReturns the DOM node name used for GPath name lookups.static NodeReturns the parent DOM node.static voidAdds sibling nodes after the supplied element using the builder closure.static voidAdds sibling nodes after every element in the supplied node list.static voidPerforms a GPath-style property assignment on an element.static NodereplaceNode(groovy.xml.dom.DOMCategory.NodesHolder self, Closure c) Replaces a single selected node with the nodes produced by the supplied builder closure.static NodereplaceNode(Node self, Closure c) Replaces a node with the nodes produced by the supplied builder closure.static voidsetGlobalKeepIgnorableWhitespace(boolean keepIgnorableWhitespace) Whether ignorable whitespace (e.g.static voidsetGlobalTrimWhitespace(boolean trimWhitespace) Whether text content is trimmed (removing leading and trailing whitespace); default false.static voidSets the value of the first child text node, creating one if the element has no children.static intsize(NamedNodeMap namedNodeMap) Returns the number of attributes in a DOM attribute map.static intReturns the number of nodes in the supplied node list.static StringReturns the text visible from a DOM node.static StringConcatenates the text visible from every node in the list.static StringRenders a DOMCategory value in a GPath-friendly string form.static StringEvaluates an XPath expression against the supplied node and returns the string result.static ObjectEvaluates an XPath expression against the supplied node.
-
Constructor Details
-
DOMCategory
public DOMCategory()
-
-
Method Details
-
isGlobalTrimWhitespace
public static boolean isGlobalTrimWhitespace()- Returns:
- true if text elements are trimmed before returning; default false
-
setGlobalTrimWhitespace
public static void setGlobalTrimWhitespace(boolean trimWhitespace) Whether 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.- Parameters:
trimWhitespace- the new value
-
isGlobalKeepIgnorableWhitespace
public static boolean isGlobalKeepIgnorableWhitespace()- Returns:
- true if ignorable whitespace (e.g. whitespace between elements) is kept; default false
-
setGlobalKeepIgnorableWhitespace
public static void setGlobalKeepIgnorableWhitespace(boolean keepIgnorableWhitespace) Whether 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.- Parameters:
keepIgnorableWhitespace- the new value
-
get
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**.- Parameters:
element- the element to queryelementName- the property or selector name- Returns:
- the matching child nodes, attribute value, parent node, or traversal view
-
get
Resolves a GPath-style property lookup against every element in a node list.- Parameters:
nodeList- the nodes to queryelementName- the property or selector name- Returns:
- an aggregated result for the supplied selector
-
get
Returns the value of a named attribute from a DOM attribute map.- Parameters:
nodeMap- the attributes to queryelementName- the attribute name- Returns:
- the attribute value
-
attributes
Returns the attributes belonging to the supplied element.- Parameters:
element- the element whose attributes should be returned- Returns:
- the element's attribute map
-
size
Returns the number of attributes in a DOM attribute map.- Parameters:
namedNodeMap- the attributes to inspect- Returns:
- the number of attributes
-
getAt
Returns the indexed child visible through DOMCategory navigation. Negative indices count backward from the end of the result.- Parameters:
o- the node or element to indexi- the zero-based index- Returns:
- the selected node, or
nullif the index is out of range
-
getAt
Returns the indexed node from an aggregated node-list view. Negative indices count backward from the end of the result.- Parameters:
o- the aggregated node-list viewi- the zero-based index- Returns:
- the selected node, or
nullif the index is out of range
-
getAt
Returns the indexed node from a simple node-list view. Negative indices count backward from the end of the result.- Parameters:
o- the node-list viewi- the zero-based index- Returns:
- the selected node, or
nullif the index is out of range
-
getAt
Returns a range of child nodes visible through DOMCategory navigation. Negative bounds count backward from the end of the result.- Parameters:
o- the node or element to slicer- the range of indices to select- Returns:
- a node-list view containing the selected nodes
-
getAt
Returns a range of nodes from an aggregated node-list view.- Parameters:
o- the aggregated node-list viewr- the range of indices to select- Returns:
- a node-list view containing the selected nodes
-
getAt
Returns a range of nodes from a simple node-list view.- Parameters:
o- the node-list viewr- the range of indices to select- Returns:
- a node-list view containing the selected nodes
-
name
Returns the DOM node name used for GPath name lookups.- Parameters:
node- the node to inspect- Returns:
- the node name
-
parent
Returns the parent DOM node.- Parameters:
node- the node whose parent should be returned- Returns:
- the parent node, or
nullfor a root node
-
text
Returns the text visible from a DOM node. Text and CDATA nodes return their value directly; other nodes concatenate descendant text.- Parameters:
node- the node to inspect- Returns:
- the concatenated text for the node
-
text
Concatenates the text visible from every node in the list.- Parameters:
nodeList- the nodes to inspect- Returns:
- the concatenated text for all nodes in document order
-
list
- Parameters:
self- the node list to copy- Returns:
- a list containing the nodes from the node list
-
depthFirst
Returns a depth-first traversal view containing the element itself followed by descendant elements.- Parameters:
self- the root element for traversal- Returns:
- a node-list view in depth-first order
-
setValue
Sets the value of the first child text node, creating one if the element has no children.- Parameters:
self- the element to updatevalue- the text value to set
-
putAt
Performs a GPath-style property assignment on an element. Attribute assignments use the@nameform; all other properties are delegated to Groovy property handling.- Parameters:
self- the element to updateproperty- the property or attribute selectorvalue- the value to assign
-
appendNode
Appends a child element with the given name.- Parameters:
self- the parent elementname- the child element name, optionally aQName- Returns:
- the appended child element
-
appendNode
Appends a child element with the given name and attributes.- Parameters:
self- the parent elementname- the child element name, optionally aQNameattributes- the attributes to apply to the new child- Returns:
- the appended child element
-
appendNode
Appends a child element with the given name and optional text value.- Parameters:
self- the parent elementname- the child element name, optionally aQNamevalue- the text value to append inside the new child, ornull- Returns:
- the appended child element
-
appendNode
Appends a child element with attributes and optional text content.- Parameters:
self- the parent elementname- the child element name, optionally aQNameattributes- the attributes to apply to the new childvalue- the text value to append inside the new child, ornull- Returns:
- the appended child element
-
replaceNode
Replaces a single selected node with the nodes produced by the supplied builder closure.- Parameters:
self- the node selection to replace; it must contain exactly one nodec- the builder closure creating replacement nodes- Returns:
- the removed node
- Throws:
GroovyRuntimeException- if the selection does not contain exactly one node
-
replaceNode
Replaces a node with the nodes produced by the supplied builder closure.- Parameters:
self- the node to replacec- the builder closure creating replacement nodes- Returns:
- the removed node
- Throws:
UnsupportedOperationException- ifselfis the document root
-
plus
Adds sibling nodes after the supplied element using the builder closure.- Parameters:
self- the element after which new siblings should be insertedc- the builder closure creating the sibling nodes- Throws:
UnsupportedOperationException- ifselfis the document root
-
localText
Returns the list of any direct String nodes of this node.- Returns:
- the list of String values from this node
- Since:
- 2.3.0
-
plus
Adds sibling nodes after every element in the supplied node list.- Parameters:
self- the elements after which new siblings should be insertedc- the builder closure creating the sibling nodes
-
breadthFirst
Returns a breadth-first traversal view containing the element and its descendants level by level.- Parameters:
self- the root element for traversal- Returns:
- a node-list view in breadth-first order
-
children
Returns 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.- Parameters:
self- the parent element- Returns:
- a node-list view of the visible children
-
toString
Renders a DOMCategory value in a GPath-friendly string form.- Parameters:
o- the value to render- Returns:
- text for text nodes, list-style output for node lists, or
o.toString()
-
xpath
Evaluates an XPath expression against the supplied node.- Parameters:
self- the context nodeexpression- the XPath expression to evaluatereturnType- the desired XPath return type- Returns:
- the XPath evaluation result
- Throws:
GroovyRuntimeException- if the expression cannot be evaluated
-
xpath
Evaluates an XPath expression against the supplied node and returns the string result.- Parameters:
self- the context nodeexpression- the XPath expression to evaluate- Returns:
- the string result of the XPath evaluation
- Throws:
GroovyRuntimeException- if the expression cannot be evaluated
-
size
Returns the number of nodes in the supplied node list.- Parameters:
self- the node list to inspect- Returns:
- the number of nodes
-
isEmpty
Determines whether the supplied node list is empty.- Parameters:
self- the node list to inspect- Returns:
trueif the node list contains no nodes
-