use of org.antlr.v4.runtime.tree.xpath.XPathLexer in project JsoupXpath by zhegexiaohuozi.
the class JXDocument method selN.
public List<JXNode> selN(String xpath) throws XpathSyntaxErrorException {
List<JXNode> finalRes = new LinkedList<>();
try {
CharStream input = CharStreams.fromString(xpath);
XpathLexer lexer = new XpathLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
XpathParser parser = new XpathParser(tokens);
parser.setErrorHandler(new DoFailOnErrorHandler());
ParseTree tree = parser.main();
XpathProcessor processor = new XpathProcessor(elements);
XValue calRes = processor.visit(tree);
if (calRes.isElements()) {
for (Element el : calRes.asElements()) {
finalRes.add(JXNode.e(el));
}
} else if (calRes.isList()) {
for (String str : calRes.asList()) {
finalRes.add(JXNode.t(str));
}
}
} catch (Exception e) {
String msg = "Please check the syntax of your xpath expr, ";
throw new XpathSyntaxErrorException(msg + ExceptionUtils.getRootCauseMessage(e), e);
}
return finalRes;
}
use of org.antlr.v4.runtime.tree.xpath.XPathLexer in project JsoupXpath by zhegexiaohuozi.
the class JXDocument method selN.
public List<JXNode> selN(String xpath) {
List<JXNode> finalRes = new LinkedList<>();
try {
CharStream input = CharStreams.fromString(xpath);
XpathLexer lexer = new XpathLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
XpathParser parser = new XpathParser(tokens);
parser.setErrorHandler(new DoFailOnErrorHandler());
ParseTree tree = parser.main();
XpathProcessor processor = new XpathProcessor(elements);
XValue calRes = processor.visit(tree);
if (calRes == null) {
finalRes.add(JXNode.create(""));
return finalRes;
}
if (calRes.isElements()) {
for (Element el : calRes.asElements()) {
finalRes.add(JXNode.create(el));
}
return finalRes;
}
if (calRes.isList()) {
for (String str : calRes.asList()) {
finalRes.add(JXNode.create(str));
}
return finalRes;
}
if (calRes.isString()) {
finalRes.add(JXNode.create(calRes.asString()));
return finalRes;
}
if (calRes.isNumber()) {
Class vType = calRes.valType();
if (vType.isAssignableFrom(Long.class) || vType.isAssignableFrom(Integer.class)) {
finalRes.add(JXNode.create(calRes.asLong()));
} else {
finalRes.add(JXNode.create(calRes.asDouble()));
}
return finalRes;
}
if (calRes.isBoolean()) {
finalRes.add(JXNode.create(calRes.asBoolean()));
return finalRes;
}
if (calRes.isDate()) {
finalRes.add(JXNode.create(calRes.asDate()));
return finalRes;
}
finalRes.add(JXNode.create(calRes.asString()));
} catch (Exception e) {
String msg = "Please check the syntax of your xpath expr or commit a Issue. ";
throw new XpathSyntaxErrorException(msg + ExceptionUtils.getRootCauseMessage(e), e);
}
return finalRes;
}
use of org.antlr.v4.runtime.tree.xpath.XPathLexer in project JsoupXpath by zhegexiaohuozi.
the class ExprTest method exp.
@Test
public void exp() {
String xpath = "//a[@id]/@href";
CharStream input = CharStreams.fromString(xpath);
XpathLexer lexer = new XpathLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
XpathParser parser = new XpathParser(tokens);
parser.setErrorHandler(new DoFailOnErrorHandler());
ParseTree tree = parser.main();
XpathProcessor processor = new XpathProcessor(root);
XValue value = processor.visit(tree);
logger.info("visit res = {}", value);
}
use of org.antlr.v4.runtime.tree.xpath.XPathLexer in project antlr4 by tunnelvisionlabs.
the class XPath method split.
// TODO: check for invalid token/rule names, bad syntax
public XPathElement[] split(String path) {
XPathLexer lexer = new XPathLexer(CharStreams.fromString(path)) {
@Override
public void recover(LexerNoViableAltException e) {
throw e;
}
};
lexer.removeErrorListeners();
lexer.addErrorListener(new XPathLexerErrorListener());
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
try {
tokenStream.fill();
} catch (LexerNoViableAltException e) {
int pos = lexer.getCharPositionInLine();
String msg = "Invalid tokens or characters at index " + pos + " in path '" + path + "'";
throw new IllegalArgumentException(msg, e);
}
List<Token> tokens = tokenStream.getTokens();
// System.out.println("path="+path+"=>"+tokens);
List<XPathElement> elements = new ArrayList<XPathElement>();
int n = tokens.size();
int i = 0;
loop: while (i < n) {
Token el = tokens.get(i);
Token next = null;
switch(el.getType()) {
case XPathLexer.ROOT:
case XPathLexer.ANYWHERE:
boolean anywhere = el.getType() == XPathLexer.ANYWHERE;
i++;
next = tokens.get(i);
boolean invert = next.getType() == XPathLexer.BANG;
if (invert) {
i++;
next = tokens.get(i);
}
XPathElement pathElement = getXPathElement(next, anywhere);
pathElement.invert = invert;
elements.add(pathElement);
i++;
break;
case XPathLexer.TOKEN_REF:
case XPathLexer.RULE_REF:
case XPathLexer.WILDCARD:
elements.add(getXPathElement(el, false));
i++;
break;
case Token.EOF:
break loop;
default:
throw new IllegalArgumentException("Unknowth path element " + el);
}
}
return elements.toArray(new XPathElement[0]);
}
use of org.antlr.v4.runtime.tree.xpath.XPathLexer in project antlr4 by antlr.
the class XPath method split.
// TODO: check for invalid token/rule names, bad syntax
public XPathElement[] split(String path) {
ANTLRInputStream in;
try {
in = new ANTLRInputStream(new StringReader(path));
} catch (IOException ioe) {
throw new IllegalArgumentException("Could not read path: " + path, ioe);
}
XPathLexer lexer = new XPathLexer(in) {
@Override
public void recover(LexerNoViableAltException e) {
throw e;
}
};
lexer.removeErrorListeners();
lexer.addErrorListener(new XPathLexerErrorListener());
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
try {
tokenStream.fill();
} catch (LexerNoViableAltException e) {
int pos = lexer.getCharPositionInLine();
String msg = "Invalid tokens or characters at index " + pos + " in path '" + path + "'";
throw new IllegalArgumentException(msg, e);
}
List<Token> tokens = tokenStream.getTokens();
// System.out.println("path="+path+"=>"+tokens);
List<XPathElement> elements = new ArrayList<XPathElement>();
int n = tokens.size();
int i = 0;
loop: while (i < n) {
Token el = tokens.get(i);
Token next = null;
switch(el.getType()) {
case XPathLexer.ROOT:
case XPathLexer.ANYWHERE:
boolean anywhere = el.getType() == XPathLexer.ANYWHERE;
i++;
next = tokens.get(i);
boolean invert = next.getType() == XPathLexer.BANG;
if (invert) {
i++;
next = tokens.get(i);
}
XPathElement pathElement = getXPathElement(next, anywhere);
pathElement.invert = invert;
elements.add(pathElement);
i++;
break;
case XPathLexer.TOKEN_REF:
case XPathLexer.RULE_REF:
case XPathLexer.WILDCARD:
elements.add(getXPathElement(el, false));
i++;
break;
case Token.EOF:
break loop;
default:
throw new IllegalArgumentException("Unknowth path element " + el);
}
}
return elements.toArray(new XPathElement[0]);
}
Aggregations