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   */
47  public abstract class AbstractStrictPatternArtifactFilter implements ArtifactFilter
48  {
49      // fields -----------------------------------------------------------------
50  
51      /**
52       * The list of artifact patterns to match, as described above.
53       */
54      private final List<String> patterns;
55  
56      /**
57       * Whether this filter should include or exclude artifacts that match the patterns.
58       */
59      private final boolean include;
60  
61      // constructors -----------------------------------------------------------
62  
63      /**
64       * Creates a new filter that matches the specified artifact patterns and includes or excludes them according to the
65       * specified flag.
66       *
67       * @param patterns
68       *            the list of artifact patterns to match, as described above
69       * @param include
70       *            <code>true</code> to include artifacts that match the patterns, or <code>false</code> to exclude
71       *            them
72       */
73      public AbstractStrictPatternArtifactFilter( List<String> patterns, boolean include )
74      {
75          this.patterns = patterns;
76          this.include = include;
77      }
78  
79      // ArtifactFilter methods -------------------------------------------------
80  
81      /** {@inheritDoc} */
82      public boolean include( Artifact artifact )
83      {
84          boolean matched = false;
85  
86          for ( String pattern : patterns )
87          {
88              if ( include( artifact, pattern ) )
89              {
90                  matched = true;
91                  break;
92              }
93          }
94  
95          return include ? matched : !matched;
96      }
97  
98      // private methods --------------------------------------------------------
99  
100     /**
101      * Gets whether the specified artifact matches the specified pattern.
102      * 
103      * @param artifact
104      *            the artifact to check
105      * @param pattern
106      *            the pattern to match, as defined above
107      * @return <code>true</code> if the specified artifact is matched by the specified pattern
108      */
109     private boolean include( Artifact artifact, String pattern )
110     {
111         String[] tokens = new String[] {
112             artifact.getGroupId(),
113             artifact.getArtifactId(),
114             artifact.getType(),
115             artifact.getBaseVersion()
116         };
117 
118         String[] patternTokens = pattern.split( ":" );
119 
120         // fail immediately if pattern tokens outnumber tokens to match
121         boolean matched = patternTokens.length <= tokens.length;
122 
123         for ( int i = 0; matched && i < patternTokens.length; i++ )
124         {
125             matched = matches( tokens[i], patternTokens[i] );
126         }
127 
128         return matched;
129     }
130 
131     /**
132      * Gets whether the specified token matches the specified pattern segment.
133      * 
134      * @param token
135      *            the token to check
136      * @param pattern
137      *            the pattern segment to match, as defined above
138      * @return <code>true</code> if the specified token is matched by the specified pattern segment
139      */
140     private boolean matches( String token, String pattern )
141     {
142         boolean matches;
143 
144         // support full wildcard and implied wildcard
145         if ( "*".equals( pattern ) || pattern.length() == 0 )
146         {
147             matches = true;
148         }
149         // support contains wildcard
150         else if ( pattern.startsWith( "*" ) && pattern.endsWith( "*" ) )
151         {
152             String contains = pattern.substring( 1, pattern.length() - 1 );
153 
154             matches = token.contains( contains );
155         }
156         // support leading wildcard
157         else if ( pattern.startsWith( "*" ) )
158         {
159             matches = token.endsWith( pattern.substring( 1 ) );
160         }
161         // support trailing wildcard
162         else if ( pattern.endsWith( "*" ) )
163         {
164             String prefix = pattern.substring( 0, pattern.length() - 1 );
165 
166             matches = token.startsWith( prefix );
167         }
168         // support versions range 
169         else if ( pattern.startsWith( "[" ) || pattern.startsWith( "(" ) )
170         {
171             matches = isVersionIncludedInRange( token, pattern );
172         }
173         // support exact match
174         else
175         {
176             matches = token.equals( pattern );
177         }
178 
179         return matches;
180     }
181 
182     private boolean isVersionIncludedInRange( final String version, final String range )
183     {
184         try
185         {
186             return VersionRange.createFromVersionSpec( range ).containsVersion( new DefaultArtifactVersion( version ) );
187         }
188         catch ( InvalidVersionSpecificationException e )
189         {
190             return false;
191         }
192     }
193 
194 }