You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by bo...@apache.org on 2005/11/15 09:31:30 UTC

svn commit: r344334 - in /ant/core/trunk: docs/manual/CoreTasks/ src/etc/testcases/taskdefs/ src/main/org/apache/tools/ant/taskdefs/ src/testcases/org/apache/tools/ant/taskdefs/

Author: bodewig
Date: Tue Nov 15 00:31:19 2005
New Revision: 344334

URL: http://svn.apache.org/viewcvs?rev=344334&view=rev
Log:
add resource support to gzip/bzip2

Modified:
    ant/core/trunk/docs/manual/CoreTasks/pack.html
    ant/core/trunk/src/etc/testcases/taskdefs/bzip2.xml
    ant/core/trunk/src/etc/testcases/taskdefs/gzip.xml
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/BZip2.java
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/GZip.java
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Pack.java
    ant/core/trunk/src/testcases/org/apache/tools/ant/taskdefs/BZip2Test.java
    ant/core/trunk/src/testcases/org/apache/tools/ant/taskdefs/GzipTest.java

Modified: ant/core/trunk/docs/manual/CoreTasks/pack.html
URL: http://svn.apache.org/viewcvs/ant/core/trunk/docs/manual/CoreTasks/pack.html?rev=344334&r1=344333&r2=344334&view=diff
==============================================================================
--- ant/core/trunk/docs/manual/CoreTasks/pack.html (original)
+++ ant/core/trunk/docs/manual/CoreTasks/pack.html Tue Nov 15 00:31:19 2005
@@ -10,9 +10,9 @@
 
 <h2><a name="pack">GZip/BZip2</a></h2>
 <h3>Description</h3>
-<p>Packs a file using the GZip or BZip2 algorithm.
+<p>Packs a resource using the GZip or BZip2 algorithm.
 The output file is only generated if it doesn't exist or the source
-file is newer.</p>
+resource is newer.</p>
 <h3>Parameters</h3>
 <table border="1" cellpadding="2" cellspacing="0">
   <tr>
@@ -23,7 +23,7 @@
   <tr>
     <td valign="top">src</td>
     <td valign="top">the file to gzip/bzip.</td>
-    <td align="center" valign="top">Yes</td>
+    <td align="center" valign="top">Yes, or a nested resource collection.</td>
   </tr>
   <tr>
     <td valign="top">destfile</td>
@@ -35,6 +35,11 @@
     <td valign="top">the <i>deprecated</i> old name of destfile.</td>
   </tr>
 </table>
+<h4>any <a href="../CoreTypes/resources.html">resource</a> or single element
+resource collection</h4>
+
+<p>The specified resource will be used as src.</p>
+
 <h3>Examples</h3>
 <blockquote><pre>
 &lt;gzip src=&quot;test.tar&quot; destfile=&quot;test.tar.gz&quot;/&gt;
@@ -42,6 +47,13 @@
 <blockquote><pre>
 &lt;bzip2 src=&quot;test.tar&quot; destfile=&quot;test.tar.bz2&quot;/&gt;
 </pre></blockquote>
+<blockquote><pre>
+&lt;gzip destfile=&quot;archive.tar.gz&quot;&gt;
+  &lt;url url="http://example.org/archive.tar"/&gt;
+&lt;/gzip&gt;
+</pre></blockquote>
+<p>downloads <i>http://example.org/archive.tar</i> and compresses it
+to <i>archive.tar.gz</i> in the project's basedir on the fly.</p>
 <hr>
 <p align="center">Copyright &copy; 2000-2005 The Apache Software Foundation. All rights
 Reserved.</p>

Modified: ant/core/trunk/src/etc/testcases/taskdefs/bzip2.xml
URL: http://svn.apache.org/viewcvs/ant/core/trunk/src/etc/testcases/taskdefs/bzip2.xml?rev=344334&r1=344333&r2=344334&view=diff
==============================================================================
--- ant/core/trunk/src/etc/testcases/taskdefs/bzip2.xml (original)
+++ ant/core/trunk/src/etc/testcases/taskdefs/bzip2.xml Tue Nov 15 00:31:19 2005
@@ -6,6 +6,12 @@
     <bzip2 src="expected/asf-logo-huge.tar" zipfile="asf-logo-huge.tar.bz2" />
   </target>
 
+  <target name="realTestWithResource">
+    <bzip2 zipfile="asf-logo-huge.tar.bz2">
+      <file file="expected/asf-logo-huge.tar"/>
+    </bzip2>
+  </target>
+
   <target name="testDateCheck">
     <touch file="asf-logo.gif.bz2"/>
     <bzip2 src="../asf-logo.gif" zipfile="asf-logo.gif.bz2" />

