Search in sources :

Example 86 with Element

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

the class XpathProcessor method visitStep.

@Override
public XValue visitStep(XpathParser.StepContext ctx) {
    if (ctx.abbreviatedStep() != null && !ctx.abbreviatedStep().isEmpty()) {
        return visit(ctx.abbreviatedStep());
    }
    boolean filterByAttr = false;
    boolean isAxisOk = false;
    if (ctx.axisSpecifier() != null && !ctx.axisSpecifier().isEmpty()) {
        XValue axis = visit(ctx.axisSpecifier());
        if (axis != null) {
            isAxisOk = true;
            if (axis.isElements()) {
                updateCurrentContext(axis.asElements());
            } else if (axis.isAttr()) {
                filterByAttr = true;
            }
        }
    }
    if (ctx.nodeTest() != null && !ctx.nodeTest().isEmpty()) {
        XValue nodeTest = visit(ctx.nodeTest());
        if (filterByAttr) {
            Elements context = currentScope().context();
            String attrName = nodeTest.asString();
            if (context.size() == 1) {
                Element el = currentScope().singleEl();
                return XValue.create(el.attr(attrName));
            } else {
                List<String> attrs = new LinkedList<>();
                for (Element el : context) {
                    attrs.add(el.attr(attrName));
                }
                return XValue.create(attrs);
            }
        } else {
            if (nodeTest.isExprStr()) {
                String tagName = nodeTest.asString();
                Elements current = currentScope().context();
                if (currentScope().isRecursion()) {
                    updateCurrentContext(current.select(tagName));
                } else {
                    Elements newContext = new Elements();
                    for (Element el : currentScope().context()) {
                        if (isAxisOk) {
                            if (el.nodeName().equals(tagName) || "*".equals(tagName)) {
                                newContext.add(el);
                            }
                        } else {
                            for (Element e : el.children()) {
                                if (e.nodeName().equals(tagName) || "*".equals(tagName)) {
                                    newContext.add(e);
                                }
                            }
                        }
                    }
                    updateCurrentContext(newContext);
                }
            } else {
                // nodeType 计算结果,直接返给上层
                return nodeTest;
            }
        }
    }
    if (ctx.predicate() != null && ctx.predicate().size() > 0) {
        for (XpathParser.PredicateContext predicate : ctx.predicate()) {
            XValue predicateVal = visit(predicate);
            updateCurrentContext(predicateVal.asElements());
        }
    }
    return XValue.create(currentScope().context());
}
Also used : XpathParser(cn.wanghaomiao.xpath.antlr.XpathParser) Element(org.jsoup.nodes.Element) Elements(org.jsoup.select.Elements) LinkedList(java.util.LinkedList)

Example 87 with Element

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

the class AncestorOrSelfSelector method apply.

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

Example 88 with Element

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

the class DescendantOrSelfSelector method apply.

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

Example 89 with Element

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

the class FollowingSelector method apply.

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

Example 90 with Element

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

the class FollowingSiblingOneSelector method apply.

/**
 * @param context
 * @return res
 */
@Override
public XValue apply(Elements context) {
    Set<Element> total = new HashSet<>();
    for (Element el : context) {
        if (el.nextElementSibling() != null) {
            total.add(el.nextElementSibling());
        }
    }
    Elements newContext = new Elements();
    newContext.addAll(total);
    return XValue.create(newContext);
}
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