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