Please find attached a patch to FileSet.java which allows creation of a
FileSet with one entry, from a single file attribute, as follows :-

  <fileset file="/tmp/file.txt"/>

which has the same effect as

   <fileset dir="/tmp">
     <include name="file.txt"/>
   </fileset>

but does not require both the directory and the filename to be known.

If the "file" attribute is specified, then specifying any of the other
fileset attributes, or specifying a nested element, throws an exception.

This addition would be useful, for example, if a build process builds
several specified files in subprojects and at the end wanted to zip them
all together, eg

     <property name="jar1" value="subproject1/release/proj1.jar"/>
     <property name="jar2" value="subproject2/release/proj2.jar"/>
     <property name="jar3" value="subproject3/release/proj3.jar"/>

      ...

     <zip file="release.zip">
       <fileset file="${jar1}"/>
       <fileset file="${jar2}"/>
       <fileset file="${jar3}"/>
     </zip>

At present the only way I can see to do this is via copying to a
temporary directory or specifying the files using two properties
(directory and name); either technique feels a bit clumsy.

Many thanks.
--- FileSet.java.orig   Thu Dec 13 10:31:08 2001
+++ FileSet.java        Fri Dec 14 17:45:21 2001
@@ -82,6 +82,7 @@
     private File dir;
     private boolean useDefaultExcludes = true;
     private boolean isCaseSensitive = true;
+    private boolean isSingleFile = false;
 
     public FileSet() {
         super();
@@ -115,6 +116,9 @@
     }
 
     public void setDir(File dir) throws BuildException {
+        if (isSingleFile) {
+            throw noDirWithSingleFileAllowed();
+        }
         if (isReference()) {
             throw tooManyAttributes();
         }
@@ -129,7 +133,66 @@
         return dir;
     }
 
+   /**
+    * Specify a single file for the FileSet.  Setting this parameter disallows
+    * specifying any nested elements, or other attributes (other than those
+    * inherited from DataType).
+    *
+    * @param file the file which will be the only file in the FileSet
+    * @throws BuildException
+    */
+   public void setFile(File file) throws BuildException {
+        if (dir != null) {
+            throw noDirWithSingleFileAllowed();
+        }
+        if (defaultPatterns.hasPatterns()) {
+            throw noAttributesWithSingleFileAllowed();
+        }
+        if (!additionalPatterns.isEmpty()) {
+            throw noChildrenWithSingleFileAllowed();
+        }
+        setDir(file.getParentFile());
+        createInclude().setName(file.getName());
+        isSingleFile = true;
+    }
+
+   /**
+    * Return true if the setFile method was used to create a single file 
FileSet.
+    *
+    * @return a boolean
+    */
+   public boolean isSingleFile() {
+        return isSingleFile;
+    }
+
+    private BuildException noDirWithSingleFileAllowed() {
+        throw new BuildException("You must not specify the dir attribute when 
using the file attribute");
+    }
+
+   /**
+    * Create an exception inidicating an invalid use of a nested element
+    * together with the "file" attribute.
+    *
+    * @return a BuildException
+    */
+    protected BuildException noChildrenWithSingleFileAllowed() {
+        throw new BuildException("You must not specify nested elements when 
using the file attribute");
+    }
+
+   /**
+    * Create an exception inidicating an invalid use of another attribute
+    * together with the "file" attribute.
+    *
+    * @return a BuildException
+    */
+    protected BuildException noAttributesWithSingleFileAllowed() {
+        throw new BuildException("You must not specify these attributes when 
using the file attribute");
+    }
+
     public PatternSet createPatternSet() {
+        if (isSingleFile) {
+            throw noChildrenWithSingleFileAllowed();
+        }
         if (isReference()) {
             throw noChildrenAllowed();
         }
@@ -142,6 +205,9 @@
      * add a name entry on the include list
      */
     public PatternSet.NameEntry createInclude() {
+        if (isSingleFile) {
+            throw noChildrenWithSingleFileAllowed();
+        }
         if (isReference()) {
             throw noChildrenAllowed();
         }
@@ -152,6 +218,9 @@
      * add a name entry on the include files list
      */
     public PatternSet.NameEntry createIncludesFile() {
+        if (isSingleFile) {
+            throw noChildrenWithSingleFileAllowed();
+        }
         if (isReference()) {
             throw noChildrenAllowed();
         }
@@ -162,6 +231,9 @@
      * add a name entry on the exclude list
      */
     public PatternSet.NameEntry createExclude() {
+        if (isSingleFile) {
+            throw noChildrenWithSingleFileAllowed();
+        }
         if (isReference()) {
             throw noChildrenAllowed();
         }
@@ -172,6 +244,9 @@
      * add a name entry on the include files list
      */
     public PatternSet.NameEntry createExcludesFile() {
+        if (isSingleFile) {
+            throw noChildrenWithSingleFileAllowed();
+        }
         if (isReference()) {
             throw noChildrenAllowed();
         }
@@ -185,6 +260,9 @@
      * @param includes the string containing the include patterns
      */
     public void setIncludes(String includes) {
+        if (isSingleFile) {
+            throw noAttributesWithSingleFileAllowed();
+        }
         if (isReference()) {
             throw tooManyAttributes();
         }
@@ -199,6 +277,9 @@
      * @param excludes the string containing the exclude patterns
      */
     public void setExcludes(String excludes) {
+        if (isSingleFile) {
+            throw noAttributesWithSingleFileAllowed();
+        }
         if (isReference()) {
             throw tooManyAttributes();
         }
@@ -211,26 +292,32 @@
      *
      * @param incl The file to fetch the include patterns from.  
      */
-     public void setIncludesfile(File incl) throws BuildException {
-         if (isReference()) {
-             throw tooManyAttributes();
-         }
+    public void setIncludesfile(File incl) throws BuildException {
+        if (isSingleFile) {
+            throw noAttributesWithSingleFileAllowed();
+        }
+        if (isReference()) {
+            throw tooManyAttributes();
+        }
 
-         defaultPatterns.setIncludesfile(incl);
-     }
+        defaultPatterns.setIncludesfile(incl);
+    }
 
     /**
      * Sets the name of the file containing the includes patterns.
      *
      * @param excl The file to fetch the exclude patterns from.  
      */
-     public void setExcludesfile(File excl) throws BuildException {
-         if (isReference()) {
-             throw tooManyAttributes();
-         }
+    public void setExcludesfile(File excl) throws BuildException {
+        if (isSingleFile) {
+            throw noAttributesWithSingleFileAllowed();
+        }
+        if (isReference()) {
+            throw tooManyAttributes();
+        }
 
-         defaultPatterns.setExcludesfile(excl);
-     }
+        defaultPatterns.setExcludesfile(excl);
+    }
 
     /**
      * Sets whether default exclusions should be used or not.
@@ -240,6 +327,9 @@
      *                           shouldn't be used.
      */
     public void setDefaultexcludes(boolean useDefaultExcludes) {
+        if (isSingleFile) {
+            throw noAttributesWithSingleFileAllowed();
+        }
         if (isReference()) {
             throw tooManyAttributes();
         }

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to