Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 231   Methods: 8
NCLOC: 116   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ContainsSelector.java 73.3% 67.9% 87.5% 71.4%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2002-2003 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.types.selectors;
 56   
 
 57   
 import java.io.BufferedReader;
 58   
 import java.io.File;
 59   
 import java.io.FileInputStream;
 60   
 import java.io.IOException;
 61   
 import java.io.InputStreamReader;
 62   
 import org.apache.tools.ant.BuildException;
 63   
 import org.apache.tools.ant.Project;
 64   
 import org.apache.tools.ant.types.Parameter;
 65   
 
 66   
 /**
 67   
  * Selector that filters files based on whether they contain a
 68   
  * particular string.
 69   
  *
 70   
  * @author <a href="mailto:bruce@callenish.com">Bruce Atherton</a>
 71   
  * @since 1.5
 72   
  */
 73   
 public class ContainsSelector extends BaseExtendSelector {
 74   
 
 75   
     private String contains = null;
 76   
     private boolean casesensitive = true;
 77   
     private boolean ignorewhitespace = false;
 78   
     public final static String CONTAINS_KEY = "text";
 79   
     public final static String CASE_KEY = "casesensitive";
 80   
     public final static String WHITESPACE_KEY = "ignorewhitespace";
 81   
 
 82   
 
 83  9
     public ContainsSelector() {
 84   
     }
 85   
 
 86  0
     public String toString() {
 87  0
         StringBuffer buf = new StringBuffer("{containsselector text: ");
 88  0
         buf.append(contains);
 89  0
         buf.append(" casesensitive: ");
 90  0
         if (casesensitive) {
 91  0
             buf.append("true");
 92   
         } else {
 93  0
             buf.append("false");
 94   
         }
 95  0
         buf.append(" ignorewhitespace: ");
 96  0
         if (ignorewhitespace) {
 97  0
             buf.append("true");
 98   
         } else {
 99  0
             buf.append("false");
 100   
         }
 101  0
         buf.append("}");
 102  0
         return buf.toString();
 103   
     }
 104   
 
 105   
     /**
 106   
      * The string to search for within a file.
 107   
      *
 108   
      * @param contains the string that a file must contain to be selected.
 109   
      */
 110  6
     public void setText(String contains) {
 111  6
         this.contains = contains;
 112   
     }
 113   
 
 114   
     /**
 115   
      * Whether to ignore case in the string being searched.
 116   
      *
 117   
      * @param casesensitive whether to pay attention to case sensitivity
 118   
      */
 119  2
     public void setCasesensitive(boolean casesensitive) {
 120  2
         this.casesensitive = casesensitive;
 121   
     }
 122   
 
 123   
     /**
 124   
      * Whether to ignore whitespace in the string being searched.
 125   
      *
 126   
      * @param whitespace whether to ignore any whitespace (spaces, tabs, etc.) in the searchstring
 127   
      */
 128  2
     public void setIgnorewhitespace(boolean ignorewhitespace) {
 129  2
         this.ignorewhitespace = ignorewhitespace;
 130   
     }
 131   
 
 132   
     /**
 133   
      * When using this as a custom selector, this method will be called.
 134   
      * It translates each parameter into the appropriate setXXX() call.
 135   
      *
 136   
      * @param parameters the complete set of parameters for this selector
 137   
      */
 138  1
     public void setParameters(Parameter[] parameters) {
 139  1
         super.setParameters(parameters);
 140  1
         if (parameters != null) {
 141  1
             for (int i = 0; i < parameters.length; i++) {
 142  1
                 String paramname = parameters[i].getName();
 143  1
                 if (CONTAINS_KEY.equalsIgnoreCase(paramname)) {
 144  0
                     setText(parameters[i].getValue());
 145   
                 }
 146  1
                 else if (CASE_KEY.equalsIgnoreCase(paramname)) {
 147  0
                     setCasesensitive(Project.toBoolean(
 148   
                         parameters[i].getValue()));
 149   
                 }
 150  1
                 else if (WHITESPACE_KEY.equalsIgnoreCase(paramname)) {
 151  0
                     setIgnorewhitespace(Project.toBoolean(
 152   
                         parameters[i].getValue()));
 153   
                 }
 154   
                 else {
 155  1
                     setError("Invalid parameter " + paramname);
 156   
                 }
 157   
             }
 158   
         }
 159   
     }
 160   
 
 161   
     /**
 162   
      * Checks to make sure all settings are kosher. In this case, it
 163   
      * means that the pattern attribute has been set.
 164   
      *
 165   
      */
 166  73
     public void verifySettings() {
 167  73
         if (contains == null) {
 168  1
             setError("The text attribute is required");
 169   
         }
 170   
     }
 171   
 
 172   
     /**
 173   
      * The heart of the matter. This is where the selector gets to decide
 174   
      * on the inclusion of a file in a particular fileset.
 175   
      *
 176   
      * @param basedir the base directory the scan is being done from
 177   
      * @param filename is the name of the file to check
 178   
      * @param file is a java.io.File object the selector can use
 179   
      * @return whether the file should be selected or not
 180   
      */
 181  75
     public boolean isSelected(File basedir, String filename, File file) {
 182   
 
 183   
         // throw BuildException on error
 184  75
         validate();
 185   
 
 186  72
         if (file.isDirectory()) {
 187  12
             return true;
 188   
         }
 189   
 
 190  60
         String userstr = contains;
 191  60
         if (!casesensitive) {
 192  10
             userstr = contains.toLowerCase();
 193   
         }
 194  60
         if (ignorewhitespace) {
 195  20
             userstr = SelectorUtils.removeWhitespace(userstr);
 196   
         }
 197  60
         BufferedReader in = null;
 198  60
         try {
 199  60
             in = new BufferedReader(new InputStreamReader(
 200   
                     new FileInputStream(file)));
 201  60
             String teststr = in.readLine();
 202  60
             while (teststr != null) {
 203  7518
                 if (!casesensitive) {
 204  1253
                     teststr = teststr.toLowerCase();
 205   
                 }
 206  7518
                 if (ignorewhitespace) {
 207  2506
                     teststr = SelectorUtils.removeWhitespace(teststr);
 208   
                 }
 209  7518
                 if (teststr.indexOf(userstr) > -1) {
 210  4
                     return true;
 211   
                 }
 212  7514
                 teststr = in.readLine();
 213   
             }
 214  56
             return false;
 215   
         }
 216   
         catch (IOException ioe) {
 217  0
             throw new BuildException("Could not read file " + filename);
 218   
         }
 219   
         finally {
 220  60
             try {
 221  60
                 in.close();
 222   
             }
 223   
             catch (Exception e) {
 224  0
                 throw new BuildException("Could not close file " + filename);
 225   
             }
 226   
         }
 227   
     }
 228   
 
 229   
 }
 230   
 
 231