org.jsoup.select.Evaluator Java Examples

The following examples show how to use org.jsoup.select.Evaluator. 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 File: XPathParser.java    From zongtui-webcrawler with 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 File: XPathParser.java    From xsoup with 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 #3
Source File: XPathParser.java    From zongtui-webcrawler with 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 #4
Source File: Element.java    From astor with 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 #5
Source File: XPathParser.java    From zongtui-webcrawler with 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 #6
Source File: XPathParser.java    From zongtui-webcrawler with 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 File: Element.java    From astor with 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 #8
Source File: Element.java    From astor with 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 #9
Source File: Element.java    From astor with 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 #10
Source File: Element.java    From astor with 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 #11
Source File: XPathParser.java    From xsoup with 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 #12
Source File: CombiningEvaluator.java    From zongtui-webcrawler with 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 #13
Source File: CombiningEvaluator.java    From xsoup with 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 #14
Source File: Element.java    From astor with 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 File: Element.java    From astor with 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 #16
Source File: XPathParser.java    From xsoup with 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 #17
Source File: XPathParser.java    From xsoup with 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 #18
Source File: Element.java    From astor with 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 File: CombiningEvaluator.java    From xsoup with 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 #20
Source File: CombiningEvaluator.java    From zongtui-webcrawler with GNU General Public License v2.0 4 votes vote down vote up
Or(Collection<Evaluator> evaluators) {
    super();
    this.evaluators.addAll(evaluators);
}
 
Example #21
Source File: StructuralEvaluator.java    From zongtui-webcrawler with GNU General Public License v2.0 4 votes vote down vote up
public Has(Evaluator evaluator) {
    this.evaluator = evaluator;
}
 
Example #22
Source File: XPathParser.java    From zongtui-webcrawler with GNU General Public License v2.0 4 votes vote down vote up
private Evaluator byAttribute(XTokenQueue cq) {
    cq.matchChomp("@");
    String key = cq.consumeToAny("=", "!=", "^=", "$=", "*=", "~="); // eq, not, start, end, contain, match, (no val)
    Validate.notEmpty(key);
    cq.consumeWhitespace();
    Evaluator evaluator;
    if (cq.isEmpty()) {
        if ("*".equals(key)) {
            evaluator = new XEvaluators.HasAnyAttribute();
        } else {
            evaluator = new Evaluator.Attribute(key);
        }
    } else {
        if (cq.matchChomp("=")) {
            String value = chompEqualValue(cq);
            //to support select one class out of all
            if (key.equals("class")) {
                String className = XTokenQueue.trimQuotes(value);
                if (!className.contains(" ")) {
                    evaluator = new Evaluator.Class(className);
                } else {
                    evaluator = new Evaluator.AttributeWithValue(key, className);
                }
            } else {
                evaluator = new Evaluator.AttributeWithValue(key, XTokenQueue.trimQuotes(value));
            }
        } else if (cq.matchChomp("!="))
            evaluator = new Evaluator.AttributeWithValueNot(key, XTokenQueue.trimQuotes(chompEqualValue(cq)));

        else if (cq.matchChomp("^="))
            evaluator = new Evaluator.AttributeWithValueStarting(key, XTokenQueue.trimQuotes(chompEqualValue(cq)));

        else if (cq.matchChomp("$="))
            evaluator = new Evaluator.AttributeWithValueEnding(key, XTokenQueue.trimQuotes(chompEqualValue(cq)));

        else if (cq.matchChomp("*="))
            evaluator = new Evaluator.AttributeWithValueContaining(key, XTokenQueue.trimQuotes(chompEqualValue(cq)));

        else if (cq.matchChomp("~="))
            evaluator = new Evaluator.AttributeWithValueMatching(key, Pattern.compile(XTokenQueue.trimQuotes(chompEqualValue(cq))));
        else
            throw new Selector.SelectorParseException("Could not parse attribute query '%s': unexpected token at '%s'", query, chompEqualValue(cq));
    }
    return evaluator;
}
 
Example #23
Source File: CombiningEvaluator.java    From xsoup with MIT License 4 votes vote down vote up
Evaluator rightMostEvaluator() {
    return evaluators.size() > 0 ? evaluators.get(evaluators.size() - 1) : null;
}
 
Example #24
Source File: CombiningEvaluator.java    From zongtui-webcrawler with GNU General Public License v2.0 4 votes vote down vote up
Or(Evaluator... evaluators) {
    this(Arrays.asList(evaluators));
}
 
Example #25
Source File: StructuralEvaluator.java    From xsoup with MIT License 4 votes vote down vote up
public ImmediatePreviousSibling(Evaluator evaluator) {
    this.evaluator = evaluator;
}
 
Example #26
Source File: CombiningEvaluator.java    From xsoup with MIT License 4 votes vote down vote up
CombiningEvaluator() {
    super();
    evaluators = new ArrayList<Evaluator>();
}
 
Example #27
Source File: DefaultXPathEvaluator.java    From xsoup with MIT License 4 votes vote down vote up
public DefaultXPathEvaluator(Evaluator evaluator, ElementOperator elementOperator) {
    this.evaluator = evaluator;
    this.elementOperator = elementOperator;
}
 
Example #28
Source File: StructuralEvaluator.java    From xsoup with MIT License 4 votes vote down vote up
public Parent(Evaluator evaluator) {
    this.evaluator = evaluator;
}
 
Example #29
Source File: CombiningEvaluator.java    From zongtui-webcrawler with GNU General Public License v2.0 4 votes vote down vote up
CombiningEvaluator() {
    super();
    evaluators = new ArrayList<Evaluator>();
}
 
Example #30
Source File: CombiningEvaluator.java    From xsoup with MIT License 4 votes vote down vote up
void replaceRightMostEvaluator(Evaluator replacement) {
    evaluators.set(evaluators.size() - 1, replacement);
}