Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 311   Methods: 8
NCLOC: 178   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Expand.java 69.2% 79.1% 75% 75.3%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2000-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   
 package org.apache.tools.ant.taskdefs;
 56   
 
 57   
 import java.io.File;
 58   
 import java.io.FileInputStream;
 59   
 import java.io.FileNotFoundException;
 60   
 import java.io.FileOutputStream;
 61   
 import java.io.IOException;
 62   
 import java.io.InputStream;
 63   
 import java.util.Date;
 64   
 import java.util.Vector;
 65   
 import java.util.zip.ZipEntry;
 66   
 import java.util.zip.ZipInputStream;
 67   
 import org.apache.tools.ant.BuildException;
 68   
 import org.apache.tools.ant.DirectoryScanner;
 69   
 import org.apache.tools.ant.Project;
 70   
 import org.apache.tools.ant.Task;
 71   
 import org.apache.tools.ant.types.FileSet;
 72   
 import org.apache.tools.ant.types.PatternSet;
 73   
 import org.apache.tools.ant.util.FileUtils;
 74   
 
 75   
 /**
 76   
  * Unzip a file.
 77   
  *
 78   
  * @author costin@dnt.ro
 79   
  * @author Stefan Bodewig
 80   
  * @author Magesh Umasankar
 81   
  *
 82   
  * @since Ant 1.1
 83   
  *
 84   
  * @ant.task category="packaging"
 85   
  *           name="unzip"
 86   
  *           name="unjar"
 87   
  *           name="unwar"
 88   
  */
 89   
 public class Expand extends Task {
 90   
     private File dest; //req
 91   
     private File source; // req
 92   
     private boolean overwrite = true;
 93   
     private Vector patternsets = new Vector();
 94   
     private Vector filesets = new Vector();
 95   
 
 96   
     /**
 97   
      * Do the work.
 98   
      *
 99   
      * @exception BuildException Thrown in unrecoverable error.
 100   
      */
 101  28
     public void execute() throws BuildException {
 102  28
         if ("expand".equals(getTaskType())) {
 103  0
             log("!! expand is deprecated. Use unzip instead. !!");
 104   
         }
 105   
 
 106  28
         if (source == null && filesets.size() == 0) {
 107  2
             throw new BuildException("src attribute and/or filesets must be "
 108   
                                      + "specified");
 109   
         }
 110   
 
 111  26
         if (dest == null) {
 112  1
             throw new BuildException(
 113   
                 "Dest attribute must be specified");
 114   
         }
 115   
 
 116  25
         if (dest.exists() && !dest.isDirectory()) {
 117  0
             throw new BuildException("Dest must be a directory.", getLocation());
 118   
         }
 119   
 
 120  25
         FileUtils fileUtils = FileUtils.newFileUtils();
 121   
 
 122  25
         if (source != null) {
 123  25
             if (source.isDirectory()) {
 124  1
                 throw new BuildException("Src must not be a directory." +
 125   
                     " Use nested filesets instead.", getLocation());
 126   
             } else {
 127  24
                 expandFile(fileUtils, source, dest);
 128   
             }
 129   
         }
 130  24
         if (filesets.size() > 0) {
 131  0
             for (int j = 0; j < filesets.size(); j++) {
 132  0
                 FileSet fs = (FileSet) filesets.elementAt(j);
 133  0
                 DirectoryScanner ds = fs.getDirectoryScanner(getProject());
 134  0
                 File fromDir = fs.getDir(getProject());
 135   
 
 136  0
                 String[] files = ds.getIncludedFiles();
 137  0
                 for (int i = 0; i < files.length; ++i) {
 138  0
                     File file = new File(fromDir, files[i]);
 139  0
                     expandFile(fileUtils, file, dest);
 140   
                 }
 141   
             }
 142   
         }
 143   
     }
 144   
 
 145   
     /*
 146   
      * This method is to be overridden by extending unarchival tasks.
 147   
      */
 148  14
     protected void expandFile(FileUtils fileUtils, File srcF, File dir) {
 149  14
         log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO);
 150  14
         ZipInputStream zis = null;
 151  14
         try {
 152   
             // code from WarExpand
 153  14
             zis = new ZipInputStream(new FileInputStream(srcF));
 154  14
             ZipEntry ze = null;
 155   
 
 156  ?
             while ((ze = zis.getNextEntry()) != null) {
 157  27
                 extractFile(fileUtils, srcF, dir, zis,
 158   
                             ze.getName(), new Date(ze.getTime()),
 159   
                             ze.isDirectory());
 160   
             }
 161   
 
 162  14
             log("expand complete", Project.MSG_VERBOSE);
 163   
         } catch (IOException ioe) {
 164  0
             throw new BuildException("Error while expanding " + srcF.getPath(),
 165   
                                      ioe);
 166   
         } finally {
 167  14
             if (zis != null) {
 168  14
                 try {
 169  14
                     zis.close();
 170   
                 } catch (IOException e) {}
 171   
             }
 172   
         }
 173   
     }
 174   
 
 175  38
     protected void extractFile(FileUtils fileUtils, File srcF, File dir,
 176   
                                InputStream compressedInputStream,
 177   
                                String entryName,
 178   
                                Date entryDate, boolean isDirectory)
 179   
                                throws IOException {
 180   
 
 181  38
         if (patternsets != null && patternsets.size() > 0) {
 182  19
             String name = entryName;
 183  19
             boolean included = false;
 184  19
             for (int v = 0; v < patternsets.size(); v++) {
 185  19
                 PatternSet p = (PatternSet) patternsets.elementAt(v);
 186  19
                 String[] incls = p.getIncludePatterns(getProject());
 187  19
                 if (incls == null || incls.length == 0) {
 188   
                     // no include pattern implicitly means includes="**"
 189  4
                     incls = new String[] {"**"};
 190   
                 }
 191   
                     
 192  19
                 for (int w = 0; w < incls.length; w++) {
 193  19
                     included = DirectoryScanner.match(incls[w], name);
 194  19
                     if (included) {
 195  15
                         break;
 196   
                     }
 197   
                 }
 198   
                 
 199  19
                 if (!included) {
 200  4
                     break;
 201   
                 }
 202   
                 
 203   
 
 204  15
                 String[] excls = p.getExcludePatterns(getProject());
 205  15
                 if (excls != null) {
 206  6
                     for (int w = 0; w < excls.length; w++) {
 207  6
                         included = !(DirectoryScanner.match(excls[w], name));
 208  6
                         if (!included) {
 209  4
                             break;
 210   
                         }
 211   
                     }
 212   
                 }
 213   
             }
 214  19
             if (!included) {
 215   
                 //Do not process this file
 216  8
                 return;
 217   
             }
 218   
         }
 219   
 
 220  30
         File f = fileUtils.resolveFile(dir, entryName);
 221  30
         try {
 222  30
             if (!overwrite && f.exists()
 223   
                 && f.lastModified() >= entryDate.getTime()) {
 224  0
                 log("Skipping " + f + " as it is up-to-date",
 225   
                     Project.MSG_DEBUG);
 226  0
                 return;
 227   
             }
 228   
 
 229  30
             log("expanding " + entryName + " to " + f,
 230   
                 Project.MSG_VERBOSE);
 231   
             // create intermediary directories - sometimes zip don't add them
 232  30
             File dirF = fileUtils.getParentFile(f);
 233  30
             if ( dirF != null ) {
 234  30
                 dirF.mkdirs();
 235   
             }
 236   
 
 237  30
             if (isDirectory) {
 238  6
                 f.mkdirs();
 239   
             } else {
 240  24
                 byte[] buffer = new byte[1024];
 241  24
                 int length = 0;
 242  24
                 FileOutputStream fos = null;
 243  24
                 try {
 244  24
                     fos = new FileOutputStream(f);
 245   
 
 246  ?
                     while ((length =
 247   
                             compressedInputStream.read(buffer)) >= 0) {
 248  108
                         fos.write(buffer, 0, length);
 249   
                     }
 250   
 
 251  24
                     fos.close();
 252  24
                     fos = null;
 253   
                 } finally {
 254  24
                     if (fos != null) {
 255  0
                         try {
 256  0
                             fos.close();
 257   
                         } catch (IOException e) {}
 258   
                     }
 259   
                 }
 260   
             }
 261   
 
 262  30
             fileUtils.setFileLastModified(f, entryDate.getTime());
 263   
         } catch (FileNotFoundException ex) {
 264  0
             log("Unable to expand to file " + f.getPath(), Project.MSG_WARN);
 265   
         }
 266   
 
 267   
     }
 268   
 
 269   
     /**
 270   
      * Set the destination directory. File will be unzipped into the
 271   
      * destination directory.
 272   
      *
 273   
      * @param d Path to the directory.
 274   
      */
 275  26
     public void setDest(File d) {
 276  26
         this.dest = d;
 277   
     }
 278   
 
 279   
     /**
 280   
      * Set the path to zip-file.
 281   
      *
 282   
      * @param s Path to zip-file.
 283   
      */
 284  26
     public void setSrc(File s) {
 285  26
         this.source = s;
 286   
     }
 287   
 
 288   
     /**
 289   
      * Should we overwrite files in dest, even if they are newer than
 290   
      * the corresponding entries in the archive?
 291   
      */
 292  0
     public void setOverwrite(boolean b) {
 293  0
         overwrite = b;
 294   
     }
 295   
 
 296   
     /**
 297   
      * Add a patternset
 298   
      */
 299  10
     public void addPatternset(PatternSet set) {
 300  10
         patternsets.addElement(set);
 301   
     }
 302   
 
 303   
     /**
 304   
      * Add a fileset
 305   
      */
 306  0
     public void addFileset(FileSet set) {
 307  0
         filesets.addElement(set);
 308   
     }
 309   
 
 310   
 }
 311