001/* 002 * Licensed under the Apache License, Version 2.0 (the "License"); 003 * you may not use this file except in compliance with the License. 004 * You may obtain a copy of the License at 005 * 006 * http://www.apache.org/licenses/LICENSE-2.0 007 * 008 * Unless required by applicable law or agreed to in writing, software 009 * distributed under the License is distributed on an "AS IS" BASIS, 010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 011 * See the License for the specific language governing permissions and 012 * limitations under the License. 013 */ 014 015package org.basepom.mojo.dvc; 016 017import static com.google.common.base.Preconditions.checkNotNull; 018import static com.google.common.base.Preconditions.checkState; 019 020import java.util.List; 021import java.util.regex.Matcher; 022import java.util.regex.Pattern; 023 024import com.google.common.annotations.VisibleForTesting; 025import com.google.common.base.Splitter; 026 027/** 028 * Matches the group-id/artifact-id pair of a qualified name. May contain wildcards (*) in either group-id and artifact-id. 029 */ 030public final class QualifiedNameMatcher { 031 032 private static final Pattern WILDCARD_REGEXP = Pattern.compile("[^*]+|(\\*)"); 033 private static final Pattern WILDCARD_MATCH = Pattern.compile(".*"); 034 035 private final Pattern groupPattern; 036 private final Pattern artifactPattern; 037 038 public static QualifiedNameMatcher fromQualifiedName(final QualifiedName name) { 039 checkNotNull(name, "name is null"); 040 041 return new QualifiedNameMatcher(name.getMinimalName()); 042 } 043 044 public QualifiedNameMatcher(final String pattern) { 045 checkNotNull(pattern, "pattern is null"); 046 final List<String> elements = Splitter.on(':').trimResults().splitToList(pattern); 047 checkState(!elements.isEmpty() && elements.size() < 3, "Pattern %s is not a valid inclusion pattern!", pattern); 048 049 this.groupPattern = compileWildcard(elements.get(0).trim()); 050 this.artifactPattern = compileWildcard(elements.size() > 1 ? elements.get(1).trim() : ""); // use wildcard match if no artifact present 051 } 052 053 public boolean matches(QualifiedName artifactName) { 054 checkNotNull(artifactName, "artifactName is null"); 055 056 return groupPattern.matcher(artifactName.getGroupId()).matches() 057 && artifactPattern.matcher(artifactName.getArtifactId()).matches(); 058 } 059 060 @VisibleForTesting 061 static Pattern compileWildcard(final String wildcard) { 062 if (wildcard.isEmpty()) { 063 return WILDCARD_MATCH; 064 } 065 066 final Matcher m = WILDCARD_REGEXP.matcher(wildcard); 067 final StringBuffer b = new StringBuffer(); 068 while (m.find()) { 069 if (m.group(1) != null) { 070 m.appendReplacement(b, ".*"); 071 } else { 072 m.appendReplacement(b, "\\\\Q" + m.group(0) + "\\\\E"); 073 } 074 } 075 m.appendTail(b); 076 return Pattern.compile(b.toString()); 077 } 078}