View Javadoc
1   package org.apache.maven.shared.artifact.filter.collection;
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.Arrays;
23  import java.util.LinkedHashSet;
24  import java.util.List;
25  import java.util.Set;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.shared.utils.StringUtils;
29  
30  /**
31   * This is the common base class of ClassifierFilter and TypeFilter
32   * 
33   * @author <a href="richardv@mxtelecom.com">Richard van der Hoff</a>
34   * @version $Id: AbstractArtifactFeatureFilter.java 1716978 2015-11-28 14:47:04Z khmarbaise $
35   */
36  public abstract class AbstractArtifactFeatureFilter
37      extends AbstractArtifactsFilter
38  {
39      /** The list of types or classifiers to include */
40      private List<String> includes;
41  
42      /**
43       * The list of types or classifiers to exclude (ignored if includes != null)
44       */
45      private List<String> excludes;
46  
47      /**
48       * @param include comma separated list with includes.
49       * @param exclude comma separated list with excludes.
50       */
51      public AbstractArtifactFeatureFilter( String include, String exclude )
52      {
53          setExcludes( exclude );
54          setIncludes( include );
55      }
56  
57      /**
58       * This function determines if filtering needs to be performed. Includes are processed before Excludes.
59       * 
60       * @param artifacts the set of dependencies to filter.
61       * @return a Set of filtered dependencies.
62       */
63      public Set<Artifact> filter( Set<Artifact> artifacts )
64      {
65          Set<Artifact> results = artifacts;
66  
67          if ( this.includes != null && !this.includes.isEmpty() )
68          {
69              results = filterIncludes( results, this.includes );
70          }
71  
72          if ( this.excludes != null && !this.excludes.isEmpty() )
73          {
74              results = filterExcludes( results, this.excludes );
75          }
76  
77          return results;
78      }
79  
80      /**
81       * Processes the dependencies list and includes the dependencies that match a filter in the list.
82       * 
83       * @param artifacts List of dependencies.
84       * @param theIncludes List of types or classifiers to include.
85       * @return a set of filtered artifacts.
86       */
87      private Set<Artifact> filterIncludes( Set<Artifact> artifacts, List<String> theIncludes )
88      {
89          Set<Artifact> result = new LinkedHashSet<Artifact>();
90  
91          for ( String include : theIncludes )
92          {
93              for ( Artifact artifact : artifacts )
94              {
95                  // if the classifier or type of the artifact
96                  // matches the feature
97                  // to include, add to the
98                  // results
99                  if ( compareFeatures( getArtifactFeature( artifact ), include ) )
100                 {
101                     result.add( artifact );
102                 }
103             }
104         }
105         return result;
106     }
107 
108     /**
109      * Processes the dependencies list and excludes the dependencies that match a filter in the list.
110      * 
111      * @param depends List of dependencies.
112      * @param excludes List of types or classifiers to exclude.
113      * @return a set of filtered artifacts.
114      */
115     private Set<Artifact> filterExcludes( Set<Artifact> artifacts, List<String> theExcludes )
116     {
117         Set<Artifact> result = new LinkedHashSet<Artifact>();
118 
119         for ( Artifact artifact : artifacts )
120         {
121             boolean exclude = false;
122             String artifactFeature = getArtifactFeature( artifact );
123 
124             // look through all types or classifiers. If no
125             // matches are found
126             // then it can be added to the results.
127             for ( String excludeFeature : theExcludes )
128             {
129                 if ( compareFeatures( artifactFeature, excludeFeature ) )
130                 {
131                     exclude = true;
132                     break;
133                 }
134             }
135 
136             if ( !exclude )
137             {
138                 result.add( artifact );
139             }
140         }
141 
142         return result;
143     }
144 
145     /**
146      * Should return the type or classifier of the given artifact, so that we can filter it
147      * 
148      * @param artifact artifact to return type or classifier of
149      * @return type or classifier
150      */
151     protected abstract String getArtifactFeature( Artifact artifact );
152 
153     /**
154      * @param excludeString comma separated list with excludes.
155      */
156     public void setExcludes( String excludeString )
157     {
158         if ( StringUtils.isNotEmpty( excludeString ) )
159         {
160             this.excludes = Arrays.asList( StringUtils.split( excludeString, "," ) );
161         }
162     }
163 
164     /**
165      * @param includeString comma separated list with includes.
166      */
167     public void setIncludes( String includeString )
168     {
169         if ( StringUtils.isNotEmpty( includeString ) )
170         {
171             this.includes = Arrays.asList( StringUtils.split( includeString, "," ) );
172         }
173     }
174 
175     /**
176      * @return Returns the excludes.
177      */
178     public List<String> getExcludes()
179     {
180         return this.excludes;
181     }
182 
183     /**
184      * @return Returns the includes.
185      */
186     public List<String> getIncludes()
187     {
188         return this.includes;
189     }
190 
191     /**
192      * Allows Feature comparison to be customized
193      * 
194      * @param lhs String artifact's feature
195      * @param rhs String feature from exclude or include list
196      * @return boolean true if features match
197      */
198     protected boolean compareFeatures( String lhs, String rhs )
199     {
200         // If lhs is null, check that rhs is null. Otherwise check if strings are equal.
201         return ( lhs == null ? rhs == null : lhs.equals( rhs ) );
202     }
203 }