Browse Source

use an adapter to use Appendable instead of instanceof checks

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@718205 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 17 years ago
parent
commit
a22e945b3c
2 changed files with 17 additions and 92 deletions
  1. +14
    -90
      src/main/org/apache/tools/ant/types/resources/MappedResource.java
  2. +3
    -2
      src/main/org/apache/tools/ant/util/ResourceUtils.java

+ 14
- 90
src/main/org/apache/tools/ant/types/resources/MappedResource.java View File

@@ -37,6 +37,7 @@ import org.apache.tools.ant.types.Resource;
*/ */
public class MappedResource extends Resource { public class MappedResource extends Resource {
private final Resource wrapped; private final Resource wrapped;
private final boolean isAppendable;


// should only be instantiated via factory, this also means we // should only be instantiated via factory, this also means we
// don't have to think about being a reference to a different // don't have to think about being a reference to a different
@@ -47,6 +48,7 @@ public class MappedResource extends Resource {
*/ */
protected MappedResource(Resource r) { protected MappedResource(Resource r) {
wrapped = r; wrapped = r;
isAppendable = wrapped.as(Appendable.class) != null;
} }


/** /**
@@ -151,29 +153,28 @@ public class MappedResource extends Resource {
throw new UnsupportedOperationException(); 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) { public static MappedResource map(Resource r) {
if (r instanceof FileProvider) { if (r instanceof FileProvider) {
if (r instanceof Touchable) { if (r instanceof Touchable) {
if (r instanceof Appendable) {
// most probably FileResource
return new AppendableTouchableFileProviderMR(r);
}
return new TouchableFileProviderMR(r); return new TouchableFileProviderMR(r);
} }
if (r instanceof Appendable) {
return new AppendableFileProviderMR(r);
}
return new FileProviderMR(r); return new FileProviderMR(r);
} }
if (r instanceof Touchable) { if (r instanceof Touchable) {
if (r instanceof Appendable) {
return new AppendableTouchableMR(r);
}
return new TouchableMR(r); return new TouchableMR(r);
} }
if (r instanceof Appendable) {
return new AppendableMR(r);
}
// no special interface // no special interface
return new MappedResource(r); return new MappedResource(r);
} }
@@ -222,25 +223,6 @@ public class MappedResource extends Resource {
} }
} }


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 private static class TouchableFileProviderMR extends FileProviderMR
implements Touchable { implements Touchable {
private final Touchable t; private final Touchable t;
@@ -263,62 +245,4 @@ public class MappedResource extends Resource {
} }
} }


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();
}
}

} }

+ 3
- 2
src/main/org/apache/tools/ant/util/ResourceUtils.java View File

@@ -625,8 +625,9 @@ public class ResourceUtils {
private static OutputStream getOutputStream(Resource resource, boolean append, Project project) private static OutputStream getOutputStream(Resource resource, boolean append, Project project)
throws IOException { throws IOException {
if (append) { 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 " project.log("Appendable OutputStream not available for non-appendable resource "
+ resource + "; using plain OutputStream", Project.MSG_VERBOSE); + resource + "; using plain OutputStream", Project.MSG_VERBOSE);


Loading…
Cancel
Save