Search in sources :

Example 1 with NodeTest

use of org.seimicrawler.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 = Constants.NUM_PATTERN.matcher(whole);
    if (matcher.find()) {
        String numStr = matcher.group();
        BigDecimal num = new BigDecimal(numStr);
        if (num.compareTo(new BigDecimal(num.longValue())) == 0) {
            return XValue.create(num.longValue());
        }
        return XValue.create(num.doubleValue());
    } else {
        return XValue.create(null);
    }
}
Also used : Matcher(java.util.regex.Matcher) XValue(org.seimicrawler.xpath.core.XValue) NodeTest(org.seimicrawler.xpath.core.NodeTest) BigDecimal(java.math.BigDecimal)

Example 2 with NodeTest

use of org.seimicrawler.xpath.core.NodeTest in project JsoupXpath by zhegexiaohuozi.

the class Scanner method registerNodeTest.

public static void registerNodeTest(Class<? extends NodeTest> nodeTestClass) {
    NodeTest nodeTest;
    try {
        nodeTest = nodeTestClass.newInstance();
        nodeTestMap.put(nodeTest.name(), nodeTest);
    } catch (Exception e) {
        logger.info(ExceptionUtils.getRootCauseMessage(e), e);
    }
}
Also used : NodeTest(org.seimicrawler.xpath.core.NodeTest) NoSuchFunctionException(org.seimicrawler.xpath.exception.NoSuchFunctionException) NoSuchAxisException(org.seimicrawler.xpath.exception.NoSuchAxisException)

Aggregations

NodeTest (org.seimicrawler.xpath.core.NodeTest)2 BigDecimal (java.math.BigDecimal)1 Matcher (java.util.regex.Matcher)1 XValue (org.seimicrawler.xpath.core.XValue)1 NoSuchAxisException (org.seimicrawler.xpath.exception.NoSuchAxisException)1 NoSuchFunctionException (org.seimicrawler.xpath.exception.NoSuchFunctionException)1