Package org.codehaus.plexus.util
Class InterpolationFilterReader
- java.lang.Object
-
- java.io.Reader
-
- java.io.FilterReader
-
- org.codehaus.plexus.util.InterpolationFilterReader
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,java.lang.Readable
public class InterpolationFilterReader extends java.io.FilterReader
A FilterReader which interpolates keyword values into a character stream. Keywords are recognized when enclosed between starting and ending delimiter strings. The keywords themselves, and their values, are fetched from a Map supplied to the constructor.When a possible keyword token is recognized (by detecting the starting and ending token delimiters):
- if the enclosed string is found in the keyword Map, the delimiters and the keyword are effectively replaced by the keyword's value;
- if the enclosed string is found in the keyword Map, but its value has zero length, then the token (delimiters and keyword) is effectively removed from the character stream;
- if the enclosed string is not found in the keyword Map, then no substitution is made; the token text is passed through unaltered.
- See Also:
s
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.String
beginToken
Character marking the beginning of a token.private int
beginTokenLength
Length of begin token.private static java.lang.String
DEFAULT_BEGIN_TOKEN
Default begin token.private static java.lang.String
DEFAULT_END_TOKEN
Default end token.private java.lang.String
endToken
Character marking the end of a token.private int
endTokenLength
Length of end token.private int
previousIndex
Index into previous dataprivate java.lang.String
replaceData
replacement text from a tokenprivate int
replaceIndex
Index into replacement dataprivate java.util.Map<?,java.lang.Object>
variables
Hashtable to hold the replacee-replacer pairs (String to String).
-
Constructor Summary
Constructors Constructor Description InterpolationFilterReader(java.io.Reader in, java.util.Map<?,java.lang.Object> variables, java.lang.String beginToken, java.lang.String endToken)
Construct a Reader to interpolate values enclosed between the given delimiter tokens.InterpolationFilterReader(java.io.Reader in, java.util.Map<java.lang.String,java.lang.Object> variables)
Construct a Reader using the default interpolation delimiter tokens "${" and "}".
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
read()
Returns the next character in the filtered stream, replacing tokens from the original stream.int
read(char[] cbuf, int off, int len)
Reads characters into a portion of an array.long
skip(long n)
Skips characters.
-
-
-
Field Detail
-
replaceData
private java.lang.String replaceData
replacement text from a token
-
replaceIndex
private int replaceIndex
Index into replacement data
-
previousIndex
private int previousIndex
Index into previous data
-
variables
private java.util.Map<?,java.lang.Object> variables
Hashtable to hold the replacee-replacer pairs (String to String).
-
beginToken
private java.lang.String beginToken
Character marking the beginning of a token.
-
endToken
private java.lang.String endToken
Character marking the end of a token.
-
beginTokenLength
private int beginTokenLength
Length of begin token.
-
endTokenLength
private int endTokenLength
Length of end token.
-
DEFAULT_BEGIN_TOKEN
private static final java.lang.String DEFAULT_BEGIN_TOKEN
Default begin token.- See Also:
- Constant Field Values
-
DEFAULT_END_TOKEN
private static final java.lang.String DEFAULT_END_TOKEN
Default end token.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
InterpolationFilterReader
public InterpolationFilterReader(java.io.Reader in, java.util.Map<?,java.lang.Object> variables, java.lang.String beginToken, java.lang.String endToken)
Construct a Reader to interpolate values enclosed between the given delimiter tokens.- Parameters:
in
- a Reader to be wrapped for interpolation.variables
- name/value pairs to be interpolated into the character stream.beginToken
- an interpolation target begins with this.endToken
- an interpolation target ends with this.
-
InterpolationFilterReader
public InterpolationFilterReader(java.io.Reader in, java.util.Map<java.lang.String,java.lang.Object> variables)
Construct a Reader using the default interpolation delimiter tokens "${" and "}".- Parameters:
in
- a Reader to be wrapped for interpolation.variables
- name/value pairs to be interpolated into the character stream.
-
-
Method Detail
-
skip
public long skip(long n) throws java.io.IOException
Skips characters. This method will block until some characters are available, an I/O error occurs, or the end of the stream is reached.- Overrides:
skip
in classjava.io.FilterReader
- Parameters:
n
- The number of characters to skip- Returns:
- the number of characters actually skipped
- Throws:
java.lang.IllegalArgumentException
- Ifn
is negative.java.io.IOException
- If an I/O error occurs
-
read
public int read(char[] cbuf, int off, int len) throws java.io.IOException
Reads characters into a portion of an array. This method will block until some input is available, an I/O error occurs, or the end of the stream is reached.- Overrides:
read
in classjava.io.FilterReader
- Parameters:
cbuf
- Destination buffer to write characters to. Must not benull
.off
- Offset at which to start storing characters.len
- Maximum number of characters to read.- Returns:
- the number of characters read, or -1 if the end of the stream has been reached
- Throws:
java.io.IOException
- If an I/O error occurs
-
read
public int read() throws java.io.IOException
Returns the next character in the filtered stream, replacing tokens from the original stream.- Overrides:
read
in classjava.io.FilterReader
- Returns:
- the next character in the resulting stream, or -1 if the end of the resulting stream has been reached
- Throws:
java.io.IOException
- if the underlying stream throws an IOException during reading
-
-