From bddaffe76d01735aee2049456c090b77cef05962 Mon Sep 17 00:00:00 2001 From: Antoine Levy-Lambert Date: Fri, 18 Apr 2003 22:02:59 +0000 Subject: [PATCH] allow to define ZipFileSet(s) outside of Zip task bugrep 17007 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@274481 13f79535-47bb-0310-9956-ffa450edef68 --- docs/manual/CoreTasks/zip.html | 61 +------ docs/manual/CoreTypes/zipfileset.html | 96 +++++++++++ docs/manual/conceptstypeslist.html | 1 + .../org/apache/tools/ant/taskdefs/Zip.java | 29 ++-- .../apache/tools/ant/types/ZipFileSet.java | 85 +++++++--- .../tools/ant/types/defaults.properties | 2 +- .../tools/ant/types/ZipFileSetTest.java | 156 ++++++++++++++++++ 7 files changed, 338 insertions(+), 92 deletions(-) create mode 100644 docs/manual/CoreTypes/zipfileset.html create mode 100644 src/testcases/org/apache/tools/ant/types/ZipFileSetTest.java diff --git a/docs/manual/CoreTasks/zip.html b/docs/manual/CoreTasks/zip.html index b106a84d4..b2808e2b0 100644 --- a/docs/manual/CoreTasks/zip.html +++ b/docs/manual/CoreTasks/zip.html @@ -60,7 +60,7 @@ Java.

Starting with Ant 1.5.2, <zip> can store Unix permissions inside the archive (see description of the filemode and dirmode -attributes for <zipfileset>). +attributes for <zipfileset>). Unfortunately there is no portable way to store these permissions. Ant uses the algorithm used by Info-Zip's implementation of the zip and unzip commands - these are the default @@ -158,62 +158,11 @@ versions of zip and unzip for many Unix and Unix-like systems.

href="../CoreTypes/fileset.html"><fileset> elements to specify the files to be included in the archive.

-

zipfileset

+

zipfileset

-

A <zipfileset> is a special form of a -<fileset> that adds some extra functionality. It -supports all attributes of <fileset> in addition to -those listed below.

- -

Parameters

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
AttributeDescriptionRequired
prefixall files in the fileset are prefixed with that - path in the archive.No
fullpaththe file described by the fileset is placed at - that exact location in the archive.No
srcmay be used in place of the dir attribute - to specify a zip file whose contents will be extracted and - included in the archive.No
filemodeA 3 digit octal string, specify the user, group - and other modes in the standard Unix fashion. Only applies to - plain files. Default is 644. since Ant 1.5.2.No
dirmodeA 3 digit octal string, specify the user, group - and other modes in the standard Unix fashion. Only applies to - directories. Default is 755. since Ant 1.5.2.No
- -

The fullpath attribute can only be set for filesets that -represent a single file. The prefix and fullpath -attributes cannot both be set on the same fileset.

- -

When using the src attribute, include and exclude patterns -may be used to specify a subset of the zip file for inclusion in the -archive as with the dir attribute.

+

The zip task supports any number of nested <zipfileset> elements to specify +the files to be included in the archive.

zipgroupfileset

A <zipgroupfileset> allows for multiple zip files to be diff --git a/docs/manual/CoreTypes/zipfileset.html b/docs/manual/CoreTypes/zipfileset.html new file mode 100644 index 000000000..ddc76189f --- /dev/null +++ b/docs/manual/CoreTypes/zipfileset.html @@ -0,0 +1,96 @@ + + + + + ZipFileSet Type + + +

ZipFileSet

+

A <zipfileset> is a special form of a <fileset> which can behave in 2 +different ways :
+

+ +

<zipfileset> supports all attributes of <fileset> +in addition to those listed below.
+

+

Since Ant 1.6, a zipfileset can be defined with the id attribute and referred to with +the refid attribute.
+

+

