git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@329409 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -10,13 +10,13 @@ | |||||
| <h2><a name="unpack">GUnzip/BUnzip2</a></h2> | <h2><a name="unpack">GUnzip/BUnzip2</a></h2> | ||||
| <h3>Description</h3> | <h3>Description</h3> | ||||
| <p>Expands a file packed using GZip or BZip2.</p> | |||||
| <p>Expands a resource packed using GZip or BZip2.</p> | |||||
| <p>If <i>dest</i> is a directory the name of the destination file is | <p>If <i>dest</i> is a directory the name of the destination file is | ||||
| the same as <i>src</i> (with the ".gz" or ".bz2" | the same as <i>src</i> (with the ".gz" or ".bz2" | ||||
| extension removed if present). If <i>dest</i> is omitted, the parent | extension removed if present). If <i>dest</i> is omitted, the parent | ||||
| dir of <i>src</i> is taken. The file is only expanded if the source | dir of <i>src</i> 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.</p> | does not exist.</p> | ||||
| <h3>Parameters</h3> | <h3>Parameters</h3> | ||||
| @@ -29,7 +29,7 @@ does not exist.</p> | |||||
| <tr> | <tr> | ||||
| <td valign="top">src</td> | <td valign="top">src</td> | ||||
| <td valign="top">the file to expand.</td> | <td valign="top">the file to expand.</td> | ||||
| <td align="center" valign="top">Yes</td> | |||||
| <td align="center" valign="top">Yes, or a nested resource collection.</td> | |||||
| </tr> | </tr> | ||||
| <tr> | <tr> | ||||
| <td valign="top">dest</td> | <td valign="top">dest</td> | ||||
| @@ -37,6 +37,13 @@ does not exist.</p> | |||||
| <td align="center" valign="top">No</td> | <td align="center" valign="top">No</td> | ||||
| </tr> | </tr> | ||||
| </table> | </table> | ||||
| <h3>Parameters specified as nested elements</h3> | |||||
| <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> | <h3>Examples</h3> | ||||
| <blockquote><pre> | <blockquote><pre> | ||||
| <gunzip src="test.tar.gz"/> | <gunzip src="test.tar.gz"/> | ||||
| @@ -55,6 +62,13 @@ does not exist.</p> | |||||
| </pre></blockquote> | </pre></blockquote> | ||||
| <p>expands <i>test.tar.gz</i> to <i>subdir/test.tar</i> (assuming | <p>expands <i>test.tar.gz</i> to <i>subdir/test.tar</i> (assuming | ||||
| subdir is a directory).</p> | subdir is a directory).</p> | ||||
| <blockquote><pre> | |||||
| <gunzip dest="."> | |||||
| <url url="http://example.org/archive.tar.gz"/> | |||||
| </gunzip> | |||||
| </pre></blockquote> | |||||
| <p>downloads <i>http://example.org/archive.tar.gz</i> and expands it | |||||
| to <i>archive.tar</i> in the project's basedir on the fly.</p> | |||||
| <h3>Related tasks</h3> | <h3>Related tasks</h3> | ||||
| @@ -6,6 +6,12 @@ | |||||
| <bunzip2 src="expected/asf-logo-huge.tar.bz2" dest="asf-logo-huge.tar" /> | <bunzip2 src="expected/asf-logo-huge.tar.bz2" dest="asf-logo-huge.tar" /> | ||||
| </target> | </target> | ||||
| <target name="realTestWithResource"> | |||||
| <bunzip2 dest="asf-logo-huge.tar"> | |||||
| <file file="expected/asf-logo-huge.tar.bz2"/> | |||||
| </bunzip2> | |||||
| </target> | |||||
| <target name="cleanup"> | <target name="cleanup"> | ||||
| <delete file="asf-logo-huge.tar" /> | <delete file="asf-logo-huge.tar" /> | ||||
| <delete file="expected/asf-logo-huge.tar" /> | <delete file="expected/asf-logo-huge.tar" /> | ||||
| @@ -24,6 +24,12 @@ | |||||
| <gunzip src="expected/asf-logo.gif.gz" dest="asf-logo.gif" /> | <gunzip src="expected/asf-logo.gif.gz" dest="asf-logo.gif" /> | ||||
| </target> | </target> | ||||
| <target name="realTestWithResource"> | |||||
| <gunzip dest="asf-logo.gif"> | |||||
| <file file="expected/asf-logo.gif.gz"/> | |||||
| </gunzip> | |||||
| </target> | |||||
| <target name="testDocumentationClaimsOnCopy"> | <target name="testDocumentationClaimsOnCopy"> | ||||
| <copy todir="."> | <copy todir="."> | ||||
| <gzipresource> | <gzipresource> | ||||
| @@ -19,9 +19,9 @@ package org.apache.tools.ant.taskdefs; | |||||
| import java.io.BufferedInputStream; | import java.io.BufferedInputStream; | ||||
| import java.io.FileInputStream; | |||||
| import java.io.FileOutputStream; | import java.io.FileOutputStream; | ||||
| import java.io.IOException; | import java.io.IOException; | ||||
| import java.io.InputStream; | |||||
| import org.apache.tools.ant.BuildException; | import org.apache.tools.ant.BuildException; | ||||
| import org.apache.tools.ant.util.FileUtils; | import org.apache.tools.ant.util.FileUtils; | ||||
| import org.apache.tools.bzip2.CBZip2InputStream; | import org.apache.tools.bzip2.CBZip2InputStream; | ||||
| @@ -58,11 +58,11 @@ public class BUnzip2 extends Unpack { | |||||
| FileOutputStream out = null; | FileOutputStream out = null; | ||||
| CBZip2InputStream zIn = null; | CBZip2InputStream zIn = null; | ||||
| FileInputStream fis = null; | |||||
| InputStream fis = null; | |||||
| BufferedInputStream bis = null; | BufferedInputStream bis = null; | ||||
| try { | try { | ||||
| out = new FileOutputStream(dest); | out = new FileOutputStream(dest); | ||||
| fis = new FileInputStream(source); | |||||
| fis = srcResource.getInputStream(); | |||||
| bis = new BufferedInputStream(fis); | bis = new BufferedInputStream(fis); | ||||
| int b = bis.read(); | int b = bis.read(); | ||||
| if (b != 'B') { | if (b != 'B') { | ||||
| @@ -90,4 +90,19 @@ public class BUnzip2 extends Unpack { | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| /** | |||||
| * Whether this task can deal with non-file resources. | |||||
| * | |||||
| * <p>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.</p> | |||||
| * | |||||
| * @since Ant 1.7 | |||||
| */ | |||||
| protected boolean supportsNonFileResources() { | |||||
| return getClass().equals(BUnzip2.class); | |||||
| } | |||||
| } | } | ||||
| @@ -17,9 +17,9 @@ | |||||
| package org.apache.tools.ant.taskdefs; | package org.apache.tools.ant.taskdefs; | ||||
| import java.io.FileInputStream; | |||||
| import java.io.FileOutputStream; | import java.io.FileOutputStream; | ||||
| import java.io.IOException; | import java.io.IOException; | ||||
| import java.io.InputStream; | |||||
| import java.util.zip.GZIPInputStream; | import java.util.zip.GZIPInputStream; | ||||
| import org.apache.tools.ant.BuildException; | import org.apache.tools.ant.BuildException; | ||||
| import org.apache.tools.ant.util.FileUtils; | import org.apache.tools.ant.util.FileUtils; | ||||
| @@ -56,10 +56,10 @@ public class GUnzip extends Unpack { | |||||
| FileOutputStream out = null; | FileOutputStream out = null; | ||||
| GZIPInputStream zIn = null; | GZIPInputStream zIn = null; | ||||
| FileInputStream fis = null; | |||||
| InputStream fis = null; | |||||
| try { | try { | ||||
| out = new FileOutputStream(dest); | out = new FileOutputStream(dest); | ||||
| fis = new FileInputStream(source); | |||||
| fis = srcResource.getInputStream(); | |||||
| zIn = new GZIPInputStream(fis); | zIn = new GZIPInputStream(fis); | ||||
| byte[] buffer = new byte[8 * 1024]; | byte[] buffer = new byte[8 * 1024]; | ||||
| int count = 0; | int count = 0; | ||||
| @@ -77,4 +77,19 @@ public class GUnzip extends Unpack { | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| /** | |||||
| * Whether this task can deal with non-file resources. | |||||
| * | |||||
| * <p>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.</p> | |||||
| * | |||||
| * @since Ant 1.7 | |||||
| */ | |||||
| protected boolean supportsNonFileResources() { | |||||
| return getClass().equals(GUnzip.class); | |||||
| } | |||||
| } | } | ||||
| @@ -21,6 +21,9 @@ package org.apache.tools.ant.taskdefs; | |||||
| import java.io.File; | import java.io.File; | ||||
| import org.apache.tools.ant.BuildException; | import org.apache.tools.ant.BuildException; | ||||
| import org.apache.tools.ant.Task; | 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. | * Abstract Base class for unpack tasks. | ||||
| @@ -32,6 +35,7 @@ public abstract class Unpack extends Task { | |||||
| protected File source; | protected File source; | ||||
| protected File dest; | protected File dest; | ||||
| protected Resource srcResource; | |||||
| /** | /** | ||||
| * @deprecated setSrc(String) is deprecated and is replaced with | * @deprecated setSrc(String) is deprecated and is replaced with | ||||
| @@ -66,7 +70,39 @@ public abstract class Unpack extends Task { | |||||
| * @param src file to expand | * @param src file to expand | ||||
| */ | */ | ||||
| public void setSrc(File src) { | 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 { | private void validate() throws BuildException { | ||||
| if (source == null) { | |||||
| if (srcResource == null) { | |||||
| throw new BuildException("No Src specified", getLocation()); | 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) { | if (dest == null) { | ||||
| dest = new File(source.getParent()); | dest = new File(source.getParent()); | ||||
| } | } | ||||
| @@ -140,4 +168,16 @@ public abstract class Unpack extends Task { | |||||
| * This is to be overridden by subclasses. | * This is to be overridden by subclasses. | ||||
| */ | */ | ||||
| protected abstract void extract(); | protected abstract void extract(); | ||||
| /** | |||||
| * Whether this task can deal with non-file resources. | |||||
| * | |||||
| * <p>This implementation returns false.</p> | |||||
| * | |||||
| * @since Ant 1.7 | |||||
| */ | |||||
| protected boolean supportsNonFileResources() { | |||||
| return false; | |||||
| } | |||||
| } | } | ||||
| @@ -42,17 +42,22 @@ public class BUnzip2Test extends BuildFileTest { | |||||
| executeTarget("cleanup"); | 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", | assertTrue("File content mismatch after bunzip2", | ||||
| FILE_UTILS.contentEquals(project.resolveFile("expected/asf-logo-huge.tar"), | FILE_UTILS.contentEquals(project.resolveFile("expected/asf-logo-huge.tar"), | ||||
| project.resolveFile("asf-logo-huge.tar"))); | project.resolveFile("asf-logo-huge.tar"))); | ||||
| } | } | ||||
| public void testDocumentationClaimsOnCopy() throws java.io.IOException { | 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"); | |||||
| } | } | ||||
| } | } | ||||
| @@ -47,20 +47,24 @@ public class GUnzipTest extends BuildFileTest { | |||||
| } | } | ||||
| public void testRealTest() throws java.io.IOException { | 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"), | assertTrue(FILE_UTILS.contentEquals(project.resolveFile("../asf-logo.gif"), | ||||
| project.resolveFile("asf-logo.gif"))); | project.resolveFile("asf-logo.gif"))); | ||||
| } | } | ||||
| public void testTestGzipTask() throws java.io.IOException { | 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 { | 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"); | |||||
| } | } | ||||
| } | } | ||||