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