Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 208   Methods: 7
NCLOC: 97   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DepthSelector.java 87.5% 82.2% 85.7% 84.2%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 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.types.selectors;
 56   
 
 57   
 import java.io.File;
 58   
 import java.util.StringTokenizer;
 59   
 import org.apache.tools.ant.BuildException;
 60   
 import org.apache.tools.ant.types.Parameter;
 61   
 
 62   
 /**
 63   
  * Selector that filters files based on the how deep in the directory
 64   
  * tree they are.
 65   
  *
 66   
  * @author <a href="mailto:bruce@callenish.com">Bruce Atherton</a>
 67   
  * @since 1.5
 68   
  */
 69   
 public class DepthSelector extends BaseExtendSelector {
 70   
 
 71   
     public int min = -1;
 72   
     public int max = -1;
 73   
     public final static String MIN_KEY = "min";
 74   
     public final static String MAX_KEY = "max";
 75   
 
 76  11
     public DepthSelector() {
 77   
     }
 78   
 
 79  0
     public String toString() {
 80  0
         StringBuffer buf = new StringBuffer("{depthselector min: ");
 81  0
         buf.append(min);
 82  0
         buf.append(" max: ");
 83  0
         buf.append(max);
 84  0
         buf.append("}");
 85  0
         return buf.toString();
 86   
     }
 87   
 
 88   
     /**
 89   
      * The minimum depth below the basedir before a file is selected.
 90   
      *
 91   
      * @param min minimum directory levels below basedir to go
 92   
      */
 93  5
     public void setMin(int min) {
 94  5
         this.min = min;
 95   
     }
 96   
 
 97   
     /**
 98   
      * The minimum depth below the basedir before a file is selected.
 99   
      *
 100   
      * @param min maximum directory levels below basedir to go
 101   
      */
 102  4
     public void setMax(int max) {
 103  4
         this.max = max;
 104   
     }
 105   
 
 106   
     /**
 107   
      * When using this as a custom selector, this method will be called.
 108   
      * It translates each parameter into the appropriate setXXX() call.
 109   
      *
 110   
      * @param parameters the complete set of parameters for this selector
 111   
      */
 112  3
     public void setParameters(Parameter[] parameters) {
 113  3
         super.setParameters(parameters);
 114  3
         if (parameters != null) {
 115  3
             for (int i = 0; i < parameters.length; i++) {
 116  3
                 String paramname = parameters[i].getName();
 117  3
                 if (MIN_KEY.equalsIgnoreCase(paramname)) {
 118  1
                     try {
 119  1
                         setMin(Integer.parseInt(parameters[i].getValue()));
 120   
                     }
 121   
                     catch (NumberFormatException nfe1) {
 122  1
                         setError("Invalid minimum value "
 123   
                             + parameters[i].getValue());
 124   
                     }
 125   
                 }
 126  2
                 else if (MAX_KEY.equalsIgnoreCase(paramname)) {
 127  1
                     try {
 128  1
                         setMax(Integer.parseInt(parameters[i].getValue()));
 129   
                     }
 130   
                     catch (NumberFormatException nfe1) {
 131  1
                         setError("Invalid maximum value "
 132   
                             + parameters[i].getValue());
 133   
                     }
 134   
                 }
 135   
                 else {
 136  1
                     setError("Invalid parameter " + paramname);
 137   
                 }
 138   
             }
 139   
         }
 140   
     }
 141   
 
 142   
     /**
 143   
      * Checks to make sure all settings are kosher. In this case, it
 144   
      * means that the max depth is not lower than the min depth.
 145   
      */
 146  62
     public void verifySettings() {
 147  62
         if (min < 0 && max < 0) {
 148  1
             setError("You must set at least one of the min or the " +
 149   
                     "max levels.");
 150   
         }
 151  62
         if (max < min && max > -1) {
 152  1
             setError("The maximum depth is lower than the minimum.");
 153   
         }
 154   
     }
 155   
 
 156   
     /**
 157   
      * The heart of the matter. This is where the selector gets to decide
 158   
      * on the inclusion of a file in a particular fileset. Most of the work
 159   
      * for this selector is offloaded into SelectorUtils, a static class
 160   
      * that provides the same services for both FilenameSelector and
 161   
      * DirectoryScanner.
 162   
      *
 163   
      * @param basedir the base directory the scan is being done from
 164   
      * @param filename is the name of the file to check
 165   
      * @param file is a java.io.File object the selector can use
 166   
      * @return whether the file should be selected or not
 167   
      */
 168  66
     public boolean isSelected(File basedir, String filename, File file) {
 169   
 
 170   
         // throw BuildException on error
 171  66
         validate();
 172   
 
 173  60
         int depth = -1;
 174   
         // If you felt daring, you could cache the basedir absolute path
 175  60
         String abs_base = basedir.getAbsolutePath();
 176  60
         String abs_file = file.getAbsolutePath();
 177  60
         StringTokenizer tok_base = new StringTokenizer(abs_base, File.separator);
 178  60
         StringTokenizer tok_file = new StringTokenizer(abs_file, File.separator);
 179  60
         while (tok_file.hasMoreTokens()) {
 180  707
             String filetoken = tok_file.nextToken();
 181  707
             if (tok_base.hasMoreTokens()) {
 182  600
                 String basetoken = tok_base.nextToken();
 183   
                 // Sanity check. Ditch it if you want faster performance
 184  600
                 if (!basetoken.equals(filetoken)) {
 185  0
                     throw new BuildException("File " + filename +
 186   
                         " does not appear within " + abs_base + "directory");
 187   
                 }
 188   
             }
 189   
             else {
 190  107
                 depth += 1;
 191  107
                 if (max > -1 && depth > max) {
 192  10
                     return false;
 193   
                 }
 194   
             }
 195   
         }
 196  50
         if (tok_base.hasMoreTokens()) {
 197  0
             throw new BuildException("File " + filename +
 198   
                 " is outside of " + abs_base + "directory tree");
 199   
         }
 200  50
         if (min > -1 && depth < min) {
 201  22
             return false;
 202   
         }
 203  28
         return true;
 204   
     }
 205   
 
 206   
 }
 207   
 
 208