Author: bodewig
Date: Fri Jul 31 17:42:27 2009
New Revision: 799668

URL: http://svn.apache.org/viewvc?rev=799668&view=rev
Log:
extract logic common to all un* tasks

Added:
    
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/ExpandBase.java
   (contents, props changed)
      - copied, changed from r799517, 
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/Uncpio.java
Modified:
    
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/Uncpio.java

Copied: 
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/ExpandBase.java
 (from r799517, 
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/Uncpio.java)
URL: 
http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/ExpandBase.java?p2=ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/ExpandBase.java&p1=ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/Uncpio.java&r1=799517&r2=799668&rev=799668&view=diff
==============================================================================
--- 
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/Uncpio.java
 (original)
+++ 
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/ExpandBase.java
 Fri Jul 31 17:42:27 2009
@@ -32,11 +32,11 @@
 import org.apache.tools.ant.util.FileNameMapper;
 import org.apache.tools.ant.util.FileUtils;
 
-import org.apache.commons.compress.archivers.cpio.CpioArchiveEntry;
-import org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream;
+import org.apache.commons.compress.archivers.ArchiveEntry;
+import org.apache.commons.compress.archivers.ArchiveInputStream;
 
 /**
- * Uncpio a file.
+ * Expand an archive.
  * <p>PatternSets are used to select files to extract
  * <I>from</I> the archive.  If no patternset is used, all files are extracted.
  * </p>
@@ -45,12 +45,10 @@
  * </p>
  * <p>File permissions will not be restored on extracted files.</p>
  */
