Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 390   Methods: 19
NCLOC: 161   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
Sync.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 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   
 /*
 56   
  * This code is based on code Copyright (c) 2002, Landmark Graphics
 57   
  * Corp that has been kindly donated to the Apache Software
 58   
  * Foundation.
 59   
  */
 60   
 
 61   
 package org.apache.tools.ant.taskdefs;
 62   
 
 63   
 import java.io.File;
 64   
 
 65   
 import java.util.Hashtable;
 66   
 
 67   
 import org.apache.tools.ant.BuildException;
 68   
 import org.apache.tools.ant.Project;
 69   
 import org.apache.tools.ant.Task;
 70   
 import org.apache.tools.ant.types.FileSet;
 71   
 import org.apache.tools.ant.util.FileNameMapper;
 72   
 import org.apache.tools.ant.util.IdentityMapper;
 73   
 
 74   
 /**
 75   
  * Synchronize a local target directory from the files defined
 76   
  * in one or more filesets.
 77   
  *
 78   
  * <p>Uses a &lt;copy&gt; task internally, but forbidding the use of
 79   
  * mappers and filter chains. Files of the destination directory not
 80   
  * present in any of the source fileset are removed.</p>
 81   
  *
 82   
  * @author <a href="mailto:ddevienne@lgc.com">Dominique Devienne</a>
 83   
  * @version $Revision: 1.2 $
 84   
  * @since Ant 1.6
 85   
  *
 86   
  * revised by <a href="mailto:daniel.armbrust@mayo.edu">Dan Armbrust</a>
 87   
  * to remove orphaned directories.
 88   
  *
 89   
  * @ant.task category="filesystem"
 90   
  */
 91   
 public class Sync extends Task {
 92   
 
 93   
     // Same as regular <copy> task... see at end-of-file!
 94   
     private MyCopy _copy;
 95   
 
 96   
     // Override Task#init
 97  0
     public void init()
 98   
         throws BuildException {
 99   
         // Instantiate it
 100  0
         _copy = new MyCopy();
 101  0
         configureTask(_copy);
 102   
 
 103   
         // Default config of <mycopy> for our purposes.
 104  0
         _copy.setFiltering(false);
 105  0
         _copy.setIncludeEmptyDirs(false);
 106  0
         _copy.setPreserveLastModified(true);
 107   
     }
 108   
 
 109  0
     private void configureTask(Task helper) {
 110  0
         helper.setProject(getProject());
 111  0
         helper.setTaskName(getTaskName());
 112  0
         helper.setOwningTarget(getOwningTarget());
 113  0
         helper.init();
 114   
     }
 115   
 
 116   
     // Override Task#execute
 117  0
     public void execute()
 118   
         throws BuildException {
 119   
         // The destination of the files to copy
 120  0
         File toDir = _copy.getToDir();
 121   
 
 122   
         // The complete list of files to copy
 123  0
         Hashtable allFiles = _copy._dest2src;
 124   
 
 125   
         // If the destination directory didn't already exist,
 126   
         // or was empty, then no previous file removal is necessary!
 127  0
         boolean noRemovalNecessary = !toDir.exists() ||
 128   
             toDir.list().length < 1;
 129   
 
 130   
         // Copy all the necessary out-of-date files
 131  0
         log("PASS#1: Copying files to " + toDir, Project.MSG_DEBUG);
 132  0
         _copy.execute();
 133   
 
 134   
         // Do we need to perform further processing?
 135  0
         if (noRemovalNecessary) {
 136  0
             log("NO removing necessary in " + toDir, Project.MSG_DEBUG);
 137  0
             return; // nope ;-)
 138   
         }
 139   
 
 140   
         // Get rid of all files not listed in the source filesets.
 141  0
         log("PASS#2: Removing orphan files from " + toDir, Project.MSG_DEBUG);
 142  0
         int[] removedFileCount = removeOrphanFiles(allFiles, toDir);
 143  0
         logRemovedCount(removedFileCount[0], "dangling director", "y", "ies");
 144  0
         logRemovedCount(removedFileCount[1], "dangling file", "", "s");
 145   
 
 146   
         // Get rid of empty directories on the destination side
 147  0
         if (!_copy.getIncludeEmptyDirs()) {
 148  0
             log("PASS#3: Removing empty directories from " + toDir,
 149   
                 Project.MSG_DEBUG);
 150  0
             int removedDirCount = removeEmptyDirectories(toDir, false);
 151  0
             logRemovedCount(removedDirCount, "empty director", "y", "ies");
 152   
         }
 153   
     }
 154   
 
 155  0
     private void logRemovedCount(int count, String prefix,
 156   
                                  String singularSuffix, String pluralSuffix) {
 157  0
         File toDir = _copy.getToDir();
 158   
 
 159  0
         String what = (prefix == null) ? "" : prefix;
 160  0
         what += (count < 2) ? singularSuffix : pluralSuffix;
 161   
 
 162  0
         if (count > 0) {
 163  0
             log("Removed " + count + " " + what + " from " + toDir,
 164   
                 Project.MSG_INFO);
 165   
         } else {
 166  0
             log("NO " + what + " to remove from " + toDir, 
 167   
                 Project.MSG_VERBOSE);
 168   
         }
 169   
     }
 170   
 
 171   
     /**
 172   
      * Removes all files and folders not found as keyes of a table
 173   
      * (used as a set!).
 174   
      *
 175   
      * <p>If the provided file is a directory, it is recursively
 176   
      * scanned for orphaned files which will be removed as well.</p>
 177   
      *
 178   
      * <p>If the directory is an orphan, it will also be removed.</p>
 179   
      *
 180   
      * @param  nonOrphans the table of all non-orphan <code>File</code>s.
 181   
      * @param  file the initial file or directory to scan or test.
 182   
      * @return the number of orphaned files and directories actually removed.
 183   
      * Position 0 of the array is the number of orphaned directories.
 184   
      * Position 1 of the array is the number or orphaned files.
 185   
      * Position 2 is meaningless.
 186   
      */
 187  0
     private int[] removeOrphanFiles(Hashtable nonOrphans, File file) {
 188  0
         int[] removedCount = new int[] {0, 0, 0};
 189  0
         if (file.isDirectory()) {
 190  0
             File[] children = file.listFiles();
 191  0
             for (int i = 0; i < children.length; ++i) {
 192  0
                 int[] temp = removeOrphanFiles(nonOrphans, children[i]);
 193  0
                 removedCount[0] += temp[0];
 194  0
                 removedCount[1] += temp[1];
 195  0
                 removedCount[2] += temp[2];
 196   
             }
 197   
 
 198  0
             if (nonOrphans.get(file) == null && removedCount[2] == 0) {
 199  0
                 log("Removing orphan directory: " + file, Project.MSG_DEBUG);
 200  0
                 file.delete();
 201  0
                 ++removedCount[0];
 202   
             } else {
 203   
                 /*
 204   
                   Contrary to what is said above, position 2 is not
 205   
                   meaningless inside the recursion.
 206   
                   Position 2 is used to carry information back up the
 207   
                   recursion about whether or not a directory contains
 208   
                   a directory or file at any depth that is not an
 209   
                   orphan
 210   
                   This has to be done, because if you have the
 211   
                   following directory structure: c:\src\a\file and
 212   
                   your mapper src files were constructed like so:
 213   
                   <include name="**\a\**\*"/>
 214   
                   The folder 'a' will not be in the hashtable of
 215   
                   nonorphans.  So, before deleting it as an orphan, we
 216   
                   have to know whether or not any of its children at
 217   
                   any level are orphans.
 218   
                   If no, then this folder is also an orphan, and may
 219   
                   be deleted.  I do this by changing position 2 to a
 220   
                   '1'.
 221   
                 */
 222  0
                 removedCount[2] = 1;
 223   
             }
 224   
 
 225   
         } else {
 226  0
             if (nonOrphans.get(file) == null) {
 227  0
                 log("Removing orphan file: " + file, Project.MSG_DEBUG);
 228  0
                 file.delete();
 229  0
                 ++removedCount[1];
 230   
             } else {
 231  0
                 removedCount[2] = 1;
 232   
             }
 233   
         }
 234  0
         return removedCount;
 235   
     }
 236   
 
 237   
     /**
 238   
      * Removes all empty directories from a directory.
 239   
      *
 240   
      * <p><em>Note that a directory that contains only empty
 241   
      * directories, directly or not, will be removed!</em></p>
 242   
      *
 243   
      * <p>Recurses depth-first to find the leaf directories
 244   
      * which are empty and removes them, then unwinds the
 245   
      * recursion stack, removing directories which have
 246   
      * become empty themselves, etc...</p>
 247   
      *
 248   
      * @param  dir the root directory to scan for empty directories.
 249   
      * @param  removeIfEmpty whether to remove the root directory
 250   
      *         itself if it becomes empty.
 251   
      * @return the number of empty directories actually removed.
 252   
      */
 253  0
     private int removeEmptyDirectories(File dir, boolean removeIfEmpty) {
 254  0
         int removedCount = 0;
 255  0
         if (dir.isDirectory()) {
 256  0
             File[] children = dir.listFiles();
 257  0
             for (int i = 0; i < children.length; ++i) {
 258  0
                 File file = children[i];
 259   
                 // Test here again to avoid method call for non-directories!
 260  0
                 if (file.isDirectory()) {
 261  0
                     removedCount += removeEmptyDirectories(file, true);
 262   
                 }
 263   
             }
 264  0
             if (children.length > 0) {
 265   
                 // This directory may have become empty...
 266   
                 // We need to re-query its children list!
 267  0
                 children = dir.listFiles();
 268   
             }
 269  0
             if (children.length < 1 && removeIfEmpty) {
 270  0
                 log("Removing empty directory: " + dir, Project.MSG_DEBUG);
 271  0
                 dir.delete();
 272  0
                 ++removedCount;
 273   
             }
 274   
         }
 275  0
         return removedCount;
 276   
     }
 277   
 
 278   
 
 279   
     //
 280   
     // Various copy attributes/subelements of <copy> passed thru to <mycopy>
 281   
     //
 282   
 
 283   
     /**
 284   
      * Sets the destination directory.
 285   
      */
 286  0
     public void setTodir(File destDir) {
 287  0
         _copy.setTodir(destDir);
 288   
     }
 289   
 
 290   
     /**
 291   
      * Used to force listing of all names of copied files.
 292   
      */
 293  0
     public void setVerbose(boolean verbose) {
 294  0
         _copy.setVerbose(verbose);
 295   
     }
 296   
 
 297   
     /**
 298   
      * Overwrite any existing destination file(s).
 299   
      */
 300  0
     public void setOverwrite(boolean overwrite) {
 301  0
         _copy.setOverwrite(overwrite);
 302   
     }
 303   
 
 304   
     /**
 305   
      * Used to copy empty directories.
 306   
      */
 307  0
     public void setIncludeEmptyDirs(boolean includeEmpty) {
 308  0
         _copy.setIncludeEmptyDirs(includeEmpty);
 309   
     }
 310   
 
 311   
     /**
 312   
      * If false, note errors to the output but keep going.
 313   
      * @param failonerror true or false
 314   
      */
 315  0
     public void setFailOnError(boolean failonerror) {
 316  0
         _copy.setFailOnError(failonerror);
 317   
     }
 318   
 
 319   
     /**
 320   
      * Adds a set of files to copy.
 321   
      */
 322  0
     public void addFileset(FileSet set) {
 323  0
         _copy.addFileset(set);
 324   
     }
 325   
 
 326   
     /**
 327   
      * Sets the character encoding
 328   
      */
 329  0
     public void setEncoding(String encoding) {
 330  0
         _copy.setEncoding(encoding);
 331   
     }
 332   
 
 333   
     /**
 334   
      * Sets the character encoding for output files.
 335   
      */
 336  0
     public void setOutputEncoding(String encoding) {
 337  0
         _copy.setOutputEncoding(encoding);
 338   
     }
 339   
 
 340   
 
 341   
     /**
 342   
      * Subclass Copy in order to access it's file/dir maps.
 343   
      */
 344   
     public static class MyCopy
 345   
         extends Copy {
 346   
 
 347   
         // List of files that must be copied, irrelevant from the
 348   
         // fact that they are newer or not than the destination.
 349   
         private Hashtable _dest2src = new Hashtable();
 350   
 
 351  0
         public MyCopy() {}
 352   
 
 353  0
         protected void buildMap(File fromDir, File toDir, String[] names,
 354   
                                 FileNameMapper mapper, Hashtable map) {
 355  0
             assertTrue("No mapper", mapper instanceof IdentityMapper);
 356   
 
 357  0
             super.buildMap(fromDir, toDir, names, mapper, map);
 358   
 
 359  0
             for (int i = 0; i < names.length; ++i) {
 360  0
                 String name = names[i];
 361  0
                 File dest = new File(toDir, name);
 362   
                 // No need to instantiate the src file, as we use the
 363   
                 // table as a set (to remain Java 1.1 compatible!!!).
 364   
                 //File src = new File(fromDir, name);
 365   
                 //_dest2src.put(dest, src);
 366  0
                 _dest2src.put(dest, fromDir);
 367   
             }
 368   
         }
 369   
 
 370  0
         public File getToDir() {
 371  0
             return destDir;
 372   
         }
 373   
 
 374  0
         public boolean getIncludeEmptyDirs() {
 375  0
             return includeEmpty;
 376   
         }
 377   
 
 378   
     }
 379   
 
 380   
     /**
 381   
      * Pseudo-assert method.
 382   
      */
 383  0
     private static void assertTrue(String message, boolean condition) {
 384  0
         if (!condition) {
 385  0
             throw new BuildException("Assertion Error: " + message);
 386   
         }
 387   
     }
 388   
 
 389   
 }
 390