Skip to content

Commit

Permalink
working on xpath
Browse files Browse the repository at this point in the history
  • Loading branch information
parrt committed Sep 10, 2013
1 parent 145aeb9 commit d2caae4
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ nbactions*.xml
*.hprof

# Playground
/tool/playground/
#/tool/playground/
42 changes: 37 additions & 5 deletions runtime/Java/src/org/antlr/v4/runtime/tree/xpath/XPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

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.
Expand All @@ -14,27 +19,54 @@
* 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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.antlr.v4.runtime.tree.xpath;

public class XPathNodeElement extends XPathElement {
public XPathNodeElement(String nodeName) {
super(nodeName);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.antlr.v4.runtime.tree.xpath;

public class XPathRootedElement extends XPathElement {
public XPathRootedElement(String nodeName) {
super(nodeName);
}
}
31 changes: 31 additions & 0 deletions tool/playground/TestXPath.java
Original file line number Diff line number Diff line change
@@ -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");
}
}

0 comments on commit d2caae4

Please sign in to comment.