Search in sources :

Example 6 with XValue

use of cn.wanghaomiao.xpath.core.XValue in project JsoupXpath by zhegexiaohuozi.

the class Num method call.

/**
 * 函数具体逻辑
 *
 * @param scope 上下文
 * @return 计算好的节点
 */
@Override
public XValue call(Scope scope) {
    NodeTest textFun = Scanner.findNodeTestByName("allText");
    XValue textVal = textFun.call(scope);
    String whole = StringUtils.join(textVal.asList(), "");
    Matcher matcher = numExt.matcher(whole);
    if (matcher.find()) {
        String numStr = matcher.group();
        BigDecimal num = new BigDecimal(numStr);
        return XValue.create(num.doubleValue());
    } else {
        return XValue.create(null);
    }
}
Also used : Matcher(java.util.regex.Matcher) XValue(cn.wanghaomiao.xpath.core.XValue) NodeTest(cn.wanghaomiao.xpath.core.NodeTest) BigDecimal(java.math.BigDecimal)

Aggregations

XValue (cn.wanghaomiao.xpath.core.XValue)6 BaseTest (cn.wanghaomiao.xpath.BaseTest)4 Element (org.jsoup.nodes.Element)4 Test (org.junit.Test)4 Elements (org.jsoup.select.Elements)3 XpathLexer (cn.wanghaomiao.xpath.antlr.XpathLexer)2 XpathParser (cn.wanghaomiao.xpath.antlr.XpathParser)2 XpathProcessor (cn.wanghaomiao.xpath.core.XpathProcessor)2 DoFailOnErrorHandler (cn.wanghaomiao.xpath.exception.DoFailOnErrorHandler)2 CharStream (org.antlr.v4.runtime.CharStream)2 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)2 ParseTree (org.antlr.v4.runtime.tree.ParseTree)2 NodeTest (cn.wanghaomiao.xpath.core.NodeTest)1 XpathSyntaxErrorException (cn.wanghaomiao.xpath.exception.XpathSyntaxErrorException)1 BigDecimal (java.math.BigDecimal)1 LinkedList (java.util.LinkedList)1 Matcher (java.util.regex.Matcher)1