Java Code Examples for us.codecraft.xsoup.XPathEvaluator

The following examples show how to use us.codecraft.xsoup.XPathEvaluator. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: xsoup   Source File: XPathParser.java    License: MIT License 6 votes vote down vote up
public XPathEvaluator parse() {

        while (!tq.isEmpty()) {
            Validate.isFalse(noEvalAllow, "XPath error! No operator allowed after attribute or function!" + tq);
            if (tq.matchChomp(OR_COMBINATOR)) {
                tq.consumeWhitespace();
                return combineXPathEvaluator(tq.remainder());
            } else if (tq.matchesAny(HIERARCHY_COMBINATORS)) {
                combinator(tq.consumeAny(HIERARCHY_COMBINATORS));
            } else {
                findElements();
            }
            tq.consumeWhitespace();
        }
        return collectXPathEvaluator();
    }
 
Example 2
Source Project: xsoup   Source File: XPathParser.java    License: MIT License 5 votes vote down vote up
private XPathEvaluator collectXPathEvaluator() {
    if (noEvalAllow) {
        return new DefaultXPathEvaluator(null, elementOperator);
    }

    if (evals.size() == 1)
        return new DefaultXPathEvaluator(evals.get(0), elementOperator);

    return new DefaultXPathEvaluator(new CombiningEvaluator.And(evals), elementOperator);
}
 
Example 3
Source Project: xsoup   Source File: CombingXPathEvaluator.java    License: MIT License 5 votes vote down vote up
@Override
public XElements evaluate(Element element) {
    List<XElements> xElementses = new ArrayList<XElements>();
    for (XPathEvaluator xPathEvaluator : xPathEvaluators) {
        xElementses.add(xPathEvaluator.evaluate(element));
    }
    return new CombiningDefaultXElements(xElementses);
}
 
Example 4
Source Project: xsoup   Source File: CombingXPathEvaluator.java    License: MIT License 5 votes vote down vote up
@Override
public boolean hasAttribute() {
    for (XPathEvaluator xPathEvaluator : xPathEvaluators) {
        if (xPathEvaluator.hasAttribute()){
            return true;
        }
    }
    return false;
}
 
Example 5
Source Project: xsoup   Source File: XPathParser.java    License: MIT License 4 votes vote down vote up
private XPathEvaluator combineXPathEvaluator(String subQuery) {
    XPathEvaluator xPathEvaluator = collectXPathEvaluator();
    return new CombingXPathEvaluator(xPathEvaluator, parse(subQuery));
}
 
Example 6
Source Project: xsoup   Source File: XPathParser.java    License: MIT License 4 votes vote down vote up
public static XPathEvaluator parse(String xpathStr) {
    XPathParser xPathParser = new XPathParser(xpathStr);
    return xPathParser.parse();
}
 
Example 7
Source Project: xsoup   Source File: CombingXPathEvaluator.java    License: MIT License 4 votes vote down vote up
public CombingXPathEvaluator(List<XPathEvaluator> xPathEvaluators) {
    this.xPathEvaluators = xPathEvaluators;
}
 
Example 8
Source Project: xsoup   Source File: CombingXPathEvaluator.java    License: MIT License 4 votes vote down vote up
public CombingXPathEvaluator(XPathEvaluator... xPathEvaluators) {
    this.xPathEvaluators = Arrays.asList(xPathEvaluators);
}
 
Example 9
Source Project: webmagic   Source File: XpathSelectorTest.java    License: Apache License 2.0 4 votes vote down vote up
@Ignore("take long time")
@Test
public void parserPerformanceTest() throws XPatherException {
    System.out.println(html.length());

    HtmlCleaner htmlCleaner = new HtmlCleaner();
    TagNode tagNode = htmlCleaner.clean(html);
    Document document = Jsoup.parse(html);

    long time =System.currentTimeMillis();
    for (int i = 0; i < 2000; i++) {
        htmlCleaner.clean(html);
    }
    System.out.println(System.currentTimeMillis()-time);

    time =System.currentTimeMillis();
    for (int i = 0; i < 2000; i++) {
        tagNode.evaluateXPath("//a");
    }
    System.out.println(System.currentTimeMillis()-time);

    System.out.println("=============");

    time =System.currentTimeMillis();
    for (int i = 0; i < 2000; i++) {
        Jsoup.parse(html);
    }
    System.out.println(System.currentTimeMillis()-time);

    time =System.currentTimeMillis();
    for (int i = 0; i < 2000; i++) {
        document.select("a");
    }
    System.out.println(System.currentTimeMillis()-time);

    System.out.println("=============");

    time =System.currentTimeMillis();
    for (int i = 0; i < 2000; i++) {
        htmlCleaner.clean(html);
    }
    System.out.println(System.currentTimeMillis()-time);

    time =System.currentTimeMillis();
    for (int i = 0; i < 2000; i++) {
        tagNode.evaluateXPath("//a");
    }
    System.out.println(System.currentTimeMillis()-time);

    System.out.println("=============");

    XPathEvaluator compile = Xsoup.compile("//a");
    time =System.currentTimeMillis();
    for (int i = 0; i < 2000; i++) {
        compile.evaluate(document);
    }
    System.out.println(System.currentTimeMillis()-time);

}