Modified: ant/core/trunk/src/etc/testcases/taskdefs/gzip.xml
URL: http://svn.apache.org/viewcvs/ant/core/trunk/src/etc/testcases/taskdefs/gzip.xml?rev=344334&r1=344333&r2=344334&view=diff
==============================================================================
--- ant/core/trunk/src/etc/testcases/taskdefs/gzip.xml (original)
+++ ant/core/trunk/src/etc/testcases/taskdefs/gzip.xml Tue Nov 15 00:31:19 2005
@@ -22,6 +22,12 @@
     <gzip src="../asf-logo.gif" zipfile="asf-logo.gif.gz" />
   </target>
 
+  <target name="realTestWithResource">
+    <gzip zipfile="asf-logo.gif.gz">
+      <file file="../asf-logo.gif"/>
+    </gzip>    
+  </target>
+
   <target name="testDateCheck">
     <touch file="asf-logo.gif.gz"/>
     <gzip src="../asf-logo.gif" zipfile="asf-logo.gif.gz" />

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/BZip2.java
URL: http://svn.apache.org/viewcvs/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/BZip2.java?rev=344334&r1=344333&r2=344334&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/BZip2.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/BZip2.java Tue Nov 15 00:31:19 2005
@@ -22,6 +22,7 @@
 import java.io.FileOutputStream;
 import java.io.IOException;
 import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.util.FileUtils;
 import org.apache.tools.bzip2.CBZip2OutputStream;
 
 /**
@@ -42,19 +43,27 @@
             bos.write('B');
             bos.write('Z');
             zOut = new CBZip2OutputStream(bos);
-            zipFile(source, zOut);
+            zipResource(getSrcResource(), zOut);
         } catch (IOException ioe) {
             String msg = "Problem creating bzip2 " + ioe.getMessage();
             throw new BuildException(msg, ioe, getLocation());
         } finally {
-            if (zOut != null) {
-                try {
-                    // close up
-                    zOut.close();
-                } catch (IOException e) {
-                    //ignore
-                }
-            }
+            FileUtils.close(zOut);
         }
+    }
+
+    /**
+     * Whether this task can deal with non-file resources.
+     *
+     * <p>This implementation returns true only if this task is
+     * &lt;bzip2&gt;.  Any subclass of this class that also wants to
+     * support non-file resources needs to override this method.  We
+     * need to do so for backwards compatibility reasons since we
+     * can't expect subclasses to support resources.</p>
+     *
+     * @since Ant 1.7
+     */
+    protected boolean supportsNonFileResources() {
+        return getClass().equals(BZip2.class);
     }
 }

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/GZip.java
URL: http://svn.apache.org/viewcvs/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/GZip.java?rev=344334&r1=344333&r2=344334&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/GZip.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/GZip.java Tue Nov 15 00:31:19 2005
@@ -21,6 +21,7 @@
 import java.io.IOException;
 import java.util.zip.GZIPOutputStream;
 import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.util.FileUtils;
 
 /**
  * Compresses a file with the GZIP algorithm. Normally used to compress
@@ -39,19 +40,27 @@
         GZIPOutputStream zOut = null;
         try {
             zOut = new GZIPOutputStream(new FileOutputStream(zipFile));
-            zipFile(source, zOut);
+            zipResource(getSrcResource(), zOut);
         } catch (IOException ioe) {
             String msg = "Problem creating gzip " + ioe.getMessage();
             throw new BuildException(msg, ioe, getLocation());
         } finally {
-            if (zOut != null) {
-                try {
-                    // close up
-                    zOut.close();
-                } catch (IOException e) {
-                    // do nothing
-                }
-            }
+            FileUtils.close(zOut);
         }
+    }
+
+    /**
+     * Whether this task can deal with non-file resources.
+     *
+     * <p>This implementation returns true only if this task is
+     * &lt;gzip&gt;.  Any subclass of this class that also wants to
+     * support non-file resources needs to override this method.  We
+     * need to do so for backwards compatibility reasons since we
+     * can't expect subclasses to support resources.</p>
+     *
+     * @since Ant 1.7
+     */
+    protected boolean supportsNonFileResources() {
+        return getClass().equals(GZip.class);
     }
 }

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Pack.java
URL: http://svn.apache.org/viewcvs/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Pack.java?rev=344334&r1=344333&r2=344334&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Pack.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Pack.java Tue Nov 15 00:31:19 2005
@@ -17,14 +17,15 @@
 
 package org.apache.tools.ant.taskdefs;
 
-
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 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.FileResource;
 
 /**
  * Abstract Base class for pack tasks.
@@ -36,6 +37,7 @@
 
     protected File zipFile;
     protected File source;
+    private Resource src;
 
     /**
      * the required destination file.
@@ -58,9 +60,37 @@
      * @param src the source file
      */
     public void setSrc(File src) {
-        source = src;
+        setSrcResource(new FileResource(src));
     }
 
+    /**
+     * The resource to pack; required.
+     * @param src resource to expand
+     */
+    public void setSrcResource(Resource src) {
+        if (src.isDirectory()) {
+            throw new BuildException("the source can't be a directory");
+        }
+        if (src instanceof FileResource) {
+            source = ((FileResource) src).getFile();
+        } else if (!supportsNonFileResources()) {
+            throw new BuildException("Only FileSystem resources are"
+                                     + " supported.");
+        }
+        this.src = src;
+    }
+
+    /**
+     * Set the source resource.
+     * @param a the resource to pack as a single element Resource collection.
+     */
+    public void addConfigured(ResourceCollection a) {
+        if (a.size() != 1) {
+            throw new BuildException("only single argument resource collections"
+                                     + " are supported as archives");
+        }
+        setSrcResource((Resource) a.iterator().next());
+    }
 
     /**
      * validation routine
@@ -76,13 +106,9 @@
                                     + "represent a directory!", getLocation());
         }
 
-        if (source == null) {
-            throw new BuildException("src attribute is required", getLocation());
-        }
-
-        if (source.isDirectory()) {
-            throw new BuildException("Src attribute must not "
-                                    + "represent a directory!", getLocation());
+        if (getSrcResource() == null) {
+            throw new BuildException("src attribute or nested resource is"
+                                     + " required", getLocation());
         }
     }
 
@@ -93,10 +119,11 @@
     public void execute() throws BuildException {
         validate();
 
-        if (!source.exists()) {
-            log("Nothing to do: " + source.getAbsolutePath()
+        Resource s = getSrcResource();
+        if (!s.isExists()) {
+            log("Nothing to do: " + s.toString()
                 + " doesn't exist.");
-        } else if (zipFile.lastModified() < source.lastModified()) {
+        } else if (zipFile.lastModified() < s.getLastModified()) {
             log("Building: " + zipFile.getAbsolutePath());
             pack();
         } else {
@@ -129,11 +156,22 @@
      */
     protected void zipFile(File file, OutputStream zOut)
         throws IOException {
-        FileInputStream fIn = new FileInputStream(file);
+        zipResource(new FileResource(file), zOut);
+    }
+
+    /**
+     * zip a resource to an output stream
+     * @param resource the resource to zip
+     * @param zOut the output stream
+     * @throws IOException on error
+     */
+    protected void zipResource(Resource resource, OutputStream zOut)
+        throws IOException {
+        InputStream rIn = resource.getInputStream();
         try {
-            zipFile(fIn, zOut);
+            zipFile(rIn, zOut);
         } finally {
-            fIn.close();
+            rIn.close();
         }
     }
 
@@ -141,4 +179,24 @@
      * subclasses must implement this method to do their compression
      */
     protected abstract void pack();
+
+    /**
+     * The source resource.
+     *
+     * @since Ant 1.7
+     */
+    public Resource getSrcResource() {
+        return src;
+    }
+
+    /**
+     * Whether this task can deal with non-file resources.
+     *
+     * <p>This implementation returns false.</p>
+     *
+     * @since Ant 1.7
+     */
+    protected boolean supportsNonFileResources() {
+        return false;
+    }
 }

Modified: ant/core/trunk/src/testcases/org/apache/tools/ant/taskdefs/BZip2Test.java
URL: http://svn.apache.org/viewcvs/ant/core/trunk/src/testcases/org/apache/tools/ant/taskdefs/BZip2Test.java?rev=344334&r1=344333&r2=344334&view=diff
==============================================================================
--- ant/core/trunk/src/testcases/org/apache/tools/ant/taskdefs/BZip2Test.java (original)
+++ ant/core/trunk/src/testcases/org/apache/tools/ant/taskdefs/BZip2Test.java Tue Nov 15 00:31:19 2005
@@ -95,6 +95,10 @@
         actualIn.close();
     }
 
+    public void testResource(){
+        executeTarget("realTestWithResource");
+    }
+
     public void testDateCheck(){
         executeTarget("testDateCheck");
         String log = getLog();

Modified: ant/core/trunk/src/testcases/org/apache/tools/ant/taskdefs/GzipTest.java
URL: http://svn.apache.org/viewcvs/ant/core/trunk/src/testcases/org/apache/tools/ant/taskdefs/GzipTest.java?rev=344334&r1=344333&r2=344334&view=diff
==============================================================================
--- ant/core/trunk/src/testcases/org/apache/tools/ant/taskdefs/GzipTest.java (original)
+++ ant/core/trunk/src/testcases/org/apache/tools/ant/taskdefs/GzipTest.java Tue Nov 15 00:31:19 2005
@@ -1,5 +1,5 @@
 /*
- * Copyright  2000-2002,2004 The Apache Software Foundation
+ * Copyright  2000-2002,2004-2005 The Apache Software Foundation
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -55,6 +55,10 @@
             + log + "'", log.startsWith("Building:"));
         assertTrue("Expecting message ending with 'asf-logo.gif.gz' but got '"
             + log + "'", log.endsWith("asf-logo.gif.gz"));
+    }
+
+    public void testResource(){
+        executeTarget("realTestWithResource");
     }
 
     public void testDateCheck(){



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org