You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by bo...@apache.org on 2008/11/17 10:13:24 UTC

svn commit: r718205 - in /ant/core/trunk/src/main/org/apache/tools/ant: types/resources/MappedResource.java util/ResourceUtils.java

Author: bodewig
Date: Mon Nov 17 01:13:24 2008
New Revision: 718205

URL: http://svn.apache.org/viewvc?rev=718205&view=rev
Log:
use an adapter to use Appendable instead of instanceof checks

Modified:
    ant/core/trunk/src/main/org/apache/tools/ant/types/resources/MappedResource.java
    ant/core/trunk/src/main/org/apache/tools/ant/util/ResourceUtils.java

Modified: ant/core/trunk/src/main/org/apache/tools/ant/types/resources/MappedResource.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/types/resources/MappedResource.java?rev=718205&r1=718204&r2=718205&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/types/resources/MappedResource.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/types/resources/MappedResource.java Mon Nov 17 01:13:24 2008
@@ -37,6 +37,7 @@
  */
 public class MappedResource extends Resource {
     private final Resource wrapped;
+    private final boolean isAppendable;
 
     // should only be instantiated via factory, this also means we
     // don't have to think about being a reference to a different
@@ -47,6 +48,7 @@
      */
     protected MappedResource(Resource r) {
         wrapped = r;
+        isAppendable = wrapped.as(Appendable.class) != null;
     }
 
     /**
@@ -151,29 +153,28 @@
         throw new UnsupportedOperationException();
     }
 
+    public Object as(Class clazz) {
+        if (clazz == Appendable.class && isAppendable) {
+            return new Appendable() {
+                public OutputStream getAppendOutputStream() throws IOException {
+                    return ((Appendable) wrapped.as(Appendable.class))
+                        .getAppendOutputStream();
+                }
+            };
+        }
+        return super.as(clazz);
+    }
+
     public static MappedResource map(Resource r) {
         if (r instanceof FileProvider) {
             if (r instanceof Touchable) {
-                if (r instanceof Appendable) {
-                    // most probably FileResource
-                    return new AppendableTouchableFileProviderMR(r);
-                }
                 return new TouchableFileProviderMR(r);
             }
-            if (r instanceof Appendable) {
-                return new AppendableFileProviderMR(r);
-            }
             return new FileProviderMR(r);
         }
         if (r instanceof Touchable) {
-            if (r instanceof Appendable) {
-                return new AppendableTouchableMR(r);
-            }
             return new TouchableMR(r);
         }
-        if (r instanceof Appendable) {
-            return new AppendableMR(r);
-        }
         // no special interface
         return new MappedResource(r);
     }
@@ -222,25 +223,6 @@
         }
     }
 
-    private static class AppendableMR extends MappedResource
-        implements Appendable {
-        private final Appendable a;
-
-        protected AppendableMR(Resource r) {
-            super(r);
-            if (!(r instanceof Appendable)) {
-                throw new IllegalArgumentException("trying to wrap something "
-                                                   + "that is not a "
-                                                   + " Appendable");
-            }
-            a = (Appendable) r;
-        }
-
-        public OutputStream getAppendOutputStream() throws IOException {
-            return a.getAppendOutputStream();
-        }
-    }
-
     private static class TouchableFileProviderMR extends FileProviderMR
         implements Touchable {
         private final Touchable t;
@@ -263,62 +245,4 @@
         }
     }
 
-    private static class AppendableFileProviderMR extends FileProviderMR
-        implements Appendable {
-        private final Appendable a;
-
-        protected AppendableFileProviderMR(Resource r) {
-            super(r);
-            if (!(r instanceof Appendable)) {
-                throw new IllegalArgumentException("trying to wrap something "
-                                                   + "that is not a "
-                                                   + " Appendable");
-            }
-            a = (Appendable) r;
-        }
-
-        public OutputStream getAppendOutputStream() throws IOException {
-            return a.getAppendOutputStream();
-        }
-    }
-
-    private static class AppendableTouchableMR extends TouchableMR
-        implements Appendable {
-        private final Appendable a;
-
-        protected AppendableTouchableMR(Resource r) {
-            super(r);
-            if (!(r instanceof Appendable)) {
-                throw new IllegalArgumentException("trying to wrap something "
-                                                   + "that is not a "
-                                                   + " Appendable");
-            }
-            a = (Appendable) r;
-        }
-
-        public OutputStream getAppendOutputStream() throws IOException {
-            return a.getAppendOutputStream();
-        }
-    }
-
-    private static class AppendableTouchableFileProviderMR
-        extends TouchableFileProviderMR
-        implements Appendable {
-        private final Appendable a;
-
-        protected AppendableTouchableFileProviderMR(Resource r) {
-            super(r);
-            if (!(r instanceof Appendable)) {
-                throw new IllegalArgumentException("trying to wrap something "
-                                                   + "that is not a "
-                                                   + " Appendable");
-            }
-            a = (Appendable) r;
-        }
-
-        public OutputStream getAppendOutputStream() throws IOException {
-            return a.getAppendOutputStream();
-        }
-    }
-
 }
\ No newline at end of file

Modified: ant/core/trunk/src/main/org/apache/tools/ant/util/ResourceUtils.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/util/ResourceUtils.java?rev=718205&r1=718204&r2=718205&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/util/ResourceUtils.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/util/ResourceUtils.java Mon Nov 17 01:13:24 2008
@@ -625,8 +625,9 @@
     private static OutputStream getOutputStream(Resource resource, boolean append, Project project)
             throws IOException {
         if (append) {
-            if (resource instanceof Appendable) {
-                return ((Appendable) resource).getAppendOutputStream();
+            Appendable a = (Appendable) resource.as(Appendable.class);
+            if (a != null) {
+                return a.getAppendOutputStream();
             }
             project.log("Appendable OutputStream not available for non-appendable resource "
                     + resource + "; using plain OutputStream", Project.MSG_VERBOSE);