Package groovy.xml.slurpersupport
Class GPathResult
java.lang.Object
groovy.lang.GroovyObjectSupport
groovy.xml.slurpersupport.GPathResult
- All Implemented Interfaces:
Buildable,GroovyObject,Writable,Iterable
- Direct Known Subclasses:
Attribute,NoChildren,NodeChild,NodeChildren
public abstract class GPathResult
extends GroovyObjectSupport
implements Writable, Buildable, Iterable
Base class for representing lazy evaluated GPath expressions.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final StringNode or attribute name selected by this result.protected final MapNamespace declarations added while traversing or rebuilding this result.protected final StringNamespace prefix constraint applied to this result, if any.Namespace prefix hints captured while parsing for stable QName rendering.protected final GPathResultPrevious step in the lazy GPath chain, used for expression backtracking. -
Constructor Summary
ConstructorsConstructorDescriptionGPathResult(GPathResult parent, String name, String namespacePrefix, Map<String, String> namespaceTagHints) Creates a new GPathResult namednamewith the parentparent, the namespacePrefixnamespacePrefixand the namespaceTagHints specified in thenamespaceTagHintsMap. -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract voidappendNode(Object newValue) Lazily appends content to the nodes represented by this result.Provides an Iterator over all the nodes of this GPathResult using a breadth-first traversal.abstract IteratorReturns an iterator over the child nodes of this GPathResult.children()Returns the children of this GPathResult as a GPathResult object.protected IteratorcreateIterator(Object obj) Creates a single-element iterator used by scalar GPath results.declareNamespace(Map newNamespaceMapping) Adds the specified map of prefix to namespace mappings to this GPathResult.Provides an Iterator over all the nodes of this GPathResult using a depth-first traversal.booleanabstract GPathResultReturns the first child of this GPathResult matching the condition(s) specified in the passed closure.abstract GPathResultReturns the children of this GPathResult matching the condition(s) specified in the passed closure.getAt(int index) Supports the subscript operator for a GPathResult.Supports the range subscript operator for a GPathResult.getBody()Creates a Closure representing the body of this GPathResult.getProperty(String property) Returns the specified Property of this GPathResult.inthashCode()booleanisEmpty()Returns true if the GPathResult is empty, i.e.abstract Iteratoriterator()Overloads the left shift operator to provide an easy way to lazily append Objects to this GPathResult.list()Creates a list of objects representing this GPathResult.lookupNamespace(String prefix) Returns the namespace mapped to the specified prefix.name()Returns the name of this GPathResult.abstract IteratorReturns the raw node iterator backing this lazy result.parent()Returns as GPathResult with the parent nodes of the current GPathResultabstract GPathResultparents()Returns the parents of this GPathResult as aGPathResult.Delegates toplus(Object)while preserving XmlSlurper replacement semantics for closures.Lazily adds the specified Object to this GPathResult.pop()Returns the parent of this GPathResult.voidA helper method to allow GPathResults to work with subscript operatorsprotected abstract voidreplaceBody(Object newValue) Lazily replaces the body content represented by this result.protected abstract voidreplaceNode(Closure newValue) Lazily replaces the nodes represented by this result.voidsetMetaClass(MetaClass metaClass) Replaces the MetaClass of this GPathResult.voidsetProperty(String property, Object newValue) Replaces the specified property of this GPathResult with a new value.abstract intsize()Returns the size of this GPathResult.abstract Stringtext()Returns the text of this GPathResult as aString.Converts the text of this GPathResult to a BigDecimal object.Converts the text of this GPathResult to a BigInteger object.Converts the text of this GPathResult to a Boolean object.toDouble()Converts the text of this GPathResult to a Double object.toFloat()Converts the text of this GPathResult to a Float object.Converts the text of this GPathResult to an Integer object.toLong()Converts the text of this GPathResult to a Long object.toString()Returns the text of this GPathResult.toURI()Converts the text of this GPathResult to a URI object.toURL()Converts the text of this GPathResult to a URL object.Methods inherited from class groovy.lang.GroovyObjectSupport
getMetaClassMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface groovy.lang.GroovyObject
invokeMethodMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
parent
Previous step in the lazy GPath chain, used for expression backtracking. -
name
Node or attribute name selected by this result. -
namespacePrefix
Namespace prefix constraint applied to this result, if any. -
namespaceMap
Namespace declarations added while traversing or rebuilding this result. -
namespaceTagHints
Namespace prefix hints captured while parsing for stable QName rendering.
-
-
Constructor Details
-
GPathResult
public GPathResult(GPathResult parent, String name, String namespacePrefix, Map<String, String> namespaceTagHints) Creates a new GPathResult namednamewith the parentparent, the namespacePrefixnamespacePrefixand the namespaceTagHints specified in thenamespaceTagHintsMap.- Parameters:
parent- the GPathResult prior to the application of the expression creating this GPathResultname- if the GPathResult corresponds to something with a name, e.g. a nodenamespacePrefix- the namespace prefix if anynamespaceTagHints- the known tag to namespace mappings
-
-
Method Details
-
setMetaClass
Replaces the MetaClass of this GPathResult.- Specified by:
setMetaClassin interfaceGroovyObject- Overrides:
setMetaClassin classGroovyObjectSupport- Parameters:
metaClass- the new MetaClass
-
getProperty
Returns the specified Property of this GPathResult.Realizes the follow shortcuts:
'..'forparent()'*'forchildren()'**'fordepthFirst()'@'for attribute access
- Specified by:
getPropertyin interfaceGroovyObject- Parameters:
property- the Property to fetch- Returns:
- the given property
-
setProperty
Replaces the specified property of this GPathResult with a new value.- Specified by:
setPropertyin interfaceGroovyObject- Parameters:
property- the property of this GPathResult to replacenewValue- the new value of the property
-
leftShift
Overloads the left shift operator to provide an easy way to lazily append Objects to this GPathResult.- Parameters:
newValue- the Object to append- Returns:
this
-
plus
Lazily adds the specified Object to this GPathResult.- Parameters:
newValue- the Object to add- Returns:
this
-
plus
Delegates toplus(Object)while preserving XmlSlurper replacement semantics for closures.- Parameters:
newValue- closure supplying replacement markup- Returns:
this
-
replaceNode
Lazily replaces the nodes represented by this result.- Parameters:
newValue- closure generating the replacement content
-
replaceBody
Lazily replaces the body content represented by this result.- Parameters:
newValue- replacement body content
-
appendNode
Lazily appends content to the nodes represented by this result.- Parameters:
newValue- content to append
-
name
Returns the name of this GPathResult.- Returns:
- the name of this GPathResult
-
pop
Returns the parent of this GPathResult. If this GPathResult has no parent the GPathResult itself is returned. This is no navigation in the XML tree. It is backtracking on the GPath expression chain. It is the behavior of parent() prior to 2.2.0. Backtracking on '..' actually goes down one level in the tree again. find() and findAll() are popped along with the level they have been applied to.- Returns:
- the parent or
this
-
parent
Returns as GPathResult with the parent nodes of the current GPathResult- Returns:
- the parents GPathResult or
thisfor the root
-
children
Returns the children of this GPathResult as a GPathResult object.- Returns:
- the children of this GPathResult
-
lookupNamespace
Returns the namespace mapped to the specified prefix.- Parameters:
prefix- the prefix lookup- Returns:
- the namespace of the prefix
-
toString
Returns the text of this GPathResult. -
toInteger
Converts the text of this GPathResult to an Integer object.- Returns:
- the GPathResult, converted to a
Integer
-
toLong
Converts the text of this GPathResult to a Long object.- Returns:
- the GPathResult, converted to a
Long
-
toFloat
Converts the text of this GPathResult to a Float object.- Returns:
- the GPathResult, converted to a
Float
-
toDouble
Converts the text of this GPathResult to a Double object.- Returns:
- the GPathResult, converted to a
Double
-
toBigDecimal
Converts the text of this GPathResult to a BigDecimal object.- Returns:
- the GPathResult, converted to a
BigDecimal
-
toBigInteger
Converts the text of this GPathResult to a BigInteger object.- Returns:
- the GPathResult, converted to a
BigInteger
-
toURL
Converts the text of this GPathResult to a URL object.- Returns:
- the GPathResult, converted to a
URL - Throws:
MalformedURLException
-
toURI
Converts the text of this GPathResult to a URI object.- Returns:
- the GPathResult, converted to a
URI - Throws:
URISyntaxException
-
toBoolean
Converts the text of this GPathResult to a Boolean object.- Returns:
- the GPathResult, converted to a
Boolean
-
declareNamespace
Adds the specified map of prefix to namespace mappings to this GPathResult. Already existing prefixes are overwritten.- Parameters:
newNamespaceMapping- the mappings to add- Returns:
this
-
hashCode
public int hashCode() -
equals
-
getAt
Supports the subscript operator for a GPathResult.import groovy.xml.slurpersupport.* import groovy.xml.XmlSlurper def text = """ <characterList> <character/> <character> <name>Gromit</name> </character> </characterList>""" GPathResult characterList = new XmlSlurper().parseText(text) assert characterList.character[1].name == 'Gromit'- Parameters:
index- an index- Returns:
- the value at the given index
-
getAt
Supports the range subscript operator for a GPathResult.import groovy.xml.slurpersupport.* import groovy.xml.XmlSlurper def text = """ <characterList> <character>Wallace</character> <character>Gromit</character> <character>Shaun</character> </characterList>""" GPathResult characterList = new XmlSlurper().parseText(text) assert characterList.character[1..2].join(',') == 'Gromit,Shaun'- Parameters:
range- a Range indicating the items to get- Returns:
- a new list based on range borders
-
putAt
A helper method to allow GPathResults to work with subscript operators- Parameters:
index- an indexnewValue- the value to put at the given index
-
depthFirst
Provides an Iterator over all the nodes of this GPathResult using a depth-first traversal.- Returns:
- the
Iteratorof (depth-first) ordered GPathResults
-
breadthFirst
Provides an Iterator over all the nodes of this GPathResult using a breadth-first traversal.- Returns:
- the
Iteratorof (breadth-first) ordered GPathResults
-
list
Creates a list of objects representing this GPathResult.- Returns:
- a list representing of this GPathResult
-
isEmpty
public boolean isEmpty()Returns true if the GPathResult is empty, i.e. if, and only if,size()is 0.- Returns:
- true if the GPathResult is empty
-
getBody
Creates a Closure representing the body of this GPathResult.- Returns:
- the body of this GPathResult, converted to a
Closure
-
size
public abstract int size()Returns the size of this GPathResult.- Returns:
- the size of this GPathResult
-
text
Returns the text of this GPathResult as aString.- Returns:
- the text of this GPathResult
-
parents
Returns the parents of this GPathResult as aGPathResult. Warning: The subclasses of this package do not implement this method yet.- Returns:
- the parents of this GPathResult
-
childNodes
Returns an iterator over the child nodes of this GPathResult.- Returns:
- an iterator over the child nodes of this GPathResult
-
iterator
-
find
Returns the first child of this GPathResult matching the condition(s) specified in the passed closure.- Parameters:
closure- a closure to filters the children of this GPathResult- Returns:
- the first child matching the closure
-
findAll
Returns the children of this GPathResult matching the condition(s) specified in the passed closure.- Parameters:
closure- a closure to filters the children of this GPathResult- Returns:
- the children matching the closure
-
nodeIterator
Returns the raw node iterator backing this lazy result.- Returns:
- the underlying node iterator
-
createIterator
Creates a single-element iterator used by scalar GPath results.- Parameters:
obj- the sole element to expose- Returns:
- an iterator yielding
objonce
-