View Javadoc

1   //////////////////////////////////////////////////////////////////////////////
2   // Clirr: compares two versions of a java library for binary compatibility
3   // Copyright (C) 2003 - 2005  Lars Kühne
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  //////////////////////////////////////////////////////////////////////////////
19  
20  package net.sf.clirr.ant;
21  
22  import net.sf.clirr.core.ClassFilter;
23  
24  import java.util.List;
25  import java.io.File;
26  
27  import org.apache.bcel.classfile.JavaClass;
28  import org.apache.tools.ant.types.selectors.SelectorUtils;
29  import org.apache.tools.ant.types.PatternSet;
30  import org.apache.tools.ant.Project;
31  
32  /***
33   * A ClassFilter that uses Ant PatternSets as the decision criteria.
34   *
35   * @author lkuehne
36   */
37  class PatternSetFilter implements ClassFilter
38  {
39      private final Project project;
40      private final List patternSets;
41  
42  
43      /***
44       * Creates a new PatternSetFilter.
45       * @param project the current Ant project
46       * @param patternSets a List of Ant PatternSet objects
47       */
48      public PatternSetFilter(Project project, List patternSets)
49      {
50          this.project = project;
51          this.patternSets = patternSets;
52      }
53  
54  
55      public boolean isSelected(JavaClass clazz)
56      {
57          // The patternset evaluation code below was copied from Apache Ant's Expand task.
58          // I feel this code should be available as a library function inside Ant somewhere...
59          String className = clazz.getClassName();
60          String name = className.replace('.', File.separatorChar);
61  
62  
63          if (patternSets == null || patternSets.isEmpty())
64          {
65              return true;
66          }
67  
68          boolean included = false;
69          for (int i = 0; i < patternSets.size(); i++)
70          {
71              PatternSet p = (PatternSet) patternSets.get(i);
72              p.getIncludePatterns(project);
73  
74              String[] incls = p.getIncludePatterns(project);
75              if (incls == null || incls.length == 0)
76              {
77                  // no include pattern implicitly means includes="**"
78                  incls = new String[] {"**"};
79              }
80  
81              for (int w = 0; w < incls.length; w++)
82              {
83                  String pattern = incls[w].replace('/', File.separatorChar)
84                          .replace('//', File.separatorChar);
85                  if (pattern.endsWith(File.separator))
86                  {
87                      pattern += "**";
88                  }
89  
90                  included = SelectorUtils.matchPath(pattern, name);
91                  if (included)
92                  {
93                      break;
94                  }
95              }
96  
97              if (!included)
98              {
99                  break;
100             }
101 
102 
103             String[] excls = p.getExcludePatterns(project);
104             if (excls != null)
105             {
106                 for (int w = 0; w < excls.length; w++)
107                 {
108                     String pattern = excls[w]
109                             .replace('/', File.separatorChar)
110                             .replace('//', File.separatorChar);
111                     if (pattern.endsWith(File.separator))
112                     {
113                         pattern += "**";
114                     }
115                     included = !(SelectorUtils.matchPath(pattern, name));
116                     if (!included)
117                     {
118                         break;
119                     }
120                 }
121             }
122         }
123         project.log("included " + className + " = " + included, Project.MSG_VERBOSE);
124         return included;
125     }
126 }