Parameters

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
AttributeDescriptionRequired
prefixall files in the fileset are prefixed with that +path in the archive.No
fullpaththe file described by the fileset is placed at +that exact location in the archive.No
srcmay be used in place of the dir attribute + to specify a zip file whose contents will be extracted and included +in the archive.No
filemodeA 3 digit octal string, specify the user, group +and other modes in the standard Unix fashion. Only applies to +plain files. Default is 644. since Ant 1.5.2.No
dirmodeA 3 digit octal string, specify the user, group +and other modes in the standard Unix fashion. Only applies to +directories. Default is 755. since Ant 1.5.2.No
+

The fullpath attribute can only be set for filesets that +represent a single file. The prefix and fullpath +attributes cannot both be set on the same fileset.

+

When using the src attribute, include and exclude patterns +may be used to specify a subset of the zip file for inclusion in the +archive as with the dir attribute.

+

Examples

+
+
  <zip destfile="${dist}/manual.zip">
<zipfileset dir="htdocs/manual" prefix="docs/user-guide"/>
<zipfileset dir="." includes="ChangeLog27.txt" fullpath="docs/ChangeLog.txt"/>
<zipfileset src="examples.zip" includes="**/*.html" prefix="docs/examples"/>
</zip>
+

zips all files in the htdocs/manual directory into +the docs/user-guide directory in the archive, adds the +file ChangeLog27.txt in the current directory as docs/ChangeLog.txt, +and includes all the html files in examples.zip under docs/examples. +The archive might end up containing the files:

+ docs/user-guide/html/index.html
+docs/ChangeLog.txt
+docs/examples/index.html
+
+
+

Copyright © 2003 Apache Software Foundation. All +rights Reserved.

+ + diff --git a/docs/manual/conceptstypeslist.html b/docs/manual/conceptstypeslist.html index efd965a41..f1be1ef7b 100644 --- a/docs/manual/conceptstypeslist.html +++ b/docs/manual/conceptstypeslist.html @@ -27,6 +27,7 @@ Path-like Structures
Selectors
XMLCatalog
+ZipFileSet

Optional Types

