ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r1154230 - in /ant/antlibs/compress/trunk/src/main/org/apache/ant/compress: resources/ taskdefs/ util/
Date Fri, 05 Aug 2011 14:12:26 GMT
Author: bodewig
Date: Fri Aug  5 14:12:26 2011
New Revision: 1154230

URL: http://svn.apache.org/viewvc?rev=1154230&view=rev
Log:
make code a tiny bit less redundant

Added:
    ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/util/StreamHelper.java   (with
props)
Modified:
    ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/resources/CommonsCompressArchiveResource.java
    ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/resources/CommonsCompressArchiveScanner.java
    ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/ArchiveBase.java
    ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/ExpandBase.java
    ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/PackBase.java
    ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/UnpackBase.java

Modified: ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/resources/CommonsCompressArchiveResource.java
URL: http://svn.apache.org/viewvc/ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/resources/CommonsCompressArchiveResource.java?rev=1154230&r1=1154229&r2=1154230&view=diff
==============================================================================
--- ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/resources/CommonsCompressArchiveResource.java
(original)
+++ ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/resources/CommonsCompressArchiveResource.java
Fri Aug  5 14:12:26 2011
@@ -28,11 +28,10 @@ import org.apache.tools.ant.BuildExcepti
 import org.apache.tools.ant.types.Reference;
 import org.apache.tools.ant.types.Resource;
 import org.apache.tools.ant.types.resources.ArchiveResource;
-import org.apache.tools.ant.types.resources.FileProvider;
 import org.apache.tools.ant.util.FileUtils;
-import org.apache.ant.compress.util.FileAwareArchiveStreamFactory;
 import org.apache.ant.compress.util.ArchiveStreamFactory;
 import org.apache.ant.compress.util.EntryHelper;
+import org.apache.ant.compress.util.StreamHelper;
 import org.apache.commons.compress.archivers.ArchiveEntry;
 import org.apache.commons.compress.archivers.ArchiveInputStream;
 
@@ -220,14 +219,9 @@ public abstract class CommonsCompressArc
 
     private ArchiveInputStream getStream() throws IOException {
         Resource archive = getArchive();
-        if (factory instanceof FileAwareArchiveStreamFactory
-            && archive.as(FileProvider.class) != null) {
-            FileProvider p = (FileProvider) archive.as(FileProvider.class);
-            FileAwareArchiveStreamFactory f =
-                (FileAwareArchiveStreamFactory) factory;
-            return f.getArchiveInputStream(p.getFile(), getEncoding());
-        }
-        return 
+        ArchiveInputStream s = StreamHelper.getInputStream(factory, archive,
+                                                           getEncoding());
+        return s != null ? s :
             factory.getArchiveStream(new BufferedInputStream(archive
                                                              .getInputStream()),
                                      getEncoding());

Modified: ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/resources/CommonsCompressArchiveScanner.java
URL: http://svn.apache.org/viewvc/ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/resources/CommonsCompressArchiveScanner.java?rev=1154230&r1=1154229&r2=1154230&view=diff
==============================================================================
--- ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/resources/CommonsCompressArchiveScanner.java
(original)
+++ ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/resources/CommonsCompressArchiveScanner.java
Fri Aug  5 14:12:26 2011
@@ -22,14 +22,13 @@ import java.io.BufferedInputStream;
 import java.io.IOException;
 import java.util.Map;
 
+import org.apache.ant.compress.util.StreamHelper;
 import org.apache.ant.compress.util.ArchiveStreamFactory;
-import org.apache.ant.compress.util.FileAwareArchiveStreamFactory;
 import org.apache.ant.compress.util.Messages;
 import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.Project;
 import org.apache.tools.ant.types.ArchiveScanner;
 import org.apache.tools.ant.types.Resource;
-import org.apache.tools.ant.types.resources.FileProvider;
 import org.apache.tools.ant.util.FileUtils;
 import org.apache.commons.compress.archivers.ArchiveEntry;
 import org.apache.commons.compress.archivers.ArchiveInputStream;
@@ -93,14 +92,8 @@ public class CommonsCompressArchiveScann
 
         try {
             try {
-                if (factory instanceof FileAwareArchiveStreamFactory
-                    && src.as(FileProvider.class) != null) {
-                    FileProvider p =
-                        (FileProvider) src.as(FileProvider.class);
-                    FileAwareArchiveStreamFactory f =
-                        (FileAwareArchiveStreamFactory) factory;
-                    ai = f.getArchiveInputStream(p.getFile(), encoding);
-                } else {
+                ai = StreamHelper.getInputStream(factory, src, encoding);
+                if (ai == null) {
                     ai =
                         factory.getArchiveStream(new BufferedInputStream(src
                                                                          .getInputStream()),

Modified: ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/ArchiveBase.java
URL: http://svn.apache.org/viewvc/ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/ArchiveBase.java?rev=1154230&r1=1154229&r2=1154230&view=diff
==============================================================================
--- ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/ArchiveBase.java
(original)
+++ ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/ArchiveBase.java
Fri Aug  5 14:12:26 2011
@@ -42,7 +42,7 @@ import org.apache.ant.compress.resources
 import org.apache.ant.compress.resources.ZipResource;
 import org.apache.ant.compress.util.ArchiveStreamFactory;
 import org.apache.ant.compress.util.EntryHelper;
-import org.apache.ant.compress.util.FileAwareArchiveStreamFactory;
+import org.apache.ant.compress.util.StreamHelper;
 
 import org.apache.commons.compress.archivers.ArchiveEntry;
 import org.apache.commons.compress.archivers.ArchiveOutputStream;
@@ -60,7 +60,6 @@ import org.apache.tools.ant.types.Enumer
 import org.apache.tools.ant.types.Resource;
 import org.apache.tools.ant.types.ResourceCollection;
 import org.apache.tools.ant.types.resources.ArchiveResource;
-import org.apache.tools.ant.types.resources.FileProvider;
 import org.apache.tools.ant.types.resources.FileResource;
 import org.apache.tools.ant.types.resources.MappedResource;
 import org.apache.tools.ant.types.resources.Resources;
@@ -517,14 +516,8 @@ public abstract class ArchiveBase extend
         try {
             String enc = Expand.NATIVE_ENCODING.equals(getEncoding())
                 ? null : getEncoding();
-            if (factory instanceof FileAwareArchiveStreamFactory
-                && getDest().as(FileProvider.class) != null) {
-                FileProvider p =
-                    (FileProvider) getDest().as(FileProvider.class);
-                FileAwareArchiveStreamFactory f =
-                    (FileAwareArchiveStreamFactory) factory;
-                out = f.getArchiveOutputStream(p.getFile(), enc);
-            } else {
+            out = StreamHelper.getOutputStream(factory, getDest(), enc);
+            if (out == null) {
                 out =
                     factory.getArchiveStream(new BufferedOutputStream(getDest()
                                                                       .getOutputStream()),

Modified: ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/ExpandBase.java
URL: http://svn.apache.org/viewvc/ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/ExpandBase.java?rev=1154230&r1=1154229&r2=1154230&view=diff
==============================================================================
--- ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/ExpandBase.java (original)
+++ ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/ExpandBase.java Fri
Aug  5 14:12:26 2011
@@ -27,13 +27,14 @@ import java.util.Date;
 
 import org.apache.ant.compress.util.ArchiveStreamFactory;
 import org.apache.ant.compress.util.EntryHelper;
-import org.apache.ant.compress.util.FileAwareArchiveStreamFactory;
 import org.apache.ant.compress.util.Messages;
+import org.apache.ant.compress.util.StreamHelper;
 
 import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.Project;
 import org.apache.tools.ant.taskdefs.Expand;
 import org.apache.tools.ant.types.Resource;
+import org.apache.tools.ant.types.resources.FileResource;
 import org.apache.tools.ant.util.FileNameMapper;
 import org.apache.tools.ant.util.FileUtils;
 
@@ -105,10 +106,9 @@ public abstract class ExpandBase extends
         }
         InputStream is = null;
         try {
-            if (factory instanceof FileAwareArchiveStreamFactory) {
-                FileAwareArchiveStreamFactory f =
-                    (FileAwareArchiveStreamFactory) factory;
-                is =  f.getArchiveInputStream(srcF, getEncoding());
+            is = StreamHelper.getInputStream(factory, new FileResource(srcF),
+                                             getEncoding());
+            if (is != null) {
                 expandArchiveStream(srcF.getPath(), (ArchiveInputStream) is,
                                     dir);
             } else {

Modified: ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/PackBase.java
URL: http://svn.apache.org/viewvc/ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/PackBase.java?rev=1154230&r1=1154229&r2=1154230&view=diff
==============================================================================
--- ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/PackBase.java (original)
+++ ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/PackBase.java Fri
Aug  5 14:12:26 2011
@@ -27,7 +27,7 @@ import java.util.Iterator;
 
 import org.apache.ant.compress.resources.CommonsCompressCompressorResource;
 import org.apache.ant.compress.util.CompressorStreamFactory;
-import org.apache.ant.compress.util.FileAwareCompressorStreamFactory;
+import org.apache.ant.compress.util.StreamHelper;
 import org.apache.commons.compress.utils.IOUtils;
 import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.Task;
@@ -187,13 +187,8 @@ public abstract class PackBase extends T
         OutputStream out = null;
         try {
             in = src.getInputStream();
-            if (factory instanceof FileAwareCompressorStreamFactory
-                && dest.as(FileProvider.class) != null) {
-                FileProvider p = (FileProvider) dest.as(FileProvider.class);
-                FileAwareCompressorStreamFactory f =
-                    (FileAwareCompressorStreamFactory) factory;
-                out =  f.getCompressorOutputStream(p.getFile());
-            } else {
+            out = StreamHelper.getOutputStream(factory, dest);
+            if (out == null) {
                 out =
                     factory.getCompressorStream(new BufferedOutputStream(dest.getOutputStream()));
             }

Modified: ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/UnpackBase.java
URL: http://svn.apache.org/viewvc/ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/UnpackBase.java?rev=1154230&r1=1154229&r2=1154230&view=diff
==============================================================================
--- ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/UnpackBase.java (original)
+++ ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/UnpackBase.java Fri
Aug  5 14:12:26 2011
@@ -24,10 +24,9 @@ import java.io.IOException;
 import java.io.InputStream;
 
 import org.apache.ant.compress.util.CompressorStreamFactory;
-import org.apache.ant.compress.util.FileAwareCompressorStreamFactory;
+import org.apache.ant.compress.util.StreamHelper;
 import org.apache.commons.compress.compressors.CompressorInputStream;
 import org.apache.tools.ant.BuildException;
-import org.apache.tools.ant.types.resources.FileProvider;
 import org.apache.tools.ant.util.FileUtils;
 import org.apache.tools.ant.taskdefs.Unpack;
 
@@ -67,14 +66,8 @@ public abstract class UnpackBase extends
             InputStream fis = null;
             try {
                 out = new FileOutputStream(dest);
-                if (factory instanceof FileAwareCompressorStreamFactory
-                    && srcResource.as(FileProvider.class) != null) {
-                    FileProvider p =
-                        (FileProvider) srcResource.as(FileProvider.class);
-                    FileAwareCompressorStreamFactory f =
-                        (FileAwareCompressorStreamFactory) factory;
-                    zIn =  f.getCompressorInputStream(p.getFile());
-                } else {
+                zIn = StreamHelper.getInputStream(factory, srcResource);
+                if (zIn == null) {
                     fis = srcResource.getInputStream();
                     zIn = factory.getCompressorStream(new BufferedInputStream(fis));
                 }

Added: ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/util/StreamHelper.java
URL: http://svn.apache.org/viewvc/ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/util/StreamHelper.java?rev=1154230&view=auto
==============================================================================
--- ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/util/StreamHelper.java (added)
+++ ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/util/StreamHelper.java Fri
Aug  5 14:12:26 2011
@@ -0,0 +1,127 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+
+package org.apache.ant.compress.util;
+
+import java.io.IOException;
+
+import org.apache.commons.compress.archivers.ArchiveInputStream;
+import org.apache.commons.compress.archivers.ArchiveOutputStream;
+import org.apache.commons.compress.compressors.CompressorInputStream;
+import org.apache.commons.compress.compressors.CompressorOutputStream;
+import org.apache.tools.ant.types.Resource;
+import org.apache.tools.ant.types.resources.FileProvider;
+
+/**
+ * Helper methods that deal with FileAware*Factory and Resources that
+ * may or may not provide files.
+ */
+public class StreamHelper {
+    private StreamHelper() { }
+
+    private static boolean isFileCombination(ArchiveStreamFactory factory,
+                                             Resource r) {
+        return factory instanceof FileAwareArchiveStreamFactory
+            && r.as(FileProvider.class) != null;
+    }
+
+    private static boolean isFileCombination(CompressorStreamFactory factory,
+                                             Resource r) {
+        return factory instanceof FileAwareCompressorStreamFactory
+            && r.as(FileProvider.class) != null;
+    }
+
+    /**
+     * If the factory knows about files and the resource can provide
+     * one, returns an ArchiveInputStream for it, otherwise returns
+     * null.
+     * @param r the resource to read from
+     * @param encoding the encoding of the entry names
+     */
+    public static
+        ArchiveInputStream getInputStream(ArchiveStreamFactory factory,
+                                          Resource r, String encoding)
+        throws IOException {
+        if (isFileCombination(factory, r)) {
+            FileProvider p = (FileProvider) r.as(FileProvider.class);
+            FileAwareArchiveStreamFactory f =
+                (FileAwareArchiveStreamFactory) factory;
+            return f.getArchiveInputStream(p.getFile(), encoding);
+        }
+        return null;
+    }
+
+    /**
+     * If the factory knows about files and the resource can provide
+     * one, returns an ArchiveOutputStream for it, otherwise returns
+     * null.
+     * @param r the resource to write to
+     * @param encoding the encoding of the entry names
+     */
+    public static
+        ArchiveOutputStream getOutputStream(ArchiveStreamFactory factory,
+                                            Resource r, String encoding)
+        throws IOException {
+        if (isFileCombination(factory, r)) {
+            FileProvider p = (FileProvider) r.as(FileProvider.class);
+            FileAwareArchiveStreamFactory f =
+                (FileAwareArchiveStreamFactory) factory;
+            return f.getArchiveOutputStream(p.getFile(), encoding);
+        }
+        return null;
+    }
+
+    /**
+     * If the factory knows about files and the resource can provide
+     * one, returns an CompressorInputStream for it, otherwise returns
+     * null.
+     * @param r the resource to read from
+     * @param encoding the encoding of the entry names
+     */
+    public static
+        CompressorInputStream getInputStream(CompressorStreamFactory factory,
+                                             Resource r)
+        throws IOException {
+        if (isFileCombination(factory, r)) {
+            FileProvider p = (FileProvider) r.as(FileProvider.class);
+            FileAwareCompressorStreamFactory f =
+                (FileAwareCompressorStreamFactory) factory;
+            return f.getCompressorInputStream(p.getFile());
+        }
+        return null;
+    }
+
+    /**
+     * If the factory knows about files and the resource can provide
+     * one, returns an CompressorOutputStream for it, otherwise returns
+     * null.
+     * @param r the resource to write to
+     */
+    public static
+        CompressorOutputStream getOutputStream(CompressorStreamFactory factory,
+                                               Resource r)
+        throws IOException {
+        if (isFileCombination(factory, r)) {
+            FileProvider p = (FileProvider) r.as(FileProvider.class);
+            FileAwareCompressorStreamFactory f =
+                (FileAwareCompressorStreamFactory) factory;
+            return f.getCompressorOutputStream(p.getFile());
+        }
+        return null;
+    }
+}

Propchange: ant/antlibs/compress/trunk/src/main/org/apache/ant/compress/util/StreamHelper.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message