-public class Uncpio extends Expand {
+public abstract class ExpandBase extends Expand {
     /**
-     * No encoding support in Uncpio.
+     * No encoding support in general.
      * @param encoding not used
-     * @throws BuildException always
-     * @since Ant 1.6
      */
     public void setEncoding(String encoding) {
         throw new BuildException("The " + getTaskName()
@@ -61,10 +59,9 @@
     /** {...@inheritdoc} */
     protected void expandFile(FileUtils fileUtils, File srcF, File dir) {
         if (!srcF.exists()) {
-            throw new BuildException("Unable to uncpio "
-                    + srcF
-                    + " as the file does not exist",
-                    getLocation());
+            throw new BuildException("Unable to expand " + srcF
+                                     + " as the file does not exist",
+                                     getLocation());
         }
         FileInputStream fis = null;
         try {
@@ -82,8 +79,7 @@
     /** {...@inheritdoc} */
     protected void expandResource(Resource srcR, File dir) {
         if (!srcR.isExists()) {
-            throw new BuildException("Unable to uncpio "
-                                     + srcR.getName()
+            throw new BuildException("Unable to expand " + srcR.getName()
                                      + " as the it does not exist",
                                      getLocation());
         }
@@ -100,20 +96,24 @@
         }
     }
 
+    protected abstract ArchiveInputStream getArchiveStream(InputStream is)
+        throws IOException;
+
+    protected abstract Date getLastModified(ArchiveEntry entry);
+
     private void expandStream(String name, InputStream stream, File dir)
         throws IOException {
-        CpioArchiveInputStream is = null;
+        ArchiveInputStream is = null;
         try {
-            is = new CpioArchiveInputStream(new BufferedInputStream(stream));
+            FileNameMapper mapper = getMapper();
             log("Expanding: " + name + " into " + dir, Project.MSG_INFO);
-            CpioArchiveEntry ent = null;
+            is = getArchiveStream(new BufferedInputStream(stream));
             boolean empty = true;
-            FileNameMapper mapper = getMapper();
-            while ((ent = is.getNextCPIOEntry()) != null) {
+            ArchiveEntry ent = null;
+            while ((ent = is.getNextEntry()) != null) {
                 empty = false;
                 extractFile(FileUtils.getFileUtils(), null, dir, is,
-                            ent.getName(),
-                            /* TODO revisit */ new Date(ent.getTime()),
+                            ent.getName(), getLastModified(ent),
                             ent.isDirectory(), mapper);
             }
             if (empty && getFailOnEmptyArchive()) {

Propchange: 
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/ExpandBase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/ExpandBase.java
------------------------------------------------------------------------------
    svn:mergeinfo = 

Modified: 
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/Uncpio.java
URL: 
http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/Uncpio.java?rev=799668&r1=799667&r2=799668&view=diff
==============================================================================
--- 
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/Uncpio.java
 (original)
+++ 
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/Uncpio.java
 Fri Jul 31 17:42:27 2009
@@ -18,110 +18,27 @@
 
 package org.apache.ant.compress.taskdefs;
 
-import java.io.BufferedInputStream;
-import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Date;
 
-import org.apache.tools.ant.BuildException;
-import org.apache.tools.ant.Project;
-import org.apache.tools.ant.taskdefs.Expand;
-import org.apache.tools.ant.types.Resource;
-import org.apache.tools.ant.util.FileNameMapper;
-import org.apache.tools.ant.util.FileUtils;
-
+import org.apache.commons.compress.archivers.ArchiveEntry;
+import org.apache.commons.compress.archivers.ArchiveInputStream;
 import org.apache.commons.compress.archivers.cpio.CpioArchiveEntry;
 import org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream;
 
 /**
  * Uncpio a file.
- * <p>PatternSets are used to select files to extract
- * <I>from</I> the archive.  If no patternset is used, all files are extracted.
- * </p>
- * <p>FileSet's may be used to select archived files
- * to perform unarchival upon.
- * </p>
- * <p>File permissions will not be restored on extracted files.</p>
  */
-public class Uncpio extends Expand {
-    /**
-     * No encoding support in Uncpio.
-     * @param encoding not used
-     * @throws BuildException always
-     * @since Ant 1.6
-     */
-    public void setEncoding(String encoding) {
-        throw new BuildException("The " + getTaskName()
-                                 + " task doesn't support the encoding"
-                                 + " attribute", getLocation());
-    }
-
-    /** {...@inheritdoc} */
-    protected void expandFile(FileUtils fileUtils, File srcF, File dir) {
-        if (!srcF.exists()) {
-            throw new BuildException("Unable to uncpio "
-                    + srcF
-                    + " as the file does not exist",
-                    getLocation());
-        }
-        FileInputStream fis = null;
-        try {
-            fis = new FileInputStream(srcF);
-            expandStream(srcF.getPath(), fis, dir);
-        } catch (IOException ioe) {
-            throw new BuildException("Error while expanding " + srcF.getPath()
-                                     + "\n" + ioe.toString(),
-                                     ioe, getLocation());
-        } finally {
-            FileUtils.close(fis);
-        }
+public class Uncpio extends ExpandBase {
+    protected ArchiveInputStream getArchiveStream(InputStream is)
+        throws IOException {
+        return new CpioArchiveInputStream(is);
     }
 
-    /** {...@inheritdoc} */
-    protected void expandResource(Resource srcR, File dir) {
-        if (!srcR.isExists()) {
-            throw new BuildException("Unable to uncpio "
-                                     + srcR.getName()
-                                     + " as the it does not exist",
-                                     getLocation());
-        }
-
-        InputStream i = null;
-        try {
-            i = srcR.getInputStream();
-            expandStream(srcR.getName(), i, dir);
-        } catch (IOException ioe) {
-            throw new BuildException("Error while expanding " + srcR.getName(),
-                                     ioe, getLocation());
-        } finally {
-            FileUtils.close(i);
-        }
+    protected Date getLastModified(ArchiveEntry entry) {
+        /* TODO - revisit */
+        return new Date(((CpioArchiveEntry) entry).getTime());
     }
 
-    private void expandStream(String name, InputStream stream, File dir)
-        throws IOException {
-        CpioArchiveInputStream is = null;
-        try {
-            is = new CpioArchiveInputStream(new BufferedInputStream(stream));
-            log("Expanding: " + name + " into " + dir, Project.MSG_INFO);
-            CpioArchiveEntry ent = null;
-            boolean empty = true;
-            FileNameMapper mapper = getMapper();
-            while ((ent = is.getNextCPIOEntry()) != null) {
-                empty = false;
-                extractFile(FileUtils.getFileUtils(), null, dir, is,
-                            ent.getName(),
-                            /* TODO revisit */ new Date(ent.getTime()),
-                            ent.isDirectory(), mapper);
-            }
-            if (empty && getFailOnEmptyArchive()) {
-                throw new BuildException("archive '" + name + "' is empty");
-            }
-            log("expand complete", Project.MSG_VERBOSE);
-        } finally {
-            FileUtils.close(is);
-        }
-    }
 }


Reply via email to