Top Description Inners Fields Constructors Methods
org.apache.tools.ant.taskdefs

public Class Replace

extends MatchingTask
Class Inheritance
Imports
java.io.BufferedReader, .BufferedWriter, .File, .IOException, .InputStream, .InputStreamReader, .OutputStream, .OutputStreamWriter, .Reader, .Writer, java.nio.file.Files, java.util.ArrayList, .Comparator, .Iterator, .List, .Properties, org.apache.tools.ant.BuildException, .DirectoryScanner, .Project, org.apache.tools.ant.types.Resource, .ResourceCollection, org.apache.tools.ant.types.resources.FileProvider, .FileResource, .Union, org.apache.tools.ant.util.FileUtils, .StreamUtils

Replaces all occurrences of one or more string tokens with given values in the indicated files. Each value can be either a string or the value of a property available in a designated property file. If you want to replace a text that crosses line boundaries, you must use a nested <replacetoken> element.
Since
Ant 1.1

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
Replace.FileInput

Class reading a file in small chunks, and presenting these chunks in a StringBuffer.

private class
Replace.FileOutput

Component writing a file in chunks, taking the chunks from the Replacefilter.

public class
Replace.NestedString

An inline string to use as the replacement text.

public class
Replace.Replacefilter

A filter to apply.

Field Summary

Modifier and TypeField and Description
private File
private String
encoding

The encoding used to read and write files - if null, uses default

private boolean
private static final FileUtils
private int
private boolean
private Properties
private Resource
private int
private Resource
private List<Replace.Replacefilter>
private Union
private File
private boolean
private Replace.NestedString
private Replace.NestedString
Inherited from org.apache.tools.ant.taskdefs.MatchingTask:
fileset

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
addConfigured(ResourceCollection
ResourceCollection
rc
)

Support arbitrary file system based resource collections.

private StringBuffer

Returns:

StringBuffer containing the output of the last filter.
buildFilterChain
(StringBuffer
StringBuffer containing the input for the first filter.
inputBuffer
)

Creates the chain of filters to operate.

private Replace.Replacefilter

Returns:

a nested Replacefilter object to be configured.
createPrimaryfilter
()

Adds the token and value as first <replacefilter> element.

public Replace.Replacefilter

Returns:

a nested Replacefilter object to be configured.
createReplacefilter
()

Add a nested <replacefilter> element.

public Replace.NestedString

Returns:

nested token NestedString to configure.
createReplaceToken
()

Create a token to filter as the text of a nested element.

public Replace.NestedString

Returns:

replacement value NestedString to configure.
createReplaceValue
()

Create a string to replace the token as the text of a nested element.

public void
execute()

Overrides org.apache.tools.ant.Task.execute.

Do the execution.
private void
flushFilterChain()

Flushes all filters.

private Iterator<String>
getOrderedIterator(Properties
Properties
props
)

Sort keys by size so that tokens that are substrings of other strings are tried later.

public Properties

Returns:

loaded Properties object.
getProperties
(File
the file to load the properties from.
propertyFile
)

Load a properties file.

public Properties

Returns:

loaded Properties object.
getProperties
(Resource
the resource to load the properties from.
propertyResource
)

Load a properties resource.

private void
logFilterChain(String
String.
filename
)

Logs the chain of filters to operate on the file.

private void
processFile(File
the source File.
src
)

Perform the replacement on the given file.

private boolean

Returns:

true if the filter chain produced new output.
processFilterChain
()

Performs the normal processing of the filters.

public void
setDir(File
File representing the base directory.
dir
)

The base directory to use when replacing a token in multiple files; required if file is not defined.

public void
setEncoding(String
the encoding to use on the files.
encoding
)

Set the file encoding to use on the files read and written by the task; optional, defaults to default JVM encoding.

public void
setFailOnNoReplacements(boolean
boolean
b
)

Whether the build should fail if nothing has been replaced.

public void
setFile(File
source File.
file
)

Set the source file; required unless dir is set.

public void
setPreserveLastModified(boolean
boolean
b
)

Whether the file timestamp shall be preserved even if the file is modified.

public void
setPropertyFile(File
File to load.
propertyFile
)

The name of a property file from which properties specified using nested <replacefilter> elements are drawn; required only if the property attribute of <replacefilter> is used.

public void
setPropertyResource(Resource
Resource to load.
propertyResource
)

A resource from which properties specified using nested <replacefilter> elements are drawn; required only if the property attribute of <replacefilter> is used.

public void
setReplaceFilterFile(File
File to load.
replaceFilterFile
)

Sets the name of a property file containing filters; optional.

public void
setReplaceFilterResource(Resource
Resource to load.
replaceFilter
)

