View Javadoc
1   package org.codehaus.mojo.clirr;
2   
3   /*
4    * Copyright 2006 The Codehaus
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import net.sf.clirr.core.ClassFilter;
20  import org.apache.bcel.classfile.JavaClass;
21  import org.codehaus.plexus.util.SelectorUtils;
22  
23  /**
24   * Filter classes by pattern sets.
25   *
26   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
27   */
28  public class ClirrClassFilter
29      implements ClassFilter
30  {
31      private final String[] excludes;
32  
33      private final String[] includes;
34  
35      private boolean alwaysTrue;
36  
37      public ClirrClassFilter( String[] includes, String[] excludes )
38      {
39          if ( excludes == null || excludes.length == 0 )
40          {
41              this.excludes = null;
42          }
43          else
44          {
45              this.excludes = (String[]) excludes.clone();
46          }
47  
48          if ( includes == null || includes.length == 0 )
49          {
50              this.includes = new String[]{"**"};
51  
52              if ( excludes == null )
53              {
54                  alwaysTrue = true;
55              }
56          }
57          else
58          {
59              this.includes = (String[]) includes.clone();
60          }
61      }
62  
63      public boolean isSelected( JavaClass javaClass )
64      {
65          boolean result = false;
66          if ( alwaysTrue )
67          {
68              result = true;
69          }
70          else
71          {
72              String path = javaClass.getClassName().replace( '.', '/' );
73              for ( int i = 0; i < includes.length && !result; i++ )
74              {
75                  result = SelectorUtils.matchPath( includes[i], path );
76              }
77  
78              if ( excludes != null )
79              {
80                  for ( int i = 0; i < excludes.length && result; i++ )
81                  {
82                      result = !SelectorUtils.matchPath( excludes[i], path );
83                  }
84              }
85          }
86  
87          return result;
88      }
89  }