Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 209   Methods: 8
NCLOC: 86   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
PresentSelector.java 45.5% 50% 62.5% 50%
 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 org.apache.tools.ant.BuildException;
 59   
 import org.apache.tools.ant.types.EnumeratedAttribute;
 60   
 import org.apache.tools.ant.types.Mapper;
 61   
 import org.apache.tools.ant.util.FileNameMapper;
 62   
 import org.apache.tools.ant.util.IdentityMapper;
 63   
 
 64   
 /**
 65   
  * Selector that filters files based on whether they appear in another
 66   
  * directory tree. It can contain a mapper element, so isn't available
 67   
  * as an ExtendSelector (since those parameters can't hold other
 68   
  * elements).
 69   
  *
 70   
  * @author <a href="mailto:bruce@callenish.com">Bruce Atherton</a>
 71   
  * @since 1.5
 72   
  */
 73   
 public class PresentSelector extends BaseSelector {
 74   
 
 75   
     private File targetdir = null;
 76   
     private Mapper mapperElement = null;
 77   
     private FileNameMapper map = null;
 78   
     private boolean destmustexist = true;
 79   
 
 80  9
     public PresentSelector() {
 81   
     }
 82   
 
 83  0
     public String toString() {
 84  0
         StringBuffer buf = new StringBuffer("{presentselector targetdir: ");
 85  0
         if (targetdir == null) {
 86  0
             buf.append("NOT YET SET");
 87   
         }
 88   
         else {
 89  0
             buf.append(targetdir.getName());
 90   
         }
 91  0
         buf.append(" present: ");
 92  0
         if (destmustexist) {
 93  0
             buf.append("both");
 94   
         } else {
 95  0
             buf.append("srconly");
 96   
         }
 97  0
         if (map != null) {
 98  0
             buf.append(map.toString());
 99   
         }
 100  0
         else if (mapperElement != null) {
 101  0
             buf.append(mapperElement.toString());
 102   
         }
 103  0
         buf.append("}");
 104  0
         return buf.toString();
 105   
     }
 106   
 
 107   
     /**
 108   
      * The name of the file or directory which is checked for matching
 109   
      * files.
 110   
      *
 111   
      * @param targetdir the directory to scan looking for matching files.
 112   
      */
 113  6
     public void setTargetdir(File targetdir) {
 114  6
         this.targetdir = targetdir;
 115   
     }
 116   
 
 117   
     /**
 118   
      * Defines the FileNameMapper to use (nested mapper element).
 119   
      */
 120  6
     public Mapper createMapper() throws BuildException {
 121  6
         if (mapperElement != null) {
 122  1
             throw new BuildException("Cannot define more than one mapper");
 123   
         }
 124  5
         mapperElement = new Mapper(getProject());
 125  5
         return mapperElement;
 126   
     }
 127   
 
 128   
 
 129   
     /**
 130   
      * This sets whether to select a file if its dest file is present.
 131   
      * It could be a <code>negate</code> boolean, but by doing things
 132   
      * this way, we get some documentation on how the system works.
 133   
      * A user looking at the documentation should clearly understand
 134   
      * that the ONLY files whose presence is being tested are those
 135   
      * that already exist in the source directory, hence the lack of
 136   
      * a <code>destonly</code> option.
 137   
      *
 138   
      * @param fp An attribute set to either <code>srconly</code or
 139   
      *           <code>both</code>.
 140   
      */
 141  0
     public void setPresent(FilePresence fp) {
 142  0
         if (fp.getIndex() == 0) {
 143  0
             destmustexist = false;
 144   
         }
 145   
     }
 146   
 
 147   
     /**
 148   
      * Checks to make sure all settings are kosher. In this case, it
 149   
      * means that the targetdir attribute has been set and we have a mapper.
 150   
      */
 151  85
     public void verifySettings() {
 152  85
         if (targetdir == null) {
 153  1
             setError("The targetdir attribute is required.");
 154   
         }
 155  85
         if (mapperElement == null) {
 156  37
             map = new IdentityMapper();
 157   
         }
 158   
         else {
 159  48
             map = mapperElement.getImplementation();
 160   
         }
 161  85
         if (map == null) {
 162  0
             setError("Could not set <mapper> element.");
 163   
         }
 164   
     }
 165   
 
 166   
     /**
 167   
      * The heart of the matter. This is where the selector gets to decide
 168   
      * on the inclusion of a file in a particular fileset.
 169   
      *
 170   
      * @param basedir the base directory the scan is being done from
 171   
      * @param filename is the name of the file to check
 172   
      * @param file is a java.io.File object the selector can use
 173   
      * @return whether the file should be selected or not
 174   
      */
 175  86
     public boolean isSelected(File basedir, String filename, File file) {
 176   
 
 177   
         // throw BuildException on error
 178  86
         validate();
 179   
 
 180   
         // Determine file whose existence is to be checked
 181  84
         String[] destfiles = map.mapFileName(filename);
 182   
         // If filename does not match the To attribute of the mapper
 183   
         // then filter it out of the files we are considering
 184  84
         if (destfiles == null) {
 185  9
             return false;
 186   
         }
 187   
         // Sanity check
 188  75
         if (destfiles.length != 1 || destfiles[0] == null) {
 189  0
             throw new BuildException("Invalid destination file results for "
 190   
                 + targetdir + " with filename " + filename);
 191   
         }
 192  75
         String destname = destfiles[0];
 193  75
         File destfile = new File(targetdir,destname);
 194  75
         return destfile.exists() == destmustexist;
 195   
     }
 196   
 
 197   
     /**
 198   
      * Enumerated attribute with the values for indicating where a file's
 199   
      * presence is allowed and required.
 200   
      */
 201   
     public static class FilePresence extends EnumeratedAttribute {
 202  0
         public String[] getValues() {
 203  0
             return new String[] {"srconly", "both"};
 204   
         }
 205   
     }
 206   
 
 207   
 }
 208   
 
 209