1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
31
32
33
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
48
49
50
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
59
60
61 public SimpleFilter(final Set<File> jars, final ArchiveFilter archiveFilter) {
62 this(jars, archiveFilter.getIncludes(), archiveFilter.getExcludes(), archiveFilter.getExcludeDefaults());
63 }
64
65
66
67
68
69
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
83 public boolean canFilter(File jar) {
84 return jars.contains(jar);
85 }
86
87
88 public boolean isFiltered(String classFile) {
89 String path = normalizePath(classFile);
90
91 return !((!excludeDefaults || isIncluded(path)) && !isExcluded(path));
92 }
93
94
95
96
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
158 public void finished() {}
159 }