View Javadoc
1   package org.apache.maven.shared.artifact.filter;
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 org.apache.maven.artifact.Artifact;
23  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
24  import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
25  import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
26  import org.apache.maven.artifact.versioning.VersionRange;
27  
28  import java.util.List;
29  
30  /**
31   * Filter to include or exclude artifacts from a list of patterns. The artifact pattern syntax is of the form:
32   * 
33   * <pre>[groupId]:[artifactId]:[type]:[version]</pre>
34   * 
35   * <p>
36   * Where each pattern segment is optional and supports full and partial <code>*</code> wildcards. An empty pattern
37   * segment is treated as an implicit wildcard.
38   * </p>
39   * 
40   * <p>
41   * For example, <code>org.apache.*</code> would match all artifacts whose group id started with
42   * <code>org.apache.</code>, and <code>:::*-SNAPSHOT</code> would match all snapshot artifacts.
43   * </p>
44   * 
45   * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
46   * @version $Id: AbstractStrictPatternArtifactFilter.java 1716978 2015-11-28 14:47:04Z khmarbaise $
47   */
48  public abstract class AbstractStrictPatternArtifactFilter implements ArtifactFilter
49  {
50      // fields -----------------------------------------------------------------
51  
52      /**
53       * The list of artifact patterns to match, as described above.
54       */
55      private final List<String> patterns;
56  
57      /**
58       * Whether this filter should include or exclude artifacts that match the patterns.
59       */
60      private final boolean include;
61  
62      // constructors -----------------------------------------------------------
63  
64      /**
65       * Creates a new filter that matches the specified artifact patterns and includes or excludes them according to the
66       * specified flag.
67       * 
68       * @param patterns
69       *            the list of artifact patterns to match, as described above
70       * @param include
71       *            <code>true</code> to include artifacts that match the patterns, or <code>false</code> to exclude
72       *            them
73       */
74      public AbstractStrictPatternArtifactFilter( List<String> patterns, boolean include )
75      {
76          this.patterns = patterns;
77          this.include = include;
78      }
79  
80      // ArtifactFilter methods -------------------------------------------------
81  
82      /** {@inheritDoc} */
83      public boolean include( Artifact artifact )
84      {
85          boolean matched = false;
86  
87          for ( String pattern : patterns )
88          {
89              if ( include( artifact, pattern ) )
90              {
91                  matched = true;
92                  break;
93              }
94          }
95  
96          return include ? matched : !matched;
97      }
98  
99      // private methods --------------------------------------------------------
100 
101     /**
102      * Gets whether the specified artifact matches the specified pattern.
103      * 
104      * @param artifact
105      *            the artifact to check
106      * @param pattern
107      *            the pattern to match, as defined above
108      * @return <code>true</code> if the specified artifact is matched by the specified pattern
109      */
110     private boolean include( Artifact artifact, String pattern )
111     {
112         String[] tokens = new String[] {
113             artifact.getGroupId(),
114             artifact.getArtifactId(),
115             artifact.getType(),
116             artifact.getBaseVersion()
117         };
118 
119         String[] patternTokens = pattern.split( ":" );
120 
121         // fail immediately if pattern tokens outnumber tokens to match
122         boolean matched = patternTokens.length <= tokens.length;
123 
124         for ( int i = 0; matched && i < patternTokens.length; i++ )
125         {
126             matched = matches( tokens[i], patternTokens[i] );
127         }
128 
129         return matched;
130     }
131 
132     /**
133      * Gets whether the specified token matches the specified pattern segment.
134      * 
135      * @param token
136      *            the token to check
137      * @param pattern
138      *            the pattern segment to match, as defined above
139      * @return <code>true</code> if the specified token is matched by the specified pattern segment
140      */
141     private boolean matches( String token, String pattern )
142     {
143         boolean matches;
144 
145         // support full wildcard and implied wildcard
146         if ( "*".equals( pattern ) || pattern.length() == 0 )
147         {
148             matches = true;
149         }
150         // support contains wildcard
151         else if ( pattern.startsWith( "*" ) && pattern.endsWith( "*" ) )
152         {
153             String contains = pattern.substring( 1, pattern.length() - 1 );
154 
155             matches = token.contains( contains );
156         }
157         // support leading wildcard
158         else if ( pattern.startsWith( "*" ) )
159         {
160             String suffix = pattern.substring( 1, pattern.length() );
161 
162             matches = token.endsWith( suffix );
163         }
164         // support trailing wildcard
165         else if ( pattern.endsWith( "*" ) )
166         {
167             String prefix = pattern.substring( 0, pattern.length() - 1 );
168 
169             matches = token.startsWith( prefix );
170         }
171         // support versions range 
172         else if ( pattern.startsWith( "[" ) || pattern.startsWith( "(" ) )
173         {
174             matches = isVersionIncludedInRange( token, pattern );
175         }
176         // support exact match
177         else
178         {
179             matches = token.equals( pattern );
180         }
181 
182         return matches;
183     }
184 
185     private boolean isVersionIncludedInRange( final String version, final String range )
186     {
187         try
188         {
189             return VersionRange.createFromVersionSpec( range ).containsVersion( new DefaultArtifactVersion( version ) );
190         }
191         catch ( InvalidVersionSpecificationException e )
192         {
193             return false;
194         }
195     }
196 
197 }