mbenson 2005/01/21 14:15:46 Modified: src/etc/testcases/taskdefs length.xml src/main/org/apache/tools/ant/taskdefs Length.java src/main/org/apache/tools/ant DirectoryScanner.java src/main/org/apache/tools/ant/types Resource.java ZipScanner.java src/testcases/org/apache/tools/ant/taskdefs LengthTest.java Log: Add size to Resource info; switch length task to use Resources instead of files so that it can report lengths for zipfilesets. Revision Changes Path 1.4 +25 -0 ant/src/etc/testcases/taskdefs/length.xml Index: length.xml =================================================================== RCS file: /home/cvs/ant/src/etc/testcases/taskdefs/length.xml,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- length.xml 11 Jan 2005 15:36:20 -0000 1.3 +++ length.xml 21 Jan 2005 22:15:45 -0000 1.4 @@ -2,6 +2,7 @@ <property name="dir" location="lengthtestdir" /> <property name="dir.a" location="${dir}/a" /> <property name="dir.b" location="${dir}/b" /> + <property name="zipfile" location="lengthtest.zip" /> <target name="init"> <mkdir dir="${dir.a}" /> @@ -116,8 +117,32 @@ </fail> </target> + <target name="testZipFileSet" depends="init"> + <zip destfile="${zipfile}"> + <fileset file="${foo}" /> + <fileset file="${bar}" /> + </zip> + <length property="length.zipfile1"> + <zipfileset src="${zipfile}" /> + </length> + <length property="length.zipfile2"> + <zipfileset src="${zipfile}" includes="bar" /> + </length> + <fail> + <condition> + <not> + <and> + <equals arg1="6" arg2="${length.zipfile1}" /> + <equals arg1="3" arg2="${length.zipfile2}" /> + </and> + </not> + </condition> + </fail> + </target> + <target name="cleanup"> <delete dir="${dir}" /> + <delete file="${zipfile}" /> </target> </project> 1.7 +35 -20 ant/src/main/org/apache/tools/ant/taskdefs/Length.java Index: Length.java =================================================================== RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/Length.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- Length.java 11 Jan 2005 15:45:22 -0000 1.6 +++ Length.java 21 Jan 2005 22:15:45 -0000 1.7 @@ -29,6 +29,7 @@ import org.apache.tools.ant.BuildException; import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.types.FileSet; +import org.apache.tools.ant.types.Resource; import org.apache.tools.ant.types.EnumeratedAttribute; import org.apache.tools.ant.util.FileUtils; @@ -147,16 +148,29 @@ } private void handleFilesets(Handler h) { - HashSet included = new HashSet(filesets.size() * 10); + HashSet included = new HashSet(filesets.size()); for (int i = 0; i < filesets.size(); i++) { FileSet fs = (FileSet) (filesets.get(i)); DirectoryScanner ds = fs.getDirectoryScanner(getProject()); - File basedir = fs.getDir(getProject()); String[] f = ds.getIncludedFiles(); for (int j = 0; j < f.length; j++) { - File file = FileUtils.getFileUtils().resolveFile(basedir, f[j]); - if (included.add(file)) { - h.handle(file); + Resource r = ds.getResource(f[j]); + if (!r.isExists()) { + log(r.getName() + " does not exist", Project.MSG_ERR); + } else if (r.isDirectory()) { + log(r.getName() + " is a directory; length unspecified", + Project.MSG_ERR); + } else { + //clone the Resource and alter path + File basedir = ds.getBasedir(); + if (basedir != null) { + r = (Resource) (r.clone()); + r.setName(FileUtils.getFileUtils().resolveFile( + basedir, r.getName()).getAbsolutePath()); + } + if (included.add(r.getName())) { + h.handle(r); + } } } } @@ -165,15 +179,6 @@ h.complete(); } - private static long getLength(File f) { - //should be an existing file - if (!(f.isFile())) { - throw new BuildException("The absolute pathname " + f - + " does not denote an existing file."); - } - return f.length(); - } - /** EnumeratedAttribute operation mode */ public static class FileMode extends EnumeratedAttribute { static final String[] MODES = new String[] {EACH, ALL}; @@ -201,7 +206,7 @@ this.ps = ps; } - protected abstract void handle(File f); + protected abstract void handle(Resource r); void complete() { ps.close(); @@ -212,11 +217,16 @@ EachHandler(PrintStream ps) { super(ps); } - protected void handle(File f) { - ps.print(f); + protected void handle(Resource r) { + ps.print(r.getName()); ps.print(" : "); //when writing to the log, we'll see what's happening: - ps.println(getLength(f)); + long size = r.getSize(); + if (size == Resource.UNKNOWN_SIZE) { + ps.println("unknown"); + } else { + ps.println(size); + } } } @@ -225,8 +235,13 @@ AllHandler(PrintStream ps) { super(ps); } - protected synchronized void handle(File f) { - length += getLength(f); + protected synchronized void handle(Resource r) { + long size = r.getSize(); + if (size == Resource.UNKNOWN_SIZE) { + log("Size unknown for " + r.getName(), Project.MSG_WARN); + } else { + length += size; + } } void complete() { ps.print(length); 1.80 +1 -1 ant/src/main/org/apache/tools/ant/DirectoryScanner.java Index: DirectoryScanner.java =================================================================== RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/DirectoryScanner.java,v retrieving revision 1.79 retrieving revision 1.80 diff -u -r1.79 -r1.80 --- DirectoryScanner.java 20 Jan 2005 14:35:15 -0000 1.79 +++ DirectoryScanner.java 21 Jan 2005 22:15:46 -0000 1.80 @@ -1349,7 +1349,7 @@ public Resource getResource(String name) { File f = FILE_UTILS.resolveFile(basedir, name); return new Resource(name, f.exists(), f.lastModified(), - f.isDirectory()); + f.isDirectory(), f.length()); } /** 1.11 +72 -26 ant/src/main/org/apache/tools/ant/types/Resource.java Index: Resource.java =================================================================== RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/types/Resource.java,v retrieving revision 1.10 retrieving revision 1.11 diff -u -r1.10 -r1.11 --- Resource.java 7 Jan 2005 15:00:46 -0000 1.10 +++ Resource.java 21 Jan 2005 22:15:46 -0000 1.11 @@ -17,28 +17,32 @@ package org.apache.tools.ant.types; /** - * describes a File or a ZipEntry + * Describes a File or a ZipEntry. * - * this class is meant to be used by classes needing to record path + * This class is meant to be used by classes needing to record path * and date/time information about a file, a zip entry or some similar - * resource (URL, archive in a version control repository, ...) + * resource (URL, archive in a version control repository, ...). * * @since Ant 1.5.2 */ public class Resource implements Cloneable, Comparable { + /** Constant unknown size */ + public static final long UNKNOWN_SIZE = -1; + private String name = null; private boolean exists = true; private long lastmodified = 0; private boolean directory = false; + private long size = UNKNOWN_SIZE; /** - * default constructor + * Default constructor. */ public Resource() { } /** - * only sets the name. + * Only sets the name. * * <p>This is a dummy, used for not existing resources.</p> * @@ -50,18 +54,20 @@ } /** - * sets the name, lastmodified flag, and exists flag + * Sets the name, lastmodified flag, and exists flag. * * @param name relative path of the resource. Expects * "/" to be used as the directory separator. - * @param exists if true, this resource exists - * @param lastmodified the last modification time of this resource + * @param exists if true, this resource exists. + * @param lastmodified the last modification time of this resource. */ public Resource(String name, boolean exists, long lastmodified) { this(name, exists, lastmodified, false); } /** + * Sets the name, lastmodified flag, exists flag, and directory flag. + * * @param name relative path of the resource. Expects * "/" to be used as the directory separator. * @param exists if true the resource exists @@ -70,14 +76,31 @@ */ public Resource(String name, boolean exists, long lastmodified, boolean directory) { + this(name, exists, lastmodified, directory, UNKNOWN_SIZE); + } + + /** + * Sets the name, lastmodified flag, exists flag, directory flag, and size. + * + * @param name relative path of the resource. Expects + * "/" to be used as the directory separator. + * @param exists if true the resource exists + * @param lastmodified the last modification time of the resource + * @param directory if true, this resource is a directory + * @param size the size of this resource. + */ + public Resource(String name, boolean exists, long lastmodified, + boolean directory, long size) { this.name = name; - this.exists = exists; - this.lastmodified = lastmodified; - this.directory = directory; + setName(name); + setExists(exists); + setLastModified(lastmodified); + setDirectory(directory); + setSize(size); } /** - * name attribute will contain the path of a file relative to the + * Name attribute will contain the path of a file relative to the * root directory of its fileset or the recorded path of a zip * entry. * @@ -86,13 +109,14 @@ * adm/resource.txt.</p> * * <p>"/" will be used as the directory separator.</p> - * @return the name of this resource + * @return the name of this resource. */ public String getName() { return name; } /** + * Set the name of this Resource. * @param name relative path of the resource. Expects * "/" to be used as the directory separator. */ @@ -101,8 +125,8 @@ } /** - * The exists attribute tells whether a file exists - * @return true if this resource exists + * The exists attribute tells whether a file exists. + * @return true if this resource exists. */ public boolean isExists() { return exists; @@ -110,33 +134,33 @@ /** * Set the exists attribute. - * @param exists if true, this resource exists + * @param exists if true, this resource exists. */ public void setExists(boolean exists) { this.exists = exists; } /** - * tells the modification time in milliseconds since 01.01.1970 of + * Tells the modification time in milliseconds since 01.01.1970 . * * @return 0 if the resource does not exist to mirror the behavior * of [EMAIL PROTECTED] java.io.File File}. */ public long getLastModified() { - return !exists || lastmodified < 0 ? 0 : lastmodified; + return !exists || lastmodified < 0 ? 0L : lastmodified; } /** * Set the last modification attribute. - * @param lastmodified the modification time in milliseconds since 01.01.1970 + * @param lastmodified the modification time in milliseconds since 01.01.1970. */ public void setLastModified(long lastmodified) { this.lastmodified = lastmodified; } /** - * tells if the resource is a directory - * @return boolean flag indicating if the resource is a directory + * Tells if the resource is a directory. + * @return boolean flag indicating if the resource is a directory. */ public boolean isDirectory() { return directory; @@ -144,14 +168,34 @@ /** * Set the directory attribute. - * @param directory if true, this resource is a directory + * @param directory if true, this resource is a directory. */ public void setDirectory(boolean directory) { this.directory = directory; } /** - * @return copy of this + * Set the size of this Resource. + * @param size the size, as a long. + * @since Ant 1.7 + */ + public void setSize(long size) { + this.size = (size > UNKNOWN_SIZE) ? size : UNKNOWN_SIZE; + } + + /** + * Get the size of this Resource. + * @return the size, as a long, 0 if the Resource does not exist (for + * compatibility with java.io.File), or UNKNOWN_SIZE if not known. + * @since Ant 1.7 + */ + public long getSize() { + return (exists) ? size : 0L; + } + + /** + * Clone this Resource. + * @return copy of this. */ public Object clone() { try { @@ -163,9 +207,10 @@ } /** - * delegates to a comparison of names. - * @param other the object to compare to - * @return true if this object's name is the same as the other object's name + * Delegates to a comparison of names. + * @param other the object to compare to. + * @return a negative integer, zero, or a positive integer as this Resource + * is less than, equal to, or greater than the specified Resource. * @since Ant 1.6 */ public int compareTo(Object other) { @@ -176,4 +221,5 @@ Resource r = (Resource) other; return getName().compareTo(r.getName()); } + } 1.27 +2 -1 ant/src/main/org/apache/tools/ant/types/ZipScanner.java Index: ZipScanner.java =================================================================== RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/types/ZipScanner.java,v retrieving revision 1.26 retrieving revision 1.27 diff -u -r1.26 -r1.27 --- ZipScanner.java 9 Mar 2004 16:48:41 -0000 1.26 +++ ZipScanner.java 21 Jan 2005 22:15:46 -0000 1.27 @@ -226,7 +226,8 @@ myentries.put(new String(entry.getName()), new Resource(entry.getName(), true, entry.getTime(), - entry.isDirectory())); + entry.isDirectory(), + entry.getSize())); } } finally { if (zf != null) { 1.5 +3 -0 ant/src/testcases/org/apache/tools/ant/taskdefs/LengthTest.java Index: LengthTest.java =================================================================== RCS file: /home/cvs/ant/src/testcases/org/apache/tools/ant/taskdefs/LengthTest.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- LengthTest.java 11 Jan 2005 15:36:21 -0000 1.4 +++ LengthTest.java 21 Jan 2005 22:15:46 -0000 1.5 @@ -71,4 +71,7 @@ executeTarget("testImmutable"); } + public void testZipFileSet() { + executeTarget("testZipFileSet"); + } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]