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