diff --git a/docs/manual/CoreTasks/unpack.html b/docs/manual/CoreTasks/unpack.html index 1a7eac29f..9cdc303dd 100644 --- a/docs/manual/CoreTasks/unpack.html +++ b/docs/manual/CoreTasks/unpack.html @@ -10,13 +10,13 @@
Expands a file packed using GZip or BZip2.
+Expands a resource packed using GZip or BZip2.
If dest is a directory the name of the destination file is the same as src (with the ".gz" or ".bz2" extension removed if present). If dest is omitted, the parent dir of src is taken. The file is only expanded if the source -file is newer than the destination file, or when the destination file +resource is newer than the destination file, or when the destination file does not exist.
The specified resource will be used as src.
+<gunzip src="test.tar.gz"/> @@ -55,6 +62,13 @@ does not exist.
expands test.tar.gz to subdir/test.tar (assuming subdir is a directory).
+++<gunzip dest="."> + <url url="http://example.org/archive.tar.gz"/> +</gunzip> +
downloads http://example.org/archive.tar.gz and expands it +to archive.tar in the project's basedir on the fly.
This implementation returns true only if this task is + * <gunzip>. 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.
+ * + * @since Ant 1.7 + */ + protected boolean supportsNonFileResources() { + return getClass().equals(BUnzip2.class); + } } diff --git a/src/main/org/apache/tools/ant/taskdefs/GUnzip.java b/src/main/org/apache/tools/ant/taskdefs/GUnzip.java index 3cca5ecdd..018491d15 100644 --- a/src/main/org/apache/tools/ant/taskdefs/GUnzip.java +++ b/src/main/org/apache/tools/ant/taskdefs/GUnzip.java @@ -17,9 +17,9 @@ package org.apache.tools.ant.taskdefs; -import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; import java.util.zip.GZIPInputStream; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.util.FileUtils; @@ -56,10 +56,10 @@ public class GUnzip extends Unpack { FileOutputStream out = null; GZIPInputStream zIn = null; - FileInputStream fis = null; + InputStream fis = null; try { out = new FileOutputStream(dest); - fis = new FileInputStream(source); + fis = srcResource.getInputStream(); zIn = new GZIPInputStream(fis); byte[] buffer = new byte[8 * 1024]; int count = 0; @@ -77,4 +77,19 @@ public class GUnzip extends Unpack { } } } + + /** + * Whether this task can deal with non-file resources. + * + *This implementation returns true only if this task is + * <gunzip>. 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.
+ * + * @since Ant 1.7 + */ + protected boolean supportsNonFileResources() { + return getClass().equals(GUnzip.class); + } } diff --git a/src/main/org/apache/tools/ant/taskdefs/Unpack.java b/src/main/org/apache/tools/ant/taskdefs/Unpack.java index 0ed834536..0ba64bdb9 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Unpack.java +++ b/src/main/org/apache/tools/ant/taskdefs/Unpack.java @@ -21,6 +21,9 @@ package org.apache.tools.ant.taskdefs; import java.io.File; 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 unpack tasks. @@ -32,6 +35,7 @@ public abstract class Unpack extends Task { protected File source; protected File dest; + protected Resource srcResource; /** * @deprecated setSrc(String) is deprecated and is replaced with @@ -66,7 +70,39 @@ public abstract class Unpack extends Task { * @param src file to expand */ public void setSrc(File src) { - source = src; + setSrcResource(new FileResource(src)); + } + + /** + * The resource to expand; required. + * @param src resource to expand + */ + public void setSrcResource(Resource src) { + if (!src.isExists()) { + throw new BuildException("the archive doesn't exist"); + } + if (src.isDirectory()) { + throw new BuildException("the archive can't be a directory"); + } + if (src instanceof FileResource) { + source = ((FileResource) src).getFile(); + } else if (!supportsNonFileResources()) { + throw new BuildException("Only FileSystem resources are" + + " supported."); + } + srcResource = src; + } + + /** + * Set the source Archive resource. + * @param a the archive 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()); } /** @@ -78,18 +114,10 @@ public abstract class Unpack extends Task { } private void validate() throws BuildException { - if (source == null) { + if (srcResource == null) { throw new BuildException("No Src specified", getLocation()); } - if (!source.exists()) { - throw new BuildException("Src doesn't exist", getLocation()); - } - - if (source.isDirectory()) { - throw new BuildException("Cannot expand a directory", getLocation()); - } - if (dest == null) { dest = new File(source.getParent()); } @@ -140,4 +168,16 @@ public abstract class Unpack extends Task { * This is to be overridden by subclasses. */ protected abstract void extract(); + + /** + * Whether this task can deal with non-file resources. + * + *This implementation returns false.
+ * + * @since Ant 1.7 + */ + protected boolean supportsNonFileResources() { + return false; + } + } diff --git a/src/testcases/org/apache/tools/ant/taskdefs/BUnzip2Test.java b/src/testcases/org/apache/tools/ant/taskdefs/BUnzip2Test.java index 8d6dd21fe..a622697e5 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/BUnzip2Test.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/BUnzip2Test.java @@ -42,17 +42,22 @@ public class BUnzip2Test extends BuildFileTest { executeTarget("cleanup"); } - public void testRealTest() throws IOException { - executeTarget("realTest"); + public void testRealTest() throws java.io.IOException { + testRealTest("realTest"); + } + + public void testRealTestWithResource() throws java.io.IOException { + testRealTest("realTestWithResource"); + } + + private void testRealTest(String target) throws java.io.IOException { + executeTarget(target); assertTrue("File content mismatch after bunzip2", FILE_UTILS.contentEquals(project.resolveFile("expected/asf-logo-huge.tar"), project.resolveFile("asf-logo-huge.tar"))); } public void testDocumentationClaimsOnCopy() throws java.io.IOException { - executeTarget("testDocumentationClaimsOnCopy"); - assertTrue("File content mismatch after bunzip2", - FILE_UTILS.contentEquals(project.resolveFile("expected/asf-logo-huge.tar"), - project.resolveFile("asf-logo-huge.tar"))); + testRealTest("testDocumentationClaimsOnCopy"); } } diff --git a/src/testcases/org/apache/tools/ant/taskdefs/GUnzipTest.java b/src/testcases/org/apache/tools/ant/taskdefs/GUnzipTest.java index 93ce873eb..65a973b0c 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/GUnzipTest.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/GUnzipTest.java @@ -47,20 +47,24 @@ public class GUnzipTest extends BuildFileTest { } public void testRealTest() throws java.io.IOException { - executeTarget("realTest"); + testRealTest("realTest"); + } + + public void testRealTestWithResource() throws java.io.IOException { + testRealTest("realTestWithResource"); + } + + private void testRealTest(String target) throws java.io.IOException { + executeTarget(target); assertTrue(FILE_UTILS.contentEquals(project.resolveFile("../asf-logo.gif"), project.resolveFile("asf-logo.gif"))); } public void testTestGzipTask() throws java.io.IOException { - executeTarget("testGzipTask"); - assertTrue(FILE_UTILS.contentEquals(project.resolveFile("../asf-logo.gif"), - project.resolveFile("asf-logo.gif"))); + testRealTest("testGzipTask"); } public void testDocumentationClaimsOnCopy() throws java.io.IOException { - executeTarget("testDocumentationClaimsOnCopy"); - assertTrue(FILE_UTILS.contentEquals(project.resolveFile("../asf-logo.gif"), - project.resolveFile("asf-logo.gif"))); + testRealTest("testDocumentationClaimsOnCopy"); } }