Browse Source

Resource-enable replacefilterfile and propertyfile attributes f <replace>. Somewhat related to PR 42702.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@724400 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 16 years ago
parent
commit
727dc821bd
2 changed files with 75 additions and 22 deletions
  1. +57
    -22
      src/main/org/apache/tools/ant/taskdefs/Replace.java
  2. +18
    -0
      src/tests/antunit/taskdefs/replace-test.xml

+ 57
- 22
src/main/org/apache/tools/ant/taskdefs/Replace.java View File

@@ -27,6 +27,7 @@ import java.io.FileOutputStream;
import java.io.FileReader; import java.io.FileReader;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.Reader; import java.io.Reader;
@@ -40,6 +41,7 @@ import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.Resource; import org.apache.tools.ant.types.Resource;
import org.apache.tools.ant.types.ResourceCollection; import org.apache.tools.ant.types.ResourceCollection;
import org.apache.tools.ant.types.resources.FileProvider; import org.apache.tools.ant.types.resources.FileProvider;
import org.apache.tools.ant.types.resources.FileResource;
import org.apache.tools.ant.types.resources.Union; import org.apache.tools.ant.types.resources.Union;
import org.apache.tools.ant.util.FileUtils; import org.apache.tools.ant.util.FileUtils;
import org.apache.tools.ant.util.StringUtils; import org.apache.tools.ant.util.StringUtils;
@@ -63,8 +65,8 @@ public class Replace extends MatchingTask {
private NestedString token = null; private NestedString token = null;
private NestedString value = new NestedString(); private NestedString value = new NestedString();


private File propertyFile = null;
private File replaceFilterFile = null;
private Resource propertyResource = null;
private Resource replaceFilterResource = null;
private Properties properties = null; private Properties properties = null;
private ArrayList replacefilters = new ArrayList(); private ArrayList replacefilters = new ArrayList();


@@ -161,10 +163,10 @@ public class Replace extends MatchingTask {


if ((property != null)) { if ((property != null)) {
//the property attribute must have access to a property file //the property attribute must have access to a property file
if (propertyFile == null) {
if (propertyResource == null) {
String message = "The replacefilter's property attribute " String message = "The replacefilter's property attribute "
+ "can only be used with the replacetask's " + "can only be used with the replacetask's "
+ "propertyFile attribute.";
+ "propertyFile/Resource attribute.";
throw new BuildException(message); throw new BuildException(message);
} }


@@ -172,7 +174,7 @@ public class Replace extends MatchingTask {
if (properties == null if (properties == null
|| properties.getProperty(property) == null) { || properties.getProperty(property) == null) {
String message = "property \"" + property String message = "property \"" + property
+ "\" was not found in " + propertyFile.getPath();
+ "\" was not found in " + propertyResource.getName();
throw new BuildException(message); throw new BuildException(message);
} }
} }
@@ -512,8 +514,8 @@ public class Replace extends MatchingTask {
} }


try { try {
if (replaceFilterFile != null) {
Properties props = getProperties(replaceFilterFile);
if (replaceFilterResource != null) {
Properties props = getProperties(replaceFilterResource);
Iterator e = props.keySet().iterator(); Iterator e = props.keySet().iterator();
while (e.hasNext()) { while (e.hasNext()) {
String tok = e.next().toString(); String tok = e.next().toString();
@@ -525,8 +527,8 @@ public class Replace extends MatchingTask {


validateAttributes(); validateAttributes();


if (propertyFile != null) {
properties = getProperties(propertyFile);
if (propertyResource != null) {
properties = getProperties(propertyResource);
} }


validateReplacefilters(); validateReplacefilters();
@@ -582,8 +584,8 @@ public class Replace extends MatchingTask {
+ "or nested resources must be specified"; + "or nested resources must be specified";
throw new BuildException(message, getLocation()); throw new BuildException(message, getLocation());
} }
if (propertyFile != null && !propertyFile.exists()) {
String message = "Property file " + propertyFile.getPath()
if (propertyResource != null && !propertyResource.isExists()) {
String message = "Property file " + propertyResource.getName()
+ " does not exist."; + " does not exist.";
throw new BuildException(message, getLocation()); throw new BuildException(message, getLocation());
} }
@@ -620,18 +622,26 @@ public class Replace extends MatchingTask {
* @throws BuildException if the file could not be found or read. * @throws BuildException if the file could not be found or read.
*/ */
public Properties getProperties(File propertyFile) throws BuildException { public Properties getProperties(File propertyFile) throws BuildException {
return getProperties(new FileResource(getProject(), propertyFile));
}

/**
* Load a properties resource.
* @param propertyResource the resource to load the properties from.
* @return loaded <code>Properties</code> object.
* @throws BuildException if the resource could not be found or read.
* @since Ant 1.8.0
*/
public Properties getProperties(Resource propertyResource)
throws BuildException {
Properties props = new Properties(); Properties props = new Properties();


FileInputStream in = null;
InputStream in = null;
try { try {
in = new FileInputStream(propertyFile);
in = propertyResource.getInputStream();
props.load(in); props.load(in);
} catch (FileNotFoundException e) {
String message = "Property file (" + propertyFile.getPath()
+ ") not found.";
throw new BuildException(message);
} catch (IOException e) { } catch (IOException e) {
String message = "Property file (" + propertyFile.getPath()
String message = "Property resource (" + propertyResource.getName()
+ ") cannot be loaded."; + ") cannot be loaded.";
throw new BuildException(message); throw new BuildException(message);
} finally { } finally {
@@ -793,7 +803,19 @@ public class Replace extends MatchingTask {
* @param replaceFilterFile <code>File</code> to load. * @param replaceFilterFile <code>File</code> to load.
*/ */
public void setReplaceFilterFile(File replaceFilterFile) { public void setReplaceFilterFile(File replaceFilterFile) {
this.replaceFilterFile = replaceFilterFile;
setReplaceFilterResource(new FileResource(getProject(),
replaceFilterFile));
}

/**
* Sets the name of a resource containing filters; optional.
* Each property will be treated as a replacefilter where token is the name
* of the property and value is the value of the property.
* @param replaceFilterFile <code>File</code> to load.
* @since Ant 1.8.0
*/
public void setReplaceFilterResource(Resource replaceFilter) {
this.replaceFilterResource = replaceFilter;
} }


/** /**
@@ -807,8 +829,8 @@ public class Replace extends MatchingTask {


/** /**
* Set the string token to replace; required unless a nested * Set the string token to replace; required unless a nested
* <code>replacetoken</code> element or the <code>replacefilterfile</code>
* attribute is used.
* <code>replacetoken</code> element or the
* <code>replacefilterresource</code> attribute is used.
* @param token token <code>String</code>. * @param token token <code>String</code>.
*/ */
public void setToken(String token) { public void setToken(String token) {
@@ -860,7 +882,20 @@ public class Replace extends MatchingTask {
* @param propertyFile <code>File</code> to load. * @param propertyFile <code>File</code> to load.
*/ */
public void setPropertyFile(File propertyFile) { public void setPropertyFile(File propertyFile) {
this.propertyFile = propertyFile;
setPropertyResource(new FileResource(propertyFile));
}

/**
* A resource from which properties specified using nested
* <code>&lt;replacefilter&gt;</code> elements are drawn; required
* only if the <i>property</i> attribute of
* <code>&lt;replacefilter&gt;</code> is used.
* @param propertyResource <code>Resource</code> to load.
*
* @since Ant 1.8.0
*/
public void setPropertyResource(Resource propertyResource) {
this.propertyResource = propertyResource;
} }


/** /**


+ 18
- 0
src/tests/antunit/taskdefs/replace-test.xml View File

@@ -18,6 +18,12 @@
<project default="antunit" xmlns:au="antlib:org.apache.ant.antunit"> <project default="antunit" xmlns:au="antlib:org.apache.ant.antunit">
<import file="../antunit-base.xml" /> <import file="../antunit-base.xml" />


<import file="../propertyhelpers.xml" as="ph"/>

<target name="tearDown" depends="antunit-base.tearDown,ph.tearDown">
<delete dir="foo"/>
</target>

<target name="setUp"> <target name="setUp">
<mkdir dir="${output}"/> <mkdir dir="${output}"/>
<echo file="${output}/text.txt"><![CDATA[ <echo file="${output}/text.txt"><![CDATA[
@@ -82,4 +88,16 @@ Hello, world!
</au:expectfailure> </au:expectfailure>
</target> </target>


<target name="testPropertyFilterResource" depends="setUp,ph.defineHelpers">
<mkdir dir="foo"/>
<echo file="foo/foo.properties"><![CDATA[
world=Ant
]]></echo>
<replace replacefilterresource="${java:foo!foo.properties}">
<file file="${output}/text.txt"/>
</replace>
<au:assertResourceContains
resource="${output}/text.txt" value="Hello, Ant!"/>
</target>

</project> </project>

Loading…
Cancel
Save