Browse Source

ReplaceTokens should allow properties files.

Bugzilla 39688


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@412040 13f79535-47bb-0310-9956-ffa450edef68
master
Antoine Levy-Lambert 19 years ago
parent
commit
ea67f0d44a
8 changed files with 72 additions and 3 deletions
  1. +1
    -0
      CONTRIBUTORS
  2. +2
    -0
      WHATSNEW
  3. +4
    -0
      contributors.xml
  4. +19
    -1
      docs/manual/CoreTypes/filterchain.html
  5. +11
    -0
      src/etc/testcases/filters/build.xml
  6. +1
    -0
      src/etc/testcases/filters/input/sample.properties
  7. +26
    -1
      src/main/org/apache/tools/ant/filters/ReplaceTokens.java
  8. +8
    -1
      src/testcases/org/apache/tools/ant/filters/ReplaceTokensTest.java

+ 1
- 0
CONTRIBUTORS View File

@@ -229,6 +229,7 @@ Tim Fennell
Timothy Gerard Endres
Tim Stephenson
Tom Ball
Tom Cunningham
Tom Dimock
Tom Eugelink
Ulrich Schmidt


+ 2
- 0
WHATSNEW View File

@@ -424,6 +424,8 @@ Other changes:

* 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
===================================



+ 4
- 0
contributors.xml View File

@@ -925,6 +925,10 @@
<first>Tom</first>
<last>Ball</last>
</name>
<name>
<first>Tom</first>
<last>Cunningham</last>
</name>
<name>
<first>Tom</first>
<last>Dimock</last>


+ 19
- 1
docs/manual/CoreTypes/filterchain.html View File

@@ -470,9 +470,15 @@ user defined values.
<tr>
<td vAlign=top>token</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>
</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>
<p>

@@ -503,6 +509,18 @@ Convenience method:
&lt;/loadfile&gt;
</pre></blockquote>

This will treat each properties file entry in sample.properties as a token/key pair :
<blockquote><pre>
&lt;loadfile srcfile=&quot;${src.file}&quot; property=&quot;${src.file.replaced}&quot;&gt;
&lt;filterchain&gt;
&lt;filterreader classname=&quot;org.apache.tools.ant.filters.ReplaceTokens&quot;&gt;
&lt;param type=&quot;propertiesfile&quot; value=&quot;sample.properties&quot;/&gt;
&lt;/filterreader&gt;
&lt;/filterchain&gt;
&lt;/loadfile&gt;
&lt;/filterchain&gt;
</pre></blockquote>

<h3><a name="stripjavacomments">StripJavaComments</a></h3>

This filter reader strips away comments from the data,


+ 11
- 0
src/etc/testcases/filters/build.xml View File

@@ -79,6 +79,17 @@
</copy>
</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">
<concat destfile="result/nonl">This has no new lines</concat>
<copy file="result/nonl" tofile="result/nonl-copyfilter">


+ 1
- 0
src/etc/testcases/filters/input/sample.properties View File

@@ -0,0 +1 @@
foo=

+ 26
- 1
src/main/org/apache/tools/ant/filters/ReplaceTokens.java View File

@@ -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");
* you may not use this file except in compliance with the License.
@@ -16,9 +16,12 @@
*/
package org.apache.tools.ant.filters;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.Reader;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import org.apache.tools.ant.types.Parameter;
import org.apache.tools.ant.BuildException;

@@ -218,6 +221,21 @@ public final class ReplaceTokens
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.
*
@@ -286,6 +304,13 @@ public final class ReplaceTokens
final String name = params[i].getName();
final String value = params[i].getValue();
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);
}
}
}
}


+ 8
- 1
src/testcases/org/apache/tools/ant/filters/ReplaceTokensTest.java View File

@@ -26,7 +26,7 @@ import org.apache.tools.ant.util.FileUtils;
/**
*/
public class ReplaceTokensTest extends BuildFileTest {
private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();

public ReplaceTokensTest(String name) {
@@ -48,4 +48,11 @@ public class ReplaceTokensTest extends BuildFileTest {
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));
}

}

Loading…
Cancel
Save