Browse Source

make <replace> support resource collections. PR 24062.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@723766 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 16 years ago
parent
commit
6b2e7156fc
4 changed files with 79 additions and 10 deletions
  1. +4
    -0
      WHATSNEW
  2. +3
    -0
      docs/manual/CoreTasks/replace.html
  3. +40
    -10
      src/main/org/apache/tools/ant/taskdefs/Replace.java
  4. +32
    -0
      src/tests/antunit/taskdefs/replace-test.xml

+ 4
- 0
WHATSNEW View File

@@ -598,6 +598,10 @@ Other changes:
collections.
Bugzilla Report 46341.

* <replace> now supports arbitrary filesystem based resource
collections.
Bugzilla Report 24062.

Changes from Ant 1.7.0 TO Ant 1.7.1
=============================================



+ 3
- 0
docs/manual/CoreTasks/replace.html View File

@@ -130,6 +130,9 @@ have been regenerated by this task.</p>
supports all attributes of <code>&lt;fileset&gt;</code> as well as the
nested <code>&lt;include&gt;</code>, <code>&lt;exclude&gt;</code> and
<code>&lt;patternset&gt;</code> elements.</p>
<p>Since Ant 1.8.0 this task supports any filesystem
based <a href="../CoreTypes/resources.html#collection">resource
collections</a> as nested elements.</p>
<p>If either the text you want to replace or the replacement text
cross line boundaries, you can use nested elements to specify
them.</p>


+ 40
- 10
src/main/org/apache/tools/ant/taskdefs/Replace.java View File

@@ -32,11 +32,15 @@ import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Properties;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.Resource;
import org.apache.tools.ant.types.ResourceCollection;
import org.apache.tools.ant.types.resources.FileProvider;
import org.apache.tools.ant.types.resources.Union;
import org.apache.tools.ant.util.FileUtils;
import org.apache.tools.ant.util.StringUtils;

@@ -55,7 +59,7 @@ public class Replace extends MatchingTask {

private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();

private File src = null;
private File sourceFile = null;
private NestedString token = null;
private NestedString value = new NestedString();

@@ -73,6 +77,8 @@ public class Replace extends MatchingTask {
/** The encoding used to read and write files - if null, uses default */
private String encoding = null;

private Union resources;

/**
* An inline string to use as the replacement text.
*/
@@ -464,9 +470,9 @@ public class Replace extends MatchingTask {
try {
if (replaceFilterFile != null) {
Properties props = getProperties(replaceFilterFile);
Enumeration e = props.keys();
while (e.hasMoreElements()) {
String tok = e.nextElement().toString();
Iterator e = props.keySet().iterator();
while (e.hasNext()) {
String tok = e.next().toString();
Replacefilter replaceFilter = createReplacefilter();
replaceFilter.setToken(tok);
replaceFilter.setValue(props.getProperty(tok));
@@ -483,8 +489,8 @@ public class Replace extends MatchingTask {
fileCount = 0;
replaceCount = 0;

if (src != null) {
processFile(src);
if (sourceFile != null) {
processFile(sourceFile);
}

if (dir != null) {
@@ -497,6 +503,15 @@ public class Replace extends MatchingTask {
}
}

if (resources != null) {
for (Iterator i = resources.iterator(); i.hasNext(); ) {
FileProvider fp =
(FileProvider) ((Resource) i.next())
.as(FileProvider.class);
processFile(fp.getFile());
}
}

if (summary) {
log("Replaced " + replaceCount + " occurrences in "
+ fileCount + " files.", Project.MSG_INFO);
@@ -515,9 +530,9 @@ public class Replace extends MatchingTask {
* mandatory attribute is missing.
*/
public void validateAttributes() throws BuildException {
if (src == null && dir == null) {
if (sourceFile == null && dir == null && resources == null) {
String message = "Either the file or the dir attribute "
+ "must be specified";
+ "or nested resources must be specified";
throw new BuildException(message, getLocation());
}
if (propertyFile != null && !propertyFile.exists()) {
@@ -704,7 +719,7 @@ public class Replace extends MatchingTask {
* @param file source <code>File</code>.
*/
public void setFile(File file) {
this.src = file;
this.sourceFile = file;
}

/**
@@ -807,6 +822,21 @@ public class Replace extends MatchingTask {
return filter;
}

/**
* Support arbitrary file system based resource collections.
*
* @since Ant 1.8.0
*/
public void addConfigured(ResourceCollection rc) {
if (!rc.isFilesystemOnly()) {
throw new BuildException("only filesystem resources are supported");
}
if (resources == null) {
resources = new Union();
}
resources.add(rc);
}

/**
* Adds the token and value as first &lt;replacefilter&gt; element.
* The token and value are always processed first.


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

@@ -0,0 +1,32 @@
<?xml version="1.0"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project default="antunit" xmlns:au="antlib:org.apache.ant.antunit">
<import file="../antunit-base.xml" />

<target name="testRCSupport">
<mkdir dir="${output}"/>
<echo file="${output}/text.txt"><![CDATA[
Hello, world!
]]></echo>
<replace token="world" value="Ant">
<file file="${output}/text.txt"/>
</replace>
<au:assertResourceContains
resource="${output}/text.txt" value="Hello, Ant!"/>
</target>
</project>

Loading…
Cancel
Save