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.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      {
44          this.includeScope = includeScope;
45          this.excludeScope = excludeScope;
46      }
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          Set results = artifacts;
62  
63          if ( StringUtils.isNotEmpty( includeScope ) )
64          {
65              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                  throw new ArtifactFilterException( "Invalid Scope in includeScope: " + includeScope );
70              }
71  
72              results = new HashSet();
73  
74              if ( Artifact.SCOPE_PROVIDED.equals( includeScope ) || Artifact.SCOPE_SYSTEM.equals( includeScope ) )
75              {
76                  results = includeSingleScope( artifacts, includeScope );
77              }
78              else
79              {
80                  ScopeArtifactFilter saf = new ScopeArtifactFilter( includeScope );
81  
82                  Iterator iter = artifacts.iterator();
83                  while ( iter.hasNext() )
84                  {
85                      Artifact artifact = (Artifact) iter.next();
86                      if ( saf.include( artifact ) )
87                      {
88                          results.add( artifact );
89                      }
90                  }
91              }
92          }
93          else if ( StringUtils.isNotEmpty( excludeScope ) )
94          {
95              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                  throw new ArtifactFilterException( "Invalid Scope in excludeScope: " + excludeScope );
100             }
101             results = new HashSet();
102             // plexus ScopeArtifactFilter doesn't handle the provided scope so
103             // we
104             // need special handling for it.
105             if ( Artifact.SCOPE_TEST.equals( excludeScope ) )
106             {
107                 throw new ArtifactFilterException( " Can't exclude Test scope, this will exclude everything." );
108             }
109             else if ( !Artifact.SCOPE_PROVIDED.equals( excludeScope ) && !Artifact.SCOPE_SYSTEM.equals( excludeScope ) )
110             {
111                 ScopeArtifactFilter saf = new ScopeArtifactFilter( excludeScope );
112 
113                 Iterator iter = artifacts.iterator();
114                 while ( iter.hasNext() )
115                 {
116                     Artifact artifact = (Artifact) iter.next();
117                     if ( !saf.include( artifact ) )
118                     {
119                         results.add( artifact );
120                     }
121                 }
122             }
123             else
124             {
125                 results = excludeSingleScope( artifacts, excludeScope );
126             }
127         }
128 
129         return results;
130     }
131 
132     private Set includeSingleScope( Set artifacts, String scope )
133     {
134         HashSet results = new HashSet();
135         Iterator iter = artifacts.iterator();
136         while ( iter.hasNext() )
137         {
138             Artifact artifact = (Artifact) iter.next();
139             if ( scope.equals( artifact.getScope() ) )
140             {
141                 results.add( artifact );
142             }
143         }
144         return results;
145     }
146 
147     private Set excludeSingleScope( Set artifacts, String scope )
148     {
149         HashSet results = new HashSet();
150         Iterator iter = artifacts.iterator();
151         while ( iter.hasNext() )
152         {
153             Artifact artifact = (Artifact) iter.next();
154             if ( !scope.equals( artifact.getScope() ) )
155             {
156                 results.add( artifact );
157             }
158         }
159         return results;
160     }
161 
162     /**
163      * @return Returns the includeScope.
164      */
165     public String getIncludeScope()
166     {
167         return this.includeScope;
168     }
169 
170     /**
171      * @param includeScope
172      *            The includeScope to set.
173      */
174     public void setIncludeScope( String scope )
175     {
176         this.includeScope = scope;
177     }
178 
179     /**
180      * @return Returns the excludeScope.
181      */
182     public String getExcludeScope()
183     {
184         return this.excludeScope;
185     }
186 
187     /**
188      * @param excludeScope
189      *            The excludeScope to set.
190      */
191     public void setExcludeScope( String excludeScope )
192     {
193         this.excludeScope = excludeScope;
194     }
195 
196 }