View Javadoc

1   package org.apache.maven.plugins.enforcer.utils;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Collection;
23  import java.util.LinkedList;
24  
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
27  import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
28  import org.apache.maven.artifact.versioning.VersionRange;
29  import org.apache.maven.plugins.enforcer.AbstractVersionEnforcer;
30  
31  /**
32   * This class is used for matching Artifacts against a list of patterns.
33   * 
34   * @author Jakub Senko
35   * @see org.apache.maven.plugins.enforcer.BanTransitiveDependencies
36   */
37  public final class ArtifactMatcher
38  {
39  
40      public static class Pattern
41      {
42          private String pattern;
43  
44          private String[] parts;
45  
46          public Pattern( String pattern )
47          {
48              if ( pattern == null )
49              {
50                  throw new NullPointerException( "pattern" );
51              }
52  
53              this.pattern = pattern;
54  
55              parts = pattern.split( ":", 7 );
56  
57              if ( parts.length == 7 )
58              {
59                  throw new IllegalArgumentException( "Pattern contains too many delimiters." );
60              }
61  
62              for ( String part : parts )
63              {
64                  if ( "".equals( part ) )
65                  {
66                      throw new IllegalArgumentException( "Pattern or its part is empty." );
67                  }
68              }
69          }
70  
71          public boolean match( Artifact artifact )
72              throws InvalidVersionSpecificationException
73          {
74              if ( artifact == null )
75              {
76                  throw new NullPointerException( "artifact" );
77              }
78  
79              switch ( parts.length )
80              {
81                  case 6:
82                      String classifier = artifact.getClassifier();
83                      if ( !matches( parts[5], classifier ) )
84                      {
85                          return false;
86                      }
87                  case 5:
88                      String scope = artifact.getScope();
89                      if ( scope == null || scope.equals( "" ) )
90                      {
91                          scope = "compile";
92                      }
93  
94                      if ( !matches( parts[4], scope ) )
95                      {
96                          return false;
97                      }
98                  case 4:
99                      String type = artifact.getType();
100                     if ( type == null || type.equals( "" ) )
101                     {
102                         type = "jar";
103                     }
104 
105                     if ( !matches( parts[3], type ) )
106                     {
107                         return false;
108                     }
109 
110                 case 3:
111                     if ( !matches( parts[2], artifact.getVersion() ) )
112                     {
113                         if ( !AbstractVersionEnforcer.containsVersion( VersionRange.createFromVersionSpec( parts[2] ),
114                                                                        new DefaultArtifactVersion(
115                                                                                                    artifact.getVersion() ) ) )
116                         {
117                             return false;
118                         }
119                     }
120 
121                 case 2:
122                     if ( !matches( parts[1], artifact.getArtifactId() ) )
123                     {
124                         return false;
125                     }
126                 case 1:
127                     if ( !matches( parts[0], artifact.getGroupId() ) )
128                     {
129                         return false;
130                     }
131                     else
132                     {
133                         return true;
134                     }
135                 default:
136                     throw new AssertionError();
137             }
138         }
139         
140         private boolean matches( String expression, String input )
141         {
142             String regex = expression.replace( ".", "\\." ).replace( "*", ".*" ).replace( ":", "\\:" ).replace( '?', '.' );
143 
144             return java.util.regex.Pattern.matches( regex , input );
145         }
146 
147         @Override
148         public String toString()
149         {
150             return pattern;
151         }
152     }
153 
154     private Collection<Pattern> patterns = new LinkedList<Pattern>();
155 
156     private Collection<Pattern> ignorePatterns = new LinkedList<Pattern>();
157 
158     /**
159      * Construct class by providing patterns as strings. Empty strings are ignored.
160      * 
161      * @throws NullPointerException if any of the arguments is null
162      */
163     public ArtifactMatcher( final Collection<String> patterns, final Collection<String> ignorePatterns )
164     {
165         if ( patterns == null )
166         {
167             throw new NullPointerException( "patterns" );
168         }
169         if ( ignorePatterns == null )
170         {
171             throw new NullPointerException( "ignorePatterns" );
172         }
173         for ( String pattern : patterns )
174         {
175             if ( pattern != null && !"".equals( pattern ) )
176             {
177                 this.patterns.add( new Pattern( pattern ) );
178             }
179         }
180 
181         for ( String ignorePattern : ignorePatterns )
182         {
183             if ( ignorePattern != null && !"".equals( ignorePattern ) )
184             {
185                 this.ignorePatterns.add( new Pattern( ignorePattern ) );
186             }
187         }
188     }
189 
190     /**
191      * Check if artifact matches patterns.
192      * 
193      * @throws InvalidVersionSpecificationException
194      */
195     public boolean match( Artifact artifact )
196         throws InvalidVersionSpecificationException
197     {
198         for ( Pattern pattern : patterns )
199         {
200             if ( pattern.match( artifact ) )
201             {
202                 for ( Pattern ignorePattern : ignorePatterns )
203                 {
204                     if ( ignorePattern.match( artifact ) )
205                     {
206                         return false;
207                     }
208                 }
209                 return true;
210             }
211         }
212         return false;
213     }
214 }