Author: bodewig
Date: Mon Aug 31 04:11:16 2009
New Revision: 809456

URL: http://svn.apache.org/viewvc?rev=809456&view=rev
Log:
gzip and bzip2 tasks

Added:
    
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/BZip2.java
   (with props)
    
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/GZip.java
   (with props)
    
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/PackBase.java
   (contents, props changed)
      - copied, changed from r809455, 
ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Pack.java
    ant/sandbox/antlibs/compress/trunk/src/tests/antunit/gzip-test.xml   
(contents, props changed)
      - copied, changed from r809455, 
ant/core/trunk/src/tests/antunit/taskdefs/gzip-test.xml
Modified:
    
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/antlib.xml
    ant/sandbox/antlibs/compress/trunk/src/tests/antunit/bunzip2-test.xml
    ant/sandbox/antlibs/compress/trunk/src/tests/antunit/gunzip-test.xml

Modified: 
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/antlib.xml
URL: 
http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/antlib.xml?rev=809456&r1=809455&r2=809456&view=diff
==============================================================================
--- 
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/antlib.xml 
(original)
+++ 
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/antlib.xml 
Mon Aug 31 04:11:16 2009
@@ -56,6 +56,14 @@
     name="bunzip2"
     classname="org.apache.ant.compress.taskdefs.BUnzip2"
     />
+  <taskdef
+    name="gzip"
+    classname="org.apache.ant.compress.taskdefs.GZip"
+    />
+  <taskdef
+    name="bzip2"
+    classname="org.apache.ant.compress.taskdefs.BZip2"
+    />
 
   <typedef
     name="tarentry"

Added: 
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/BZip2.java
URL: 
http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/BZip2.java?rev=809456&view=auto
==============================================================================
--- 
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/BZip2.java
 (added)
+++ 
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/BZip2.java
 Mon Aug 31 04:11:16 2009
@@ -0,0 +1,32 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+
+package org.apache.ant.compress.taskdefs;
+
+import org.apache.ant.compress.util.BZip2StreamFactory;
+
+/**
+ * Compresses using gzip.
+ */
+public final class BZip2 extends PackBase {
+
+    public BZip2() {
+        super(new BZip2StreamFactory());
+    }
+
+}
\ No newline at end of file

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

Added: 
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/GZip.java
URL: 
http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/GZip.java?rev=809456&view=auto
==============================================================================
--- 
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/GZip.java
 (added)
+++ 
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/GZip.java
 Mon Aug 31 04:11:16 2009
@@ -0,0 +1,32 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+
+package org.apache.ant.compress.taskdefs;
+
+import org.apache.ant.compress.util.GZipStreamFactory;
+
+/**
+ * Compresses using gzip.
+ */
+public final class GZip extends PackBase {
+
+    public GZip() {
+        super(new GZipStreamFactory());
+    }
+
+}
\ No newline at end of file

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

Copied: 
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/PackBase.java
 (from r809455, ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Pack.java)
URL: 
http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/PackBase.java?p2=ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/PackBase.java&p1=ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Pack.java&r1=809455&r2=809456&rev=809456&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Pack.java (original)
+++ 
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/PackBase.java
 Mon Aug 31 04:11:16 2009
@@ -16,71 +16,87 @@
  *
  */
 
-package org.apache.tools.ant.taskdefs;
+package org.apache.ant.compress.taskdefs;
 
+import java.io.BufferedOutputStream;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.util.Iterator;
+
+import org.apache.ant.compress.util.CompressorStreamFactory;
+import org.apache.commons.compress.utils.IOUtils;
 import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.Task;
 import org.apache.tools.ant.types.Resource;
 import org.apache.tools.ant.types.ResourceCollection;
-import org.apache.tools.ant.types.resources.FileProvider;
 import org.apache.tools.ant.types.resources.FileResource;
+import org.apache.tools.ant.types.resources.Resources;
+import org.apache.tools.ant.util.FileUtils;
 
 /**
  * Abstract Base class for pack tasks.
- *
- * @since Ant 1.5
  */
