Search in sources :

Example 1 with Matcher

use of org.apache.ivy.plugins.matcher.Matcher in project ant-ivy by apache.

the class AbstractPatternsBasedResolver method resolveTokenValues.

private Set<Map<String, String>> resolveTokenValues(String[] tokens, String pattern, Map<String, Object> criteria, boolean noMd) {
    Set<Map<String, String>> result = new LinkedHashSet<>();
    Set<String> tokenSet = new HashSet<>(Arrays.asList(tokens));
    Map<String, String> tokenValues = new HashMap<>();
    for (Map.Entry<String, Object> entry : criteria.entrySet()) {
        Object value = entry.getValue();
        if (value instanceof String) {
            tokenValues.put(entry.getKey(), (String) value);
        }
    }
    if (tokenSet.isEmpty()) {
        // no more tokens to resolve
        result.add(tokenValues);
        return result;
    }
    String partiallyResolvedPattern = IvyPatternHelper.substituteTokens(pattern, tokenValues);
    String token = IvyPatternHelper.getFirstToken(partiallyResolvedPattern);
    if (token == null && exist(partiallyResolvedPattern)) {
        // no more tokens to resolve
        result.add(tokenValues);
        return result;
    }
    tokenSet.remove(token);
    Matcher matcher = null;
    Object criteriaForToken = criteria.get(token);
    if (criteriaForToken instanceof Matcher) {
        matcher = (Matcher) criteriaForToken;
    }
    String[] values = listTokenValues(partiallyResolvedPattern, token);
    if (values == null) {
        return result;
    }
    List<String> valueList = new ArrayList<>(Arrays.asList(values));
    filterNames(valueList);
    for (String value : valueList) {
        if (matcher != null && !matcher.matches(value)) {
            continue;
        }
        tokenValues.put(token, value);
        String moreResolvedPattern = IvyPatternHelper.substituteTokens(partiallyResolvedPattern, tokenValues);
        Map<String, Object> newCriteria = new HashMap<>(criteria);
        newCriteria.put(token, value);
        if (noMd && "artifact".equals(token)) {
            newCriteria.put("module", value);
        } else if (noMd && "module".equals(token)) {
            newCriteria.put("artifact", value);
        }
        result.addAll(resolveTokenValues(tokenSet.toArray(new String[tokenSet.size()]), moreResolvedPattern, newCriteria, noMd));
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) Matcher(org.apache.ivy.plugins.matcher.Matcher) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 2 with Matcher

use of org.apache.ivy.plugins.matcher.Matcher in project ant-ivy by apache.

the class SearchEngine method findModuleRevisionIds.

public Collection<ModuleRevisionId> findModuleRevisionIds(DependencyResolver resolver, ModuleRevisionId pattern, PatternMatcher matcher) {
    Collection<ModuleRevisionId> mrids = new ArrayList<>();
    String resolverName = resolver.getName();
    Message.verbose("looking for modules matching " + pattern + " using " + matcher.getName());
    Namespace fromNamespace = null;
    if (resolver instanceof AbstractResolver) {
        fromNamespace = resolver.getNamespace();
    }
    Collection<ModuleEntry> modules = new ArrayList<>();
    OrganisationEntry[] orgs = resolver.listOrganisations();
    if (orgs == null || orgs.length == 0) {
        // hack for resolvers which are not able to list organisation, we try to see if the
        // asked organisation is not an exact one:
        String org = pattern.getOrganisation();
        if (fromNamespace != null) {
            org = NameSpaceHelper.transform(pattern.getModuleId(), fromNamespace.getFromSystemTransformer()).getOrganisation();
        }
        modules.addAll(Arrays.asList(resolver.listModules(new OrganisationEntry(resolver, org))));
    } else {
        Matcher orgMatcher = matcher.getMatcher(pattern.getOrganisation());
        for (OrganisationEntry oe : orgs) {
            String org = oe.getOrganisation();
            String systemOrg = (fromNamespace == null) ? org : NameSpaceHelper.transformOrganisation(org, fromNamespace.getToSystemTransformer());
            if (orgMatcher.matches(systemOrg)) {
                modules.addAll(Arrays.asList(resolver.listModules(new OrganisationEntry(resolver, org))));
            }
        }
    }
    Message.debug("found " + modules.size() + " modules for " + pattern.getOrganisation() + " on " + resolverName);
    boolean foundModule = false;
    for (ModuleEntry mEntry : modules) {
        ModuleId foundMid = new ModuleId(mEntry.getOrganisation(), mEntry.getModule());
        ModuleId systemMid = foundMid;
        if (fromNamespace != null) {
            systemMid = NameSpaceHelper.transform(foundMid, fromNamespace.getToSystemTransformer());
        }
        if (MatcherHelper.matches(matcher, pattern.getModuleId(), systemMid)) {
            // The module corresponds to the searched module pattern
            foundModule = true;
            RevisionEntry[] rEntries = resolver.listRevisions(mEntry);
            Message.debug("found " + rEntries.length + " revisions for [" + mEntry.getOrganisation() + ", " + mEntry.getModule() + "] on " + resolverName);
            boolean foundRevision = false;
            for (RevisionEntry rEntry : rEntries) {
                ModuleRevisionId foundMrid = ModuleRevisionId.newInstance(mEntry.getOrganisation(), mEntry.getModule(), rEntry.getRevision());
                ModuleRevisionId systemMrid = foundMrid;
                if (fromNamespace != null) {
                    systemMrid = fromNamespace.getToSystemTransformer().transform(foundMrid);
                }
                if (MatcherHelper.matches(matcher, pattern, systemMrid)) {
                    // We have a matching module revision
                    foundRevision = true;
                    mrids.add(systemMrid);
                }
            }
            if (!foundRevision) {
                Message.debug("no revision found matching " + pattern + " in [" + mEntry.getOrganisation() + "," + mEntry.getModule() + "] using " + resolverName);
            }
        }
    }
    if (!foundModule) {
        Message.debug("no module found matching " + pattern + " using " + resolverName);
    }
    return mrids;
}
Also used : Matcher(org.apache.ivy.plugins.matcher.Matcher) PatternMatcher(org.apache.ivy.plugins.matcher.PatternMatcher) ArrayList(java.util.ArrayList) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) Namespace(org.apache.ivy.plugins.namespace.Namespace) ModuleId(org.apache.ivy.core.module.id.ModuleId) AbstractResolver(org.apache.ivy.plugins.resolver.AbstractResolver)

