Coverage Report - org.apache.maven.plugin.dependency.utils.filters.ScopeFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
ScopeFilter
100% 
100% 
3.25
 
 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.HashSet;
 23  
 import java.util.Iterator;
 24  
 import java.util.Set;
 25  
 
 26  
 import org.apache.maven.artifact.Artifact;
 27  
 import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
 28  
 import org.apache.maven.plugin.MojoExecutionException;
 29  
 import org.apache.maven.plugin.logging.Log;
 30  
 import org.codehaus.plexus.util.StringUtils;
 31  
 
 32  
 /**
 33  
  * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
 34  
  * @version $Id: ScopeFilter.java 552528 2007-07-02 16:12:47Z markh $
 35  
  */
 36  
 public class ScopeFilter
 37  
     extends AbstractArtifactsFilter
 38  
 {
 39  
 
 40  
     private String includeScope;
 41  
 
 42  
     private String excludeScope;
 43  
 
 44  
     public ScopeFilter( String includeScope, String excludeScope )
 45  116
     {
 46  116
         this.includeScope = includeScope;
 47  116
         this.excludeScope = excludeScope;
 48  116
     }
 49  
 
 50  
     /**
 51  
      * This function determines if filtering needs to be performed. Excludes are
 52  
      * ignored if Includes are used.
 53  
      * 
 54  
      * @param dependencies
 55  
      *            the set of dependencies to filter.
 56  
      * 
 57  
      * @return a Set of filtered dependencies.
 58  
      * @throws MojoExecutionException
 59  
      */
 60  
     public Set filter( Set artifacts, Log log )
 61  
         throws MojoExecutionException
 62  
     {
 63  114
         Set results = artifacts;
 64  
 
 65  114
         if ( StringUtils.isNotEmpty( includeScope ) )
 66  
         {
 67  16
             if ( !Artifact.SCOPE_COMPILE.equals( includeScope ) && !Artifact.SCOPE_TEST.equals( includeScope )
 68  
                 && !Artifact.SCOPE_PROVIDED.equals( includeScope ) && !Artifact.SCOPE_RUNTIME.equals( includeScope )
 69  
                 && !Artifact.SCOPE_SYSTEM.equals( includeScope ) )
 70  
             {
 71  1
                 throw new MojoExecutionException( "Invalid Scope in includeScope: " + includeScope );
 72  
             }
 73  
 
 74  15
             results = new HashSet();
 75  
 
 76  15
             if ( Artifact.SCOPE_PROVIDED.equals( includeScope ) || Artifact.SCOPE_SYSTEM.equals( includeScope ) )
 77  
             {
 78  6
                 results = includeSingleScope( artifacts, includeScope );
 79  
             }
 80  
             else
 81  
             {
 82  9
                 ScopeArtifactFilter saf = new ScopeArtifactFilter( includeScope );
 83  
 
 84  9
                 Iterator iter = artifacts.iterator();
 85  54
                 while ( iter.hasNext() )
 86  
                 {
 87  45
                     Artifact artifact = (Artifact) iter.next();
 88  45
                     if ( saf.include( artifact ) )
 89  
                     {
 90  30
                         results.add( artifact );
 91  
                     }
 92  45
                 }
 93  9
             }
 94  
         }
 95  98
         else if ( StringUtils.isNotEmpty( excludeScope ) )
 96  
         {
 97  15
             if ( !Artifact.SCOPE_COMPILE.equals( excludeScope ) && !Artifact.SCOPE_TEST.equals( excludeScope )
 98  
                 && !Artifact.SCOPE_PROVIDED.equals( excludeScope ) && !Artifact.SCOPE_RUNTIME.equals( excludeScope )
 99  
                 && !Artifact.SCOPE_SYSTEM.equals( excludeScope ) )
 100  
             {
 101  1
                 throw new MojoExecutionException( "Invalid Scope in excludeScope: " + excludeScope );
 102  
             }
 103  14
             results = new HashSet();
 104  
             // plexus ScopeArtifactFilter doesn't handle the provided scope so
 105  
             // we
 106  
             // need special handling for it.
 107  14
             if ( Artifact.SCOPE_TEST.equals( excludeScope ) )
 108  
             {
 109  3
                 throw new MojoExecutionException( " Can't exclude Test scope, this will exclude everything." );
 110  
             }
 111  11
             else if ( !Artifact.SCOPE_PROVIDED.equals( excludeScope ) && !Artifact.SCOPE_SYSTEM.equals( excludeScope ) )
 112  
             {
 113  5
                 ScopeArtifactFilter saf = new ScopeArtifactFilter( excludeScope );
 114  
 
 115  5
                 Iterator iter = artifacts.iterator();
 116  30
                 while ( iter.hasNext() )
 117  
                 {
 118  25
                     Artifact artifact = (Artifact) iter.next();
 119  25
                     if ( !saf.include( artifact ) )
 120  
                     {
 121  12
                         results.add( artifact );
 122  
                     }
 123  25
                 }
 124  5
             }
 125  
             else
 126  
             {
 127  6
                 results = excludeSingleScope( artifacts, excludeScope );
 128  
             }
 129  
         }
 130  
 
 131  109
         return results;
 132  
     }
 133  
 
 134  
     private Set includeSingleScope( Set artifacts, String scope )
 135  
     {
 136  6
         HashSet results = new HashSet();
 137  6
         Iterator iter = artifacts.iterator();
 138  36
         while ( iter.hasNext() )
 139  
         {
 140  30
             Artifact artifact = (Artifact) iter.next();
 141  30
             if ( scope.equals( artifact.getScope() ) )
 142  
             {
 143  6
                 results.add( artifact );
 144  
             }
 145  30
         }
 146  6
         return results;
 147  
     }
 148  
 
 149  
     private Set excludeSingleScope( Set artifacts, String scope )
 150  
     {
 151  6
         HashSet results = new HashSet();
 152  6
         Iterator iter = artifacts.iterator();
 153  36
         while ( iter.hasNext() )
 154  
         {
 155  30
             Artifact artifact = (Artifact) iter.next();
 156  30
             if ( !scope.equals( artifact.getScope() ) )
 157  
             {
 158  24
                 results.add( artifact );
 159  
             }
 160  30
         }
 161  6
         return results;
 162  
     }
 163  
 
 164  
     /**
 165  
      * @return Returns the includeScope.
 166  
      */
 167  
     public String getIncludeScope()
 168  
     {
 169  2
         return this.includeScope;
 170  
     }
 171  
 
 172  
     /**
 173  
      * @param includeScope
 174  
      *            The includeScope to set.
 175  
      */
 176  
     public void setIncludeScope( String scope )
 177  
     {
 178  1
         this.includeScope = scope;
 179  1
     }
 180  
 
 181  
     /**
 182  
      * @return Returns the excludeScope.
 183  
      */
 184  
     public String getExcludeScope()
 185  
     {
 186  2
         return this.excludeScope;
 187  
     }
 188  
 
 189  
     /**
 190  
      * @param excludeScope
 191  
      *            The excludeScope to set.
 192  
      */
 193  
     public void setExcludeScope( String excludeScope )
 194  
     {
 195  1
         this.excludeScope = excludeScope;
 196  1
     }
 197  
 
 198  
 }