mbenson 2004/06/22 12:34:54 Modified: . Tag: ANT_16_BRANCH TODO WHATSNEW docs/manual/CoreTasks Tag: ANT_16_BRANCH loadproperties.html src/etc/testcases/taskdefs Tag: ANT_16_BRANCH loadproperties.xml src/main/org/apache/tools/ant/taskdefs Tag: ANT_16_BRANCH LoadProperties.java src/testcases/org/apache/tools/ant/taskdefs Tag: ANT_16_BRANCH LoadPropertiesTest.java Log: merge "<loadproperties> from resource" from HEAD Revision Changes Path No revision No revision 1.3.2.18 +0 -2 ant/Attic/TODO Index: TODO =================================================================== RCS file: /home/cvs/ant/Attic/TODO,v retrieving revision 1.3.2.17 retrieving revision 1.3.2.18 diff -u -r1.3.2.17 -r1.3.2.18 --- TODO 21 Jun 2004 22:15:05 -0000 1.3.2.17 +++ TODO 22 Jun 2004 19:34:54 -0000 1.3.2.18 @@ -9,8 +9,6 @@ * merge <redirector>s from HEAD [Matt] -* merge "<loadproperties> from resource" from HEAD [Matt] - * merge "<apply> differentiates between empty & up-to-date" from HEAD [Matt] * Fix or at least document support for tomcat 5.0 jsp 1.503.2.108 +2 -0 ant/WHATSNEW Index: WHATSNEW =================================================================== RCS file: /home/cvs/ant/WHATSNEW,v retrieving revision 1.503.2.107 retrieving revision 1.503.2.108 diff -u -r1.503.2.107 -r1.503.2.108 --- WHATSNEW 21 Jun 2004 22:15:05 -0000 1.503.2.107 +++ WHATSNEW 22 Jun 2004 19:34:54 -0000 1.503.2.108 @@ -201,6 +201,8 @@ * <fail> accepts a nested <condition>. +* <loadproperties> supports loading from a resource. + Changes from Ant 1.6.0 to Ant 1.6.1 ============================================= No revision No revision 1.6.2.3 +20 -3 ant/docs/manual/CoreTasks/loadproperties.html Index: loadproperties.html =================================================================== RCS file: /home/cvs/ant/docs/manual/CoreTasks/loadproperties.html,v retrieving revision 1.6.2.2 retrieving revision 1.6.2.3 diff -u -r1.6.2.2 -r1.6.2.3 --- loadproperties.html 9 Feb 2004 22:12:07 -0000 1.6.2.2 +++ loadproperties.html 22 Jun 2004 19:34:54 -0000 1.6.2.3 @@ -11,7 +11,7 @@ <h3>Description</h3> <p> Load a file's contents as Ant properties. This is equivalent -to <property file="..."/> except that it +to <property file|resource="..."/> except that it supports nested <filterchain> elements. </p> @@ -30,17 +30,34 @@ <tr> <td valign="top">srcFile</td> <td valign="top">source file</td> - <td valign="top" align="center">Yes</td> + <td valign="top" rowspan="2" align="center">One of these</td> + </tr> + <tr> + <td valign="top">resource</td> + <td valign="top">the resource name of the property file</td> </tr> <tr> <td valign="top">encoding</td> <td valign="top">encoding to use when loading the file</td> <td align="center" valign="top">No</td> </tr> + <tr> + <td valign="top">classpath</td> + <td valign="top">the classpath to use when looking up a resource.</td> + <td align="center" valign="top">No</td> + </tr> + <tr> + <td valign="top">classpathref</td> + <td valign="top">the classpath to use when looking up a resource, + given as <a href="../using.html#references">reference</a> + to a <path> defined elsewhere..</td> + <td align="center" valign="top">No</td> + </tr> </table> <p> The LoadProperties task supports nested <a href="../CoreTypes/filterchain.html"> -FilterChain</a>s. +FilterChain</a>s, as well as a nested <code><classpath></code> +element for use with the <i>resource</i> attribute. <h3>Examples</h3> <pre> <loadproperties srcFile="file.properties"/> No revision No revision 1.1.4.1 +23 -0 ant/src/etc/testcases/taskdefs/loadproperties.xml Index: loadproperties.xml =================================================================== RCS file: /home/cvs/ant/src/etc/testcases/taskdefs/loadproperties.xml,v retrieving revision 1.1 retrieving revision 1.1.4.1 diff -u -r1.1 -r1.1.4.1 --- loadproperties.xml 8 Mar 2002 02:02:20 -0000 1.1 +++ loadproperties.xml 22 Jun 2004 19:34:54 -0000 1.1.4.1 @@ -30,6 +30,29 @@ value="http://${server1.http.server}:${server1.http.port}"/> </target> + <target name="testPropertiesFromResource" depends="init"> + <echo file="properties.tmp"> +#tpfr.a=a +tpfr.a=A +tpfr.b=b\ + e [EMAIL PROTECTED]@ + </echo> + <loadproperties resource="properties.tmp" classpath="${basedir}"> + <filterchain> + <replacetokens> + <token key="C" value="sea"/> + </replacetokens> + </filterchain> + </loadproperties> + <condition property="testPropertiesFromResource.ok"> + <equals arg1="Abesea" arg2="${tpfr.a}${tpfr.b}${tpfr.c}" /> + </condition> + <fail unless="testPropertiesFromResource.ok"> +$${tpfr.a}$${tpfr.b}$${tpfr.c}="${tpfr.a}${tpfr.b}${tpfr.c}" + </fail> + </target> + <target name="cleanup"> <delete file="properties.tmp"/> </target> No revision No revision 1.16.2.9 +98 -21 ant/src/main/org/apache/tools/ant/taskdefs/LoadProperties.java Index: LoadProperties.java =================================================================== RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/LoadProperties.java,v retrieving revision 1.16.2.8 retrieving revision 1.16.2.9 diff -u -r1.16.2.8 -r1.16.2.9 --- LoadProperties.java 9 Mar 2004 17:01:33 -0000 1.16.2.8 +++ LoadProperties.java 22 Jun 2004 19:34:54 -0000 1.16.2.9 @@ -19,6 +19,7 @@ import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.File; +import java.io.InputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; @@ -26,9 +27,12 @@ import java.util.Enumeration; import java.util.Properties; import java.util.Vector; +import org.apache.tools.ant.Project; import org.apache.tools.ant.BuildException; 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.FilterChain; /** @@ -45,18 +49,27 @@ private File srcFile = null; /** + * Resource + */ + private String resource = null; + + /** + * Classpath + */ + private Path classpath = null; + + /** * Holds filterchains */ private final Vector filterChains = new Vector(); /** - * Encoding to use for filenames, defaults to the platform's default - * encoding. + * Encoding to use for input; defaults to the platform's default encoding. */ private String encoding = null; /** - * Sets the file to load. + * Set the file to load. * * @param srcFile The new SrcFile value */ @@ -65,6 +78,15 @@ } /** + * Set the resource name of a property file to load. + * + * @param resource resource on classpath + */ + public void setResource(String resource) { + this.resource = resource; + } + + /** * Encoding to use for input, defaults to the platform's default * encoding. <p> * @@ -75,41 +97,96 @@ * * @param encoding The new Encoding value */ - public final void setEncoding(final String encoding) { this.encoding = encoding; } /** - * read in a source file's contents and load them up as Ant properties + * Set the classpath to use when looking up a resource. + * @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); + } + } + + /** + * Add a classpath to use when looking up a resource. + */ + public Path createClasspath() { + if (this.classpath == null) { + this.classpath = new Path(getProject()); + } + return this.classpath.createPath(); + } + + /** + * Set the classpath to use when looking up a resource, + * given as reference to a <path> defined elsewhere + */ + public void setClasspathRef(Reference r) { + createClasspath().setRefid(r); + } + + /** + * get the classpath used by this <CODE>LoadProperties</CODE>. + */ + public Path getClasspath() { + return classpath; + } + + /** + * load Ant properties from the source file or resource * * @exception BuildException if something goes wrong with the build */ public final void execute() throws BuildException { //validation - if (srcFile == null) { - throw new BuildException("Source file not defined."); + if (srcFile == null && resource == null) { + throw new BuildException( + "One of \"srcfile\" or \"resource\" is required."); } - if (!srcFile.exists()) { - throw new BuildException("Source file does not exist."); - } + BufferedInputStream bis = null; + + if (srcFile != null ) { + if (!srcFile.exists()) { + throw new BuildException("Source file does not exist."); + } + + if (!srcFile.isFile()) { + throw new BuildException("Source file is not a file."); + } - if (!srcFile.isFile()) { - throw new BuildException("Source file is not a file."); + 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); + return; + } } - FileInputStream fis = null; - BufferedInputStream bis = null; Reader instream = null; ByteArrayInputStream tis = null; try { - final long len = srcFile.length(); - - //open up the file - fis = new FileInputStream(srcFile); - bis = new BufferedInputStream(fis); if (encoding == null) { instream = new InputStreamReader(bis); } else { @@ -150,8 +227,8 @@ throw be; } finally { try { - if (fis != null) { - fis.close(); + if (bis != null) { + bis.close(); } } catch (IOException ioex) { //ignore No revision No revision 1.3.2.5 +4 -0 ant/src/testcases/org/apache/tools/ant/taskdefs/LoadPropertiesTest.java Index: LoadPropertiesTest.java =================================================================== RCS file: /home/cvs/ant/src/testcases/org/apache/tools/ant/taskdefs/LoadPropertiesTest.java,v retrieving revision 1.3.2.4 retrieving revision 1.3.2.5 diff -u -r1.3.2.4 -r1.3.2.5 --- LoadPropertiesTest.java 9 Mar 2004 17:02:01 -0000 1.3.2.4 +++ LoadPropertiesTest.java 22 Jun 2004 19:34:54 -0000 1.3.2.5 @@ -47,4 +47,8 @@ String url = project.getProperty("server1.http.url"); assertEquals("http://localhost:80", url); } + + public void testPropertiesFromResource() { + executeTarget("testPropertiesFromResource"); + } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]