Coverage Report - net.sourceforge.cobertura.instrument.ClassPattern
 
Classes in this File Line Coverage Branch Coverage Complexity
ClassPattern
100%
24/24
79%
11/14
2
 
 1  
 /*
 2  
  * Cobertura - http://cobertura.sourceforge.net/
 3  
  *
 4  
  * Copyright (C) 2006 John Lewis
 5  
  *
 6  
  * Note: This file is dual licensed under the GPL and the Apache
 7  
  * Source License (so that it can be used from both the main
 8  
  * Cobertura classes and the ant tasks).
 9  
  *
 10  
  * Cobertura is free software; you can redistribute it and/or modify
 11  
  * it under the terms of the GNU General Public License as published
 12  
  * by the Free Software Foundation; either version 2 of the License,
 13  
  * or (at your option) any later version.
 14  
  *
 15  
  * Cobertura is distributed in the hope that it will be useful, but
 16  
  * WITHOUT ANY WARRANTY; without even the implied warranty of
 17  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 18  
  * General Public License for more details.
 19  
  *
 20  
  * You should have received a copy of the GNU General Public License
 21  
  * along with Cobertura; if not, write to the Free Software
 22  
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 23  
  * USA
 24  
  */
 25  
 
 26  
 package net.sourceforge.cobertura.instrument;
 27  
 
 28  
 import java.util.Collection;
 29  
 import java.util.HashSet;
 30  
 
 31  
 import net.sourceforge.cobertura.util.RegexUtil;
 32  
 
 33  
 /**
 34  
  * This class represents a collection of regular expressions that will be used to see
 35  
  * if a classname matches them.
 36  
  * 
 37  
  * Regular expressions are specified by calling add methods.  If no add methods are
 38  
  * called, this class will match any classname.
 39  
  * 
 40  
  * @author John Lewis
 41  
  *
 42  
  */
 43  4
 public class ClassPattern
 44  
 {
 45  
 
 46  4
         private Collection includeClassesRegexes = new HashSet();
 47  
 
 48  4
         private Collection excludeClassesRegexes = new HashSet();
 49  
 
 50  
         private static final String WEBINF_CLASSES = "WEB-INF/classes/";
 51  
 
 52  
         /**
 53  
          * Returns true if any regular expressions have been specified by calling the
 54  
          * add methods.  If none are specified, this class matches anything.
 55  
          * 
 56  
          * @return true if any regular expressions have been specified
 57  
          */
 58  
         boolean isSpecified()
 59  
         {
 60  40
                 return includeClassesRegexes.size() > 0;
 61  
         }
 62  
 
 63  
         /**
 64  
          * Check to see if a class matches this ClassPattern
 65  
          * 
 66  
          * If a pattern has not been specified, this matches anything.
 67  
          * 
 68  
          * This method also looks for "WEB-INF/classes" at the beginning of the
 69  
          * classname.  It is removed before checking for a match.
 70  
          * 
 71  
          * @param filename Either a full classname or a full class filename
 72  
          * @return true if the classname matches this ClassPattern or if this ClassPattern
 73  
          * has not been specified.
 74  
          */
 75  
         boolean matches(String filename)
 76  
         {
 77  20
                 boolean matches = true;
 78  
 
 79  20
                 if (isSpecified())
 80  
                 {
 81  15
                         matches = false;
 82  
                         // Remove .class extension if it exists
 83  15
                         if (filename.endsWith(".class"))
 84  
                         {
 85  15
                                 filename = filename.substring(0, filename.length() - 6);
 86  
                         }
 87  15
                         filename = filename.replace('\\', '/');
 88  
 
 89  15
                         filename = removeAnyWebInfClassesString(filename);
 90  
 
 91  15
                         filename = filename.replace('/', '.');
 92  15
                         if (RegexUtil.matches(includeClassesRegexes, filename))
 93  
                         {
 94  15
                                 matches = true;
 95  
                         }
 96  15
                         if (matches && RegexUtil.matches(excludeClassesRegexes, filename))
 97  
                         {
 98  6
                                 matches = false;
 99  
                         }
 100  
                 }
 101  20
                 return matches;
 102  
         }
 103  
 
 104  
         private String removeAnyWebInfClassesString(String filename)
 105  
         {
 106  15
                 if (filename.startsWith(WEBINF_CLASSES))
 107  
                 {
 108  3
                         filename = filename.substring(WEBINF_CLASSES.length());
 109  
                 }
 110  15
                 return filename;
 111  
         }
 112  
 
 113  
         /**
 114  
          * Add a regex to the list of class regexes to include.
 115  
          * 
 116  
          * @param regex A regular expression to add.
 117  
          */
 118  
         void addIncludeClassesRegex(String regex)
 119  
         {
 120  2
                 RegexUtil.addRegex(includeClassesRegexes, regex);
 121  2
         }
 122  
 
 123  
         /**
 124  
          * Add a regex to the list of class regexes to exclude.
 125  
          * 
 126  
          * @param regex
 127  
          */
 128  
         void addExcludeClassesRegex(String regex)
 129  
         {
 130  4
                 RegexUtil.addRegex(excludeClassesRegexes, regex);
 131  4
         }
 132  
 
 133  
 }