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.Collections;
27  import java.util.Set;
28  
29  import org.apache.maven.plugins.shade.mojo.ArchiveFilter;
30  
31  /**
32   * @author David Blevins
33   */
34  /**
35   * @author kama
36   *
37   */
38  public class SimpleFilter
39      implements Filter
40  {
41  
42      private Set<File> jars;
43  
44      private Set<String> includes;
45  
46      private Set<String> excludes;
47  
48      private boolean excludeDefaults = true;
49  
50      /**
51       * @deprecated As of release 3.2.2, replaced by {@link #SimpleFilter(Set, ArchiveFilter)}}
52       * @param jars set of {@link File}s.
53       * @param includes set of includes.
54       * @param excludes set of excludes.
55       */
56      @Deprecated
57      public SimpleFilter( Set<File> jars, Set<String> includes, Set<String> excludes )
58      {
59          this( jars, includes, excludes, true );
60      }
61  
62      /**
63       * @param jars set of {@link File}s.
64       * @param archiveFilter set of {@link ArchiveFilter}s.
65       */
66      public SimpleFilter( final Set<File> jars, final ArchiveFilter archiveFilter )
67      {
68          this( jars, archiveFilter.getIncludes(), archiveFilter.getExcludes(), archiveFilter.getExcludeDefaults() );
69      }
70  
71      /**
72       * @param jars set of {@link File}s.
73       * @param includes set of includes.
74       * @param excludes set of excludes.
75       * @param excludeDefaults whether to exclude default includes once includes are provided explicitly.
76       */
77      private SimpleFilter( final Set<File> jars, final Set<String> includes, final Set<String> excludes,
78        final boolean excludeDefaults )
79      {
80          this.jars = ( jars != null ) ? Collections.<File>unmodifiableSet( jars ) : Collections.<File>emptySet();
81          this.includes = normalizePatterns( includes );
82          this.excludes = normalizePatterns( excludes );
83          this.excludeDefaults = excludeDefaults;
84      }
85  
86      /** {@inheritDoc} */
87      public boolean canFilter( File jar )
88      {
89          return jars.contains( jar );
90      }
91  
92      /** {@inheritDoc} */
93      public boolean isFiltered( String classFile )
94      {
95          String path = normalizePath( classFile );
96  
97          return !( ( !excludeDefaults || isIncluded( path ) ) && !isExcluded( path ) );
98      }
99  
100     /**
101      * @param classFile The class file.
102      * @return true if included false otherwise.
103      */
104     public boolean isSpecificallyIncluded( String classFile )
105     {
106         if ( includes == null || includes.isEmpty() )
107         {
108             return false;
109         }
110 
111         String path = normalizePath( classFile );
112 
113         return isIncluded( path );
114     }
115 
116     private boolean isIncluded( String classFile )
117     {
118         if ( includes == null || includes.isEmpty() )
119         {
120             return true;
121         }
122 
123         return matchPaths( includes, classFile );
124     }
125 
126     private boolean isExcluded( String classFile )
127     {
128         if ( excludes == null || excludes.isEmpty() )
129         {
130             return false;
131         }
132 
133         return matchPaths( excludes, classFile );
134     }
135 
136     private boolean matchPaths( Set<String> patterns, String classFile )
137     {
138         for ( String pattern : patterns )
139         {
140 
141             if ( SelectorUtils.matchPath( pattern, classFile ) )
142             {
143                 return true;
144             }
145         }
146 
147         return false;
148     }
149 
150     private String normalizePath( String path )
151     {
152         return ( path != null ) ? path.replace( File.separatorChar == '/' ? '\\' : '/', File.separatorChar ) : null;
153     }
154 
155     private Set<String> normalizePatterns( Set<String> patterns )
156     {
157         Set<String> result = new HashSet<>();
158 
159         if ( patterns != null )
160         {
161             for ( String pattern : patterns )
162             {
163                 pattern = normalizePath( pattern );
164 
165                 if ( pattern.endsWith( File.separator ) )
166                 {
167                     pattern += "**";
168                 }
169 
170                 result.add( pattern );
171             }
172         }
173 
174         return result;
175     }
176 
177     /** {@inheritDoc} */
178     public void finished()
179     {
180     }
181 }