Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 169   Methods: 10
NCLOC: 75   Classes: 4
 
 Source file Conditionals Statements Methods TOTAL
ReportFilters.java 70% 85.3% 80% 81.5%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2001-2002 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 
 55   
 package org.apache.tools.ant.taskdefs.optional.sitraka;
 56   
 
 57   
 import java.util.Vector;
 58   
 import org.apache.tools.ant.util.regexp.RegexpMatcher;
 59   
 import org.apache.tools.ant.util.regexp.RegexpMatcherFactory;
 60   
 
 61   
 /**
 62   
  * Filters information from coverage, somewhat similar to a <tt>FileSet</tt>.
 63   
  *
 64   
  * @author <a href="mailto:sbailliez@imediation.com">Stephane Bailliez</a>
 65   
  */
 66   
 public class ReportFilters {
 67   
 
 68   
     /** user defined filters */
 69   
     protected Vector filters = new Vector();
 70   
 
 71   
     /** cached matcher for each filter */
 72   
     protected Vector matchers = null;
 73   
 
 74  1
     public ReportFilters() {
 75   
     }
 76   
 
 77  1
     public void addInclude(Include incl) {
 78  1
         filters.addElement(incl);
 79   
     }
 80   
 
 81  0
     public void addExclude(Exclude excl) {
 82  0
         filters.addElement(excl);
 83   
     }
 84   
 
 85  0
     public int size() {
 86  0
         return filters.size();
 87   
     }
 88   
 
 89   
     /**
 90   
      * Check whether a given &lt;classname&gt;&lt;method&gt;() is accepted by the list
 91   
      * of filters or not.
 92   
      * @param methodname the full method name in the format &lt;classname&gt;&lt;method&gt;()
 93   
      */
 94  75286
     public boolean accept(String methodname) {
 95   
         // I'm deferring matcher instantiations at runtime to avoid computing
 96   
         // the filters at parsing time
 97  75286
         if (matchers == null) {
 98  1
             createMatchers();
 99   
         }
 100  75286
         boolean result = false;
 101   
         // assert filters.size() == matchers.size()
 102  75286
         final int size = filters.size();
 103  75286
         for (int i = 0; i < size; i++) {
 104  75286
             FilterElement filter = (FilterElement) filters.elementAt(i);
 105  75286
             RegexpMatcher matcher = (RegexpMatcher) matchers.elementAt(i);
 106  75286
             if (filter instanceof Include) {
 107  75286
                 result = result || matcher.matches(methodname);
 108  0
             } else if (filter instanceof Exclude) {
 109  0
                 result = result && !matcher.matches(methodname);
 110   
             } else {
 111   
                 //not possible
 112  0
                 throw new IllegalArgumentException("Invalid filter element: " + filter.getClass().getName());
 113   
             }
 114   
         }
 115  75286
         return result;
 116   
     }
 117   
 
 118   
     /** should be called only once to cache matchers */
 119  1
     protected void createMatchers() {
 120  1
         RegexpMatcherFactory factory = new RegexpMatcherFactory();
 121  1
         final int size = filters.size();
 122  1
         matchers = new Vector();
 123  1
         for (int i = 0; i < size; i++) {
 124  1
             FilterElement filter = (FilterElement) filters.elementAt(i);
 125  1
             RegexpMatcher matcher = factory.newRegexpMatcher();
 126  1
             String pattern = filter.getAsPattern();
 127  1
             matcher.setPattern(pattern);
 128  1
             matchers.addElement(matcher);
 129   
         }
 130   
     }
 131   
 
 132   
 
 133   
     /** default abstract filter element class */
 134   
     public abstract static class FilterElement {
 135   
         protected String clazz = "*"; // default is all classes
 136   
         protected String method = "*"; // default is all methods
 137   
 
 138  1
         public void setClass(String value) {
 139  1
             clazz = value;
 140   
         }
 141   
 
 142  1
         public void setMethod(String value) {
 143  1
             method = value;
 144   
         }
 145   
 
 146  1
         public String getAsPattern() {
 147  1
             StringBuffer buf = new StringBuffer(toString());
 148  1
             StringUtil.replace(buf, ".", "\\.");
 149  1
             StringUtil.replace(buf, "*", ".*");
 150  1
             StringUtil.replace(buf, "(", "\\(");
 151  1
             StringUtil.replace(buf, ")", "\\)");
 152  1
             return buf.toString();
 153   
         }
 154   
 
 155  1
         public String toString() {
 156  1
             return clazz + "." + method + "()";
 157   
         }
 158   
     }
 159   
 
 160   
     /** concrete include class */
 161   
     public static class Include extends FilterElement {
 162   
     }
 163   
 
 164   
     /** concrete exclude class */
 165   
     public static class Exclude extends FilterElement {
 166   
     }
 167   
 }
 168   
 
 169