Sets the name of a resource containing filters; optional.

public void
setSummary(boolean
boolean whether a summary of the replace operation should be logged.
summary
)

Indicates whether a summary of the replace operation should be produced, detailing how many token occurrences and files were processed; optional, default=false.

public void
setToken(String
token String.
token
)

Set the string token to replace; required unless a nested replacetoken element or the replacefilterresource attribute is used.

public void
setValue(String
replacement value.
value
)

Set the string value to use as token replacement; optional, default is the empty string "".

private void
stringReplace(StringBuilder
StringBuilder
str
,
String
String
str1
,
String
String
str2
)

Replace occurrences of str1 in StringBuffer str with str2.

public void
validateAttributes()

Validate attributes provided for this task in .xml build file.

public void
validateReplacefilters()

Validate nested elements.

Inherited from org.apache.tools.ant.taskdefs.MatchingTask:
addaddAndaddContainsaddContainsRegexpaddCustomaddDateaddDependaddDepthaddDifferentaddFilenameaddMajorityaddModifiedaddNoneaddNotaddOraddPresentaddSelectoraddSizeaddTypeappendSelectorcreateExcludecreateExcludesFilecreateIncludecreateIncludesFilecreatePatternSetgetDirectoryScannergetImplicitFileSetgetSelectorshasSelectorsselectorCountselectorElementssetCaseSensitivesetDefaultexcludessetExcludessetExcludesfilesetFollowSymlinkssetIncludessetIncludesfilesetProjectXsetIgnoreXsetItems

Field Detail

dirback to summary
private File dir
encodingback to summary
private String encoding

The encoding used to read and write files - if null, uses default

failOnNoReplacementsback to summary
private boolean failOnNoReplacements
FILE_UTILSback to summary
private static final FileUtils FILE_UTILS
fileCountback to summary
private int fileCount
preserveLastModifiedback to summary
private boolean preserveLastModified
propertiesback to summary
private Properties properties
propertyResourceback to summary
private Resource propertyResource
replaceCountback to summary
private int replaceCount
replaceFilterResourceback to summary
private Resource replaceFilterResource
replacefiltersback to summary
private List<Replace.Replacefilter> replacefilters
resourcesback to summary
private Union resources
sourceFileback to summary
private File sourceFile
summaryback to summary
private boolean summary
tokenback to summary
private Replace.NestedString token
valueback to summary
private Replace.NestedString value

Constructor Detail

Replaceback to summary
public Replace()

Method Detail

addConfiguredback to summary
public void addConfigured(ResourceCollection rc)

Support arbitrary file system based resource collections.

Parameters
rc:ResourceCollection

ResourceCollection

Since
Ant 1.8.0
buildFilterChainback to summary
private StringBuffer buildFilterChain(StringBuffer inputBuffer)

Creates the chain of filters to operate.

Parameters
inputBuffer:StringBuffer

StringBuffer containing the input for the first filter.

Returns:StringBuffer

StringBuffer containing the output of the last filter.

createPrimaryfilterback to summary
private Replace.Replacefilter createPrimaryfilter()

Adds the token and value as first <replacefilter> element. The token and value are always processed first.

Returns:Replace.Replacefilter

a nested Replacefilter object to be configured.

createReplacefilterback to summary
public Replace.Replacefilter createReplacefilter()

Add a nested <replacefilter> element.

Returns:Replace.Replacefilter

a nested Replacefilter object to be configured.

createReplaceTokenback to summary
public Replace.NestedString createReplaceToken()

Create a token to filter as the text of a nested element.

Returns:Replace.NestedString

nested token NestedString to configure.

createReplaceValueback to summary
public Replace.NestedString createReplaceValue()

Create a string to replace the token as the text of a nested element.

Returns:Replace.NestedString

replacement value NestedString to configure.

executeback to summary
public void execute() throws BuildException

Overrides org.apache.tools.ant.Task.execute.

Do the execution.

Annotations
@Override
Exceptions
BuildException:
if we can't build
flushFilterChainback to summary
private void flushFilterChain()

Flushes all filters.

getOrderedIteratorback to summary
private Iterator<String> getOrderedIterator(Properties props)

Sort keys by size so that tokens that are substrings of other strings are tried later.

Parameters
props:Properties

Properties

getPropertiesback to summary
public Properties getProperties(File propertyFile) throws BuildException

Load a properties file.

Parameters
propertyFile:File

the file to load the properties from.

Returns:Properties

loaded Properties object.

Exceptions
BuildException:
if the file could not be found or read.
getPropertiesback to summary
public Properties getProperties(Resource propertyResource) throws BuildException

