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

public final Class ReplaceTokens

extends BaseParamFilterReader
implements ChainableReader
Class Inheritance
All Implemented Interfaces
org.apache.tools.ant.filters.ChainableReader
Imports
java.io.File, .IOException, .InputStream, .Reader, java.util.Hashtable, .Map, .Properties, .SortedMap, .TreeMap, org.apache.tools.ant.Project, org.apache.tools.ant.types.Parameter, .Resource, org.apache.tools.ant.types.resources.FileResource, org.apache.tools.ant.util.FileUtils

Replaces tokens in the original input with user-supplied values. Example:
<replacetokens begintoken="#" endtoken="#">
  <token key="DATE" value="${TODAY}"/>
</replacetokens>
Or:
<filterreader classname="org.apache.tools.ant.filters.ReplaceTokens">
  <param type="tokenchar" name="begintoken" value="#"/>
  <param type="tokenchar" name="endtoken" value="#"/>
  <param type="token" name="DATE" value="${TODAY}"/>
</filterreader>

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
ReplaceTokens.Token

Holds a token

Field Summary

Modifier and TypeField and Description
private String
beginToken

Character marking the beginning of a token.

private static final String
DEFAULT_BEGIN_TOKEN

Default "begin token" character.

private static final String
DEFAULT_END_TOKEN

Default "end token" character.

private String
endToken

Character marking the end of a token.

private Hashtable<String, String>
hash

Hashtable to holds the original replacee-replacer pairs (String to String).

private String
readBuffer

Used for comparisons and lookup into the resolvedTokens map.

private String
replaceData

replacement test from a token

private int
replaceIndex

Index into replacement data

private final TreeMap<String, String>
resolvedTokens

This map holds the "resolved" tokens (begin- and end-tokens are added to make searching simpler)

private boolean

Constructor Summary

AccessConstructor and Description
public
ReplaceTokens()

Constructor for "dummy" instances.

public
ReplaceTokens(final Reader
A Reader object providing the underlying stream. Must not be null.
in
)

Creates a new filtered reader.

Method Summary

Modifier and TypeMethod and Description
public void
addConfiguredToken(final ReplaceTokens.Token
The token to add to the map of replacements. Must not be null.
token
)

Adds a token element to the map of tokens to replace.

public Reader

Returns:

a new filter based on this configuration, but filtering the specified reader
chain
(final Reader
A Reader object providing the underlying stream. Must not be null.
rdr
)

Implements org.apache.tools.ant.filters.ChainableReader.chain.

Creates a new ReplaceTokens using the passed in Reader for instantiation.
private String

Returns:

the character used to denote the beginning of a token
getBeginToken
()

Returns the "begin token" character.

private String

Returns:

the character used to denote the end of a token
getEndToken
()

Returns the "end token" character.

private int

Returns:

the first character from the read buffer or -1 if read buffer is empty.
getFirstCharacterFromReadBuffer
()

private Properties
getProperties(Resource
The resource to load properties from.
resource
)

Returns properties from a specified properties file.

private Hashtable<String, String>

Returns:

a map (String->String) of token keys to replacement values
getTokens
()

Returns the map of tokens which will be replaced.

private void
initialize()

Initializes tokens and loads the replacee-replacer hashtable.

private void
public int

Returns:

the next character in the resulting stream, or -1 if the end of the resulting stream has been reached
read
()

Overrides java.io.FilterReader.read.

Returns the next character in the filtered stream, replacing tokens from the original stream.
public void
setBeginToken(final String
the character used to denote the beginning of a token
beginToken
)

Sets the "begin token" character.

public void
setEndToken(final String
the character used to denote the end of a token
endToken
)

Sets the "end token" character.

public void
setPropertiesResource(Resource
Resource
r
)

A resource containing properties, each of which is interpreted as a token/value pair.

private void
setTokens(final Hashtable<String, String>
A map (String->String) of token keys to replacement values. Must not be null.
hash
)

Sets the map of tokens to replace.

Inherited from org.apache.tools.ant.filters.BaseParamFilterReader:
getParameterssetParameters

Field Detail

beginTokenback to summary
private String beginToken

Character marking the beginning of a token.

DEFAULT_BEGIN_TOKENback to summary
private static final String DEFAULT_BEGIN_TOKEN

Default "begin token" character.

DEFAULT_END_TOKENback to summary
private static final String DEFAULT_END_TOKEN

Default "end token" character.

endTokenback to summary
private String endToken

Character marking the end of a token.

hashback to summary
private Hashtable<String, String> hash

Hashtable to holds the original replacee-replacer pairs (String to String).

