1 package org.apache.maven.plugins.enforcer.utils;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.artifact.Artifact;
23 import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
24 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
25 import org.apache.maven.artifact.versioning.VersionRange;
26 import org.apache.maven.plugins.enforcer.AbstractVersionEnforcer;
27 import java.util.Collection;
28 import java.util.LinkedList;
29
30
31
32
33
34
35
36 public final class ArtifactMatcher
37 {
38
39
40
41
42 public static class Pattern
43 {
44 private String pattern;
45
46 private String[] parts;
47
48 public Pattern( String pattern )
49 {
50 if ( pattern == null )
51 {
52 throw new NullPointerException( "pattern" );
53 }
54
55 this.pattern = pattern;
56
57 parts = pattern.split( ":", 7 );
58
59 if ( parts.length == 7 )
60 {
61 throw new IllegalArgumentException( "Pattern contains too many delimiters." );
62 }
63
64 for ( String part : parts )
65 {
66 if ( "".equals( part ) )
67 {
68 throw new IllegalArgumentException( "Pattern or its part is empty." );
69 }
70 }
71 }
72
73 public boolean match( Artifact artifact )
74 throws InvalidVersionSpecificationException
75 {
76 if ( artifact == null )
77 {
78 throw new NullPointerException( "artifact" );
79 }
80
81 switch ( parts.length )
82 {
83 case 6:
84 String classifier = artifact.getClassifier();
85 if ( !matches( parts[5], classifier ) )
86 {
87 return false;
88 }
89 case 5:
90 String scope = artifact.getScope();
91 if ( scope == null || scope.equals( "" ) )
92 {
93 scope = Artifact.SCOPE_COMPILE;
94 }
95
96 if ( !matches( parts[4], scope ) )
97 {
98 return false;
99 }
100 case 4:
101 String type = artifact.getType();
102 if ( type == null || type.equals( "" ) )
103 {
104 type = "jar";
105 }
106
107 if ( !matches( parts[3], type ) )
108 {
109 return false;
110 }
111
112 case 3:
113 if ( !matches( parts[2], artifact.getVersion() ) )
114 {
115
116 if ( !AbstractVersionEnforcer.containsVersion( VersionRange.createFromVersionSpec( parts[2] ),
117 new DefaultArtifactVersion(
118 artifact.getVersion() ) ) )
119
120 {
121 return false;
122 }
123 }
124
125 case 2:
126 if ( !matches( parts[1], artifact.getArtifactId() ) )
127 {
128 return false;
129 }
130 case 1:
131 return matches( parts[0], artifact.getGroupId() );
132 default:
133 throw new AssertionError();
134 }
135 }
136
137 private boolean matches( String expression, String input )
138 {
139 String regex =
140 expression.replace( ".", "\\." ).replace( "*", ".*" ).replace( ":", "\\:" ).replace( '?', '.' )
141 .replace( "[", "\\[" ).replace( "]", "\\]" ).replace( "(", "\\(" ).replace( ")", "\\)" );
142
143
144 if ( input == null )
145 {
146 input = "";
147 }
148
149 return java.util.regex.Pattern.matches( regex, input );
150 }
151
152 @Override
153 public String toString()
154 {
155 return pattern;
156 }
157 }
158
159 private Collection<Pattern> patterns = new LinkedList<Pattern>();
160
161 private Collection<Pattern> ignorePatterns = new LinkedList<Pattern>();
162
163
164
165
166
167
168 public ArtifactMatcher( final Collection<String> patterns, final Collection<String> ignorePatterns )
169 {
170 if ( patterns == null )
171 {
172 throw new NullPointerException( "patterns" );
173 }
174 if ( ignorePatterns == null )
175 {
176 throw new NullPointerException( "ignorePatterns" );
177 }
178 for ( String pattern : patterns )
179 {
180 if ( pattern != null && !"".equals( pattern ) )
181 {
182 this.patterns.add( new Pattern( pattern ) );
183 }
184 }
185
186 for ( String ignorePattern : ignorePatterns )
187 {
188 if ( ignorePattern != null && !"".equals( ignorePattern ) )
189 {
190 this.ignorePatterns.add( new Pattern( ignorePattern ) );
191 }
192 }
193 }
194
195
196
197
198
199
200 public boolean match( Artifact artifact )
201 throws InvalidVersionSpecificationException
202 {
203 for ( Pattern pattern : patterns )
204 {
205 if ( pattern.match( artifact ) )
206 {
207 for ( Pattern ignorePattern : ignorePatterns )
208 {
209 if ( ignorePattern.match( artifact ) )
210 {
211 return false;
212 }
213 }
214 return true;
215 }
216 }
217 return false;
218 }
219 }