Java Code Examples for org.jsoup.select.Evaluator

The following examples show how to use org.jsoup.select.Evaluator. 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: zongtui-webcrawler   Author: zongtui   File: XPathParser.java    License: GNU General Public License v2.0 6 votes vote down vote up
private Evaluator byFunction(XTokenQueue predicatesQueue) {
    for (Map.Entry<String, FunctionEvaluator> entry : FUNCTION_MAPPING.entrySet()) {
        if (predicatesQueue.matchChomp(entry.getKey())) {
            String paramString = predicatesQueue.chompBalanced('(', ')');
            List<String> params = XTokenQueue.trimQuotes(XTokenQueue.parseFuncionParams(paramString));

            if (params.get(0).startsWith("@")) {
                params.set(0, params.get(0).substring(1));
                return entry.getValue().call(params.toArray(new String[0]));
            } else {
                return null;
            }
        }
    }

    throw new Selector.SelectorParseException("Could not parse query '%s': unexpected token at '%s'", query, predicatesQueue.remainder());
}
 
Example #2
Source Project: xsoup   Author: code4craft   File: CombiningEvaluator.java    License: MIT License 5 votes vote down vote up
@Override
public boolean matches(Element root, Element node) {
    for (int i = 0; i < evaluators.size(); i++) {
        Evaluator s = evaluators.get(i);
        if (s.matches(root, node))
            return true;
    }
    return false;
}
 
Example #3
Source Project: xsoup   Author: code4craft   File: XPathParser.java    License: MIT License 5 votes vote down vote up
private void byTag() {
    String tagName = tq.consumeElementSelector();
    Validate.notEmpty(tagName);

    // namespaces: if element name is "abc:def", selector must be "abc|def", so flip:
    if (tagName.contains("|"))
        tagName = tagName.replace("|", ":");

    evals.add(new Evaluator.Tag(tagName.trim().toLowerCase()));
}
 
Example #4
Source Project: xsoup   Author: code4craft   File: XPathParser.java    License: MIT License 5 votes vote down vote up
public void calc(Evaluator evaluator, Operation operation) {
    if (size() == 0) {
        push(evaluator);
    } else {
        if (operation == Operation.AND) {
            evaluator = new CombiningEvaluator.And(pop(), evaluator);
        } else {
            mergeOr();
        }
        push(evaluator);
    }
}
 
Example #5
Source Project: zongtui-webcrawler   Author: zongtui   File: CombiningEvaluator.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean matches(Element root, Element node) {
    for (int i = 0; i < evaluators.size(); i++) {
        Evaluator s = evaluators.get(i);
        if (s.matches(root, node))
            return true;
    }
    return false;
}
 
Example #6
Source Project: zongtui-webcrawler   Author: zongtui   File: XPathParser.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void calc(Evaluator evaluator, Operation operation) {
    if (size() == 0) {
        push(evaluator);
    } else {
        if (operation == Operation.AND) {
            evaluator = new CombiningEvaluator.And(pop(), evaluator);
        } else {
            mergeOr();
        }
        push(evaluator);
    }
}
 
Example #7
Source Project: zongtui-webcrawler   Author: zongtui   File: XPathParser.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void mergeOr() {
    if (size() >= 2) {
        Evaluator pop1 = pop();
        Evaluator pop2 = pop();
        Evaluator tempEvaluator = new CombiningEvaluator.Or(pop2, pop1);
        push(tempEvaluator);
    }
}
 
Example #8
Source Project: zongtui-webcrawler   Author: zongtui   File: XPathParser.java    License: GNU General Public License v2.0 5 votes vote down vote up
private Evaluator consumePredicates(String queue) {
    XTokenQueue predicatesQueue = new XTokenQueue(queue);
    EvaluatorStack evaluatorStack = new EvaluatorStack();
    Operation currentOperation = null;
    predicatesQueue.consumeWhitespace();
    while (!predicatesQueue.isEmpty()) {
        if (predicatesQueue.matchChomp("and")) {
            currentOperation = Operation.AND;
        } else if (predicatesQueue.matchChomp("or")) {
            currentOperation = Operation.OR;
        } else {
            if (currentOperation == null && evaluatorStack.size() > 0) {
                throw new IllegalArgumentException(String.format("Need AND/OR between two predicate! %s", predicatesQueue.remainder()));
            }
            Evaluator evaluator;
            if (predicatesQueue.matches("(")) {
                evaluator = consumePredicates(predicatesQueue.chompBalanced('(', ')'));
            } else if (predicatesQueue.matches("@")) {
                evaluator = byAttribute(predicatesQueue);
            } else if (predicatesQueue.matchesRegex("\\w+.*")) {
                evaluator = byFunction(predicatesQueue);
            } else {
                throw new Selector.SelectorParseException("Could not parse query '%s': unexpected token at '%s'", query, predicatesQueue.remainder());
            }
            evaluatorStack.calc(evaluator, currentOperation);
            //consume operator
            currentOperation = null;
        }
        predicatesQueue.consumeWhitespace();
    }
    evaluatorStack.mergeOr();
    return evaluatorStack.peek();
}
 
