working on xpath
This commit is contained in:
parent
145aeb955d
commit
d2caae46a7
|
@ -27,4 +27,4 @@ nbactions*.xml
|
|||
*.hprof
|
||||
|
||||
# Playground
|
||||
/tool/playground/
|
||||
#/tool/playground/
|
||||
|
|
|
@ -2,7 +2,12 @@ package org.antlr.v4.runtime.tree.xpath;
|
|||
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/** Represent a subset of XPath paths for use in identifying nodes in
|
||||
* parse trees.
|
||||
|
@ -14,27 +19,54 @@ import java.util.Collection;
|
|||
* ID all IDs anywhere
|
||||
* /ID an ID node if at root
|
||||
* /classdef/field all field children of classdef at root.
|
||||
* //ID all IDs anywhere (same as ID)
|
||||
* //ID INVALID (same as ID)
|
||||
* classdef//funcdef all funcs under classdef somewhere
|
||||
* classdef/* all children of classdefs anywhere in tree
|
||||
*
|
||||
* The "root" is relative to the node passed to evaluate().
|
||||
*/
|
||||
public class XPath {
|
||||
|
||||
public static final String WILDCARD = "*"; // word not operator/separator
|
||||
|
||||
protected String path;
|
||||
protected XPathElement[] elements;
|
||||
|
||||
public XPath(String path) {
|
||||
this.path = path;
|
||||
elements = split(path);
|
||||
}
|
||||
|
||||
public XPathElement[] split(String path) {
|
||||
Pattern pattern = Pattern.compile("//|/|\\w+|\\*");
|
||||
Matcher matcher = pattern.matcher(path);
|
||||
System.out.println("path="+path);
|
||||
List<String> pathStrings = new ArrayList<String>();
|
||||
while (matcher.find()) {
|
||||
pathStrings.add(matcher.group());
|
||||
}
|
||||
List<XPathElement> elements = new ArrayList<XPathElement>();
|
||||
for (String el : pathStrings) {
|
||||
System.out.println("\t"+ el);
|
||||
if ( el.equals("/") ) {
|
||||
|
||||
}
|
||||
else if ( el.equals("//") ) {
|
||||
|
||||
}
|
||||
else if ( el.equals("*") ) {
|
||||
|
||||
}
|
||||
else {
|
||||
elements.add(new XPathNodeElement(el));
|
||||
}
|
||||
}
|
||||
return elements.toArray(new XPathElement[0]);
|
||||
}
|
||||
|
||||
// following java xpath like methods; not sure it's best way
|
||||
/** Return a list of all nodes starting at t that satisfy the path.
|
||||
*
|
||||
/** Return a list of all nodes starting at t as root that satisfy the path.
|
||||
*/
|
||||
Collection<? extends ParseTree> evaluate(ParseTree t) {
|
||||
public Collection<? extends ParseTree> evaluate(ParseTree t) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
package org.antlr.v4.runtime.tree.xpath;
|
||||
|
||||
/** Either ID at start of path or ...//ID in middle of path */
|
||||
public class XPathAnywhereElement extends XPathElement {
|
||||
public XPathAnywhereElement(String nodeName) {
|
||||
super(nodeName);
|
||||
}
|
||||
}
|
|
@ -1,14 +1,12 @@
|
|||
package org.antlr.v4.runtime.tree.xpath;
|
||||
|
||||
public class XPathElement {
|
||||
public XPathOperator op;
|
||||
public abstract class XPathElement {
|
||||
public String nodeName;
|
||||
|
||||
/** Construct element like /ID or //ID or ID or "/*" etc...
|
||||
/** Construct element like /ID or or ID or "/*" etc...
|
||||
* op is null if just node
|
||||
*/
|
||||
public XPathElement(XPathOperator op, String nodeName) {
|
||||
public XPathElement(String nodeName) {
|
||||
this.nodeName = nodeName;
|
||||
this.op = op;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
package org.antlr.v4.runtime.tree.xpath;
|
||||
|
||||
public class XPathNodeElement extends XPathElement {
|
||||
public XPathNodeElement(String nodeName) {
|
||||
super(nodeName);
|
||||
}
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
package org.antlr.v4.runtime.tree.xpath;
|
||||
|
||||
public enum XPathOperator {
|
||||
ROOT, // "/" first in path
|
||||
SEPARATOR, // "/" not first in path
|
||||
BELOW // "//"
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package org.antlr.v4.runtime.tree.xpath;
|
||||
|
||||
public class XPathRootedElement extends XPathElement {
|
||||
public XPathRootedElement(String nodeName) {
|
||||
super(nodeName);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
import org.antlr.v4.runtime.ANTLRFileStream;
|
||||
import org.antlr.v4.runtime.CharStream;
|
||||
import org.antlr.v4.runtime.CommonTokenStream;
|
||||
import org.antlr.v4.runtime.ParserRuleContext;
|
||||
import org.antlr.v4.runtime.tree.xpath.XPath;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class TestXPath {
|
||||
public static void main(String[] args) throws IOException {
|
||||
CharStream input = new ANTLRFileStream("TestXPath.java");
|
||||
JavaLexer lex = new JavaLexer(input);
|
||||
CommonTokenStream tokens = new CommonTokenStream(lex);
|
||||
JavaParser parser = new JavaParser(tokens);
|
||||
|
||||
parser.setBuildParseTree(true);
|
||||
ParserRuleContext tree = parser.compilationUnit();
|
||||
System.out.println(tree.toStringTree(parser));
|
||||
|
||||
XPath p = new XPath("ID");
|
||||
p.evaluate(tree);
|
||||
new XPath("A/B");
|
||||
new XPath("/A/B");
|
||||
new XPath("A//B");
|
||||
new XPath("A/*");
|
||||
new XPath("*");
|
||||
new XPath("*/A");
|
||||
new XPath("A/*/B");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue