ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r292261 - in /ant/core/trunk/src/main/org/apache/tools/ant/taskdefs: Expand.java Untar.java
Date Wed, 28 Sep 2005 19:08:13 GMT
Author: bodewig
Date: Wed Sep 28 12:08:09 2005
New Revision: 292261

URL: http://svn.apache.org/viewcvs?rev=292261&view=rev
Log:
resource collection support for unzip/jar/war/tar

Modified:
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Expand.java
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Untar.java

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Expand.java
URL: http://svn.apache.org/viewcvs/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Expand.java?rev=292261&r1=292260&r2=292261&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Expand.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Expand.java Wed Sep 28 12:08:09
2005
@@ -24,6 +24,7 @@
 import java.io.InputStream;
 import java.util.Date;
 import java.util.Enumeration;
+import java.util.Iterator;
 import java.util.Vector;
 
 import org.apache.tools.ant.BuildException;
@@ -33,6 +34,10 @@
 import org.apache.tools.ant.types.FileSet;
 import org.apache.tools.ant.types.Mapper;
 import org.apache.tools.ant.types.PatternSet;
+import org.apache.tools.ant.types.Resource;
+import org.apache.tools.ant.types.ResourceCollection;
+import org.apache.tools.ant.types.resources.FileResource;
+import org.apache.tools.ant.types.resources.Union;
 import org.apache.tools.ant.types.selectors.SelectorUtils;
 import org.apache.tools.ant.util.FileNameMapper;
 import org.apache.tools.ant.util.FileUtils;
@@ -56,7 +61,7 @@
     private boolean overwrite = true;
     private Mapper mapperElement = null;
     private Vector patternsets = new Vector();
-    private Vector filesets = new Vector();
+    private Union resources = new Union();
 
     private static final String NATIVE_ENCODING = "native-encoding";
 
@@ -76,8 +81,8 @@
             log("!! expand is deprecated. Use unzip instead. !!");
         }
 
-        if (source == null && filesets.size() == 0) {
-            throw new BuildException("src attribute and/or filesets must be "
+        if (source == null && resources.size() == 0) {
+            throw new BuildException("src attribute and/or resources must be "
                                      + "specified");
         }
 
@@ -98,19 +103,19 @@
                 expandFile(FILE_UTILS, source, dest);
             }
         }
-        if (filesets.size() > 0) {
-            for (int j = 0, size = filesets.size(); j < size; j++) {
-                FileSet fs = (FileSet) filesets.elementAt(j);
-                DirectoryScanner ds = fs.getDirectoryScanner(getProject());
-                File fromDir = fs.getDir(getProject());
-
-                String[] files = ds.getIncludedFiles();
-                for (int i = 0; i < files.length; ++i) {
-                    File file = new File(fromDir, files[i]);
-                    expandFile(FILE_UTILS, file, dest);
-                }
-            }
-        }
+	Iterator iter = resources.iterator();
+	while (iter.hasNext()) {
+	    Resource r = (Resource) iter.next();
+	    if (!r.isExists()) {
+		continue;
+	    }
+
+	    if (r instanceof FileResource) {
+		expandFile(FILE_UTILS, ((FileResource) r).getFile(), dest);
+	    } else {
+		expandResource(r, dest);
+	    }
+	}
     }
 
     /**
@@ -144,6 +149,17 @@
     }
 
     /**
+     * This method is to be overridden by extending unarchival tasks.
+     *
+     * @param r         the source resource
+     * @param dir       the destination directory
+     */
+    protected void expandResource(Resource srcR, File dir) {
+	throw new BuildException("only filesystem based resources are"
+				 + " supported by this task.");
+    }
+
+    /**
      * get a mapper for a file
      * @return a filenamemapper for a file
      */
