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 @Override
84 public boolean canFilter(File jar) {
85 return jars.contains(jar);
86 }
87
88
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
98
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
160 @Override
161 public void finished() {}
162 }