+public abstract class PackBase extends Task {
 
-public abstract class Pack extends Task {
     private static final int BUFFER_SIZE = 8 * 1024;
-    // CheckStyle:VisibilityModifier OFF - bc
-    protected File zipFile;
-    protected File source;
-    // CheckStyle:VisibilityModifier ON
+
+    private final CompressorStreamFactory factory;
+
     private Resource src;
+    private Resource dest;
+
+    protected PackBase(CompressorStreamFactory factory) {
+        this.factory = factory;
+    }
 
     /**
      * the required destination file.
-     * @param zipFile the destination file
      */
-    public void setZipfile(File zipFile) {
-        this.zipFile = zipFile;
+    public void setDestfile(File dest) {
+        setDest(new FileResource(dest));
     }
 
     /**
-     * the required destination file.
-     * @param zipFile the destination file
+     * The resource to pack; required.
+     * @param src resource to expand
      */
-    public void setDestfile(File zipFile) {
-        setZipfile(zipFile);
+    public void setDest(Resource dest) {
+        if (this.dest != null) {
+            throw new BuildException("Can only have one destination 
resource.");
+        }
+        this.dest = dest;
+    }
+
+    /**
+     * The archive to create.
+     */
+    public void addConfiguredDest(Resources r) {
+        for (Iterator it = r.iterator(); it.hasNext(); ) {
+            setDest((Resource) it.next());
+        }
     }
 
     /**
      * the file to compress; required.
      * @param src the source file
      */
-    public void setSrc(File src) {
-        setSrcResource(new FileResource(src));
+    public void setSrcfile(File src) {
+        setSrc(new FileResource(src));
     }
 
     /**
      * The resource to pack; required.
      * @param src resource to expand
      */
-    public void setSrcResource(Resource src) {
+    public void setSrc(Resource src) {
+        if (this.dest != null) {
+            throw new BuildException("Can only have one source resource.");
+        }
         if (src.isDirectory()) {
             throw new BuildException("the source can't be a directory");
         }
-        FileProvider fp = (FileProvider) src.as(FileProvider.class);
-        if (fp != null) {
-            source = fp.getFile();
-        } else if (!supportsNonFileResources()) {
-            throw new BuildException("Only FileSystem resources are 
supported.");
-        }
         this.src = src;
     }
 
@@ -89,16 +105,9 @@
      * @param a the resource to pack as a single element Resource collection.
      */
     public void addConfigured(ResourceCollection a) {
-        if (a.size() == 0) {
-            throw new BuildException("No resource selected, " + getTaskName()
-                    + " needs exactly one resource.");
-        }
-        if (a.size() != 1) {
-            throw new BuildException(getTaskName()
-                    + " cannot handle multiple resources at once. (" + a.size()
-                    + " resources were selected.)");
+        for (Iterator it = a.iterator(); it.hasNext(); ) {
+            setSrc((Resource) it.next());
         }
-        setSrcResource((Resource) a.iterator().next());
     }
 
     /**
@@ -106,19 +115,30 @@
      * @throws BuildException if anything is invalid
      */
     private void validate() throws BuildException {
-        if (zipFile == null) {
-            throw new BuildException("zipfile attribute is required", 
getLocation());
+        if (src == null) {
+            throw new BuildException("source resource is required.",
+                                     getLocation());
         }
 
-        if (zipFile.isDirectory()) {
-            throw new BuildException("zipfile attribute must not "
+        if (src.isDirectory()) {
+            throw new BuildException("source resource must not "
                                     + "represent a directory!", getLocation());
         }
 
-        if (getSrcResource() == null) {
-            throw new BuildException("src attribute or nested resource is"
-                                     + " required", getLocation());
+        if (!src.isExists()) {
+            throw new BuildException("source resource must exist.");
+        }
+
+        if (dest == null) {
+            throw new BuildException("dest resource is required.",
+                                     getLocation());
+        }
+
+        if (dest.isDirectory()) {
+            throw new BuildException("dest resource must not "
+                                    + "represent a directory!", getLocation());
         }
+
     }
 
     /**
@@ -128,84 +148,32 @@
     public void execute() throws BuildException {
         validate();
 
-        Resource s = getSrcResource();
-        if (!s.isExists()) {
-            log("Nothing to do: " + s.toString()
-                + " doesn't exist.");
-        } else if (zipFile.lastModified() < s.getLastModified()) {
-            log("Building: " + zipFile.getAbsolutePath());
-            pack();
+        if (dest.isExists() && dest.getLastModified() < src.getLastModified()) 
{
+            log("Nothing to do: " + dest.getName() + " is up to date.");
         } else {
-            log("Nothing to do: " + zipFile.getAbsolutePath()
-                + " is up to date.");
+            log("Building: " + dest.getName());
+            pack();
         }
     }
 
     /**
-     * zip a stream to an output stream
-     * @param in   the stream to zip
-     * @param zOut the output stream
-     * @throws IOException
-     */
-    private void zipFile(InputStream in, OutputStream zOut)
-        throws IOException {
-        byte[] buffer = new byte[BUFFER_SIZE];
-        int count = 0;
-        do {
-            zOut.write(buffer, 0, count);
-            count = in.read(buffer, 0, buffer.length);
-        } while (count != -1);
-    }
-
-    /**
-     * zip a file to an output stream
-     * @param file the file to zip
-     * @param zOut the output stream
-     * @throws IOException on error
-     */
-    protected void zipFile(File file, OutputStream zOut)
-        throws IOException {
-        zipResource(new FileResource(file), zOut);
-    }
-
-    /**
-     * zip a resource to an output stream
-     * @param resource the resource to zip
-     * @param zOut the output stream
+     * packs a resource to an output stream
      * @throws IOException on error
      */
-    protected void zipResource(Resource resource, OutputStream zOut)
-        throws IOException {
-        InputStream rIn = resource.getInputStream();
+    private void pack() {
+        InputStream in = null;
+        OutputStream out = null;
         try {
-            zipFile(rIn, zOut);
+            in = src.getInputStream();
+            out =
+                factory.getCompressorStream(new 
BufferedOutputStream(dest.getOutputStream()));
+            IOUtils.copy(in, out, BUFFER_SIZE);
+        } catch (IOException e) {
+            throw new BuildException("Error compressing " + src.getName()
+                                     + " to " + dest.getName(), e);
         } finally {
-            rIn.close();
+            FileUtils.close(in);
+            FileUtils.close(out);
         }
     }
-
-    /**
-     * subclasses must implement this method to do their compression
-     */
-    protected abstract void pack();
-
-    /**
-     * The source resource.
-     * @return the source.
-     * @since Ant 1.7
-     */
-    public Resource getSrcResource() {
-        return src;
-    }
-
-    /**
-     * Whether this task can deal with non-file resources.
-     *
-     * <p>This implementation returns false.</p>
-     * @return false.
-     * @since Ant 1.7
-     */
-    protected boolean supportsNonFileResources() {
-        return false;
-    }
 }

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

Propchange: 
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/PackBase.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

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

Modified: ant/sandbox/antlibs/compress/trunk/src/tests/antunit/bunzip2-test.xml
URL: 
http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/tests/antunit/bunzip2-test.xml?rev=809456&r1=809455&r2=809456&view=diff
==============================================================================
--- ant/sandbox/antlibs/compress/trunk/src/tests/antunit/bunzip2-test.xml 
(original)
+++ ant/sandbox/antlibs/compress/trunk/src/tests/antunit/bunzip2-test.xml Mon 
Aug 31 04:11:16 2009
@@ -36,6 +36,15 @@
                          actual="${output}/asf-logo.gif"/>
   </target>
 
+  <target name="testAntlibBzip2Task" depends="setUp">
+    <cmp:bzip2 src="../resources/asf-logo.gif"
+               destfile="${output}/asf-logo.gif.bz2"/>
+    <cmp:bunzip2 src="${output}/asf-logo.gif.bz2"
+                 dest="${output}/asf-logo.gif" />
+    <au:assertFilesMatch expected="../resources/asf-logo.gif"
+                         actual="${output}/asf-logo.gif"/>
+  </target>
+
   <target name="testNativeBzip2" depends="setUp">
     <cmp:bunzip2 src="../resources/asf-logo.gif.bz2"
                  dest="${output}/asf-logo.gif" />

Modified: ant/sandbox/antlibs/compress/trunk/src/tests/antunit/gunzip-test.xml
URL: 
http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/tests/antunit/gunzip-test.xml?rev=809456&r1=809455&r2=809456&view=diff
==============================================================================
--- ant/sandbox/antlibs/compress/trunk/src/tests/antunit/gunzip-test.xml 
(original)
+++ ant/sandbox/antlibs/compress/trunk/src/tests/antunit/gunzip-test.xml Mon 
Aug 31 04:11:16 2009
@@ -46,6 +46,14 @@
                          actual="${output}/asf-logo.gif"/>
   </target>
 
+  <target name="testAntlibGzipTask" depends="setUp">
+    <cmp:gzip src="../resources/asf-logo.gif"
+              destfile="${output}/asf-logo.gif.gz"/>
+    <cmp:gunzip src="${output}/asf-logo.gif.gz" dest="${output}/asf-logo.gif" 
/>
+    <au:assertFilesMatch expected="../resources/asf-logo.gif"
+                         actual="${output}/asf-logo.gif"/>
+  </target>
+
   <target name="testNativeGzip" depends="setUp">
     <cmp:gunzip src="../resources/asf-logo.gif.gz"
                 dest="${output}/asf-logo.gif" />

Copied: ant/sandbox/antlibs/compress/trunk/src/tests/antunit/gzip-test.xml 
(from r809455, ant/core/trunk/src/tests/antunit/taskdefs/gzip-test.xml)
URL: 
http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/tests/antunit/gzip-test.xml?p2=ant/sandbox/antlibs/compress/trunk/src/tests/antunit/gzip-test.xml&p1=ant/core/trunk/src/tests/antunit/taskdefs/gzip-test.xml&r1=809455&r2=809456&rev=809456&view=diff
==============================================================================
--- ant/core/trunk/src/tests/antunit/taskdefs/gzip-test.xml (original)
+++ ant/sandbox/antlibs/compress/trunk/src/tests/antunit/gzip-test.xml Mon Aug 
31 04:11:16 2009
@@ -15,10 +15,11 @@
    See the License for the specific language governing permissions and
    limitations under the License.
 -->
-<project default="antunit" xmlns:au="antlib:org.apache.ant.antunit">
+<project default="antunit"
+         xmlns:au="antlib:org.apache.ant.antunit"
+         xmlns:cmp="antlib:org.apache.ant.compress">
 
-    <!-- note relies on antunit 1.1 -->
-    <import file="../antunit-base.xml" />
+  <import file="antunit-base.xml" />
 
     <target name="setUp">
         <mkdir dir="${output}" />
@@ -28,18 +29,38 @@
     </target>
 
     <target name="testFailNone">
-        <au:expectfailure expectedmessage="No resource selected, gzip needs 
exactly one resource." message="Should have thrown an exception">
-            <gzip destfile="${output}/file.gz">
+        <au:expectfailure expectedmessage="source resource is required.">
+            <cmp:gzip destfile="${output}/file.gz">
                 <fileset dir="${output}/empty" />
-            </gzip>
+            </cmp:gzip>
         </au:expectfailure>
     </target>
 
     <target name="testFailTwo">
-        <au:expectfailure expectedmessage="gzip cannot handle multiple 
resources at once. (2 resources were selected.)" message="Should have thrown an 
exception">
-            <gzip destfile="${output}/file.gz">
+        <au:expectfailure expectedmessage="Can only have one source resource.">
+            <cmp:gzip destfile="${output}/file.gz">
                 <fileset dir="${output}" />
-            </gzip>
+            </cmp:gzip>
+        </au:expectfailure>
+    </target>
+
+    <target name="testFailNoDest">
+        <au:expectfailure expectedmessage="dest resource is required.">
+            <cmp:gzip src="${output}/fileone">
+              <dest>
+                <fileset dir="${output}/empty" />
+              </dest>
+            </cmp:gzip>
+        </au:expectfailure>
+    </target>
+
+    <target name="testFailTwoDests">
+        <au:expectfailure expectedmessage="Can only have one destination 
resource.">
+            <cmp:gzip src="${output}/fileone">
+              <dest>
+                <fileset dir="${output}" />
+              </dest>
+            </cmp:gzip>
         </au:expectfailure>
     </target>
 

Propchange: ant/sandbox/antlibs/compress/trunk/src/tests/antunit/gzip-test.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: ant/sandbox/antlibs/compress/trunk/src/tests/antunit/gzip-test.xml
------------------------------------------------------------------------------
    svn:mergeinfo = 


Reply via email to