use of cn.wanghaomiao.xpath.exception.XpathParserException in project JsoupXpath by zhegexiaohuozi.
the class XpathProcessor method visitAdditiveExpr.
@Override
public XValue visitAdditiveExpr(XpathParser.AdditiveExprContext ctx) {
List<XpathParser.MultiplicativeExprContext> multiplicativeExprContexts = ctx.multiplicativeExpr();
if (multiplicativeExprContexts.size() == 1) {
return visit(multiplicativeExprContexts.get(0));
} else {
Double res = visit(multiplicativeExprContexts.get(0)).asDouble();
String op = null;
for (int i = 1; i < ctx.getChildCount(); i++) {
ParseTree chiCtx = ctx.getChild(i);
if (chiCtx instanceof XpathParser.MultiplicativeExprContext) {
XValue next = visit(chiCtx);
if ("+".equals(op)) {
res += next.asDouble();
} else if ("-".equals(op)) {
res -= next.asDouble();
} else {
throw new XpathParserException("syntax error, " + ctx.getText());
}
} else {
op = chiCtx.getText();
}
}
return XValue.create(res);
}
}
use of cn.wanghaomiao.xpath.exception.XpathParserException in project JsoupXpath by zhegexiaohuozi.
the class XpathProcessor method visitPredicate.
@Override
public XValue visitPredicate(XpathParser.PredicateContext ctx) {
Elements newContext = new Elements();
for (int i = 0; i < currentScope().context().size(); i++) {
Element e = currentScope().context().get(i);
scopeStack.push(Scope.create(e).setParent(currentScope()));
XValue exprVal = visit(ctx.expr());
scopeStack.pop();
if (exprVal.isNumber()) {
long index = exprVal.asLong();
if (index < 0) {
index = currentScope().context().size() + index + 1;
}
if (index == i + 1) {
newContext.add(e);
}
} else if (exprVal.isBoolean()) {
if (exprVal.asBoolean()) {
newContext.add(e);
}
} else {
throw new XpathParserException("unknown expr val:" + exprVal);
}
}
return XValue.create(newContext);
}
Aggregations