Load a properties resource.

Parameters
propertyResource:Resource

the resource to load the properties from.

Returns:Properties

loaded Properties object.

Exceptions
BuildException:
if the resource could not be found or read.
Since
Ant 1.8.0
logFilterChainback to summary
private void logFilterChain(String filename)

Logs the chain of filters to operate on the file.

Parameters
filename:String

String.

processFileback to summary
private void processFile(File src) throws BuildException

Perform the replacement on the given file. The replacement is performed on a temporary file which then replaces the original file.

Parameters
src:File

the source File.

processFilterChainback to summary
private boolean processFilterChain()

Performs the normal processing of the filters.

Returns:boolean

true if the filter chain produced new output.

setDirback to summary
public void setDir(File dir)

The base directory to use when replacing a token in multiple files; required if file is not defined.

Parameters
dir:File

File representing the base directory.

setEncodingback to summary
public void setEncoding(String encoding)

Set the file encoding to use on the files read and written by the task; optional, defaults to default JVM encoding.

Parameters
encoding:String

the encoding to use on the files.

setFailOnNoReplacementsback to summary
public void setFailOnNoReplacements(boolean b)

Whether the build should fail if nothing has been replaced.

Parameters
b:boolean

boolean

Since
Ant 1.8.0
setFileback to summary
public void setFile(File file)

Set the source file; required unless dir is set.

Parameters
file:File

source File.

setPreserveLastModifiedback to summary
public void setPreserveLastModified(boolean b)

Whether the file timestamp shall be preserved even if the file is modified.

Parameters
b:boolean

boolean

Since
Ant 1.8.0
setPropertyFileback to summary
public void setPropertyFile(File propertyFile)

The name of a property file from which properties specified using nested <replacefilter> elements are drawn; required only if the property attribute of <replacefilter> is used.

Parameters
propertyFile:File

File to load.

setPropertyResourceback to summary
public void setPropertyResource(Resource propertyResource)

A resource from which properties specified using nested <replacefilter> elements are drawn; required only if the property attribute of <replacefilter> is used.

Parameters
propertyResource:Resource

Resource to load.

Since
Ant 1.8.0
setReplaceFilterFileback to summary
public void setReplaceFilterFile(File replaceFilterFile)

Sets the name of a property file 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.

Parameters
replaceFilterFile:File

File to load.

setReplaceFilterResourceback to summary
public void setReplaceFilterResource(Resource replaceFilter)

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.

Parameters
replaceFilter:Resource

Resource to load.

Since
Ant 1.8.0
setSummaryback to summary
public void setSummary(boolean summary)

Indicates whether a summary of the replace operation should be produced, detailing how many token occurrences and files were processed; optional, default=false.

Parameters
summary:boolean

boolean whether a summary of the replace operation should be logged.

setTokenback to summary
public void setToken(String token)

Set the string token to replace; required unless a nested replacetoken element or the replacefilterresource attribute is used.

Parameters
token:String

token String.

setValueback to summary
public void setValue(String value)

Set the string value to use as token replacement; optional, default is the empty string "".

Parameters
value:String

replacement value.

stringReplaceback to summary
private void stringReplace(StringBuilder str, String str1, String str2)

Replace occurrences of str1 in StringBuffer str with str2.

Parameters
str:StringBuilder

StringBuilder

str1:String

String

str2:String

String

validateAttributesback to summary
public void validateAttributes() throws BuildException

Validate attributes provided for this task in .xml build file.

Exceptions
BuildException:
if any supplied attribute is invalid or any mandatory attribute is missing.
validateReplacefiltersback to summary
public void validateReplacefilters() throws BuildException

Validate nested elements.

Exceptions
BuildException:
if any supplied attribute is invalid or any mandatory attribute is missing.
org.apache.tools.ant.taskdefs back to summary

private Class Replace.FileInput

extends Object
implements AutoCloseable
Class Inheritance
All Implemented Interfaces
java.lang.AutoCloseable

Class reading a file in small chunks, and presenting these chunks in a StringBuffer. Compatible with the Replacefilter.
Since
1.7

Field Summary

Modifier and TypeField and Description
private static final int
private char[]
private final InputStream
private StringBuffer
private Reader

Constructor Summary

AccessConstructor and Description
pack-priv
FileInput(File
The file to read from.
source
)

Constructs the input component.

Method Summary

Modifier and TypeMethod and Description
public void
close()

Implements java.lang.AutoCloseable.close.

Closes the file.
pack-priv StringBuffer

Returns:

The StringBuffer containing the output of this filter.
getOutputBuffer
()

Retrieves the output buffer of this filter.

pack-priv boolean

