Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SimpleFilter |
|
| 3.0;3 |
1 | package org.apache.maven.plugins.shade.filter; | |
2 | ||
3 | /* | |
4 | * Licensed to the Apache Software Foundation (ASF) under one | |
5 | * or more contributor license agreements. See the NOTICE file | |
6 | * distributed with this work for additional information | |
7 | * regarding copyright ownership. The ASF licenses this file | |
8 | * to you under the Apache License, Version 2.0 (the | |
9 | * "License"); you may not use this file except in compliance | |
10 | * with the License. You may obtain a copy of the License at | |
11 | * | |
12 | * http://www.apache.org/licenses/LICENSE-2.0 | |
13 | * | |
14 | * Unless required by applicable law or agreed to in writing, | |
15 | * software distributed under the License is distributed on an | |
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
17 | * KIND, either express or implied. See the License for the | |
18 | * specific language governing permissions and limitations | |
19 | * under the License. | |
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 | * @author David Blevins | |
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 | 8 | { |
44 | 8 | this.jars = ( jars != null ) ? new HashSet( jars ) : new HashSet(); |
45 | 8 | this.includes = normalizePatterns( includes ); |
46 | 8 | this.excludes = normalizePatterns( excludes ); |
47 | 8 | } |
48 | ||
49 | public boolean canFilter( File jar ) | |
50 | { | |
51 | 0 | return jars.contains( jar ); |
52 | } | |
53 | ||
54 | public boolean isFiltered( String classFile ) | |
55 | { | |
56 | 23 | String path = normalizePath( classFile ); |
57 | ||
58 | 23 | return !( isIncluded( path ) && !isExcluded( path ) ); |
59 | } | |
60 | ||
61 | private boolean isIncluded( String classFile ) | |
62 | { | |
63 | 23 | if ( includes == null || includes.isEmpty() ) |
64 | { | |
65 | 16 | return true; |
66 | } | |
67 | ||
68 | 7 | return matchPaths( includes, classFile ); |
69 | } | |
70 | ||
71 | private boolean isExcluded( String classFile ) | |
72 | { | |
73 | 20 | if ( excludes == null || excludes.isEmpty() ) |
74 | { | |
75 | 8 | return false; |
76 | } | |
77 | ||
78 | 12 | return matchPaths( excludes, classFile ); |
79 | } | |
80 | ||
81 | private boolean matchPaths( Set patterns, String classFile ) | |
82 | { | |
83 | 19 | for ( Iterator iterator = patterns.iterator(); iterator.hasNext(); ) |
84 | { | |
85 | 19 | String pattern = (String) iterator.next(); |
86 | ||
87 | 19 | if ( SelectorUtils.matchPath( pattern, classFile ) ) |
88 | { | |
89 | 10 | return true; |
90 | } | |
91 | 9 | } |
92 | ||
93 | 9 | return false; |
94 | } | |
95 | ||
96 | private String normalizePath( String path ) | |
97 | { | |
98 | 29 | return ( path != null ) ? path.replace( File.separatorChar == '/' ? '\\' : '/', File.separatorChar ) : null; |
99 | } | |
100 | ||
101 | private Set normalizePatterns( Set patterns ) | |
102 | { | |
103 | 16 | Set result = new HashSet(); |
104 | ||
105 | 16 | if ( patterns != null ) |
106 | { | |
107 | 14 | for ( Iterator it = patterns.iterator(); it.hasNext(); ) |
108 | { | |
109 | 6 | String pattern = (String) it.next(); |
110 | ||
111 | 6 | pattern = normalizePath( pattern ); |
112 | ||
113 | 6 | if ( pattern.endsWith( File.separator ) ) |
114 | { | |
115 | 1 | pattern += "**"; |
116 | } | |
117 | ||
118 | 6 | result.add( pattern ); |
119 | 6 | } |
120 | } | |
121 | ||
122 | 16 | return result; |
123 | } | |
124 | ||
125 | } |