@@ -322,7 +338,16 @@
      * @param set a file set
      */
     public void addFileset(FileSet set) {
-        filesets.addElement(set);
+	add(set);
+    }
+
+    /**
+     * Add a resource collection.
+     * @param rc a resource collection.
+     * @since Ant 1.7
+     */
+    public void add(ResourceCollection rc) {
+	resources.add(rc);
     }
 
     /**

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Untar.java
URL: http://svn.apache.org/viewcvs/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Untar.java?rev=292261&r1=292260&r2=292261&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Untar.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Untar.java Wed Sep 28 12:08:09 2005
@@ -27,6 +27,7 @@
 import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.Project;
 import org.apache.tools.ant.types.EnumeratedAttribute;
+import org.apache.tools.ant.types.Resource;
 import org.apache.tools.ant.util.FileNameMapper;
 import org.apache.tools.ant.util.FileUtils;
 import org.apache.tools.bzip2.CBZip2InputStream;
@@ -91,34 +92,62 @@
      */
     protected void expandFile(FileUtils fileUtils, File srcF, File dir) {
         FileInputStream fis = null;
-        TarInputStream tis = null;
         try {
-            log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO);
-            fis = new FileInputStream(srcF);
-            tis = new TarInputStream(
-                compression.decompress(srcF, new BufferedInputStream(fis)));
-            TarEntry te = null;
-            FileNameMapper mapper = getMapper();
-            while ((te = tis.getNextEntry()) != null) {
-                extractFile(fileUtils, srcF, dir, tis,
-                            te.getName(), te.getModTime(),
-                            te.isDirectory(), mapper);
-            }
-            log("expand complete", Project.MSG_VERBOSE);
-
+	    fis = new FileInputStream(srcF);
+	    expandStream(srcF.getPath(), fis, dir);
         } catch (IOException ioe) {
             throw new BuildException("Error while expanding " + srcF.getPath(),
                                      ioe, getLocation());
         } finally {
-            FileUtils.close(tis);
-            if (tis == null) {
-                FileUtils.close(fis);
-            }
-            
+	    FileUtils.close(fis);
         }
     }
 
     /**
+     * This method is to be overridden by extending unarchival tasks.
+     *
+     * @param r         the source resource
+     * @param dir       the destination directory
+     * @since Ant 1.7
+     */
+    protected void expandResource(Resource srcR, File dir) {
+	InputStream i = null;
+        try {
+	    i = srcR.getInputStream();
+	    expandStream(srcR.getName(), i, dir);
+        } catch (IOException ioe) {
+            throw new BuildException("Error while expanding " + srcR.getName(),
+                                     ioe, getLocation());
+        } finally {
+	    FileUtils.close(i);
+        }
+    }
+
+    /**
+     * @since Ant 1.7
+     */
+    private void expandStream(String name, InputStream stream, File dir)
+	throws IOException {
+	TarInputStream tis = null;
+        try {
+	    tis = 
+		new TarInputStream(compression.decompress(name,
+							  new BufferedInputStream(stream)));
+	    log("Expanding: " + name + " into " + dir, Project.MSG_INFO);
+	    TarEntry te = null;
+	    FileNameMapper mapper = getMapper();
+	    while ((te = tis.getNextEntry()) != null) {
+		extractFile(FileUtils.getFileUtils(), null, dir, tis,
+			    te.getName(), te.getModTime(),
+			    te.isDirectory(), mapper);
+	    }
+	    log("expand complete", Project.MSG_VERBOSE);
+        } finally {
+            FileUtils.close(tis);
+	}
+    }
+
+    /**
      * Valid Modes for Compression attribute to Untar Task
      *
      */
@@ -168,7 +197,7 @@
          *  @exception BuildException thrown if bzip stream does not
          *     start with expected magic values
          */
-        private InputStream decompress(final File file,
+        private InputStream decompress(final String name,
                                        final InputStream istream)
             throws IOException, BuildException {
             final String v = getValue();
@@ -180,7 +209,7 @@
                     for (int i = 0; i < magic.length; i++) {
                         if (istream.read() != magic[i]) {
                             throw new BuildException(
-                                "Invalid bz2 file." + file.toString());
+                                "Invalid bz2 file." + name);
                         }
                     }
                     return new CBZip2InputStream(istream);



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Mime
View raw message