Returns:

true when the end of the file has not been reached.
readChunk
()

Reads some data from the file.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

BUFF_SIZEback to summary
private static final int BUFF_SIZE
bufferback to summary
private char[] buffer
isback to summary
private final InputStream is
outputBufferback to summary
private StringBuffer outputBuffer
readerback to summary
private Reader reader

Constructor Detail

FileInputback to summary
pack-priv FileInput(File source) throws IOException

Constructs the input component. Opens the file for reading.

Parameters
source:File

The file to read from.

Exceptions
IOException:
When the file cannot be read from.

Method Detail

closeback to summary
public void close() throws IOException

Implements java.lang.AutoCloseable.close.

Closes the file.

Annotations
@Override
Exceptions
IOException:
When the file cannot be closed.
getOutputBufferback to summary
pack-priv StringBuffer getOutputBuffer()

Retrieves the output buffer of this filter. The component guarantees that data is only appended to the end of this StringBuffer.

Returns:StringBuffer

The StringBuffer containing the output of this filter.

readChunkback to summary
pack-priv boolean readChunk() throws IOException

Reads some data from the file.

Returns:boolean

true when the end of the file has not been reached.

Exceptions
IOException:
When the file cannot be read from.
org.apache.tools.ant.taskdefs back to summary

private Class Replace.FileOutput

extends Object
implements AutoCloseable
Class Inheritance
All Implemented Interfaces
java.lang.AutoCloseable

Component writing a file in chunks, taking the chunks from the Replacefilter.
Since
1.7

Field Summary

Modifier and TypeField and Description
private StringBuffer
private final OutputStream
private Writer

Constructor Summary

AccessConstructor and Description
pack-priv
FileOutput(File
The file to read to.
out
)

Constructs the output component.

Method Summary

Modifier and TypeMethod and Description
public void
close()

Implements java.lang.AutoCloseable.close.

Closes the file.
pack-priv void
flush()

Processes the buffer to the end.

pack-priv boolean

Returns:

false to be inline with the Replacefilter. (Yes defining an interface crossed my mind, but would publish the internal behavior.)
process
()

Writes the buffer as far as possible.

pack-priv void
setInputBuffer(StringBuffer
The input for this filter.
input
)

Sets the input buffer for this component.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

inputBufferback to summary
private StringBuffer inputBuffer
osback to summary
private final OutputStream os
writerback to summary
private Writer writer

Constructor Detail

FileOutputback to summary
pack-priv FileOutput(File out) throws IOException

Constructs the output component. Opens the file for writing.

Parameters
out:File

The file to read to.

Exceptions
IOException:
When the file cannot be read from.

Method Detail

closeback to summary
public void close() throws IOException

Implements java.lang.AutoCloseable.close.

Closes the file.

Annotations
@Override
Exceptions
IOException:
When the file cannot be closed.
flushback to summary
pack-priv void flush() throws IOException

Processes the buffer to the end.

Exceptions
IOException:
when the output cannot be flushed.
processback to summary
pack-priv boolean process() throws IOException

Writes the buffer as far as possible.

Returns:boolean

false to be inline with the Replacefilter. (Yes defining an interface crossed my mind, but would publish the internal behavior.)

Exceptions
IOException:
when the output cannot be written.
setInputBufferback to summary
pack-priv void setInputBuffer(StringBuffer input)

Sets the input buffer for this component. The filter expects from the component providing the input that data is only added by that component to the end of this StringBuffer. This StringBuffer will be modified by this filter, and expects that another component will only append to this StringBuffer.

Parameters
input:StringBuffer

The input for this filter.

org.apache.tools.ant.taskdefs back to summary

public Class Replace.NestedString

extends Object
Class Inheritance

An inline string to use as the replacement text.

Field Summary

Modifier and TypeField and Description
private StringBuffer
private boolean

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
addText(String
the string to add
val
)

The text of the element.

public String

Returns:

the text
getText
()

public void
setExpandProperties(boolean
boolean
b
)

Whether properties should be expanded in nested test.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

bufback to summary
private StringBuffer buf
expandPropertiesback to summary
private boolean expandProperties

Constructor Detail

NestedStringback to summary
public NestedString()

Method Detail

addTextback to summary
public void addText(String val)

The text of the element.

Parameters
val:String

the string to add

getTextback to summary
public String getText()
Returns:String

the text

setExpandPropertiesback to summary
public void setExpandProperties(boolean b)

Whether properties should be expanded in nested test.

If you use this class via its Java interface the text you add via addText has most likely been expanded already so you do not want to set this to true.

Parameters
b:boolean

boolean

Since
Ant 1.8.0
org.apache.tools.ant.taskdefs back to summary

