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  import org.apache.maven.plugins.enforcer.BanTransitiveDependencies;
31  
32  /**
33   * This class is used for matching Artifacts against a list of patterns.
34   * 
35   * @author Jakub Senko
36   * @see BanTransitiveDependencies
37   */
38  public final class ArtifactMatcher
39  {
40  
41      public static class Pattern
42      {
43          private String pattern;
44  
45          private String[] parts;
46  
47          public Pattern( String pattern )
48          {
49              if ( pattern == null )
50                  throw new NullPointerException( "pattern" );
51  
52              this.pattern = pattern;
53  
54              parts = pattern.split( ":", 6 );
55  
56              if ( parts.length == 6 )
57                  throw new IllegalArgumentException( "Pattern contains too many delimiters." );
58  
59              for ( String part : parts )
60              {
61                  if ( "".equals( part ) )
62                      throw new IllegalArgumentException( "Pattern or its part is empty." );
63              }
64          }
65  
66          public boolean match( Artifact artifact )
67              throws InvalidVersionSpecificationException
68          {
69              if ( artifact == null )
70                  throw new NullPointerException( "artifact" );
71  
72              switch ( parts.length )
73              {
74                  case 5:
75                      String scope = artifact.getScope();
76                      if ( scope == null || scope.equals( "" ) )
77                      {
78                          scope = "compile";
79                      }
80  
81                      if ( !"*".equals( parts[4] ) && !parts[4].equals( scope ) )
82                          return false;
83  
84                  case 4:
85                      String type = artifact.getType();
86                      if ( type == null || type.equals( "" ) )
87                      {
88                          type = "jar";
89                      }
90  
91                      if ( !"*".equals( parts[3] ) && !parts[3].equals( type ) )
92                          return false;
93  
94                  case 3:
95                      if ( !"*".equals( parts[2] ) && !parts[2].equals( artifact.getVersion() ) )
96                      {
97                          if ( !AbstractVersionEnforcer.containsVersion( VersionRange.createFromVersionSpec( parts[2] ),
98                                                                         new DefaultArtifactVersion(
99                                                                                                     artifact.getVersion() ) ) )
100                         {
101                             return false;
102                         }
103                     }
104 
105                 case 2:
106                     if ( !"*".equals( parts[1] ) && !parts[1].equals( artifact.getArtifactId() ) )
107                         return false;
108 
109                 case 1:
110                     if ( !"*".equals( parts[0] ) && !parts[0].equals( artifact.getGroupId() ) )
111                         return false;
112                     else
113                         return true;
114                 default:
115                     throw new AssertionError();
116             }
117         }
118 
119         @Override
120         public String toString()
121         {
122             return pattern;
123         }
124     }
125 
126     private Collection<Pattern> patterns = new LinkedList<Pattern>();
127 
128     private Collection<Pattern> ignorePatterns = new LinkedList<Pattern>();
129 
130     /**
131      * Construct class by providing patterns as strings. Empty strings are ignored.
132      * 
133      * @throws NullPointerException if any of the arguments is null
134      */
135     public ArtifactMatcher( final Collection<String> patterns, final Collection<String> ignorePatterns )
136     {
137         if ( patterns == null )
138             throw new NullPointerException( "patterns" );
139         if ( ignorePatterns == null )
140             throw new NullPointerException( "ignorePatterns" );
141 
142         for ( String pattern : patterns )
143         {
144             if ( pattern != null && !"".equals( pattern ) )
145             {
146                 this.patterns.add( new Pattern( pattern ) );
147             }
148         }
149 
150         for ( String ignorePattern : ignorePatterns )
151         {
152             if ( ignorePattern != null && !"".equals( ignorePattern ) )
153             {
154                 this.ignorePatterns.add( new Pattern( ignorePattern ) );
155             }
156         }
157     }
158 
159     /**
160      * Check if artifact matches patterns.
161      * 
162      * @throws InvalidVersionSpecificationException
163      */
164     public boolean match( Artifact artifact )
165         throws InvalidVersionSpecificationException
166     {
167         for ( Pattern pattern : patterns )
168         {
169             if ( pattern.match( artifact ) )
170             {
171                 for ( Pattern ignorePattern : ignorePatterns )
172                 {
173                     if ( ignorePattern.match( artifact ) )
174                         return false;
175                 }
176 
177                 return true;
178             }
179         }
180 
181         return false;
182     }
183 }