Example #9
Source Project: astor   Author: SpoonLabs   File: Element.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Finds elements, including and recursively under this element, with the specified tag name.
 * @param tagName The tag name to search for (case insensitively).
 * @return a matching unmodifiable list of elements. Will be empty if this element and none of its children match.
 */
public Elements getElementsByTag(String tagName) {
    Validate.notEmpty(tagName);
    tagName = tagName.toLowerCase().trim();

    return Collector.collect(new Evaluator.Tag(tagName), this);
}
 
Example #10
Source Project: astor   Author: SpoonLabs   File: Element.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Find elements that have a named attribute set. Case insensitive.
 *
 * @param key name of the attribute, e.g. {@code href}
 * @return elements that have this attribute, empty if none
 */
public Elements getElementsByAttribute(String key) {
    Validate.notEmpty(key);
    key = key.trim().toLowerCase();

    return Collector.collect(new Evaluator.Attribute(key), this);
}
 
Example #11
Source Project: astor   Author: SpoonLabs   File: Element.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Find elements that have an attribute name starting with the supplied prefix. Use {@code data-} to find elements
 * that have HTML5 datasets.
 * @param keyPrefix name prefix of the attribute e.g. {@code data-}
 * @return elements that have attribute names that start with with the prefix, empty if none.
 */
public Elements getElementsByAttributeStarting(String keyPrefix) {
    Validate.notEmpty(keyPrefix);
    keyPrefix = keyPrefix.trim().toLowerCase();

    return Collector.collect(new Evaluator.AttributeStarting(keyPrefix), this);
}
 
Example #12
Source Project: xsoup   Author: code4craft   File: XPathParser.java    License: MIT License 5 votes vote down vote up
public void mergeOr() {
    if (size() >= 2) {
        Evaluator pop1 = pop();
        Evaluator pop2 = pop();
        Evaluator tempEvaluator = new CombiningEvaluator.Or(pop2, pop1);
        push(tempEvaluator);
    }
}
 
Example #13
Source Project: xsoup   Author: code4craft   File: XPathParser.java    License: MIT License 5 votes vote down vote up
private Evaluator consumePredicates(String queue) {
    XTokenQueue predicatesQueue = new XTokenQueue(queue);
    EvaluatorStack evaluatorStack = new EvaluatorStack();
    Operation currentOperation = null;
    predicatesQueue.consumeWhitespace();
    while (!predicatesQueue.isEmpty()) {
        if (predicatesQueue.matchChomp("and")) {
            currentOperation = Operation.AND;
        } else if (predicatesQueue.matchChomp("or")) {
            currentOperation = Operation.OR;
        } else {
            if (currentOperation == null && evaluatorStack.size() > 0) {
                throw new IllegalArgumentException(String.format("Need AND/OR between two predicate! %s", predicatesQueue.remainder()));
            }
            Evaluator evaluator;
            if (predicatesQueue.matches("(")) {
                evaluator = consumePredicates(predicatesQueue.chompBalanced('(', ')'));
            } else if (predicatesQueue.matches("@")) {
                evaluator = byAttribute(predicatesQueue);
            } else if (predicatesQueue.matchesRegex("\\w+.*")) {
                evaluator = byFunction(predicatesQueue);
            } else {
                throw new Selector.SelectorParseException("Could not parse query '%s': unexpected token at '%s'", query, predicatesQueue.remainder());
            }
            evaluatorStack.calc(evaluator, currentOperation);
            //consume operator
            currentOperation = null;
        }
        predicatesQueue.consumeWhitespace();
    }
    evaluatorStack.mergeOr();
    return evaluatorStack.peek();
}
 
