Search in sources :

Example 1 with NodeTest

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

the class Text method call.

/**
 * 函数具体逻辑
 *
 * @param scope 上下文
 * @return 计算好的节点
 */
@Override
public XValue call(Scope scope) {
    Elements context = scope.context();
    List<String> res = new LinkedList<>();
    if (context != null && context.size() > 0) {
        if (scope.isRecursion()) {
            NodeTest allTextFun = Scanner.findNodeTestByName("allText");
            return allTextFun.call(scope);
        } else {
            for (Element e : context) {
                if ("script".equals(e.nodeName())) {
                    res.add(e.data());
                } else {
                    res.add(e.ownText());
                }
            }
        }
    }
    return XValue.create(res);
}
Also used : Element(org.jsoup.nodes.Element) Elements(org.jsoup.select.Elements) LinkedList(java.util.LinkedList) NodeTest(cn.wanghaomiao.xpath.core.NodeTest)

Example 2 with NodeTest

use of cn.wanghaomiao.xpath.core.NodeTest 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

NodeTest (cn.wanghaomiao.xpath.core.NodeTest)2 XValue (cn.wanghaomiao.xpath.core.XValue)1 BigDecimal (java.math.BigDecimal)1 LinkedList (java.util.LinkedList)1 Matcher (java.util.regex.Matcher)1 Element (org.jsoup.nodes.Element)1 Elements (org.jsoup.select.Elements)1