Class Fileset
diff --git a/src/main/org/apache/tools/ant/taskdefs/Zip.java b/src/main/org/apache/tools/ant/taskdefs/Zip.java index d79910524..ef00761dd 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Zip.java +++ b/src/main/org/apache/tools/ant/taskdefs/Zip.java @@ -67,7 +67,6 @@ import java.util.Stack; import java.util.Vector; import java.util.zip.CRC32; import java.util.zip.ZipFile; -import java.util.zip.ZipInputStream; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.DirectoryScanner; @@ -541,10 +540,10 @@ public class Zip extends MatchingTask { ZipFileSet zfs = null; if (fileset instanceof ZipFileSet) { zfs = (ZipFileSet) fileset; - prefix = zfs.getPrefix(); - fullpath = zfs.getFullpath(); - dirMode = zfs.getDirMode(); - fileMode = zfs.getFileMode(); + prefix = zfs.getPrefix(getProject()); + fullpath = zfs.getFullpath(getProject()); + dirMode = zfs.getDirMode(getProject()); + fileMode = zfs.getFileMode(getProject()); } if (prefix.length() > 0 && fullpath.length() > 0) { @@ -570,11 +569,11 @@ public class Zip extends MatchingTask { boolean dealingWithFiles = false; File base = null; - if (zfs == null || zfs.getSrc() == null) { + if (zfs == null || zfs.getSrc(getProject()) == null) { dealingWithFiles = true; base = fileset.getDir(getProject()); } else { - zf = new ZipFile(zfs.getSrc()); + zf = new ZipFile(zfs.getSrc(getProject())); } for (int i = 0; i < resources.length; i++) { @@ -604,7 +603,7 @@ public class Zip extends MatchingTask { zf.getEntry(resources[i].getName()); if (ze != null) { zipFile(zf.getInputStream(ze), zOut, prefix + name, - ze.getTime(), zfs.getSrc(), fileMode); + ze.getTime(), zfs.getSrc(getProject()), fileMode); } } } @@ -763,7 +762,7 @@ public class Zip extends MatchingTask { for (int i = 0; i < filesets.length; i++) { if (!(fileset instanceof ZipFileSet) - || ((ZipFileSet) fileset).getSrc() == null) { + || ((ZipFileSet) fileset).getSrc(getProject()) == null) { File base = filesets[i].getDir(getProject()); for (int j = 0; j < initialResources[i].length; j++) { @@ -787,20 +786,20 @@ public class Zip extends MatchingTask { FileNameMapper myMapper = new IdentityMapper(); if (filesets[i] instanceof ZipFileSet) { ZipFileSet zfs = (ZipFileSet) filesets[i]; - if (zfs.getFullpath() != null - && !zfs.getFullpath().equals("") ) { + if (zfs.getFullpath(getProject()) != null + && !zfs.getFullpath(getProject()).equals("") ) { // in this case all files from origin map to // the fullPath attribute of the zipfileset at // destination MergingMapper fm = new MergingMapper(); - fm.setTo(zfs.getFullpath()); + fm.setTo(zfs.getFullpath(getProject())); myMapper = fm; - } else if (zfs.getPrefix() != null - && !zfs.getPrefix().equals("")) { + } else if (zfs.getPrefix(getProject()) != null + && !zfs.getPrefix(getProject()).equals("")) { GlobPatternMapper gm=new GlobPatternMapper(); gm.setFrom("*"); - String prefix = zfs.getPrefix(); + String prefix = zfs.getPrefix(getProject()); if (!prefix.endsWith("/") && !prefix.endsWith("\\")) { prefix += "/"; } diff --git a/src/main/org/apache/tools/ant/types/ZipFileSet.java b/src/main/org/apache/tools/ant/types/ZipFileSet.java index a6280be31..fe2061fc8 100644 --- a/src/main/org/apache/tools/ant/types/ZipFileSet.java +++ b/src/main/org/apache/tools/ant/types/ZipFileSet.java @@ -68,15 +68,10 @@ import org.apache.tools.zip.UnixStat; * entries of a Zip file for inclusion in another Zip file. It also includes * a prefix attribute which is prepended to each entry in the output Zip file. * - * At present, ZipFileSets are not surfaced in the public API. FileSets - * nested in a Zip task are instantiated as ZipFileSets, and their attributes - * are only recognized in the context of the the Zip task. - * It is not possible to define a ZipFileSet outside of the Zip task and - * refer to it via a refid. However a standard FileSet may be included by - * reference in the Zip task, and attributes in the refering ZipFileSet - * can augment FileSet definition. + * Since ant 1.6 ZipFileSet can be defined with an id and referenced in packaging tasks * * @author Don Ferguson don@bea.com + * @author Antoine Levy-Lambert */ public class ZipFileSet extends FileSet { @@ -126,6 +121,9 @@ public class ZipFileSet extends FileSet { * from being specified. */ public void setDir(File dir) throws BuildException { + if (isReference()) { + throw tooManyAttributes(); + } if (srcFile != null) { throw new BuildException("Cannot set both dir and src attributes"); } else { @@ -141,6 +139,9 @@ public class ZipFileSet extends FileSet { * @param srcFile The zip file from which to extract entries. */ public void setSrc(File srcFile) { + if (isReference()) { + throw tooManyAttributes(); + } if (hasDir) { throw new BuildException("Cannot set both dir and src attributes"); } @@ -152,41 +153,62 @@ public class ZipFileSet extends FileSet { * References are not followed, since it is not possible * to have a reference to a ZipFileSet, only to a FileSet. */ - public File getSrc() { + public File getSrc(Project p) { + if (isReference()) { + return ((ZipFileSet)getRef(p)).getSrc(p); + } return srcFile; } /** * Prepend this prefix to the path for each zip entry. - * Does not perform reference test; the referenced file set - * can be augmented with a prefix. + * Prevents both prefix and fullpath from being specified * * @param prefix The prefix to prepend to entries in the zip file. */ public void setPrefix(String prefix) { + if (isReference()) { + throw tooManyAttributes(); + } + if (!fullpath.equals("")) { + throw new BuildException("Cannot set both fullpath and prefix attributes"); + } this.prefix = prefix; } /** * Return the prefix prepended to entries in the zip file. */ - public String getPrefix() { + public String getPrefix(Project p) { + if (isReference()) { + return ((ZipFileSet)getRef(p)).getPrefix(p); + } return prefix; } /** * Set the full pathname of the single entry in this fileset. + * Prevents both prefix and fullpath from being specified * * @param fullpath the full pathname of the single entry in this fileset. */ public void setFullpath(String fullpath) { + if (isReference()) { + throw tooManyAttributes(); + } + if (!prefix.equals("")) { + throw new BuildException("Cannot set both fullpath and prefix attributes"); + } this.fullpath = fullpath; } /** * Return the full pathname of the single entry in this fileset. */ - public String getFullpath() { + public String getFullpath(Project p) { + if (isReference()) { + return ((ZipFileSet)getRef(p)).getFullpath(p); + } return fullpath; } @@ -219,14 +241,20 @@ public class ZipFileSet extends FileSet { * @since Ant 1.5.2 */ public void setFileMode(String octalString) { - this.fileMode = + if (isReference()) { + throw tooManyAttributes(); + } + this.fileMode = UnixStat.FILE_FLAG | Integer.parseInt(octalString, 8); } /** * @since Ant 1.5.2 */ - public int getFileMode() { + public int getFileMode(Project p) { + if (isReference()) { + return ((ZipFileSet)getRef(p)).getFileMode(p); + } return fileMode; } @@ -238,20 +266,25 @@ public class ZipFileSet extends FileSet { * @since Ant 1.6 */ public void setDirMode(String octalString) { - this.dirMode = + if (isReference()) { + throw tooManyAttributes(); + } + this.dirMode = UnixStat.DIR_FLAG | Integer.parseInt(octalString, 8); } /** * @since Ant 1.6 */ - public int getDirMode() { + public int getDirMode(Project p) { + if (isReference()) { + return ((ZipFileSet)getRef(p)).getDirMode(p); + } return dirMode; } /** - * A ZipFileset can accept any fileset as a reference as it just uses the - * standard directory scanner. + * A ZipFileset accepts only another ZipFileSet as reference */ protected AbstractFileSet getRef(Project p) { if (!isChecked()) { @@ -261,11 +294,23 @@ public class ZipFileSet extends FileSet { } Object o = getRefid().getReferencedObject(p); - if (!(o instanceof FileSet)) { - String msg = getRefid().getRefId() + " doesn\'t denote a fileset"; + if (!(o instanceof ZipFileSet)) { + String msg = getRefid().getRefId() + " doesn\'t denote a zipfileset"; throw new BuildException(msg); } else { return (AbstractFileSet) o; } } + /** + * Return a ZipFileSet that has the same properties + * as this one. + * @since Ant 1.6 + */ + public Object clone() { + if (isReference()) { + return new ZipFileSet((ZipFileSet) getRef(getProject())); + } else { + return new ZipFileSet(this); + } + } } diff --git a/src/main/org/apache/tools/ant/types/defaults.properties b/src/main/org/apache/tools/ant/types/defaults.properties index 34b5d168a..fbe7d36cb 100644 --- a/src/main/org/apache/tools/ant/types/defaults.properties +++ b/src/main/org/apache/tools/ant/types/defaults.properties @@ -16,5 +16,5 @@ extensionSet=org.apache.tools.ant.taskdefs.optional.extension.ExtensionSet extension=org.apache.tools.ant.taskdefs.optional.extension.ExtensionAdapter libfileset=org.apache.tools.ant.taskdefs.optional.extension.LibFileSet selector=org.apache.tools.ant.types.selectors.SelectSelector +zipfileset=org.apache.tools.ant.types.ZipFileSet scriptfilter=org.apache.tools.ant.types.optional.ScriptFilter - diff --git a/src/testcases/org/apache/tools/ant/types/ZipFileSetTest.java b/src/testcases/org/apache/tools/ant/types/ZipFileSetTest.java new file mode 100644 index 000000000..38794618b --- /dev/null +++ b/src/testcases/org/apache/tools/ant/types/ZipFileSetTest.java @@ -0,0 +1,156 @@ +/* + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowlegement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowlegement may appear in the software itself, + * if and wherever such third-party acknowlegements normally appear. + * + * 4. The names "Ant" and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Group. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +package org.apache.tools.ant.types; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.Project; + +import junit.framework.TestCase; +import junit.framework.AssertionFailedError; + +import java.io.File; + +/** + * JUnit 3 testcases for org.apache.tools.ant.types.ZipFileSet. + * + *

This doesn't actually test much, mainly reference handling. + * + * @author Antoine Levy-Lambert + */ + +public class ZipFileSetTest extends AbstractFileSetTest { + + public ZipFileSetTest(String name) { + super(name); + } + + protected AbstractFileSet getInstance() { + return new ZipFileSet(); + } + public final void testAttributes() { + ZipFileSet f = (ZipFileSet)getInstance(); + //check that dir and src are incompatible + f.setSrc(new File("example.zip")); + try { + f.setDir(new File("examples")); + fail("can add dir to " + + f.getDataTypeName() + + " when a src is already present"); + } catch (BuildException be) { + assertEquals("Cannot set both dir and src attributes",be.getMessage()); + } + f = (ZipFileSet)getInstance(); + //check that dir and src are incompatible + f.setDir(new File("examples")); + try { + f.setSrc(new File("example.zip")); + fail("can add src to " + + f.getDataTypeName() + + " when a dir is already present"); + } catch (BuildException be) { + assertEquals("Cannot set both dir and src attributes",be.getMessage()); + } + //check that fullpath and prefix are incompatible + f = (ZipFileSet)getInstance(); + f.setSrc(new File("example.zip")); + f.setPrefix("/examples"); + try { + f.setFullpath("/doc/manual/index.html"); + fail("Can add fullpath to " + + f.getDataTypeName() + + " when a prefix is already present"); + } catch (BuildException be) { + assertEquals("Cannot set both fullpath and prefix attributes", be.getMessage()); + } + f = (ZipFileSet)getInstance(); + f.setSrc(new File("example.zip")); + f.setFullpath("/doc/manual/index.html"); + try { + f.setPrefix("/examples"); + fail("Can add prefix to " + + f.getDataTypeName() + + " when a fullpath is already present"); + } catch (BuildException be) { + assertEquals("Cannot set both fullpath and prefix attributes", be.getMessage()); + } + // check that reference zipfilesets cannot have specific attributes + f = (ZipFileSet)getInstance(); + f.setRefid(new Reference("test")); + try { + f.setSrc(new File("example.zip")); + fail("Can add src to " + + f.getDataTypeName() + + " when a refid is already present"); + } catch (BuildException be) { + assertEquals("You must not specify more than one " + + "attribute when using refid", be.getMessage()); + } + // check that a reference zipfileset gets the same attributes as the original + f = (ZipFileSet)getInstance(); + f.setSrc(new File("example.zip")); + f.setPrefix("/examples"); + f.setFileMode("600"); + f.setDirMode("530"); + getProject().addReference("test",f); + ZipFileSet zid=(ZipFileSet)getInstance(); + zid.setRefid(new Reference("test")); + assertTrue("src attribute copied by copy constructor",zid.getSrc(getProject()).equals(f.getSrc(getProject()))); + assertTrue("prefix attribute copied by copy constructor",f.getPrefix(getProject()).equals(zid.getPrefix(getProject()))); + assertTrue("file mode attribute copied by copy constructor",f.getFileMode(getProject())==zid.getFileMode(getProject())); + assertTrue("dir mode attribute copied by copy constructor",f.getDirMode(getProject())==zid.getDirMode(getProject())); + } + + +}