Example #14
Source Project: astor   Author: SpoonLabs   File: Element.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Find an element by ID, including or under this element.
 * <p>
 * Note that this finds the first matching ID, starting with this element. If you search down from a different
 * starting point, it is possible to find a different element by ID. For unique element by ID within a Document,
 * use {@link Document#getElementById(String)}
 * @param id The ID to search for.
 * @return The first matching element by ID, starting with this element, or null if none found.
 */
public Element getElementById(String id) {
    Validate.notEmpty(id);
    
    Elements elements = Collector.collect(new Evaluator.Id(id), this);
    if (elements.size() > 0)
        return elements.get(0);
    else
        return null;
}
 
Example #15
Source Project: astor   Author: SpoonLabs   File: Element.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Find elements that have an attribute name starting with the supplied prefix. Use {@code data-} to find elements
 * that have HTML5 datasets.
 * @param keyPrefix name prefix of the attribute e.g. {@code data-}
 * @return elements that have attribute names that start with with the prefix, empty if none.
 */
public Elements getElementsByAttributeStarting(String keyPrefix) {
    Validate.notEmpty(keyPrefix);
    keyPrefix = keyPrefix.trim();

    return Collector.collect(new Evaluator.AttributeStarting(keyPrefix), this);
}
 
Example #16
Source Project: xsoup   Author: code4craft   File: CombiningEvaluator.java    License: MIT License 5 votes vote down vote up
@Override
public boolean matches(Element root, Element node) {
    for (int i = 0; i < evaluators.size(); i++) {
        Evaluator s = evaluators.get(i);
        if (!s.matches(root, node))
            return false;
    }
    return true;
}
 
Example #17
Source Project: astor   Author: SpoonLabs   File: Element.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Finds elements, including and recursively under this element, with the specified tag name.
 * @param tagName The tag name to search for (case insensitively).
 * @return a matching unmodifiable list of elements. Will be empty if this element and none of its children match.
 */
public Elements getElementsByTag(String tagName) {
    Validate.notEmpty(tagName);
    tagName = normalize(tagName);

    return Collector.collect(new Evaluator.Tag(tagName), this);
}
 
Example #18
Source Project: astor   Author: SpoonLabs   File: Element.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Find an element by ID, including or under this element.
 * <p>
 * Note that this finds the first matching ID, starting with this element. If you search down from a different
 * starting point, it is possible to find a different element by ID. For unique element by ID within a Document,
 * use {@link Document#getElementById(String)}
 * @param id The ID to search for.
 * @return The first matching element by ID, starting with this element, or null if none found.
 */
public Element getElementById(String id) {
    Validate.notEmpty(id);
    
    Elements elements = Collector.collect(new Evaluator.Id(id), this);
    if (elements.size() > 0)
        return elements.get(0);
    else
        return null;
}
 
Example #19
Source Project: astor   Author: SpoonLabs   File: Element.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Find elements that have an attribute name starting with the supplied prefix. Use {@code data-} to find elements
 * that have HTML5 datasets.
 * @param keyPrefix name prefix of the attribute e.g. {@code data-}
 * @return elements that have attribute names that start with with the prefix, empty if none.
 */
public Elements getElementsByAttributeStarting(String keyPrefix) {
    Validate.notEmpty(keyPrefix);
    keyPrefix = keyPrefix.trim();

    return Collector.collect(new Evaluator.AttributeStarting(keyPrefix), this);
}
 
Example #20
Source Project: zongtui-webcrawler   Author: zongtui   File: CombiningEvaluator.java    License: GNU General Public License v2.0 4 votes vote down vote up
CombiningEvaluator() {
    super();
    evaluators = new ArrayList<Evaluator>();
}
 
Example #21
Source Project: xsoup   Author: code4craft   File: CombiningEvaluator.java    License: MIT License 4 votes vote down vote up
public void add(Evaluator e) {
    evaluators.add(e);
}
 
Example #22
Source Project: zongtui-webcrawler   Author: zongtui   File: CombiningEvaluator.java    License: GNU General Public License v2.0 4 votes vote down vote up
And(Evaluator... evaluators) {
    this(Arrays.asList(evaluators));
}
 
Example #23
Source Project: zongtui-webcrawler   Author: zongtui   File: CombiningEvaluator.java    License: GNU General Public License v2.0 4 votes vote down vote up
Or(Collection<Evaluator> evaluators) {
    super();
    this.evaluators.addAll(evaluators);
}
 
Example #24
Source Project: zongtui-webcrawler   Author: zongtui   File: CombiningEvaluator.java    License: GNU General Public License v2.0 4 votes vote down vote up
Or(Evaluator... evaluators) {
    this(Arrays.asList(evaluators));
}
 
Example #25
Source Project: zongtui-webcrawler   Author: zongtui   File: StructuralEvaluator.java    License: GNU General Public License v2.0 4 votes vote down vote up
public Has(Evaluator evaluator) {
    this.evaluator = evaluator;
}
 
Example #26
Source Project: zongtui-webcrawler   Author: zongtui   File: StructuralEvaluator.java    License: GNU General Public License v2.0 4 votes vote down vote up
public Parent(Evaluator evaluator) {
    this.evaluator = evaluator;
}
 
Example #27
Source Project: xsoup   Author: code4craft   File: CombiningEvaluator.java    License: MIT License 4 votes vote down vote up
And(Evaluator... evaluators) {
    this(Arrays.asList(evaluators));
}
 
Example #28
Source Project: zongtui-webcrawler   Author: zongtui   File: StructuralEvaluator.java    License: GNU General Public License v2.0 4 votes vote down vote up
public PreviousSibling(Evaluator evaluator) {
    this.evaluator = evaluator;
}
 
Example #29
Source Project: zongtui-webcrawler   Author: zongtui   File: StructuralEvaluator.java    License: GNU General Public License v2.0 4 votes vote down vote up
public ImmediatePreviousSibling(Evaluator evaluator) {
    this.evaluator = evaluator;
}
 
Example #30
Source Project: zongtui-webcrawler   Author: zongtui   File: XPathParser.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public Evaluator call(String... param) {
    Validate.isTrue(param.length == 2, String.format("Error argument of %s", "contains"));
    return new Evaluator.AttributeWithValueContaining(param[0], param[1]);
}