forked from jasder/antlr
tweak
This commit is contained in:
parent
d2caae46a7
commit
d505a393ef
|
@ -22,6 +22,8 @@ import java.util.regex.Pattern;
|
||||||
* //ID INVALID (same as ID)
|
* //ID INVALID (same as ID)
|
||||||
* classdef//funcdef all funcs under classdef somewhere
|
* classdef//funcdef all funcs under classdef somewhere
|
||||||
* classdef/* all children of classdefs anywhere in tree
|
* classdef/* all children of classdefs anywhere in tree
|
||||||
|
* * INVALID
|
||||||
|
* * slash * INVALID
|
||||||
*
|
*
|
||||||
* The "root" is relative to the node passed to evaluate().
|
* The "root" is relative to the node passed to evaluate().
|
||||||
*/
|
*/
|
||||||
|
@ -34,30 +36,53 @@ public class XPath {
|
||||||
public XPath(String path) {
|
public XPath(String path) {
|
||||||
this.path = path;
|
this.path = path;
|
||||||
elements = split(path);
|
elements = split(path);
|
||||||
|
System.out.println(Arrays.toString(elements));
|
||||||
}
|
}
|
||||||
|
|
||||||
public XPathElement[] split(String path) {
|
public XPathElement[] split(String path) {
|
||||||
Pattern pattern = Pattern.compile("//|/|\\w+|\\*");
|
Pattern pattern = Pattern.compile("//|/|\\w+|\\*");
|
||||||
Matcher matcher = pattern.matcher(path);
|
Matcher matcher = pattern.matcher(path);
|
||||||
System.out.println("path="+path);
|
|
||||||
List<String> pathStrings = new ArrayList<String>();
|
List<String> pathStrings = new ArrayList<String>();
|
||||||
while (matcher.find()) {
|
while (matcher.find()) {
|
||||||
pathStrings.add(matcher.group());
|
pathStrings.add(matcher.group());
|
||||||
}
|
}
|
||||||
|
System.out.println("path="+path+"=>"+pathStrings);
|
||||||
List<XPathElement> elements = new ArrayList<XPathElement>();
|
List<XPathElement> elements = new ArrayList<XPathElement>();
|
||||||
for (String el : pathStrings) {
|
int n = pathStrings.size();
|
||||||
System.out.println("\t"+ el);
|
int i=0;
|
||||||
|
while ( i<n ) {
|
||||||
|
String el = pathStrings.get(i);
|
||||||
if ( el.equals("/") ) {
|
if ( el.equals("/") ) {
|
||||||
|
i++;
|
||||||
|
if ( i>=n ) {
|
||||||
|
System.out.println("missing element name after operator");
|
||||||
|
}
|
||||||
|
String next = pathStrings.get(i);
|
||||||
|
if ( i==1 ) { // "/ID" is rooted element if '/' is first el
|
||||||
|
elements.add(new XPathRootedElement(el));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
elements.add(new XPathNodeElement(next));
|
||||||
|
}
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
else if ( el.equals("//") ) {
|
else if ( el.equals("//") ) {
|
||||||
|
i++;
|
||||||
}
|
if ( i>=n ) {
|
||||||
else if ( el.equals("*") ) {
|
System.out.println("missing element name after operator");
|
||||||
|
}
|
||||||
|
String next = pathStrings.get(i);
|
||||||
|
elements.add(new XPathAnywhereElement(next));
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
elements.add(new XPathNodeElement(el));
|
if ( i==0 ) { // "ID" is first element w/o a "//" or "/"
|
||||||
|
elements.add(new XPathAnywhereElement(el));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
elements.add(new XPathNodeElement(el));
|
||||||
|
}
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return elements.toArray(new XPathElement[0]);
|
return elements.toArray(new XPathElement[0]);
|
||||||
|
|
|
@ -9,4 +9,9 @@ public abstract class XPathElement {
|
||||||
public XPathElement(String nodeName) {
|
public XPathElement(String nodeName) {
|
||||||
this.nodeName = nodeName;
|
this.nodeName = nodeName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return getClass().getSimpleName()+"["+nodeName+"]";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue