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;
}
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;
}
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())));
}
}
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;
}
Aggregations