Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 176   Methods: 6
NCLOC: 66   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Pack.java 78.6% 88.5% 100% 87%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2001-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   
 
 58   
 import java.io.File;
 59   
 import java.io.FileInputStream;
 60   
 import java.io.IOException;
 61   
 import java.io.InputStream;
 62   
 import java.io.OutputStream;
 63   
 import org.apache.tools.ant.BuildException;
 64   
 import org.apache.tools.ant.Task;
 65   
 
 66   
 /**
 67   
  * Abstract Base class for pack tasks.
 68   
  *
 69   
  * @author Magesh Umasankar
 70   
  *
 71   
  * @since Ant 1.5
 72   
  */
 73   
 
 74   
 public abstract class Pack extends Task {
 75   
 
 76   
     protected File zipFile;
 77   
     protected File source;
 78   
 
 79   
     /**
 80   
      * the required destination file.
 81   
      * @param zipFile
 82   
      */
 83  7
     public void setZipfile(File zipFile) {
 84  7
         this.zipFile = zipFile;
 85   
     }
 86   
 
 87   
     /**
 88   
      * the file to compress; required.
 89   
      * @param src
 90   
      */
 91  7
     public void setSrc(File src) {
 92  7
         source = src;
 93   
     }
 94   
 
 95   
 
 96   
     /**
 97   
      * validation routine
 98   
      * @throws BuildException if anything is invalid
 99   
      */
 100  9
     private void validate() throws BuildException {
 101  9
         if (zipFile == null) {
 102  2
             throw new BuildException("zipfile attribute is required", getLocation());
 103   
         }
 104   
 
 105  7
         if (zipFile.isDirectory()) {
 106  2
             throw new BuildException("zipfile attribute must not " +
 107   
                                      "represent a directory!", getLocation());
 108   
         }
 109   
 
 110  5
         if (source == null) {
 111  0
             throw new BuildException("src attribute is required", getLocation());
 112   
         }
 113   
 
 114  5
         if (source.isDirectory()) {
 115  0
             throw new BuildException("Src attribute must not " +
 116   
                                      "represent a directory!", getLocation());
 117   
         }
 118   
     }
 119   
 
 120   
     /**
 121   
      * validate, then hand off to the subclass
 122   
      * @throws BuildException
 123   
      */
 124  9
     public void execute() throws BuildException {
 125  9
         validate();
 126   
 
 127  5
         if (!source.exists()) {
 128  0
             log("Nothing to do: " + source.getAbsolutePath() +
 129   
                 " doesn't exist.");
 130  5
         } else if (zipFile.lastModified() < source.lastModified()) {
 131  3
             log("Building: " + zipFile.getAbsolutePath());
 132  3
             pack();
 133   
         } else {
 134  2
             log("Nothing to do: " + zipFile.getAbsolutePath() +
 135   
                 " is up to date.");
 136   
         }
 137   
     }
 138   
 
 139   
     /**
 140   
      * zip a stream to an output stream
 141   
      * @param in
 142   
      * @param zOut
 143   
      * @throws IOException
 144   
      */
 145  3
     private void zipFile(InputStream in, OutputStream zOut)
 146   
         throws IOException {
 147  3
         byte[] buffer = new byte[8 * 1024];
 148  3
         int count = 0;
 149  3
         do {
 150  398
             zOut.write(buffer, 0, count);
 151  398
             count = in.read(buffer, 0, buffer.length);
 152  398
         } while (count != -1);
 153   
     }
 154   
 
 155   
     /**
 156   
      * zip a file to an output stream
 157   
      * @param file
 158   
      * @param zOut
 159   
      * @throws IOException
 160   
      */
 161  3
     protected void zipFile(File file, OutputStream zOut)
 162   
         throws IOException {
 163  3
         FileInputStream fIn = new FileInputStream(file);
 164  3
         try {
 165  3
             zipFile(fIn, zOut);
 166   
         } finally {
 167  3
             fIn.close();
 168   
         }
 169   
     }
 170   
 
 171   
     /**
 172   
      * subclasses must implement this method to do their compression
 173   
      */
 174   
     protected abstract void pack();
 175   
 }
 176