use of org.seimicrawler.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 org.seimicrawler.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 (Element e : currentScope().context()) {
scopeStack.push(Scope.create(e).setParent(currentScope()));
XValue exprVal = visit(ctx.expr());
scopeStack.pop();
if (exprVal.isNumber()) {
long index = exprVal.asLong();
if (index < 0) {
if (Objects.equals(e.tagName(), Constants.DEF_TEXT_TAG_NAME)) {
index = CommonUtil.getJxSameTagNumsInSiblings(e) + index + 1;
} else {
index = CommonUtil.sameTagElNums(e, currentScope()) + index + 1;
}
if (index < 0) {
index = 1;
}
}
if (Objects.equals(e.tagName(), Constants.DEF_TEXT_TAG_NAME)) {
if (index == CommonUtil.getJxSameTagIndexInSiblings(e)) {
newContext.add(e);
}
} else {
if (index == CommonUtil.getElIndexInSameTags(e, currentScope())) {
newContext.add(e);
}
}
} else if (exprVal.isBoolean()) {
if (exprVal.asBoolean()) {
newContext.add(e);
}
} else if (exprVal.isString()) {
// 根据表达式执行结果是否为空作为条件,如 //*[@foo]
if (StringUtils.isNotBlank(exprVal.asString())) {
newContext.add(e);
}
} else if (exprVal.isElements()) {
// 根据表达式执行结果是否为空进行过滤,如 //div[./a]
Elements els = exprVal.asElements();
if (els.size() > 0) {
newContext.add(e);
}
} else if (exprVal.isList()) {
// 根据表达式执行结果是否为空进行过滤,如 //div[./a/text()]
List<String> stringList = exprVal.asList();
if (stringList.size() > 0) {
newContext.add(e);
}
} else {
throw new XpathParserException("unknown expr val:" + exprVal);
}
}
return XValue.create(newContext);
}
use of org.seimicrawler.xpath.exception.XpathParserException in project JsoupXpath by zhegexiaohuozi.
the class FormatDate method call.
@Override
public XValue call(Scope scope, List<XValue> params) {
String value = params.get(0).asString();
String patten = params.get(1).asString();
try {
if (params.size() > 2 && null != params.get(2)) {
final Locale locale = Locale.forLanguageTag(params.get(2).asString());
final SimpleDateFormat format = new SimpleDateFormat(patten, locale);
return XValue.create(format.parse(value));
}
return XValue.create(FastDateFormat.getInstance(patten).parse(value));
} catch (ParseException e) {
throw new XpathParserException("date format exception!", e);
}
}
Aggregations