From 861a4490644853d4b9bff9b0d0b871d7e88b0784 Mon Sep 17 00:00:00 2001
From: Stefan Bodewig
-The LoadProperties task supports nested
-FilterChains, as well as a nested <classpath>
-element for use with the resource attribute.
+
+
The specified resource will be used as src. Since Ant 1.7
+ +for use with the resource attribute.
<loadproperties srcFile="file.properties"/>+or +
+ <loadproperties> + <file file="file.properties"/> + </loadproperties> +Load contents of file.properties as Ant properties.
<loadproperties srcFile="file.properties"> @@ -77,6 +93,17 @@ Load contents of file.properties as Ant properties. Read the lines that contain the string "import." from the file "file.properties" and load them as Ant properties. + ++ <loadproperties> + <gzipresource> + <url url="http://example.org/url.properties.gz"/> + </gzipresource> + </loadproperties> ++Load contents of http://example.org/url.properties.gz, uncompress it +on the fly and load the contents as Ant properties. +
Copyright © 2002-2005 The Apache Software Foundation. All rights diff --git a/src/etc/testcases/taskdefs/loadproperties.xml b/src/etc/testcases/taskdefs/loadproperties.xml index 8259f7092..49cf9ae92 100644 --- a/src/etc/testcases/taskdefs/loadproperties.xml +++ b/src/etc/testcases/taskdefs/loadproperties.xml @@ -30,7 +30,7 @@ http.@SERVER@ = ${server} value="http://${server1.http.server}:${server1.http.port}"/> -
+ + + #tpfr.a=a tpfr.a=A @@ -38,6 +38,9 @@ tpfr.b=b\ e tpfr.c=@C@ ++ + + @@ -45,6 +48,22 @@ tpfr.c=@C@ + + ++ ++ ++ + ++ ++ diff --git a/src/main/org/apache/tools/ant/taskdefs/LoadProperties.java b/src/main/org/apache/tools/ant/taskdefs/LoadProperties.java index cc7a24c4d..4b94586b6 100644 --- a/src/main/org/apache/tools/ant/taskdefs/LoadProperties.java +++ b/src/main/org/apache/tools/ant/taskdefs/LoadProperties.java @@ -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"); + } + } } \ No newline at end of file diff --git a/src/testcases/org/apache/tools/ant/taskdefs/LoadPropertiesTest.java b/src/testcases/org/apache/tools/ant/taskdefs/LoadPropertiesTest.java index be56f55af..8775b1533 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/LoadPropertiesTest.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/LoadPropertiesTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002,2004 The Apache Software Foundation + * Copyright 2002,2004-2005 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -50,5 +50,11 @@ public class LoadPropertiesTest extends BuildFileTest { public void testPropertiesFromResource() { executeTarget("testPropertiesFromResource"); + executeTarget("loadPropertiesCheck"); + } + + public void testPropertiesFromFileSet() { + executeTarget("testPropertiesFromFileSet"); + executeTarget("loadPropertiesCheck"); } }