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