Java Code Examples for us.codecraft.xsoup.XTokenQueue#trimQuotes()

The following examples show how to use us.codecraft.xsoup.XTokenQueue#trimQuotes() . 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 xsoup with MIT License 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 6 votes vote down vote up
private void functionRegex(String remainder) {
    Validate.isTrue(remainder.endsWith(")"), "Unclosed bracket for function! " + remainder);
    List<String> params = XTokenQueue.trimQuotes(XTokenQueue.parseFuncionParams(remainder.substring("regex(".length(), remainder.length() - 1)));
    if (params.size() == 1) {
        elementOperator = new ElementOperator.Regex(params.get(0));
    } else if (params.size() == 2) {
        if (params.get(0).startsWith("@")) {
            elementOperator = new ElementOperator.Regex(params.get(1), params.get(0).substring(1));
        } else {
            elementOperator = new ElementOperator.Regex(params.get(0), null, Integer.parseInt(params.get(1)));
        }
    } else if (params.size() == 3) {
        elementOperator = new ElementOperator.Regex(params.get(1), params.get(0).substring(1), Integer.parseInt(params.get(2)));
    } else {
        throw new Selector.SelectorParseException("Unknown usage for regex()" + remainder);
    }
}
 
Example 3
Source File: XPathParser.java    From xsoup with MIT License 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;
}