From e3707fdec844ab18d5a75404f061041efb8670d9 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Sat, 29 Oct 2005 13:09:43 +0000 Subject: [PATCH] resource collections for [bg]unzip2? git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@329409 13f79535-47bb-0310-9956-ffa450edef68 --- docs/manual/CoreTasks/unpack.html | 20 ++++++- src/etc/testcases/taskdefs/bunzip2.xml | 6 ++ src/etc/testcases/taskdefs/gunzip.xml | 6 ++ .../apache/tools/ant/taskdefs/BUnzip2.java | 21 ++++++- .../org/apache/tools/ant/taskdefs/GUnzip.java | 21 ++++++- .../org/apache/tools/ant/taskdefs/Unpack.java | 60 +++++++++++++++---- .../tools/ant/taskdefs/BUnzip2Test.java | 17 ++++-- .../apache/tools/ant/taskdefs/GUnzipTest.java | 18 +++--- 8 files changed, 137 insertions(+), 32 deletions(-) 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 @@

GUnzip/BUnzip2

Description

-

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.

Parameters

@@ -29,7 +29,7 @@ does not exist.

src the file to expand. - Yes + Yes, or a nested resource collection. dest @@ -37,6 +37,13 @@ does not exist.

No +

Parameters specified as nested elements

+ +

any resource or single element +resource collection

+ +

The specified resource will be used as src.

+

Examples

 <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.

Related tasks

diff --git a/src/etc/testcases/taskdefs/bunzip2.xml b/src/etc/testcases/taskdefs/bunzip2.xml index d5878bc17..d726befc7 100644 --- a/src/etc/testcases/taskdefs/bunzip2.xml +++ b/src/etc/testcases/taskdefs/bunzip2.xml @@ -6,6 +6,12 @@ + + + + + + diff --git a/src/etc/testcases/taskdefs/gunzip.xml b/src/etc/testcases/taskdefs/gunzip.xml index 5e3b0e972..588901591 100644 --- a/src/etc/testcases/taskdefs/gunzip.xml +++ b/src/etc/testcases/taskdefs/gunzip.xml @@ -24,6 +24,12 @@ + + + + + + diff --git a/src/main/org/apache/tools/ant/taskdefs/BUnzip2.java b/src/main/org/apache/tools/ant/taskdefs/BUnzip2.java index 8833d6144..18bba9b46 100644 --- a/src/main/org/apache/tools/ant/taskdefs/BUnzip2.java +++ b/src/main/org/apache/tools/ant/taskdefs/BUnzip2.java @@ -19,9 +19,9 @@ package org.apache.tools.ant.taskdefs; import java.io.BufferedInputStream; -import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.util.FileUtils; import org.apache.tools.bzip2.CBZip2InputStream; @@ -58,11 +58,11 @@ public class BUnzip2 extends Unpack { FileOutputStream out = null; CBZip2InputStream zIn = null; - FileInputStream fis = null; + InputStream fis = null; BufferedInputStream bis = null; try { out = new FileOutputStream(dest); - fis = new FileInputStream(source); + fis = srcResource.getInputStream(); bis = new BufferedInputStream(fis); int b = bis.read(); if (b != 'B') { @@ -90,4 +90,19 @@ public class BUnzip2 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(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"); } }