Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SimpleFilter |
|
| 2.9;2.9 |
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.Set; | |
27 | ||
28 | /** | |
29 | * @author David Blevins | |
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 | 9 | { |
43 | 9 | this.jars = ( jars != null ) ? new HashSet( jars ) : new HashSet(); |
44 | 9 | this.includes = normalizePatterns( includes ); |
45 | 9 | this.excludes = normalizePatterns( excludes ); |
46 | 9 | } |
47 | ||
48 | public boolean canFilter( File jar ) | |
49 | { | |
50 | 2 | return jars.contains( jar ); |
51 | } | |
52 | ||
53 | public boolean isFiltered( String classFile ) | |
54 | { | |
55 | 23 | String path = normalizePath( classFile ); |
56 | ||
57 | 23 | return !( isIncluded( path ) && !isExcluded( path ) ); |
58 | } | |
59 | ||
60 | public boolean isSpecificallyIncluded( String classFile ) | |
61 | { | |
62 | 0 | if ( includes == null || includes.isEmpty() ) |
63 | { | |
64 | 0 | return false; |
65 | } | |
66 | ||
67 | 0 | String path = normalizePath( classFile ); |
68 | ||
69 | 0 | return isIncluded( path ); |
70 | } | |
71 | ||
72 | private boolean isIncluded( String classFile ) | |
73 | { | |
74 | 23 | if ( includes == null || includes.isEmpty() ) |
75 | { | |
76 | 16 | return true; |
77 | } | |
78 | ||
79 | 7 | return matchPaths( includes, classFile ); |
80 | } | |
81 | ||
82 | private boolean isExcluded( String classFile ) | |
83 | { | |
84 | 20 | if ( excludes == null || excludes.isEmpty() ) |
85 | { | |
86 | 8 | return false; |
87 | } | |
88 | ||
89 | 12 | return matchPaths( excludes, classFile ); |
90 | } | |
91 | ||
92 | private boolean matchPaths( Set<String> patterns, String classFile ) | |
93 | { | |
94 | 19 | for ( String pattern : patterns ) |
95 | { | |
96 | ||
97 | 19 | if ( SelectorUtils.matchPath( pattern, classFile ) ) |
98 | { | |
99 | 10 | return true; |
100 | } | |
101 | } | |
102 | ||
103 | 9 | return false; |
104 | } | |
105 | ||
106 | private String normalizePath( String path ) | |
107 | { | |
108 | 29 | return ( path != null ) ? path.replace( File.separatorChar == '/' ? '\\' : '/', File.separatorChar ) : null; |
109 | } | |
110 | ||
111 | private Set normalizePatterns( Set<String> patterns ) | |
112 | { | |
113 | 18 | Set<String> result = new HashSet<String>(); |
114 | ||
115 | 18 | if ( patterns != null ) |
116 | { | |
117 | 14 | for ( String pattern : patterns ) |
118 | { | |
119 | 6 | pattern = normalizePath( pattern ); |
120 | ||
121 | 6 | if ( pattern.endsWith( File.separator ) ) |
122 | { | |
123 | 1 | pattern += "**"; |
124 | } | |
125 | ||
126 | 6 | result.add( pattern ); |
127 | } | |
128 | } | |
129 | ||
130 | 18 | return result; |
131 | } | |
132 | ||
133 | public void finished() | |
134 | { | |
135 | 0 | } |
136 | } |