Class SubstitutingWriter
- java.lang.Object
-
- java.io.Writer
-
- de.xima.fc.common.io.SubstitutingWriter
-
- All Implemented Interfaces:
Closeable,Flushable,Appendable,AutoCloseable
public final class SubstitutingWriter extends Writer
Wraps a writer and applies substitutions "source -> target" to the content.Tokens are searched for and replaced in the order they are given. Usually, it is recommended to provide tokens in order of decreasing length, to ensure that longer tokens are replaced before shorter tokens that are contained in the longer tokens. For example, if you want to replace "abc" with "x" and "ab" with "y", you should provide the source tokens in the order "abc", "ab"; and the target tokens in the order "x", "y". If you provide the source tokens in the order "ab", "abc"; and the target tokens in the order "y", "x", the result will be that "abc" is replaced with "yc" instead of "x".
The implementation implements a lookahead equal to the largest source token, then inspects each character and checks if it is the start of any source token. If yes, writes the previous content to the writer, then skips the source token and writes the replacement target token instead.
Note:
Flushingthis writer does not flush characters that are still in the lookahead buffer, as they have not yet been processed for substitutions.This writer is not thread-safe and must be synchronized externally if used by multiple threads.
- Since:
- 8.5.4
-
-
Constructor Summary
Constructors Constructor Description SubstitutingWriter(Writer writer, char[][] sourceTokens, char[][] targetTokens)Creates a new substituting writer that applies the given substitutions to the content written to the given writer.SubstitutingWriter(Writer writer, String[] sourceTokens, String[] targetTokens)Creates a new substituting writer that applies the given substitutions to the content written to the given writer.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()voidflush()voidwrite(char[] chars, int off, int len)
-
-
-
Constructor Detail
-
SubstitutingWriter
public SubstitutingWriter(Writer writer, char[][] sourceTokens, char[][] targetTokens)
Creates a new substituting writer that applies the given substitutions to the content written to the given writer. The target tokens array should not contain more tokens than the source tokens array. Extra tokens are ignored. If there are fewer target tokens than source tokens, the last target token is used as replacement.- Parameters:
writer- The writer to wrap. Must not be null.sourceTokens- The source tokens to replace. Must not be null or empty, and every token must have a length greater than 0.targetTokens- The target tokens to replace with. Must not be null or empty.
-
SubstitutingWriter
public SubstitutingWriter(Writer writer, String[] sourceTokens, String[] targetTokens)
Creates a new substituting writer that applies the given substitutions to the content written to the given writer. The target tokens array should not contain more tokens than the source tokens array. Extra tokens are ignored. If there are fewer target tokens than source tokens, the last target token is used as replacement.- Parameters:
writer- The writer to wrap. Must not be null.sourceTokens- The source tokens to replace. Must not be null or empty.targetTokens- The target tokens to replace with. Must not be null or empty.
-
-
Method Detail
-
close
public void close() throws IOException- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein classWriter- Throws:
IOException
-
flush
public void flush() throws IOException- Specified by:
flushin interfaceFlushable- Specified by:
flushin classWriter- Throws:
IOException
-
write
public void write(char[] chars, int off, int len) throws IOException- Specified by:
writein classWriter- Throws:
IOException
-
-