public class VariablesWriter extends Writer
${...}
notation. Values are injected on the fly, while the character stream is written to target writer. This
class gets a variables map from where retrieve values by name. If a variable from stream has a name not existing into
variables map, stream variable remains unresolved.
+----------+ (1) +-----------------------+ (2) +---------------+ | template +---------> VariablesWriter.write +---------> target writer | +----------+ +-----------------------+ +---------------+ 1 - variables stream 2 - stream with values injected
Variables writer is in fact a decorator. It adds variable injection functionality to an ordinary writer. If none provided on
constructor uses an internal StringWriter
.
// variables writer with string target writer VariablesWriter writer = new VariablesWriter(variables); // copy template and inject values on the fly Files.copy(new StringReader(template), writer); // string with values resolved String string = writer.toString();
Modifier and Type | Class and Description |
---|---|
private static class |
VariablesWriter.State
Internal state machine for variables writer parser.
|
Modifier and Type | Field and Description |
---|---|
private VariablesWriter.State |
state
Current state of the finite states machine used by variables parser.
|
private Writer |
targetWriter
Target writer is where stream with variables injected is actually being write.
|
private StringBuilder |
variableBuilder
String builder for variable name discovered on variables writer.
|
private Map<String,String> |
variables
Variable values map.
|
Constructor and Description |
---|
VariablesWriter(Map<String,String> variables)
Construct a variables writer instance targeting an internal string writer.
|
VariablesWriter(Writer targetWriter,
Map<String,String> variables)
Construct a variables writer decorator for given target writer.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Close target writer.
|
void |
flush()
Flush target writer.
|
String |
toString()
Return target writer string value.
|
void |
write(char[] cbuf,
int off,
int len)
Inject values into given variables stream and write the result to target writer.
|
private Writer targetWriter
private StringBuilder variableBuilder
private VariablesWriter.State state
public VariablesWriter(Map<String,String> variables)
toString()
.variables
- variables map.public void write(char[] cbuf, int off, int len) throws IOException
write
in class Writer
cbuf
- characters from variables stream,off
- buffer offset,len
- buffer length.IOException
public void flush() throws IOException
flush
in interface Flushable
flush
in class Writer
IOException
public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
close
in class Writer
IOException
Copyright © 2018. All rights reserved.