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.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      {
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 }