Example 3 with Matcher

use of org.apache.ivy.plugins.matcher.Matcher 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)

Example 4 with Matcher

use of org.apache.ivy.plugins.matcher.Matcher in project ant-ivy by apache.

the class PatternVersionMatcher method accept.

/**
 * {@inheritDoc}
 */
public boolean accept(ModuleRevisionId askedMrid, ModuleRevisionId foundMrid) {
    init();
    boolean accept = false;
    String revision = askedMrid.getRevision();
    int bracketIndex = revision.indexOf('(');
    if (bracketIndex > 0) {
        revision = revision.substring(0, bracketIndex);
    }
    List<Match> revMatches = revisionMatches.get(revision);
    if (revMatches != null) {
        for (Match match : revMatches) {
            Matcher matcher = match.getPatternMatcher(askedMrid);
            accept = matcher.matches(foundMrid.getRevision());
            if (accept) {
                break;
            }
        }
    }
    return accept;
}
Also used : Matcher(org.apache.ivy.plugins.matcher.Matcher)

Aggregations

Matcher (org.apache.ivy.plugins.matcher.Matcher)4 ArrayList (java.util.ArrayList)3 PatternMatcher (org.apache.ivy.plugins.matcher.PatternMatcher)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 ModuleId (org.apache.ivy.core.module.id.ModuleId)1 ModuleRevisionId (org.apache.ivy.core.module.id.ModuleRevisionId)1 ExactPatternMatcher (org.apache.ivy.plugins.matcher.ExactPatternMatcher)1 Namespace (org.apache.ivy.plugins.namespace.Namespace)1 AbstractResolver (org.apache.ivy.plugins.resolver.AbstractResolver)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