Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 206   Methods: 9
NCLOC: 88   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Patch.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2000,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;
 56   
 
 57   
 import java.io.File;
 58   
 import java.io.IOException;
 59   
 import org.apache.tools.ant.BuildException;
 60   
 import org.apache.tools.ant.Project;
 61   
 import org.apache.tools.ant.Task;
 62   
 import org.apache.tools.ant.types.Commandline;
 63   
 
 64   
 /**
 65   
  * Patches a file by applying a 'diff' file to it; requires "patch" to be
 66   
  * on the execution path.
 67   
  * @author Stefan Bodewig
 68   
  *
 69   
  * @since Ant 1.1
 70   
  *
 71   
  * @ant.task category="utility"
 72   
  */
 73   
 public class Patch extends Task {
 74   
 
 75   
     private File originalFile;
 76   
     private File directory;
 77   
     private boolean havePatchfile = false;
 78   
     private Commandline cmd = new Commandline();
 79   
 
 80   
     /**
 81   
      * The file to patch; optional if it can be inferred from
 82   
      * the diff file
 83   
      */
 84  0
     public void setOriginalfile(File file) {
 85  0
         originalFile = file;
 86   
     }
 87   
 
 88   
     /**
 89   
      * The file containing the diff output; required.
 90   
      */
 91  0
     public void setPatchfile(File file) {
 92  0
         if (!file.exists()) {
 93  0
             throw new BuildException("patchfile " + file + " doesn\'t exist", 
 94   
                                      getLocation());
 95   
         }
 96  0
         cmd.createArgument().setValue("-i");
 97  0
         cmd.createArgument().setFile(file);
 98  0
         havePatchfile = true;
 99   
     }
 100   
 
 101   
     /**
 102   
      * flag to create backups; optional, default=false
 103   
      */
 104  0
     public void setBackups(boolean backups) {
 105  0
         if (backups) {
 106  0
             cmd.createArgument().setValue("-b");
 107   
         }
 108   
     }
 109   
 
 110   
     /**
 111   
      * flag to ignore whitespace differences; default=false
 112   
      */
 113  0
     public void setIgnorewhitespace(boolean ignore) {
 114  0
         if (ignore) {
 115  0
             cmd.createArgument().setValue("-l");
 116   
         }
 117   
     }
 118   
 
 119   
     /**
 120   
      * Strip the smallest prefix containing <i>num</i> leading slashes
 121   
      * from filenames.
 122   
      *
 123   
      * <p>patch's <i>-p</i> option.
 124   
      * @param num number of lines to strip
 125   
      */
 126  0
     public void setStrip(int num) throws BuildException {
 127  0
         if (num < 0) {
 128  0
             throw new BuildException("strip has to be >= 0", getLocation());
 129   
         }
 130  0
         cmd.createArgument().setValue("-p" + num);
 131   
     }
 132   
 
 133   
     /**
 134   
      * Work silently unless an error occurs; optional, default=false
 135   
      */
 136  0
     public void setQuiet(boolean q) {
 137  0
         if (q) {
 138  0
             cmd.createArgument().setValue("-s");
 139   
         }
 140   
     }
 141   
 
 142   
     /**
 143   
      * Assume patch was created with old and new files swapped; optional,
 144   
      * default=false
 145   
      */
 146  0
     public void setReverse(boolean r) {
 147  0
         if (r) {
 148  0
             cmd.createArgument().setValue("-R");
 149   
         }
 150   
     }
 151   
 
 152   
     /**
 153   
      * The directory to run the patch command in, defaults to the
 154   
      * project's base directory.
 155   
      *
 156   
      * @since Ant 1.5
 157   
      */
 158  0
     public void setDir(File directory) throws BuildException {
 159  0
         this.directory = directory;
 160   
     }
 161   
 
 162   
     /**
 163   
      * execute patch
 164   
      * @throws BuildException when it all goes a bit pear shaped
 165   
      */
 166  0
     public void execute() throws BuildException {
 167  0
         if (!havePatchfile) {
 168  0
             throw new BuildException("patchfile argument is required", 
 169   
                                      getLocation());
 170   
         } 
 171  0
         Commandline toExecute = (Commandline) cmd.clone();
 172  0
         toExecute.setExecutable("patch");
 173   
 
 174  0
         if (originalFile != null) {
 175  0
             toExecute.createArgument().setFile(originalFile);
 176   
         }
 177   
 
 178  0
         Execute exe = new Execute(new LogStreamHandler(this, Project.MSG_INFO,
 179   
                                                        Project.MSG_WARN), 
 180   
                                   null);
 181  0
         exe.setCommandline(toExecute.getCommandline());
 182   
 
 183  0
         if (directory != null) {
 184  0
             if (directory.exists() && directory.isDirectory()) {
 185  0
                 exe.setWorkingDirectory(directory);
 186  0
             } else if (!directory.isDirectory()) {
 187  0
                 throw new BuildException(directory + " is not a directory.",
 188   
                                          getLocation());
 189   
             } else {
 190  0
                 throw new BuildException("directory " + directory
 191   
                                          + " doesn\'t exist", getLocation());
 192   
             }
 193   
         } else {
 194  0
             exe.setWorkingDirectory(getProject().getBaseDir());
 195   
         }
 196   
 
 197  0
         log(toExecute.describeCommand(), Project.MSG_VERBOSE);
 198  0
         try {
 199  0
             exe.execute();
 200   
         } catch (IOException e) {
 201  0
             throw new BuildException(e, getLocation());
 202   
         }
 203   
     }
 204   
 
 205   
 }// Patch
 206