Bugzilla 39688 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@412040 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -229,6 +229,7 @@ Tim Fennell | |||||
| Timothy Gerard Endres | Timothy Gerard Endres | ||||
| Tim Stephenson | Tim Stephenson | ||||
| Tom Ball | Tom Ball | ||||
| Tom Cunningham | |||||
| Tom Dimock | Tom Dimock | ||||
| Tom Eugelink | Tom Eugelink | ||||
| Ulrich Schmidt | Ulrich Schmidt | ||||
| @@ -424,6 +424,8 @@ Other changes: | |||||
| * new <antversion> condition. Bugzilla report 32804. | * new <antversion> condition. Bugzilla report 32804. | ||||
| * ReplaceTokens should allow properties files. Bugzilla report 39688. | |||||
| Changes from Ant 1.6.4 to Ant 1.6.5 | Changes from Ant 1.6.4 to Ant 1.6.5 | ||||
| =================================== | =================================== | ||||
| @@ -925,6 +925,10 @@ | |||||
| <first>Tom</first> | <first>Tom</first> | ||||
| <last>Ball</last> | <last>Ball</last> | ||||
| </name> | </name> | ||||
| <name> | |||||
| <first>Tom</first> | |||||
| <last>Cunningham</last> | |||||
| </name> | |||||
| <name> | <name> | ||||
| <first>Tom</first> | <first>Tom</first> | ||||
| <last>Dimock</last> | <last>Dimock</last> | ||||
| @@ -470,9 +470,15 @@ user defined values. | |||||
| <tr> | <tr> | ||||
| <td vAlign=top>token</td> | <td vAlign=top>token</td> | ||||
| <td vAlign=top>User defined String.</td> | <td vAlign=top>User defined String.</td> | ||||
| <td vAlign=top>User defined search String</td> | |||||
| <td vAlign=top>User defined search String.</td> | |||||
| <td vAlign=top align="center">Yes</td> | <td vAlign=top align="center">Yes</td> | ||||
| </tr> | </tr> | ||||
| <tr> | |||||
| <td vAlign=top>propertiesfile</td> | |||||
| <td vAlign=top>Not applicable.</td> | |||||
| <td vAlign=top>Properties file to take tokens from.</td> | |||||
| <td vAlign=top align="center">No</td> | |||||
| </tr> | |||||
| </table> | </table> | ||||
| <p> | <p> | ||||
| @@ -503,6 +509,18 @@ Convenience method: | |||||
| </loadfile> | </loadfile> | ||||
| </pre></blockquote> | </pre></blockquote> | ||||
| This will treat each properties file entry in sample.properties as a token/key pair : | |||||
| <blockquote><pre> | |||||
| <loadfile srcfile="${src.file}" property="${src.file.replaced}"> | |||||
| <filterchain> | |||||
| <filterreader classname="org.apache.tools.ant.filters.ReplaceTokens"> | |||||
| <param type="propertiesfile" value="sample.properties"/> | |||||
| </filterreader> | |||||
| </filterchain> | |||||
| </loadfile> | |||||
| </filterchain> | |||||
| </pre></blockquote> | |||||
| <h3><a name="stripjavacomments">StripJavaComments</a></h3> | <h3><a name="stripjavacomments">StripJavaComments</a></h3> | ||||
| This filter reader strips away comments from the data, | This filter reader strips away comments from the data, | ||||
| @@ -79,6 +79,17 @@ | |||||
| </copy> | </copy> | ||||
| </target> | </target> | ||||
| <target name="testReplaceTokensPropertyFile" depends="init"> | |||||
| <copy tofile="result/replacetokensPropertyFile.test"> | |||||
| <fileset dir="input" includes="replacetokens.test" /> | |||||
| <filterchain> | |||||
| <filterreader classname="org.apache.tools.ant.filters.ReplaceTokens"> | |||||
| <param type="propertiesfile" value="${basedir}/input/sample.properties"/> | |||||
| </filterreader> | |||||
| </filterchain> | |||||
| </copy> | |||||
| </target> | |||||
| <target name="testNoAddNewLine" depends="init"> | <target name="testNoAddNewLine" depends="init"> | ||||
| <concat destfile="result/nonl">This has no new lines</concat> | <concat destfile="result/nonl">This has no new lines</concat> | ||||
| <copy file="result/nonl" tofile="result/nonl-copyfilter"> | <copy file="result/nonl" tofile="result/nonl-copyfilter"> | ||||
| @@ -0,0 +1 @@ | |||||
| foo= | |||||
| @@ -1,5 +1,5 @@ | |||||
| /* | /* | ||||
| * Copyright 2002-2005 The Apache Software Foundation | |||||
| * Copyright 2002-2006 The Apache Software Foundation | |||||
| * | * | ||||
| * Licensed under the Apache License, Version 2.0 (the "License"); | * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
| * you may not use this file except in compliance with the License. | * you may not use this file except in compliance with the License. | ||||
| @@ -16,9 +16,12 @@ | |||||
| */ | */ | ||||
| package org.apache.tools.ant.filters; | package org.apache.tools.ant.filters; | ||||
| import java.io.FileInputStream; | |||||
| import java.io.IOException; | import java.io.IOException; | ||||
| import java.io.Reader; | import java.io.Reader; | ||||
| import java.util.Enumeration; | |||||
| import java.util.Hashtable; | import java.util.Hashtable; | ||||
| import java.util.Properties; | |||||
| import org.apache.tools.ant.types.Parameter; | import org.apache.tools.ant.types.Parameter; | ||||
| import org.apache.tools.ant.BuildException; | import org.apache.tools.ant.BuildException; | ||||
| @@ -218,6 +221,21 @@ public final class ReplaceTokens | |||||
| hash.put(token.getKey(), token.getValue()); | hash.put(token.getKey(), token.getValue()); | ||||
| } | } | ||||
| /** | |||||
| * Returns properties from a specified properties file. | |||||
| * | |||||
| * @param fileName The file to load properties from. | |||||
| */ | |||||
| private Properties getPropertiesFromFile (String fileName) { | |||||
| Properties props = new Properties(); | |||||
| try { | |||||
| props.load(new FileInputStream(fileName)); | |||||
| } catch (IOException ioe) { | |||||
| ioe.printStackTrace(); | |||||
| } | |||||
| return props; | |||||
| } | |||||
| /** | /** | ||||
| * Sets the map of tokens to replace. | * Sets the map of tokens to replace. | ||||
| * | * | ||||
| @@ -286,6 +304,13 @@ public final class ReplaceTokens | |||||
| final String name = params[i].getName(); | final String name = params[i].getName(); | ||||
| final String value = params[i].getValue(); | final String value = params[i].getValue(); | ||||
| hash.put(name, value); | hash.put(name, value); | ||||
| } else if ("propertiesfile".equals(type)) { | |||||
| Properties props = getPropertiesFromFile(params[i].getValue()); | |||||
| for (Enumeration e = props.keys(); e.hasMoreElements();) { | |||||
| String key = (String) e.nextElement(); | |||||
| String value = props.getProperty(key); | |||||
| hash.put(key, value); | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| @@ -26,7 +26,7 @@ import org.apache.tools.ant.util.FileUtils; | |||||
| /** | /** | ||||
| */ | */ | ||||
| public class ReplaceTokensTest extends BuildFileTest { | public class ReplaceTokensTest extends BuildFileTest { | ||||
| private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); | private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); | ||||
| public ReplaceTokensTest(String name) { | public ReplaceTokensTest(String name) { | ||||
| @@ -48,4 +48,11 @@ public class ReplaceTokensTest extends BuildFileTest { | |||||
| assertTrue(FILE_UTILS.contentEquals(expected, result)); | assertTrue(FILE_UTILS.contentEquals(expected, result)); | ||||
| } | } | ||||
| public void testReplaceTokensPropertyFile() throws IOException { | |||||
| executeTarget("testReplaceTokensPropertyFile"); | |||||
| File expected = FILE_UTILS.resolveFile(getProjectDir(), "expected/replacetokens.test"); | |||||
| File result = FILE_UTILS.resolveFile(getProjectDir(), "result/replacetokensPropertyFile.test"); | |||||
| assertTrue(FILE_UTILS.contentEquals(expected, result)); | |||||
| } | |||||
| } | } | ||||