|
40 | 40 | */ |
41 | 41 | public class SubdirectoryPattern { |
42 | 42 |
|
| 43 | + /** The subdirectory into which matching artifacts should be installed. */ |
43 | 44 | public String subdirectory; |
| 45 | + |
| 46 | + /** |
| 47 | + * List of pattern strings. An artifact matching any of these patterns will be |
| 48 | + * installed into the affiliated subdirectory. Valid pattern syntaxes are: |
| 49 | + * <ol> |
| 50 | + * <li>classifier</li> |
| 51 | + * <li>groupId:artifactId</li> |
| 52 | + * <li>groupId:artifactId:version</li> |
| 53 | + * <li>groupId:artifactId:version:classifier</li> |
| 54 | + * <li>groupId:artifactId:version:classifier:packaging</li> |
| 55 | + * </ol> |
| 56 | + * <p> |
| 57 | + * Additionally, the wildcard character ({@code *}) is allowed and matches |
| 58 | + * anything. For example, the pattern {@code org.scijava:*:*:natives-*} |
| 59 | + * would match any artifact with groupId {@code org.scijava} and classifier |
| 60 | + * beginning with {@code natives-}. |
| 61 | + * </p> |
| 62 | + */ |
44 | 63 | public List<String> patterns; |
45 | 64 |
|
| 65 | + /** Returns true iff this pattern matches the given artifact. */ |
46 | 66 | public boolean matches(final Artifact artifact) { |
47 | | - return patterns.contains(artifact.getClassifier()); |
| 67 | + return patterns.stream().anyMatch(pattern -> matches(artifact, pattern)); |
| 68 | + } |
| 69 | + |
| 70 | + private static boolean matches(final Artifact artifact, final String pattern) { |
| 71 | + final String[] tokens = pattern.split(":"); |
| 72 | + final String g, a, v, c, p; |
| 73 | + if (tokens.length == 1) { |
| 74 | + g = a = v = p = "*"; |
| 75 | + c = tokens[0]; |
| 76 | + } |
| 77 | + else if (tokens.length == 2) { |
| 78 | + g = tokens[0]; |
| 79 | + a = tokens[1]; |
| 80 | + v = c = p = "*"; |
| 81 | + } |
| 82 | + else if (tokens.length == 3) { |
| 83 | + g = tokens[0]; |
| 84 | + a = tokens[1]; |
| 85 | + v = tokens[2]; |
| 86 | + c = p = "*"; |
| 87 | + } |
| 88 | + else if (tokens.length == 4) { |
| 89 | + g = tokens[0]; |
| 90 | + a = tokens[1]; |
| 91 | + v = tokens[2]; |
| 92 | + c = tokens[3]; |
| 93 | + p = "*"; |
| 94 | + } |
| 95 | + else if (tokens.length == 5) { |
| 96 | + g = tokens[0]; |
| 97 | + a = tokens[1]; |
| 98 | + v = tokens[2]; |
| 99 | + c = tokens[3]; |
| 100 | + p = tokens[4]; |
| 101 | + } |
| 102 | + else { |
| 103 | + throw new IllegalArgumentException("Invalid subdirectory pattern: " + pattern); |
| 104 | + } |
| 105 | + return matches(artifact.getGroupId(), g) && // |
| 106 | + matches(artifact.getArtifactId(), a) && // |
| 107 | + matches(artifact.getVersion(), v) && // |
| 108 | + matches(artifact.getClassifier(), c) && // |
| 109 | + matches(artifact.getType(), p); |
| 110 | + } |
| 111 | + |
| 112 | + private static boolean matches(final String string, final String pattern) { |
| 113 | + final String s = string == null ? "" : string; |
| 114 | + final String regex = pattern.replaceAll("\\*", ".*"); |
| 115 | + return s.matches(regex); |
48 | 116 | } |
49 | 117 | } |
0 commit comments