public Class Replace.Replacefilter

extends Object
Class Inheritance

A filter to apply.

Field Summary

Modifier and TypeField and Description
private StringBuffer
private StringBuffer
private String
private String
private Replace.NestedString
private Replace.NestedString

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Replace.NestedString

Returns:

nested token NestedString to configure.
createReplaceToken
()

Create a token to filter as the text of a nested element.

public Replace.NestedString

Returns:

replacement value NestedString to configure.
createReplaceValue
()

Create a string to replace the token as the text of a nested element.

pack-priv void
flush()

Processes the buffer to the end.

pack-priv StringBuffer

Returns:

The StringBuffer containing the output of this filter.
getOutputBuffer
()

Retrieves the output buffer of this filter.

public String

Returns:

property or null.
getProperty
()

Get the name of the property whose value is to serve as the replacement value.

public String

Returns:

the replacement value
getReplaceValue
()

Get the replacement value for this filter token.

public String

Returns:

current String token.
getToken
()

Get the string to search for.

public String

Returns:

replacement or null.
getValue
()

Get replacement String.

pack-priv boolean

Returns:

true if some data has been made available in the output buffer.
process
()

Processes the buffer as far as possible.

private int

Returns:

The position of the last character that was inserted as replacement.
replace
()

Performs the replace operation.

pack-priv void
setInputBuffer(StringBuffer
The input for this filter.
input
)

Sets the input buffer for this filter.

public void
setProperty(String
property name.
property
)

Set the name of the property whose value is to serve as the replacement value; required if value is not set.

public void
setToken(String
String token.
t
)

Set the token to replace.

public void
setValue(String
String value to replace.
value
)

The replacement string; required if property is not set.

public void
validate()

Validate the filter's configuration.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

inputBufferback to summary
private StringBuffer inputBuffer
outputBufferback to summary
private StringBuffer outputBuffer
propertyback to summary
private String property
replaceValueback to summary
private String replaceValue
tokenback to summary
private Replace.NestedString token
valueback to summary
private Replace.NestedString value

Constructor Detail

Replacefilterback to summary
public Replacefilter()

Method Detail

createReplaceTokenback to summary
public Replace.NestedString createReplaceToken()

Create a token to filter as the text of a nested element.

Returns:Replace.NestedString

nested token NestedString to configure.

Since
Ant 1.8.0
createReplaceValueback to summary
public Replace.NestedString createReplaceValue()

Create a string to replace the token as the text of a nested element.

Returns:Replace.NestedString

replacement value NestedString to configure.

Since
Ant 1.8.0
flushback to summary
pack-priv void flush()

Processes the buffer to the end. Does not take into account that appended data may make it possible to replace the end of the already received data.

getOutputBufferback to summary
pack-priv StringBuffer getOutputBuffer()

Retrieves the output buffer of this filter. The filter guarantees that data is only appended to the end of this StringBuffer.

Returns:StringBuffer

The StringBuffer containing the output of this filter.

getPropertyback to summary
public String getProperty()

Get the name of the property whose value is to serve as the replacement value.

Returns:String

property or null.

getReplaceValueback to summary
public String getReplaceValue()

Get the replacement value for this filter token.

Returns:String

the replacement value

getTokenback to summary
public String getToken()

Get the string to search for.

Returns:String

current String token.

getValueback to summary
public String getValue()

Get replacement String.

Returns:String

replacement or null.

processback to summary
pack-priv boolean process()

Processes the buffer as far as possible. Takes into account that appended data may make it possible to replace the end of the already received data, when the token is split over the "old" and the "new" part.

Returns:boolean

true if some data has been made available in the output buffer.

replaceback to summary
private int replace()

Performs the replace operation.

Returns:int

The position of the last character that was inserted as replacement.

setInputBufferback to summary
pack-priv void setInputBuffer(StringBuffer input)

Sets the input buffer for this filter. The filter expects from the component providing the input that data is only added by that component to the end of this StringBuffer. This StringBuffer will be modified by this filter, and expects that another component will only append to this StringBuffer.

Parameters
input:StringBuffer

The input for this filter.

setPropertyback to summary
public void setProperty(String property)

Set the name of the property whose value is to serve as the replacement value; required if value is not set.

Parameters
property:String

property name.

setTokenback to summary
public void setToken(String t)

Set the token to replace.

Parameters
t:String

String token.

setValueback to summary
public void setValue(String value)

The replacement string; required if property is not set.

Parameters
value:String

String value to replace.

validateback to summary
public void validate() throws BuildException

Validate the filter's configuration.

Exceptions
BuildException:
if any part is invalid.