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  /**
32   * @author kama
33   *
34   */
35  public class SimpleFilter
36      implements Filter
37  {
38  
39      private Set<File> jars;
40  
41      private Set<String> includes;
42  
43      private Set<String> excludes;
44  
45      /**
46       * @param jars set of {@link File}s.
47       * @param includes set of includes.
48       * @param excludes set of excludes
49       */
50      public SimpleFilter( Set<File> jars, Set<String> includes, Set<String> excludes )
51      {
52          this.jars = ( jars != null ) ? new HashSet<File>( jars ) : new HashSet<File>();
53          this.includes = normalizePatterns( includes );
54          this.excludes = normalizePatterns( excludes );
55      }
56  
57      /** {@inheritDoc} */
58      public boolean canFilter( File jar )
59      {
60          return jars.contains( jar );
61      }
62  
63      /** {@inheritDoc} */
64      public boolean isFiltered( String classFile )
65      {
66          String path = normalizePath( classFile );
67  
68          return !( isIncluded( path ) && !isExcluded( path ) );
69      }
70  
71      /**
72       * @param classFile The class file.
73       * @return true if included false otherwise.
74       */
75      public boolean isSpecificallyIncluded( String classFile )
76      {
77          if ( includes == null || includes.isEmpty() )
78          {
79              return false;
80          }
81  
82          String path = normalizePath( classFile );
83  
84          return isIncluded( path );
85      }
86  
87      private boolean isIncluded( String classFile )
88      {
89          if ( includes == null || includes.isEmpty() )
90          {
91              return true;
92          }
93  
94          return matchPaths( includes, classFile );
95      }
96  
97      private boolean isExcluded( String classFile )
98      {
99          if ( excludes == null || excludes.isEmpty() )
100         {
101             return false;
102         }
103 
104         return matchPaths( excludes, classFile );
105     }
106 
107     private boolean matchPaths( Set<String> patterns, String classFile )
108     {
109         for ( String pattern : patterns )
110         {
111 
112             if ( SelectorUtils.matchPath( pattern, classFile ) )
113             {
114                 return true;
115             }
116         }
117 
118         return false;
119     }
120 
121     private String normalizePath( String path )
122     {
123         return ( path != null ) ? path.replace( File.separatorChar == '/' ? '\\' : '/', File.separatorChar ) : null;
124     }
125 
126     private Set<String> normalizePatterns( Set<String> patterns )
127     {
128         Set<String> result = new HashSet<String>();
129 
130         if ( patterns != null )
131         {
132             for ( String pattern : patterns )
133             {
134                 pattern = normalizePath( pattern );
135 
136                 if ( pattern.endsWith( File.separator ) )
137                 {
138                     pattern += "**";
139                 }
140 
141                 result.add( pattern );
142             }
143         }
144 
145         return result;
146     }
147 
148     /** {@inheritDoc} */
149     public void finished()
150     {
151     }
152 }