readBufferback to summary
private String readBuffer

Used for comparisons and lookup into the resolvedTokens map.

replaceDataback to summary
private String replaceData

replacement test from a token

replaceIndexback to summary
private int replaceIndex

Index into replacement data

resolvedTokensback to summary
private final TreeMap<String, String> resolvedTokens

This map holds the "resolved" tokens (begin- and end-tokens are added to make searching simpler)

resolvedTokensBuiltback to summary
private boolean resolvedTokensBuilt

Constructor Detail

ReplaceTokensback to summary
public ReplaceTokens()

Constructor for "dummy" instances.

See Also
BaseFilterReader#BaseFilterReader()
ReplaceTokensback to summary
public ReplaceTokens(final Reader in)

Creates a new filtered reader.

Parameters
in:Reader

A Reader object providing the underlying stream. Must not be null.

Method Detail

addConfiguredTokenback to summary
public void addConfiguredToken(final ReplaceTokens.Token token)

Adds a token element to the map of tokens to replace.

Parameters
token:ReplaceTokens.Token

The token to add to the map of replacements. Must not be null.

chainback to summary
public Reader chain(final Reader rdr)

Implements org.apache.tools.ant.filters.ChainableReader.chain.

Creates a new ReplaceTokens using the passed in Reader for instantiation.

Parameters
rdr:Reader

A Reader object providing the underlying stream. Must not be null.

Returns:Reader

a new filter based on this configuration, but filtering the specified reader

getBeginTokenback to summary
private String getBeginToken()

Returns the "begin token" character.

Returns:String

the character used to denote the beginning of a token

getEndTokenback to summary
private String getEndToken()

Returns the "end token" character.

Returns:String

the character used to denote the end of a token

getFirstCharacterFromReadBufferback to summary
private int getFirstCharacterFromReadBuffer()
Returns:int

the first character from the read buffer or -1 if read buffer is empty.

getPropertiesback to summary
private Properties getProperties(Resource resource)

Returns properties from a specified properties file.

Parameters
resource:Resource

The resource to load properties from.

getTokensback to summary
private Hashtable<String, String> getTokens()

Returns the map of tokens which will be replaced.

Returns:Hashtable<String, String>

a map (String->String) of token keys to replacement values

initializeback to summary
private void initialize()

Initializes tokens and loads the replacee-replacer hashtable.

makeTokensFromPropertiesback to summary
private void makeTokensFromProperties(Resource r)
readback to summary
public int read() throws IOException

Overrides java.io.FilterReader.read.

Returns the next character in the filtered stream, replacing tokens from the original stream.

Returns:int

the next character in the resulting stream, or -1 if the end of the resulting stream has been reached

Exceptions
IOException:
if the underlying stream throws an IOException during reading
setBeginTokenback to summary
public void setBeginToken(final String beginToken)

Sets the "begin token" character.

Parameters
beginToken:String

the character used to denote the beginning of a token

setEndTokenback to summary
public void setEndToken(final String endToken)

Sets the "end token" character.

Parameters
endToken:String

the character used to denote the end of a token

setPropertiesResourceback to summary
public void setPropertiesResource(Resource r)

A resource containing properties, each of which is interpreted as a token/value pair.

Parameters
r:Resource

Resource

Since
Ant 1.8.0
setTokensback to summary
private void setTokens(final Hashtable<String, String> hash)

Sets the map of tokens to replace.

Parameters
hash:Hashtable<String, String>

A map (String->String) of token keys to replacement values. Must not be null.

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

public Class ReplaceTokens.Token

extends Object
Class Inheritance

Holds a token

Field Summary

Modifier and TypeField and Description
private String
key

Token key

private String
value

Token value

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public final String

Returns:

the key for this token
getKey
()

Returns the key for this token.

public final String

Returns:

the value for this token
getValue
()

Returns the value for this token.

public final void
setKey(String
The key for this token. Must not be null.
key
)

Sets the token key

public final void
setValue(String
The value for this token. Must not be null.
value
)

Sets the token value

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

keyback to summary
private String key

Token key

valueback to summary
private String value

Token value

Constructor Detail

Tokenback to summary
public Token()

Method Detail

getKeyback to summary
public final String getKey()

Returns the key for this token.

Returns:String

the key for this token

getValueback to summary
public final String getValue()

Returns the value for this token.

Returns:String

the value for this token

setKeyback to summary
public final void setKey(String key)

Sets the token key

Parameters
key:String

The key for this token. Must not be null.

setValueback to summary
public final void setValue(String value)

Sets the token value

Parameters
value:String

The value for this token. Must not be null.