Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 215   Methods: 5
NCLOC: 78   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
Untar.java 66.7% 92.3% 100% 86%
 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.BufferedInputStream;
 58   
 import java.io.File;
 59   
 import java.io.FileInputStream;
 60   
 import java.io.IOException;
 61   
 import java.io.InputStream;
 62   
 import java.util.zip.GZIPInputStream;
 63   
 import org.apache.tools.ant.BuildException;
 64   
 import org.apache.tools.ant.Project;
 65   
 import org.apache.tools.ant.types.EnumeratedAttribute;
 66   
 import org.apache.tools.ant.util.FileUtils;
 67   
 import org.apache.tools.bzip2.CBZip2InputStream;
 68   
 import org.apache.tools.tar.TarEntry;
 69   
 import org.apache.tools.tar.TarInputStream;
 70   
 
 71   
 
 72   
 
 73   
 /**
 74   
  * Untar a file.
 75   
  * <p>For JDK 1.1 &quot;last modified time&quot; field is set to current time instead of being
 76   
  * carried from the archive file.</p>
 77   
  * <p>PatternSets are used to select files to extract
 78   
  * <I>from</I> the archive.  If no patternset is used, all files are extracted.
 79   
  * </p>
 80   
  * <p>FileSet>s may be used used to select archived files
 81   
  * to perform unarchival upon.
 82   
  * </p>
 83   
  * <p>File permissions will not be restored on extracted files.</p>
 84   
  * <p>The untar task recognizes the long pathname entries used by GNU tar.<p>
 85   
  *
 86   
  * @author Stefan Bodewig
 87   
  * @author Magesh Umasankar
 88   
  *
 89   
  * @since Ant 1.1
 90   
  *
 91   
  * @ant.task category="packaging"
 92   
  */
 93   
 public class Untar extends Expand {
 94   
     /**
 95   
      *   compression method
 96   
      */
 97   
     private UntarCompressionMethod compression = new UntarCompressionMethod();
 98   
 
 99   
     /**
 100   
      * Set decompression algorithm to use; default=none.
 101   
      *
 102   
      * Allowable values are
 103   
      * <ul>
 104   
      *   <li>none - no compression
 105   
      *   <li>gzip - Gzip compression
 106   
      *   <li>bzip2 - Bzip2 compression
 107   
      * </ul>
 108   
      *
 109   
      * @param method compression method
 110   
      */
 111  6
     public void setCompression(UntarCompressionMethod method) {
 112  6
         compression = method;
 113   
     }
 114   
 
 115  10
     protected void expandFile(FileUtils fileUtils, File srcF, File dir) {
 116  10
         TarInputStream tis = null;
 117  10
         try {
 118  10
             log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO);
 119  10
             tis = new TarInputStream(
 120   
                 compression.decompress(srcF,
 121   
                     new BufferedInputStream(
 122   
                         new FileInputStream(srcF))));
 123  10
             TarEntry te = null;
 124   
 
 125  ?
             while ((te = tis.getNextEntry()) != null) {
 126  11
                 extractFile(fileUtils, srcF, dir, tis,
 127   
                             te.getName(), te.getModTime(), te.isDirectory());
 128   
             }
 129  10
             log("expand complete", Project.MSG_VERBOSE);
 130   
 
 131   
         } catch (IOException ioe) {
 132  0
             throw new BuildException("Error while expanding " + srcF.getPath(),
 133   
                                      ioe, getLocation());
 134   
         } finally {
 135  10
             if (tis != null) {
 136  10
                 try {
 137  10
                     tis.close();
 138   
                 } catch (IOException e) {}
 139   
             }
 140   
         }
 141   
     }
 142   
 
 143   
     /**
 144   
      * Valid Modes for Compression attribute to Untar Task
 145   
      *
 146   
      */
 147   
     public static final class UntarCompressionMethod
 148   
         extends EnumeratedAttribute {
 149   
 
 150   
         // permissable values for compression attribute
 151   
         /**
 152   
          *  No compression
 153   
          */
 154   
         private static final String NONE = "none";
 155   
         /**
 156   
          *  GZIP compression
 157   
          */
 158   
         private static final String GZIP = "gzip";
 159   
         /**
 160   
          *  BZIP2 compression
 161   
          */
 162   
         private static final String BZIP2 = "bzip2";
 163   
 
 164   
 
 165   
         /**
 166   
          *  Constructor
 167   
          */
 168  17
         public UntarCompressionMethod() {
 169  17
             super();
 170  17
             setValue(NONE);
 171   
         }
 172   
 
 173   
         /**
 174   
          * Get valid enumeration values
 175   
          *
 176   
          * @return valid values
 177   
          */
 178  23
         public String[] getValues() {
 179  23
             return new String[] { NONE, GZIP, BZIP2 };
 180   
         }
 181   
 
 182   
         /**
 183   
          *  This method wraps the input stream with the
 184   
          *     corresponding decompression method
 185   
          *
 186   
          *  @param file provides location information for BuildException
 187   
          *  @param istream input stream
 188   
          *  @return input stream with on-the-fly decompression
 189   
          *  @exception IOException thrown by GZIPInputStream constructor
 190   
          *  @exception BuildException thrown if bzip stream does not
 191   
          *     start with expected magic values
 192   
          */
 193  10
         private InputStream decompress(final File file,
 194   
                                        final InputStream istream)
 195   
             throws IOException, BuildException {
 196  10
             final String value = getValue();
 197  10
             if (GZIP.equals(value)) {
 198  3
                 return new GZIPInputStream(istream);
 199   
             } else {
 200  7
                 if (BZIP2.equals(value)) {
 201  3
                     final char[] magic = new char[] { 'B', 'Z' };
 202  3
                     for (int i = 0; i < magic.length; i++) {
 203  6
                         if (istream.read() != magic[i]) {
 204  0
                             throw new BuildException(
 205   
                                 "Invalid bz2 file." + file.toString());
 206   
                         }
 207   
                     }
 208  3
                     return new CBZip2InputStream(istream);
 209   
                 }
 210   
             }
 211  4
             return istream;
 212   
         }
 213   
     }
 214   
 }
 215