View Javadoc
1   package org.apache.maven.shared.artifact.filter.resolve.transform;
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.ArrayList;
23  import java.util.Collection;
24  
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.artifact.resolver.filter.AndArtifactFilter;
27  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
28  import org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter;
29  import org.apache.maven.shared.artifact.filter.PatternExcludesArtifactFilter;
30  import org.apache.maven.shared.artifact.filter.PatternIncludesArtifactFilter;
31  import org.apache.maven.shared.artifact.filter.resolve.AbstractFilter;
32  import org.apache.maven.shared.artifact.filter.resolve.AndFilter;
33  import org.apache.maven.shared.artifact.filter.resolve.ExclusionsFilter;
34  import org.apache.maven.shared.artifact.filter.resolve.FilterTransformer;
35  import org.apache.maven.shared.artifact.filter.resolve.OrFilter;
36  import org.apache.maven.shared.artifact.filter.resolve.PatternExclusionsFilter;
37  import org.apache.maven.shared.artifact.filter.resolve.PatternInclusionsFilter;
38  import org.apache.maven.shared.artifact.filter.resolve.ScopeFilter;
39  import org.apache.maven.shared.artifact.filter.resolve.TransformableFilter;
40  
41  /**
42   * Makes it possible to use the TransformableFilters for Aether and as classic Maven ArtifactFilter.
43   *
44   * <strong>Note:</strong> the {@link AndFilter} and {@link ExclusionsFilter} are transformed to {@link ArtifactFilter}
45   * implementations of Maven Core
46   *
47   * @author Robert Scholte
48   * @since 3.0
49   */
50  public class ArtifactIncludeFilterTransformer implements FilterTransformer<ArtifactFilter>
51  {
52  
53      private boolean includeNullScope = true;
54  
55      private boolean actTransitivelyPattern = false;
56  
57      /**
58       * Used by {@link #transform(ScopeFilter)}
59       *
60       * When filtering on artifacts it is possible that the scope is unknown.
61       * Decide if artifact should be included if its scope is {@code null}, default is {@code true}
62       *
63       * @param includeNullScope set to {@code false} if {@code null}-scoped Artifacts should not be included
64       */
65      public void setIncludeNullScope( boolean includeNullScope )
66      {
67          this.includeNullScope = includeNullScope;
68      }
69  
70      /**
71       * Used by {@link #transform(PatternExclusionsFilter)} and {@link #transform(PatternInclusionsFilter)} Determines
72       * whether the include/exclude patterns will be applied to the transitive path of a given artifact. If {@code true},
73       * and the current artifact is a transitive dependency brought in by another artifact which matches an inclusion or
74       * exclusion pattern, then the current artifact has the same inclusion/exclusion logic applied to it as well.
75       * Default is {@code false}
76       *
77       * @param actTransitivelyPattern set to {@code true} if this artifact should be included/excluded just like one of
78       *            its ancestors.
79       */
80      public void setActTransitivelyPattern( boolean actTransitivelyPattern )
81      {
82          this.actTransitivelyPattern = actTransitivelyPattern;
83      }
84  
85      /** {@inheritDoc} */
86      @Override
87      public ArtifactFilter transform( final ScopeFilter scopeFilter )
88      {
89          return new ArtifactFilter()
90          {
91              @Override
92              public boolean include( Artifact artifact )
93              {
94                  if ( artifact.getScope() == null )
95                  {
96                      return includeNullScope;
97                  }
98  
99                  boolean isIncluded;
100 
101                 if ( scopeFilter.getIncluded() != null )
102                 {
103                     isIncluded = scopeFilter.getIncluded().contains( artifact.getScope() );
104                 }
105                 else
106                 {
107                     isIncluded = true;
108                 }
109 
110                 boolean isExcluded;
111 
112                 if ( scopeFilter.getExcluded() != null )
113                 {
114                     isExcluded = scopeFilter.getExcluded().contains( artifact.getScope() );
115                 }
116                 else
117                 {
118                     isExcluded = false;
119                 }
120 
121                 return isIncluded && !isExcluded;
122             }
123         };
124     }
125 
126     /** {@inheritDoc} */
127     @Override
128     public AndArtifactFilter transform( AndFilter andFilter )
129     {
130         AndArtifactFilter filter = new AndArtifactFilter();
131 
132         for ( TransformableFilter subFilter : andFilter.getFilters() )
133         {
134             filter.add( subFilter.transform( this ) );
135         }
136 
137         return filter;
138     }
139 
140     /** {@inheritDoc} */
141     @Override
142     public ArtifactFilter transform( final ExclusionsFilter exclusionsFilter )
143     {
144         return new ExcludesArtifactFilter( new ArrayList<>( exclusionsFilter.getExcludes() ) );
145     }
146 
147     /** {@inheritDoc} */
148     @Override
149     public ArtifactFilter transform( OrFilter orFilter )
150     {
151         final Collection<ArtifactFilter> filters = new ArrayList<>( orFilter.getFilters().size() );
152 
153         for ( TransformableFilter subFilter : orFilter.getFilters() )
154         {
155             filters.add( subFilter.transform( this ) );
156         }
157 
158         return new ArtifactFilter()
159         {
160             @Override
161             public boolean include( Artifact artifact )
162             {
163                 for ( ArtifactFilter filter : filters )
164                 {
165                     if ( filter.include( artifact ) )
166                     {
167                         return true;
168                     }
169                 }
170                 return false;
171             }
172         };
173     }
174 
175     /** {@inheritDoc} */
176     @Override
177     public ArtifactFilter transform( PatternExclusionsFilter patternExclusionsFilter )
178     {
179         return new PatternExcludesArtifactFilter( patternExclusionsFilter.getExcludes(), actTransitivelyPattern );
180     }
181 
182     /** {@inheritDoc} */
183     @Override
184     public ArtifactFilter transform( PatternInclusionsFilter patternInclusionsFilter )
185     {
186         return new PatternIncludesArtifactFilter( patternInclusionsFilter.getIncludes(), actTransitivelyPattern );
187     }
188 
189     /** {@inheritDoc} */
190     @Override
191     public ArtifactFilter transform( final AbstractFilter filter )
192     {
193         return new ArtifactFilter()
194         {
195             @Override
196             public boolean include( Artifact artifact )
197             {
198                 return filter.accept( new ArtifactIncludeNode( artifact ), null );
199             }
200         };
201     }
202 }