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      @Override
84      public boolean canFilter(File jar) {
85          return jars.contains(jar);
86      }
87  
88      /** {@inheritDoc} */
89      @Override
90      public boolean isFiltered(String classFile) {
91          String path = normalizePath(classFile);
92  
93          return !((!excludeDefaults || isIncluded(path)) && !isExcluded(path));
94      }
95  
96      /**
97       * @param classFile The class file.
98       * @return true if included false otherwise.
99       */
100     public boolean isSpecificallyIncluded(String classFile) {
101         if (includes == null || includes.isEmpty()) {
102             return false;
103         }
104 
105         String path = normalizePath(classFile);
106 
107         return isIncluded(path);
108     }
109 
110     private boolean isIncluded(String classFile) {
111         if (includes == null || includes.isEmpty()) {
112             return true;
113         }
114 
115         return matchPaths(includes, classFile);
116     }
117 
118     private boolean isExcluded(String classFile) {
119         if (excludes == null || excludes.isEmpty()) {
120             return false;
121         }
122 
123         return matchPaths(excludes, classFile);
124     }
125 
126     private boolean matchPaths(Set<String> patterns, String classFile) {
127         for (String pattern : patterns) {
128 
129             if (SelectorUtils.matchPath(pattern, classFile)) {
130                 return true;
131             }
132         }
133 
134         return false;
135     }
136 
137     private String normalizePath(String path) {
138         return (path != null) ? path.replace(File.separatorChar == '/' ? '\\' : '/', File.separatorChar) : null;
139     }
140 
141     private Set<String> normalizePatterns(Set<String> patterns) {
142         Set<String> result = new HashSet<>();
143 
144         if (patterns != null) {
145             for (String pattern : patterns) {
146                 pattern = normalizePath(pattern);
147 
148                 if (pattern.endsWith(File.separator)) {
149                     pattern += "**";
150                 }
151 
152                 result.add(pattern);
153             }
154         }
155 
156         return result;
157     }
158 
159     /** {@inheritDoc} */
160     @Override
161     public void finished() {}
162 }