Search in sources :

Example 91 with Element

use of org.jsoup.nodes.Element in project JsoupXpath by zhegexiaohuozi.

the class JXDocument method selN.

public List<JXNode> selN(String xpath) throws XpathSyntaxErrorException {
    List<JXNode> finalRes = new LinkedList<>();
    try {
        CharStream input = CharStreams.fromString(xpath);
        XpathLexer lexer = new XpathLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        XpathParser parser = new XpathParser(tokens);
        parser.setErrorHandler(new DoFailOnErrorHandler());
        ParseTree tree = parser.main();
        XpathProcessor processor = new XpathProcessor(elements);
        XValue calRes = processor.visit(tree);
        if (calRes.isElements()) {
            for (Element el : calRes.asElements()) {
                finalRes.add(JXNode.e(el));
            }
        } else if (calRes.isList()) {
            for (String str : calRes.asList()) {
                finalRes.add(JXNode.t(str));
            }
        }
    } catch (Exception e) {
        String msg = "Please check the syntax of your xpath expr, ";
        throw new XpathSyntaxErrorException(msg + ExceptionUtils.getRootCauseMessage(e), e);
    }
    return finalRes;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) XpathLexer(cn.wanghaomiao.xpath.antlr.XpathLexer) Element(org.jsoup.nodes.Element) XValue(cn.wanghaomiao.xpath.core.XValue) LinkedList(java.util.LinkedList) CharStream(org.antlr.v4.runtime.CharStream) DoFailOnErrorHandler(cn.wanghaomiao.xpath.exception.DoFailOnErrorHandler) XpathSyntaxErrorException(cn.wanghaomiao.xpath.exception.XpathSyntaxErrorException) XpathParser(cn.wanghaomiao.xpath.antlr.XpathParser) XpathSyntaxErrorException(cn.wanghaomiao.xpath.exception.XpathSyntaxErrorException) XpathProcessor(cn.wanghaomiao.xpath.core.XpathProcessor) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 92 with Element

use of org.jsoup.nodes.Element in project JsoupXpath by zhegexiaohuozi.

the class CommonUtil method precedingSibling.

public static Elements precedingSibling(Element el) {
    Elements rs = new Elements();
    Element tmp = el.previousElementSibling();
    while (tmp != null) {
        rs.add(tmp);
        tmp = tmp.previousElementSibling();
    }
    if (rs.size() > 0) {
        return rs;
    }
    return null;
}
Also used : Element(org.jsoup.nodes.Element) Elements(org.jsoup.select.Elements)

Example 93 with Element

use of org.jsoup.nodes.Element 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 94 with Element

use of org.jsoup.nodes.Element in project JsoupXpath by zhegexiaohuozi.

the class ParentSelector method apply.

@Override
public XValue apply(Elements context) {
    Set<Element> total = new HashSet<>();
    Elements parents = new Elements();
    for (Element el : context) {
        total.add(el.parent());
    }
    parents.addAll(total);
    return XValue.create(parents);
}
Also used : Element(org.jsoup.nodes.Element) Elements(org.jsoup.select.Elements) HashSet(java.util.HashSet)

Example 95 with Element

use of org.jsoup.nodes.Element in project JsoupXpath by zhegexiaohuozi.

the class PrecedingSelector method apply.

/**
 * @param context
 * @return res
 */
@Override
public XValue apply(Elements context) {
    Elements preceding = new Elements();
    Set<Element> total = new HashSet<>();
    for (Element el : context) {
        Elements p = el.parents();
        for (Element pe : p) {
            Elements ps = CommonUtil.precedingSibling(pe);
            if (ps == null) {
                continue;
            }
            total.addAll(ps);
        }
        Elements ps = CommonUtil.precedingSibling(el);
        if (ps == null) {
            continue;
        }
        total.addAll(ps);
    }
    preceding.addAll(total);
    return XValue.create(preceding);
}
Also used : Element(org.jsoup.nodes.Element) Elements(org.jsoup.select.Elements) HashSet(java.util.HashSet)

Aggregations

Element (org.jsoup.nodes.Element)1237 Document (org.jsoup.nodes.Document)559 Elements (org.jsoup.select.Elements)529 ArrayList (java.util.ArrayList)316 IOException (java.io.IOException)220 Test (org.junit.Test)144 ElementHandlerImpl (org.asqatasun.ruleimplementation.ElementHandlerImpl)90 File (java.io.File)87 URL (java.net.URL)82 Matcher (java.util.regex.Matcher)73 List (java.util.List)60 HashMap (java.util.HashMap)57 Pattern (java.util.regex.Pattern)54 Node (org.jsoup.nodes.Node)50 TextNode (org.jsoup.nodes.TextNode)48 InputStream (java.io.InputStream)38 JSONException (org.json.JSONException)36 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)35 Map (java.util.Map)34 JSONObject (org.json.JSONObject)34