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