public class FormattedTextWriter extends java.lang.Object implements java.lang.Appendable, java.io.Closeable, IIndentedTextWriter<FormattedTextWriter>
Appendable
object.
It supports writing to the underlying appendable and particularly provides
easy to use indentation.Constructor and Description |
---|
FormattedTextWriter(java.lang.Appendable output)
Creates a new instance that writes to the given output object.
|
FormattedTextWriter(java.io.File outputFile)
Creates a new instance that writes to the given output file with UTF-8 character encoding.
|
FormattedTextWriter(java.io.File outputFile,
java.nio.charset.Charset charEncoding)
Creates a new instance that writes to the given output file with the specified character encoding.
|
Modifier and Type | Method and Description |
---|---|
java.lang.Appendable |
append(char c) |
java.lang.Appendable |
append(java.lang.CharSequence csq) |
java.lang.Appendable |
append(java.lang.CharSequence csq,
int start,
int end) |
protected java.lang.String |
asString(java.lang.Object object) |
protected void |
checkOpenState() |
void |
close() |
static FormattedTextWriter |
create(java.lang.Appendable output)
Creates a new instance that writes to the given output object.
|
static FormattedTextWriter |
create(java.lang.Appendable output,
int indentSize)
Creates a new instance that writes to the given output object an indentation size.
|
static FormattedTextWriter |
create(java.lang.Appendable output,
int indentSize,
NewLine newLine)
Creates a new instance that writes to the given output object, indentation size and newline characters.
|
static FormattedTextWriter |
create(java.io.File outputFile)
Creates a new instance that writes to the given output file with UTF-8 character encoding.
|
static FormattedTextWriter |
create(java.io.File outputFile,
java.nio.charset.Charset charEncoding)
Creates a new instance that writes to the given output file with the specified character encoding.
|
int |
getIndentLevel() |
int |
getIndentSize()
Returns the number of spaces to be used for indentation per indentation level.
|
protected int |
getMaxIndentSize() |
java.lang.String |
getNewline()
Returns the new line character(s) used by this writer.
|
protected int |
getNumberOfIndentSpaces() |
protected java.lang.Appendable |
getOutput() |
FormattedTextWriter |
indent()
Increases the indentation level by 1.
|
protected boolean |
indentationRequired() |
FormattedTextWriter |
newLine()
Writes the newline character(s).
|
FormattedTextWriter |
outdent()
Decreases the indentation level by 1.
|
FormattedTextWriter |
self() |
FormattedTextWriter |
setIndentSize(int indentSize)
Sets the number of spaces to be used for indentation per indentation level.
|
FormattedTextWriter |
setNewline(java.lang.String newline)
Sets the new line character(s) to be used by this writer.
|
protected StringUtil |
stringUtil() |
FormattedTextWriter |
write(IStringRepresentation object)
The same as
write(String, Object...) , except that only the given
object is converted to its string representation. |
FormattedTextWriter |
write(java.lang.String text,
java.lang.Object... args)
Replaces any placeholders (e.g.
|
FormattedTextWriter |
writeAll(java.lang.Object... objects)
Writes all given arguments to the output target.
|
protected FormattedTextWriter |
writeIndentation() |
FormattedTextWriter |
writeln(IStringRepresentation object)
The same as
writeln(String, Object...) , except that only the given
object is converted to its string representation. |
FormattedTextWriter |
writeln(java.lang.String text,
java.lang.Object... args)
Replaces any placeholders (e.g.
|
FormattedTextWriter |
writelnAll(java.lang.Object... objects)
Writes all given arguments to the output target and finally appends a new line.
|
FormattedTextWriter |
writelnText(java.lang.String text)
Writes the given text.
|
FormattedTextWriter |
writeText(java.lang.String text)
Writes the given text.
|
public FormattedTextWriter(java.lang.Appendable output)
output
- The target object to append data to (must not be null).java.lang.IllegalArgumentException
- if the given output is null.public FormattedTextWriter(java.io.File outputFile, java.nio.charset.Charset charEncoding) throws java.io.IOException
outputFile
- The target file to append data to (must not be null).charEncoding
- The encoding of the text to be written to the file.java.io.IOException
- if the file cannot be found or not be opened.java.lang.IllegalArgumentException
- if the given outputFile or charEncoding is null.public FormattedTextWriter(java.io.File outputFile) throws java.io.IOException
outputFile
- The target file to append data to (must not be null).java.io.IOException
- if the file cannot be found or not be opened.java.lang.IllegalArgumentException
- if the given outputFile or charEncoding is null.public static FormattedTextWriter create(java.lang.Appendable output)
output
- The target object to append data to (must not be null).java.lang.IllegalArgumentException
- if the given output is null.public static FormattedTextWriter create(java.lang.Appendable output, int indentSize)
output
- The target object to append data to (must not be null).indentSize
- The number of spaces per indentation level (must be in the range of 0-20).java.lang.IllegalArgumentException
- if the given output is null.java.lang.IllegalArgumentException
- if the given indentation size is less than 0 or greater than 20.public static FormattedTextWriter create(java.lang.Appendable output, int indentSize, NewLine newLine)
output
- The target object to append data to (must not be null).indentSize
- The number of spaces per indentation level (must be in the range of 0-20).newLine
- The new line character sequence to use.java.lang.IllegalArgumentException
- if the given output is null.java.lang.IllegalArgumentException
- if the given indentation size is less than 0 or greater than 20.public static FormattedTextWriter create(java.io.File outputFile, java.nio.charset.Charset charEncoding) throws java.io.IOException
outputFile
- The target file to append data to (must not be null).charEncoding
- The encoding of the text to be written to the file.java.io.IOException
- if the file cannot be found or not be opened.java.lang.IllegalArgumentException
- if the given outputFile or charEncoding is null.public static FormattedTextWriter create(java.io.File outputFile) throws java.io.IOException
outputFile
- The target file to append data to (must not be null).java.io.IOException
- if the file cannot be found or not be opened.java.lang.IllegalArgumentException
- if the given outputFile or charEncoding is null.public FormattedTextWriter write(java.lang.String text, java.lang.Object... args)
text
- The text to write.args
- The optional arguments to replace the placeholders in text.java.util.IllegalFormatException
- If a format string contains an illegal syntax, a format
specifier that is incompatible with the given arguments,
insufficient arguments given the format string, or other
illegal conditions. For specification of all possible
formatting errors, see the Details section of the
formatter class specification.java.lang.NullPointerException
- If the text is nullIORuntimeException
- If any IOException occurred by writing to the underlying output.java.lang.IllegalStateException
- If this writer has been closed.public FormattedTextWriter write(IStringRepresentation object)
write(String, Object...)
, except that only the given
object is converted to its string representation.public FormattedTextWriter writeAll(java.lang.Object... objects)
writeAll
in interface IIndentedTextWriter<FormattedTextWriter>
public FormattedTextWriter writeText(java.lang.String text)
writeText
in interface IIndentedTextWriter<FormattedTextWriter>
public FormattedTextWriter writelnText(java.lang.String text)
writelnText
in interface IIndentedTextWriter<FormattedTextWriter>
public FormattedTextWriter writeln(java.lang.String text, java.lang.Object... args)
String.format(String, Object...)
.text
- The text to write.args
- The optional arguments to replace the placeholders in text.java.util.IllegalFormatException
- If a format string contains an illegal syntax, a format
specifier that is incompatible with the given arguments,
insufficient arguments given the format string, or other
illegal conditions. For specification of all possible
formatting errors, see the Details section of the
formatter class specification.java.lang.NullPointerException
- If the text is nullIORuntimeException
- If any IOException occurred by writing to the underlying output.java.lang.IllegalStateException
- If this writer has been closed.public FormattedTextWriter writeln(IStringRepresentation object)
writeln(String, Object...)
, except that only the given
object is converted to its string representation.public FormattedTextWriter writelnAll(java.lang.Object... objects)
writelnAll
in interface IIndentedTextWriter<FormattedTextWriter>
public FormattedTextWriter newLine()
public FormattedTextWriter indent()
indent
in interface IIndentedTextWriter<FormattedTextWriter>
public FormattedTextWriter outdent()
outdent
in interface IIndentedTextWriter<FormattedTextWriter>
public java.lang.Appendable append(char c) throws java.io.IOException
append
in interface java.lang.Appendable
java.lang.IllegalStateException
- If this writer has been closed.java.io.IOException
public java.lang.Appendable append(java.lang.CharSequence csq) throws java.io.IOException
append
in interface java.lang.Appendable
java.lang.IllegalStateException
- If this writer has been closed.java.io.IOException
public java.lang.Appendable append(java.lang.CharSequence csq, int start, int end) throws java.io.IOException
append
in interface java.lang.Appendable
java.lang.IllegalStateException
- If this writer has been closed.java.io.IOException
public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
java.io.IOException
public int getIndentSize()
getIndentSize
in interface IIndentedTextWriter<FormattedTextWriter>
public FormattedTextWriter setIndentSize(int indentSize)
setIndentSize
in interface IIndentedTextWriter<FormattedTextWriter>
indentSize
- The number of spaces per indentation level (must be in the range of 0-20).java.lang.IllegalArgumentException
- if the given size is less than 0 or greater than 20.public java.lang.String getNewline()
public FormattedTextWriter setNewline(java.lang.String newline)
java.lang.IllegalArgumentException
- if the given string is null.public int getIndentLevel()
public FormattedTextWriter self()
self
in interface IExtensibleFluentApi<FormattedTextWriter>
protected java.lang.Appendable getOutput()
protected int getMaxIndentSize()
protected boolean indentationRequired()
protected int getNumberOfIndentSpaces()
protected FormattedTextWriter writeIndentation()
protected void checkOpenState()
protected java.lang.String asString(java.lang.Object object)
protected StringUtil stringUtil()