1 package org.apache.maven.plugins.shade.filter;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.codehaus.plexus.util.SelectorUtils;
23
24 import java.io.File;
25 import java.util.HashSet;
26 import java.util.Collections;
27 import java.util.Set;
28
29 import org.apache.maven.plugins.shade.mojo.ArchiveFilter;
30
31
32
33
34
35
36
37
38 public class SimpleFilter
39 implements Filter
40 {
41
42 private Set<File> jars;
43
44 private Set<String> includes;
45
46 private Set<String> excludes;
47
48 private boolean excludeDefaults = true;
49
50
51
52
53
54
55
56 @Deprecated
57 public SimpleFilter( Set<File> jars, Set<String> includes, Set<String> excludes )
58 {
59 this( jars, includes, excludes, true );
60 }
61
62
63
64
65
66 public SimpleFilter( final Set<File> jars, final ArchiveFilter archiveFilter )
67 {
68 this( jars, archiveFilter.getIncludes(), archiveFilter.getExcludes(), archiveFilter.getExcludeDefaults() );
69 }
70
71
72
73
74
75
76
77 private SimpleFilter( final Set<File> jars, final Set<String> includes, final Set<String> excludes,
78 final boolean excludeDefaults )
79 {
80 this.jars = ( jars != null ) ? Collections.<File>unmodifiableSet( jars ) : Collections.<File>emptySet();
81 this.includes = normalizePatterns( includes );
82 this.excludes = normalizePatterns( excludes );
83 this.excludeDefaults = excludeDefaults;
84 }
85
86
87 public boolean canFilter( File jar )
88 {
89 return jars.contains( jar );
90 }
91
92
93 public boolean isFiltered( String classFile )
94 {
95 String path = normalizePath( classFile );
96
97 return !( ( !excludeDefaults || isIncluded( path ) ) && !isExcluded( path ) );
98 }
99
100
101
102
103
104 public boolean isSpecificallyIncluded( String classFile )
105 {
106 if ( includes == null || includes.isEmpty() )
107 {
108 return false;
109 }
110
111 String path = normalizePath( classFile );
112
113 return isIncluded( path );
114 }
115
116 private boolean isIncluded( String classFile )
117 {
118 if ( includes == null || includes.isEmpty() )
119 {
120 return true;
121 }
122
123 return matchPaths( includes, classFile );
124 }
125
126 private boolean isExcluded( String classFile )
127 {
128 if ( excludes == null || excludes.isEmpty() )
129 {
130 return false;
131 }
132
133 return matchPaths( excludes, classFile );
134 }
135
136 private boolean matchPaths( Set<String> patterns, String classFile )
137 {
138 for ( String pattern : patterns )
139 {
140
141 if ( SelectorUtils.matchPath( pattern, classFile ) )
142 {
143 return true;
144 }
145 }
146
147 return false;
148 }
149
150 private String normalizePath( String path )
151 {
152 return ( path != null ) ? path.replace( File.separatorChar == '/' ? '\\' : '/', File.separatorChar ) : null;
153 }
154
155 private Set<String> normalizePatterns( Set<String> patterns )
156 {
157 Set<String> result = new HashSet<>();
158
159 if ( patterns != null )
160 {
161 for ( String pattern : patterns )
162 {
163 pattern = normalizePath( pattern );
164
165 if ( pattern.endsWith( File.separator ) )
166 {
167 pattern += "**";
168 }
169
170 result.add( pattern );
171 }
172 }
173
174 return result;
175 }
176
177
178 public void finished()
179 {
180 }
181 }