@@ -32,7 +32,12 @@ import org.apache.tools.ant.Task;
import org.apache.tools.ant.filters.util.ChainReaderHelper;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Reference;
import org.apache.tools.ant.types.Resource;
import org.apache.tools.ant.types.ResourceCollection;
import org.apache.tools.ant.types.FilterChain;
import org.apache.tools.ant.types.resources.FileResource;
import org.apache.tools.ant.types.resources.JavaResource;
import org.apache.tools.ant.util.FileUtils;
/**
* Load a file's contents as Ant properties.
@@ -43,19 +48,9 @@ import org.apache.tools.ant.types.FilterChain;
public class LoadProperties extends Task {
/**
* Source file
* Source resource.
*/
private File srcFile = null;
/**
* Resource
*/
private String resource = null;
/**
* Classpath
*/
private Path classpath = null;
private Resource src = null;
/**
* Holds filterchains
@@ -73,7 +68,7 @@ public class LoadProperties extends Task {
* @param srcFile The new SrcFile value
*/
public final void setSrcFile(final File srcFile) {
this.srcFile = srcFile ;
addConfigured(new FileResource(srcFile)) ;
}
/**
@@ -82,7 +77,8 @@ public class LoadProperties extends Task {
* @param resource resource on classpath
*/
public void setResource(String resource) {
this.resource = resource;
assertSrcIsJavaResource();
((JavaResource) src).setName(resource);
}
/**
@@ -105,11 +101,8 @@ public class LoadProperties extends Task {
* @param classpath to add to any existing classpath
*/
public void setClasspath(Path classpath) {
if (this.classpath == null) {
this.classpath = classpath;
} else {
this.classpath.append(classpath);
}
assertSrcIsJavaResource();
((JavaResource) src).setClasspath(classpath);
}
/**
@@ -117,10 +110,8 @@ public class LoadProperties extends Task {
* @return The classpath to be configured
*/
public Path createClasspath() {
if (this.classpath == null) {
this.classpath = new Path(getProject());
}
return this.classpath.createPath();
assertSrcIsJavaResource();
return ((JavaResource) src).createClasspath();
}
/**
@@ -129,7 +120,8 @@ public class LoadProperties extends Task {
* @param r The reference value
*/
public void setClasspathRef(Reference r) {
createClasspath().setRefid(r);
assertSrcIsJavaResource();
((JavaResource) src).setClasspathRef(r);
}
/**
@@ -137,7 +129,8 @@ public class LoadProperties extends Task {
* @return The classpath
*/
public Path getClasspath() {
return classpath;
assertSrcIsJavaResource();
return ((JavaResource) src).getClasspath();
}
/**
@@ -147,48 +140,24 @@ public class LoadProperties extends Task {
*/
public final void execute() throws BuildException {
//validation
if (srcFile == null && resource == null) {
throw new BuildException(
"One of \"srcfile\" or \"resource\" is required.");
if (src == null) {
throw new BuildException("A source resource is required.");
}
BufferedInputStream bis = null;
if (srcFile != null) {
if (!srcFile.exists()) {
throw new BuildException("Source file does not exist :"+srcFile);
}
if (!srcFile.isFile()) {
throw new BuildException("Source file is not a file :"+srcFile);
}
try {
bis = new BufferedInputStream(new FileInputStream(srcFile));
} catch (IOException eyeOhEx) {
throw new BuildException(eyeOhEx);
}
} else {
ClassLoader cL = (classpath != null)
? getProject().createClassLoader(classpath)
: LoadProperties.class.getClassLoader();
InputStream is = (cL == null)
? ClassLoader.getSystemResourceAsStream(resource)
: cL.getResourceAsStream(resource);
if (is != null) {
bis = new BufferedInputStream(is);
} else { // do it like Property
log("Unable to find resource " + resource, Project.MSG_WARN);
if (!src.isExists()) {
if (src instanceof JavaResource) {
// dreaded backwards compatibility
log("Unable to find resource " + src, Project.MSG_WARN);
return;
}
throw new BuildException("Source resource does not exist: " + src);
}
BufferedInputStream bis = null;
Reader instream = null;
ByteArrayInputStream tis = null;
try {
bis = new BufferedInputStream(src.getInputStream());
if (encoding == null) {
instream = new InputStreamReader(bis);
} else {
@@ -224,23 +193,9 @@ public class LoadProperties extends Task {
} catch (final IOException ioe) {
final String message = "Unable to load file: " + ioe.toString();
throw new BuildException(message, ioe, getLocation());
} catch (final BuildException be) {
throw be;
} finally {
try {
if (bis != null) {
bis.close();
}
} catch (IOException ioex) {
//ignore
}
try {
if (tis != null) {
tis.close();
}
} catch (IOException ioex) {
//ignore
}
FileUtils.close(bis);
FileUtils.close(tis);
}
}
@@ -252,4 +207,28 @@ public class LoadProperties extends Task {
filterChains.addElement(filter);
}
/**
* Set the source resource.
* @param a the resource to load as a single element Resource collection.
* @since Ant 1.7
*/
public void addConfigured(ResourceCollection a) {
if (src != null) {
throw new BuildException("only a single source is supported");
}
if (a.size() != 1) {
throw new BuildException("only single argument resource collections"
+ " are supported");
}
src = (Resource) a.iterator().next();
}
private void assertSrcIsJavaResource() {
if (src == null) {
src = new JavaResource();
src.setProject(getProject());
} else if (!(src instanceof JavaResource)) {
throw new BuildException("expected a java resource as source");
}
}
}