Search in sources :

Example 1 with Filter

use of org.apache.ivy.util.filter.Filter in project ant-ivy by apache.

the class IvyEventFilter method parseExpression.

private Filter<IvyEvent> parseExpression(String filterExpression) {
    // expressions handled for the moment: (informal grammar)
    // EXP := SIMPLE_EXP | AND_EXP | OR_EXP | NOT_EXP
    // AND_EXP := EXP && EXP
    // OR_EXP := EXP || EXP
    // NOT_EXP := ! EXP
    // SIMPLE_EXP := attname = comma, separated, list, of, accepted, values
    // example: organisation = foo && module = bar, baz
    filterExpression = filterExpression.trim();
    int index = filterExpression.indexOf(AND);
    if (index == -1) {
        index = filterExpression.indexOf(OR);
        if (index == -1) {
            if (filterExpression.startsWith(NOT)) {
                return new NotFilter<>(parseExpression(filterExpression.substring(NOT.length())));
            } else {
                index = filterExpression.indexOf("=");
                if (index == -1) {
                    throw new IllegalArgumentException("bad filter expression: " + filterExpression + ": no equal sign found");
                }
                final String attname = filterExpression.substring(0, index).trim();
                final List<Matcher> matchers = new ArrayList<>();
                for (String value : splitToArray(filterExpression.substring(index + 1))) {
                    matchers.add(matcher.getMatcher(value));
                }
                return new Filter<IvyEvent>() {

                    public boolean accept(IvyEvent e) {
                        String val = e.getAttributes().get(attname);
                        if (val == null) {
                            return false;
                        }
                        for (Matcher matcher : matchers) {
                            if (matcher.matches(val)) {
                                return true;
                            }
                        }
                        return false;
                    }
                };
            }
        } else {
            return new OrFilter<>(parseExpression(filterExpression.substring(0, index)), parseExpression(filterExpression.substring(index + OR.length())));
        }
    } else {
        return new AndFilter<>(parseExpression(filterExpression.substring(0, index)), parseExpression(filterExpression.substring(index + AND.length())));
    }
}
Also used : AndFilter(org.apache.ivy.util.filter.AndFilter) ExactPatternMatcher(org.apache.ivy.plugins.matcher.ExactPatternMatcher) Matcher(org.apache.ivy.plugins.matcher.Matcher) PatternMatcher(org.apache.ivy.plugins.matcher.PatternMatcher) AndFilter(org.apache.ivy.util.filter.AndFilter) Filter(org.apache.ivy.util.filter.Filter) NotFilter(org.apache.ivy.util.filter.NotFilter) OrFilter(org.apache.ivy.util.filter.OrFilter) NoFilter(org.apache.ivy.util.filter.NoFilter) NotFilter(org.apache.ivy.util.filter.NotFilter) ArrayList(java.util.ArrayList) OrFilter(org.apache.ivy.util.filter.OrFilter)

Aggregations

ArrayList (java.util.ArrayList)1 ExactPatternMatcher (org.apache.ivy.plugins.matcher.ExactPatternMatcher)1 Matcher (org.apache.ivy.plugins.matcher.Matcher)1 PatternMatcher (org.apache.ivy.plugins.matcher.PatternMatcher)1 AndFilter (org.apache.ivy.util.filter.AndFilter)1 Filter (org.apache.ivy.util.filter.Filter)1 NoFilter (org.apache.ivy.util.filter.NoFilter)1 NotFilter (org.apache.ivy.util.filter.NotFilter)1 OrFilter (org.apache.ivy.util.filter.OrFilter)1