View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.shared.artifact.filter.resolve.transform;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  
24  import org.apache.maven.artifact.resolver.filter.AndArtifactFilter;
25  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
26  import org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter;
27  import org.apache.maven.shared.artifact.filter.PatternExcludesArtifactFilter;
28  import org.apache.maven.shared.artifact.filter.PatternIncludesArtifactFilter;
29  import org.apache.maven.shared.artifact.filter.resolve.AbstractFilter;
30  import org.apache.maven.shared.artifact.filter.resolve.AndFilter;
31  import org.apache.maven.shared.artifact.filter.resolve.ExclusionsFilter;
32  import org.apache.maven.shared.artifact.filter.resolve.FilterTransformer;
33  import org.apache.maven.shared.artifact.filter.resolve.OrFilter;
34  import org.apache.maven.shared.artifact.filter.resolve.PatternExclusionsFilter;
35  import org.apache.maven.shared.artifact.filter.resolve.PatternInclusionsFilter;
36  import org.apache.maven.shared.artifact.filter.resolve.ScopeFilter;
37  import org.apache.maven.shared.artifact.filter.resolve.TransformableFilter;
38  
39  /**
40   * Makes it possible to use the TransformableFilters for Aether and as classic Maven ArtifactFilter.
41   *
42   * <strong>Note:</strong> the {@link AndFilter} and {@link ExclusionsFilter} are transformed to {@link ArtifactFilter}
43   * implementations of Maven Core
44   *
45   * @author Robert Scholte
46   * @since 3.0
47   */
48  public class ArtifactIncludeFilterTransformer implements FilterTransformer<ArtifactFilter> {
49  
50      private boolean includeNullScope = true;
51  
52      private boolean actTransitivelyPattern = false;
53  
54      /**
55       * Used by {@link #transform(ScopeFilter)}
56       *
57       * When filtering on artifacts it is possible that the scope is unknown.
58       * Decide if artifact should be included if its scope is {@code null}, default is {@code true}
59       *
60       * @param includeNullScope set to {@code false} if {@code null}-scoped Artifacts should not be included
61       */
62      public void setIncludeNullScope(boolean includeNullScope) {
63          this.includeNullScope = includeNullScope;
64      }
65  
66      /**
67       * Used by {@link #transform(PatternExclusionsFilter)} and {@link #transform(PatternInclusionsFilter)} Determines
68       * whether the include/exclude patterns will be applied to the transitive path of a given artifact. If {@code true},
69       * and the current artifact is a transitive dependency brought in by another artifact which matches an inclusion or
70       * exclusion pattern, then the current artifact has the same inclusion/exclusion logic applied to it as well.
71       * Default is {@code false}
72       *
73       * @param actTransitivelyPattern set to {@code true} if this artifact should be included/excluded just like one of
74       *            its ancestors.
75       */
76      public void setActTransitivelyPattern(boolean actTransitivelyPattern) {
77          this.actTransitivelyPattern = actTransitivelyPattern;
78      }
79  
80      /** {@inheritDoc} */
81      @Override
82      public ArtifactFilter transform(final ScopeFilter scopeFilter) {
83          return artifact -> {
84              if (artifact.getScope() == null) {
85                  return includeNullScope;
86              }
87  
88              boolean isIncluded;
89  
90              if (scopeFilter.getIncluded() != null) {
91                  isIncluded = scopeFilter.getIncluded().contains(artifact.getScope());
92              } else {
93                  isIncluded = true;
94              }
95  
96              boolean isExcluded;
97  
98              if (scopeFilter.getExcluded() != null) {
99                  isExcluded = scopeFilter.getExcluded().contains(artifact.getScope());
100             } else {
101                 isExcluded = false;
102             }
103 
104             return isIncluded && !isExcluded;
105         };
106     }
107 
108     /** {@inheritDoc} */
109     @Override
110     public AndArtifactFilter transform(AndFilter andFilter) {
111         AndArtifactFilter filter = new AndArtifactFilter();
112 
113         for (TransformableFilter subFilter : andFilter.getFilters()) {
114             filter.add(subFilter.transform(this));
115         }
116 
117         return filter;
118     }
119 
120     /** {@inheritDoc} */
121     @Override
122     public ArtifactFilter transform(final ExclusionsFilter exclusionsFilter) {
123         return new ExcludesArtifactFilter(new ArrayList<>(exclusionsFilter.getExcludes()));
124     }
125 
126     /** {@inheritDoc} */
127     @Override
128     public ArtifactFilter transform(OrFilter orFilter) {
129         final Collection<ArtifactFilter> filters =
130                 new ArrayList<>(orFilter.getFilters().size());
131 
132         for (TransformableFilter subFilter : orFilter.getFilters()) {
133             filters.add(subFilter.transform(this));
134         }
135 
136         return artifact -> {
137             for (ArtifactFilter filter : filters) {
138                 if (filter.include(artifact)) {
139                     return true;
140                 }
141             }
142             return false;
143         };
144     }
145 
146     /** {@inheritDoc} */
147     @Override
148     public ArtifactFilter transform(PatternExclusionsFilter patternExclusionsFilter) {
149         return new PatternExcludesArtifactFilter(patternExclusionsFilter.getExcludes(), actTransitivelyPattern);
150     }
151 
152     /** {@inheritDoc} */
153     @Override
154     public ArtifactFilter transform(PatternInclusionsFilter patternInclusionsFilter) {
155         return new PatternIncludesArtifactFilter(patternInclusionsFilter.getIncludes(), actTransitivelyPattern);
156     }
157 
158     /** {@inheritDoc} */
159     @Override
160     public ArtifactFilter transform(final AbstractFilter filter) {
161         return artifact -> filter.accept(new ArtifactIncludeNode(artifact), null);
162     }
163 }