Coverage Report - org.apache.maven.plugin.dependency.utils.filters.AbstractArtifactFeatureFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractArtifactFeatureFilter
100% 
100% 
2.1
 
 1  
 package org.apache.maven.plugin.dependency.utils.filters;
 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.HashSet;
 24  
 import java.util.Iterator;
 25  
 import java.util.List;
 26  
 import java.util.Set;
 27  
 
 28  
 import org.apache.maven.artifact.Artifact;
 29  
 import org.apache.maven.plugin.logging.Log;
 30  
 import org.codehaus.plexus.util.StringUtils;
 31  
 
 32  
 /**
 33  
  * This is the common base class of ClassifierFilter and
 34  
  * TypeFilter
 35  
  * 
 36  
  * @author <a href="richardv@mxtelecom.com">Richard van der
 37  
  *         Hoff</a>
 38  
  * @version $Id: AbstractArtifactFeatureFilter.java 522374
 39  
  *          2007-03-25 22:58:03Z brianf $
 40  
  */
 41  
 public abstract class AbstractArtifactFeatureFilter
 42  
     extends AbstractArtifactsFilter
 43  
 {
 44  
     /** The list of types or classifiers to include */
 45  
     private List includes;
 46  
 
 47  
     /**
 48  
      * The list of types or classifiers to exclude (ignored
 49  
      * if includes != null)
 50  
      */
 51  
     private List excludes;
 52  
 
 53  
     /**
 54  
      * The configuration string for the include list - comma
 55  
      * separated
 56  
      */
 57  
     private String includeString;
 58  
 
 59  
     /**
 60  
      * The configuration string for the exclude list - comma
 61  
      * separated
 62  
      */
 63  
     private String excludeString;
 64  
 
 65  
     /**
 66  
      * The name of the feature we are filtering on - for
 67  
      * logging - "Classifiers" or "Types"
 68  
      */
 69  
     private String featureName;
 70  
 
 71  
     public AbstractArtifactFeatureFilter( String include, String exclude, String theFeatureName )
 72  427
     {
 73  427
         setExcludes( exclude );
 74  427
         setIncludes( include );
 75  427
         featureName = theFeatureName;
 76  427
     }
 77  
 
 78  
     /**
 79  
      * This function determines if filtering needs to be
 80  
      * performed. Includes are processed before Excludes.
 81  
      * 
 82  
      * @param dependencies the set of dependencies to
 83  
      *            filter.
 84  
      * 
 85  
      * @return a Set of filtered dependencies.
 86  
      */
 87  
     public Set filter( Set artifacts, Log log )
 88  
     {
 89  414
         Set results = artifacts;
 90  
 
 91  414
         if ( this.includes != null && !this.includes.isEmpty() )
 92  
         {
 93  22
             int size = includes.size();
 94  22
             log.debug( "Including only " + size + " " + featureName + ( ( size > 1 ) ? "s" : "" ) + ": "
 95  
                 + this.includeString );
 96  
 
 97  22
             results = filterIncludes( results, this.includes );
 98  
         }
 99  
 
 100  414
         if ( this.excludes != null && !this.excludes.isEmpty() )
 101  
         {
 102  27
             int size = excludes.size();
 103  27
             log.debug( "Excluding " + size + " " + featureName + ( ( size > 1 ) ? "s" : "" ) + ": "
 104  
                 + this.excludeString );
 105  27
             results = filterExcludes( results, this.excludes );
 106  
         }
 107  
 
 108  414
         return results;
 109  
     }
 110  
 
 111  
     /**
 112  
      * Processes the dependencies list and includes the
 113  
      * dependencies that match a filter in the list.
 114  
      * 
 115  
      * @param depends List of dependencies.
 116  
      * @param includes List of types or classifiers to
 117  
      *            include.
 118  
      * 
 119  
      * @return a set of filtered artifacts.
 120  
      */
 121  
     private Set filterIncludes( Set artifacts, List theIncludes )
 122  
     {
 123  22
         Set result = new HashSet();
 124  
 
 125  22
         Iterator includeIter = theIncludes.iterator();
 126  51
         while ( includeIter.hasNext() )
 127  
         {
 128  29
             String include = (String) includeIter.next();
 129  29
             Iterator iter = artifacts.iterator();
 130  146
             while ( iter.hasNext() )
 131  
             {
 132  117
                 Artifact artifact = (Artifact) iter.next();
 133  
 
 134  
                 // if the classifier or type of the artifact
 135  
                 // matches the feature
 136  
                 // to include, add to the
 137  
                 // results
 138  117
                 if ( compareFeatures( getArtifactFeature( artifact ), include ) )
 139  
                 {
 140  28
                     result.add( artifact );
 141  
                 }
 142  117
             }
 143  29
         }
 144  22
         return result;
 145  
     }
 146  
 
 147  
     /**
 148  
      * Processes the dependencies list and excludes the
 149  
      * dependencies that match a filter in the list.
 150  
      * 
 151  
      * @param depends List of dependencies.
 152  
      * @param excludes List of types or classifiers to
 153  
      *            exclude.
 154  
      * 
 155  
      * @return a set of filtered artifacts.
 156  
      */
 157  
     private Set filterExcludes( Set artifacts, List theExcludes )
 158  
     {
 159  27
         Set result = new HashSet();
 160  
 
 161  27
         Iterator iter = artifacts.iterator();
 162  101
         while ( iter.hasNext() )
 163  
         {
 164  74
             boolean exclude = false;
 165  74
             Artifact artifact = (Artifact) iter.next();
 166  74
             String artifactFeature = getArtifactFeature( artifact );
 167  
 
 168  
             // look through all types or classifiers. If no
 169  
             // matches are found
 170  
             // then it can be added to the results.
 171  74
             Iterator excludeIter = theExcludes.iterator();
 172  141
             while ( excludeIter.hasNext() )
 173  
             {
 174  99
                 String excludeFeature = (String) excludeIter.next();
 175  99
                 if ( compareFeatures( artifactFeature, excludeFeature ) )
 176  
                 {
 177  32
                     exclude = true;
 178  32
                     break;
 179  
                 }
 180  67
             }
 181  
 
 182  74
             if ( !exclude )
 183  
             {
 184  42
                 result.add( artifact );
 185  
             }
 186  74
         }
 187  
 
 188  27
         return result;
 189  
     }
 190  
 
 191  
     /**
 192  
      * Should return the type or classifier of the given
 193  
      * artifact, so that we can filter it
 194  
      * 
 195  
      * @param artifact artifact to return type or classifier
 196  
      *            of
 197  
      * @return type or classifier
 198  
      */
 199  
     protected abstract String getArtifactFeature( Artifact artifact );
 200  
 
 201  
     public void setExcludes( String excludeString )
 202  
     {
 203  427
         this.excludeString = excludeString;
 204  
 
 205  427
         if ( StringUtils.isNotEmpty( excludeString ) )
 206  
         {
 207  32
             this.excludes = Arrays.asList( StringUtils.split( excludeString, "," ) );
 208  
         }
 209  427
     }
 210  
 
 211  
     public void setIncludes( String includeString )
 212  
     {
 213  427
         this.includeString = includeString;
 214  
 
 215  427
         if ( StringUtils.isNotEmpty( includeString ) )
 216  
         {
 217  27
             this.includes = Arrays.asList( StringUtils.split( includeString, "," ) );
 218  
         }
 219  427
     }
 220  
 
 221  
     /**
 222  
      * @return Returns the excludes.
 223  
      */
 224  
     public List getExcludes()
 225  
     {
 226  4
         return this.excludes;
 227  
     }
 228  
 
 229  
     /**
 230  
      * @return Returns the includes.
 231  
      */
 232  
     public List getIncludes()
 233  
     {
 234  4
         return this.includes;
 235  
     }
 236  
 
 237  
     /**
 238  
      * Allows Feature comparison to be customized
 239  
      * 
 240  
      * @param lhs String artifact's feature
 241  
      * @param rhs String feature from exclude or include
 242  
      *            list
 243  
      * @return boolean true if features match
 244  
      */
 245  
     protected boolean compareFeatures( String lhs, String rhs )
 246  
     {
 247  126
         return ( lhs.equals( rhs ) );
 248  
     }
 249  
 }