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.Set;
27
28
29
30
31
32
33
34
35 public class SimpleFilter
36 implements Filter
37 {
38
39 private Set<File> jars;
40
41 private Set<String> includes;
42
43 private Set<String> excludes;
44
45
46
47
48
49
50 public SimpleFilter( Set<File> jars, Set<String> includes, Set<String> excludes )
51 {
52 this.jars = ( jars != null ) ? new HashSet<File>( jars ) : new HashSet<File>();
53 this.includes = normalizePatterns( includes );
54 this.excludes = normalizePatterns( excludes );
55 }
56
57
58 public boolean canFilter( File jar )
59 {
60 return jars.contains( jar );
61 }
62
63
64 public boolean isFiltered( String classFile )
65 {
66 String path = normalizePath( classFile );
67
68 return !( isIncluded( path ) && !isExcluded( path ) );
69 }
70
71
72
73
74
75 public boolean isSpecificallyIncluded( String classFile )
76 {
77 if ( includes == null || includes.isEmpty() )
78 {
79 return false;
80 }
81
82 String path = normalizePath( classFile );
83
84 return isIncluded( path );
85 }
86
87 private boolean isIncluded( String classFile )
88 {
89 if ( includes == null || includes.isEmpty() )
90 {
91 return true;
92 }
93
94 return matchPaths( includes, classFile );
95 }
96
97 private boolean isExcluded( String classFile )
98 {
99 if ( excludes == null || excludes.isEmpty() )
100 {
101 return false;
102 }
103
104 return matchPaths( excludes, classFile );
105 }
106
107 private boolean matchPaths( Set<String> patterns, String classFile )
108 {
109 for ( String pattern : patterns )
110 {
111
112 if ( SelectorUtils.matchPath( pattern, classFile ) )
113 {
114 return true;
115 }
116 }
117
118 return false;
119 }
120
121 private String normalizePath( String path )
122 {
123 return ( path != null ) ? path.replace( File.separatorChar == '/' ? '\\' : '/', File.separatorChar ) : null;
124 }
125
126 private Set<String> normalizePatterns( Set<String> patterns )
127 {
128 Set<String> result = new HashSet<String>();
129
130 if ( patterns != null )
131 {
132 for ( String pattern : patterns )
133 {
134 pattern = normalizePath( pattern );
135
136 if ( pattern.endsWith( File.separator ) )
137 {
138 pattern += "**";
139 }
140
141 result.add( pattern );
142 }
143 }
144
145 return result;
146 }
147
148
149 public void finished()
150 {
151 }
152 }