View Javadoc

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.Iterator;
26  import java.util.Set;
27  
28  /**
29   * @author David Blevins
30   */
31  public class SimpleFilter
32      implements Filter
33  {
34      private File jar;
35  
36      private Set includes;
37  
38      private Set excludes;
39  
40      public SimpleFilter( File jar, Set includes, Set excludes )
41      {
42          this.jar = jar;
43          this.includes = includes;
44          this.excludes = excludes;
45      }
46  
47      public boolean canFilter( File jar )
48      {
49          return this.jar.equals( jar );
50      }
51  
52      public boolean isFiltered( String classFile )
53      {
54  
55          return !( isIncluded( classFile ) && !isExcluded( classFile ) );
56      }
57  
58      private boolean isIncluded( String classFile )
59      {
60          if ( includes == null || includes.size() == 0 )
61          {
62              return true;
63          }
64  
65          return matchPaths( includes, classFile );
66      }
67  
68      private boolean isExcluded( String classFile )
69      {
70          if ( excludes == null || excludes.size() == 0 )
71          {
72              return false;
73          }
74  
75          return matchPaths( excludes, classFile );
76      }
77  
78      private boolean matchPaths( Set patterns, String classFile )
79      {
80          for ( Iterator iterator = patterns.iterator(); iterator.hasNext(); )
81          {
82              String pattern = (String) iterator.next();
83  
84              if ( SelectorUtils.matchPath( pattern, classFile ) )
85              {
86                  return true;
87              }
88          }
89  
90          return false;
91      }
92  }