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.plugins.shade.filter;
20  
21  import java.io.File;
22  import java.util.Collections;
23  import java.util.HashSet;
24  import java.util.Set;
25  
26  import org.apache.maven.plugins.shade.mojo.ArchiveFilter;
27  import org.codehaus.plexus.util.SelectorUtils;
28  
29  /**
30   * @author David Blevins
31   */
32  /**
33   * @author kama
34   *
35   */
36  public class SimpleFilter implements Filter {
37  
38      private Set<File> jars;
39  
40      private Set<String> includes;
41  
42      private Set<String> excludes;
43  
44      private boolean excludeDefaults = true;
45  
46      /**
47       * @deprecated As of release 3.2.2, replaced by {@link #SimpleFilter(Set, ArchiveFilter)}}
48       * @param jars set of {@link File}s.
49       * @param includes set of includes.
50       * @param excludes set of excludes.
51       */
52      @Deprecated
53      public SimpleFilter(Set<File> jars, Set<String> includes, Set<String> excludes) {
54          this(jars, includes, excludes, true);
55      }
56  
57      /**
58       * @param jars set of {@link File}s.
59       * @param archiveFilter set of {@link ArchiveFilter}s.
60       */
61      public SimpleFilter(final Set<File> jars, final ArchiveFilter archiveFilter) {
62          this(jars, archiveFilter.getIncludes(), archiveFilter.getExcludes(), archiveFilter.getExcludeDefaults());
63      }
64  
65      /**
66       * @param jars set of {@link File}s.
67       * @param includes set of includes.
68       * @param excludes set of excludes.
69       * @param excludeDefaults whether to exclude default includes once includes are provided explicitly.
70       */
71      private SimpleFilter(
72              final Set<File> jars,
73              final Set<String> includes,
74              final Set<String> excludes,
75              final boolean excludeDefaults) {
76          this.jars = (jars != null) ? Collections.<File>unmodifiableSet(jars) : Collections.<File>emptySet();
77          this.includes = normalizePatterns(includes);
78          this.excludes = normalizePatterns(excludes);
79          this.excludeDefaults = excludeDefaults;
80      }
81  
82      /** {@inheritDoc} */
83      public boolean canFilter(File jar) {
84          return jars.contains(jar);
85      }
86  
87      /** {@inheritDoc} */
88      public boolean isFiltered(String classFile) {
89          String path = normalizePath(classFile);
90  
91          return !((!excludeDefaults || isIncluded(path)) && !isExcluded(path));
92      }
93  
94      /**
95       * @param classFile The class file.
96       * @return true if included false otherwise.
97       */
98      public boolean isSpecificallyIncluded(String classFile) {
99          if (includes == null || includes.isEmpty()) {
100             return false;
101         }
102 
103         String path = normalizePath(classFile);
104 
105         return isIncluded(path);
106     }
107 
108     private boolean isIncluded(String classFile) {
109         if (includes == null || includes.isEmpty()) {
110             return true;
111         }
112 
113         return matchPaths(includes, classFile);
114     }
115 
116     private boolean isExcluded(String classFile) {
117         if (excludes == null || excludes.isEmpty()) {
118             return false;
119         }
120 
121         return matchPaths(excludes, classFile);
122     }
123 
124     private boolean matchPaths(Set<String> patterns, String classFile) {
125         for (String pattern : patterns) {
126 
127             if (SelectorUtils.matchPath(pattern, classFile)) {
128                 return true;
129             }
130         }
131 
132         return false;
133     }
134 
135     private String normalizePath(String path) {
136         return (path != null) ? path.replace(File.separatorChar == '/' ? '\\' : '/', File.separatorChar) : null;
137     }
138 
139     private Set<String> normalizePatterns(Set<String> patterns) {
140         Set<String> result = new HashSet<>();
141 
142         if (patterns != null) {
143             for (String pattern : patterns) {
144                 pattern = normalizePath(pattern);
145 
146                 if (pattern.endsWith(File.separator)) {
147                     pattern += "**";
148                 }
149 
150                 result.add(pattern);
151             }
152         }
153 
154         return result;
155     }
156 
157     /** {@inheritDoc} */
158     public void finished() {}
159 }