org.pfsw.text
Class FormattedTextWriter

java.lang.Object
  extended by org.pfsw.text.FormattedTextWriter
All Implemented Interfaces:
java.io.Closeable, java.lang.Appendable

public class FormattedTextWriter
extends java.lang.Object
implements java.lang.Appendable, java.io.Closeable

This is a wrapper on an Appendable object. It supports writing to the underlying appendable and particularly provides easy to use indentation.


Constructor Summary
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.
 
Method Summary
 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  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 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.
 FormattedTextWriter write(org.pfsw.bif.text.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.
protected  FormattedTextWriter writeIndentation()
           
 FormattedTextWriter writeln(org.pfsw.bif.text.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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FormattedTextWriter

public FormattedTextWriter(java.lang.Appendable output)
Creates a new instance that writes to the given output object.

Parameters:
output - The target object to append data to (must not be null).
Throws:
java.lang.IllegalArgumentException - if the given output is null.

FormattedTextWriter

public FormattedTextWriter(java.io.File outputFile,
                           java.nio.charset.Charset charEncoding)
                    throws java.io.IOException
Creates a new instance that writes to the given output file with the specified character encoding.

Parameters:
outputFile - The target file to append data to (must not be null).
charEncoding - The encoding of the text to be written to the file.
Throws:
java.io.IOException - if the file cannot be found or not be opened.
java.lang.IllegalArgumentException - if the given outputFile or charEncoding is null.

FormattedTextWriter

public FormattedTextWriter(java.io.File outputFile)
                    throws java.io.IOException
Creates a new instance that writes to the given output file with UTF-8 character encoding.

Parameters:
outputFile - The target file to append data to (must not be null).
Throws:
java.io.IOException - if the file cannot be found or not be opened.
java.lang.IllegalArgumentException - if the given outputFile or charEncoding is null.
Method Detail

create

public static FormattedTextWriter create(java.lang.Appendable output)
Creates a new instance that writes to the given output object.

Parameters:
output - The target object to append data to (must not be null).
Returns:
The new created writer.
Throws:
java.lang.IllegalArgumentException - if the given output is null.

create

public static FormattedTextWriter create(java.lang.Appendable output,
                                         int indentSize)
Creates a new instance that writes to the given output object an indentation size.

Parameters:
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).
Returns:
The new created writer.
Throws:
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.

create

public 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.

Parameters:
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.
Returns:
The new created writer.
Throws:
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.

create

public static FormattedTextWriter create(java.io.File outputFile,
                                         java.nio.charset.Charset charEncoding)
                                  throws java.io.IOException
Creates a new instance that writes to the given output file with the specified character encoding.

Parameters:
outputFile - The target file to append data to (must not be null).
charEncoding - The encoding of the text to be written to the file.
Throws:
java.io.IOException - if the file cannot be found or not be opened.
java.lang.IllegalArgumentException - if the given outputFile or charEncoding is null.

create

public static FormattedTextWriter create(java.io.File outputFile)
                                  throws java.io.IOException
Creates a new instance that writes to the given output file with UTF-8 character encoding.

Parameters:
outputFile - The target file to append data to (must not be null).
Throws:
java.io.IOException - if the file cannot be found or not be opened.
java.lang.IllegalArgumentException - if the given outputFile or charEncoding is null.

write

public FormattedTextWriter write(java.lang.String text,
                                 java.lang.Object... args)
Replaces any placeholders (e.g. %s, %d) in text by the specified arguments an appends it to the underlying output.

Parameters:
text - The text to write.
args - The optional arguments to replace the placeholders in text.
Returns:
This object to allow fluent API usage.
Throws:
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 null
org.pfsw.bif.exceptions.IORuntimeException - If any IOException occurred by writing to the underlying output.
java.lang.IllegalStateException - If this writer has been closed.

write

public FormattedTextWriter write(org.pfsw.bif.text.IStringRepresentation object)
The same as write(String, Object...), except that only the given object is converted to its string representation.


writeln

public FormattedTextWriter writeln(java.lang.String text,
                                   java.lang.Object... args)
Replaces any placeholders (e.g. %s, %d) in text by the specified arguments an appends it to the underlying output, followed by a line break. The placeholder replacement is based on String.format(String, Object...).

Parameters:
text - The text to write.
args - The optional arguments to replace the placeholders in text.
Returns:
This object to allow fluent API usage.
Throws:
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 null
org.pfsw.bif.exceptions.IORuntimeException - If any IOException occurred by writing to the underlying output.
java.lang.IllegalStateException - If this writer has been closed.

writeln

public FormattedTextWriter writeln(org.pfsw.bif.text.IStringRepresentation object)
The same as writeln(String, Object...), except that only the given object is converted to its string representation.


newLine

public FormattedTextWriter newLine()
Writes the newline character(s).


indent

public FormattedTextWriter indent()
Increases the indentation level by 1.


outdent

public FormattedTextWriter outdent()
Decreases the indentation level by 1.


append

public java.lang.Appendable append(char c)
                            throws java.io.IOException
Specified by:
append in interface java.lang.Appendable
Throws:
java.lang.IllegalStateException - If this writer has been closed.
java.io.IOException

append

public java.lang.Appendable append(java.lang.CharSequence csq)
                            throws java.io.IOException
Specified by:
append in interface java.lang.Appendable
Throws:
java.lang.IllegalStateException - If this writer has been closed.
java.io.IOException

append

public java.lang.Appendable append(java.lang.CharSequence csq,
                                   int start,
                                   int end)
                            throws java.io.IOException
Specified by:
append in interface java.lang.Appendable
Throws:
java.lang.IllegalStateException - If this writer has been closed.
java.io.IOException

close

public void close()
           throws java.io.IOException
Specified by:
close in interface java.io.Closeable
Throws:
java.io.IOException

getIndentSize

public int getIndentSize()
Returns the number of spaces to be used for indentation per indentation level.


setIndentSize

public FormattedTextWriter setIndentSize(int indentSize)
Sets the number of spaces to be used for indentation per indentation level.

Parameters:
indentSize - The number of spaces per indentation level (must be in the range of 0-20).
Throws:
java.lang.IllegalArgumentException - if the given size is less than 0 or greater than 20.

getNewline

public java.lang.String getNewline()
Returns the new line character(s) used by this writer.


setNewline

public FormattedTextWriter setNewline(java.lang.String newline)
Sets the new line character(s) to be used by this writer.

Throws:
java.lang.IllegalArgumentException - if the given string is null.

getIndentLevel

public int getIndentLevel()

getOutput

protected java.lang.Appendable getOutput()

getMaxIndentSize

protected int getMaxIndentSize()

indentationRequired

protected boolean indentationRequired()

getNumberOfIndentSpaces

protected int getNumberOfIndentSpaces()

writeIndentation

protected FormattedTextWriter writeIndentation()

checkOpenState

protected void checkOpenState()