Top Description Fields Constructors Methods
org.antlr.v4.runtime.tree.xpath

public Class XPath

extends Object
Class Inheritance
Imports
org.antlr.v4.runtime.ANTLRInputStream, .CommonTokenStream, .LexerNoViableAltException, .Parser, .ParserRuleContext, .Token, org.antlr.v4.runtime.tree.ParseTree, java.io.IOException, .StringReader, java.util.ArrayList, .Collection, .Collections, .LinkedHashSet, .List

Represent a subset of XPath XML path syntax for use in identifying nodes in parse trees.

Split path into words and separators / and // via ANTLR itself then walk path elements from left to right. At each separator-word pair, find set of nodes. Next stage uses those as work list.

The basic interface is ParseTree.findAll(tree, pathString, parser). But that is just shorthand for:

XPath p = new XPath(parser, pathString);
return p.evaluate(tree);

See org.antlr.v4.test.TestXPath for descriptions. In short, this allows operators:

/
root
//
anywhere
!
invert; this must appear directly after root or anywhere operator

and path elements:

ID
token name
'string'
any string literal token from the grammar
expr
rule name
*
wildcard matching any node

Whitespace is not allowed.

Field Summary

Modifier and TypeField and Description
protected XPathElement[]
public static final String
protected Parser
protected String
public static final String

Constructor Summary

AccessConstructor and Description
public
XPath(Parser parser, String path)

Method Summary

Modifier and TypeMethod and Description
public Collection<ParseTree>
evaluate(final ParseTree t)

Return a list of all nodes starting at t as root that satisfy the path.

public static Collection<ParseTree>
findAll(ParseTree tree, String xpath, Parser parser)

protected XPathElement
getXPathElement(Token wordToken, boolean anywhere)

Convert word like * or ID or expr to a path element.

public XPathElement[]
split(String path)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

elementsback to summary
protected XPathElement[] elements
NOTback to summary
public static final String NOT
parserback to summary
protected Parser parser
pathback to summary
protected String path
WILDCARDback to summary
public static final String WILDCARD

Constructor Detail

XPathback to summary
public XPath(Parser parser, String path)

Method Detail

evaluateback to summary
public Collection<ParseTree> evaluate(final ParseTree t)

Return a list of all nodes starting at t as root that satisfy the path. The root / is relative to the node passed to evaluate.

findAllback to summary
public static Collection<ParseTree> findAll(ParseTree tree, String xpath, Parser parser)
getXPathElementback to summary
protected XPathElement getXPathElement(Token wordToken, boolean anywhere)

Convert word like * or ID or expr to a path element. anywhere is true if // precedes the word.

splitback to summary
public XPathElement[] split(String path)