com.overzealous.remark.util
Class BlockWriter

java.lang.Object
  extended by java.io.Writer
      extended by java.io.PrintWriter
          extended by com.overzealous.remark.util.BlockWriter
All Implemented Interfaces:
Closeable, Flushable, Appendable

public final class BlockWriter
extends PrintWriter

This is a customized subclass of BufferedWriter that handles working with Markdown block-level elements. In the case of a non-block-level element occurring outside a block, it is automatically promoted.

Author:
Phil DeJarnett

Field Summary
 
Fields inherited from class java.io.PrintWriter
out
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
BlockWriter(OutputStream out)
           
BlockWriter(OutputStream out, boolean autoFlush)
           
BlockWriter(Writer out)
           
BlockWriter(Writer out, boolean autoFlush)
           
 
Method Summary
static BlockWriter create()
          Creates a new, empty BlockWriter with a StringWriter as the buffer.
static BlockWriter create(int initialSize)
          Creates a new, empty BlockWriter with a StringWriter as the buffer.
 void endBlock()
          Ends a block.
 int getBlockDepth()
          Returns how deep the number of blocks is.
 StringWriter getBuffer()
          If this object has been created using create(), returns the StringWriter output buffer.
 String getPrependNewlineString()
          Returns the string being prepended to new lines, if set.
 boolean isEmpty()
          Returns true if nothing has been written to the stream yet.
 void printBlock(Object blockText)
          Alias for writeBlock(Object).
 void println()
           
 BlockWriter setPrependNewlineString(String prependNewLineString)
          Sets the string to prepend to new lines.
 BlockWriter setPrependNewlineString(String prependNewLineString, boolean skipFirstLine)
          Sets the string to prepend to new lines.
 void startBlock()
          Starts a new block.
 String toString()
          If this object has been created using create(), this will return the contents of the StringWriter buffer.
 void write(char[] cbuf, int off, int len)
           
 void write(int c)
           
 void write(String s, int off, int len)
           
 void writeBlock(Object blockText)
          Writes an entire block in one go.
 
Methods inherited from class java.io.PrintWriter
append, append, append, checkError, clearError, close, flush, format, format, print, print, print, print, print, print, print, print, print, printf, printf, println, println, println, println, println, println, println, println, println, setError, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BlockWriter

public BlockWriter(Writer out)

BlockWriter

public BlockWriter(Writer out,
                   boolean autoFlush)

BlockWriter

public BlockWriter(OutputStream out)

BlockWriter

public BlockWriter(OutputStream out,
                   boolean autoFlush)
Method Detail

create

public static BlockWriter create()
Creates a new, empty BlockWriter with a StringWriter as the buffer. To get the contents of the StringWriter, call BlockWriter.toString()

Returns:
new BlockWriter
See Also:
toString()

create

public static BlockWriter create(int initialSize)
Creates a new, empty BlockWriter with a StringWriter as the buffer. To get the contents of the StringWriter, call BlockWriter.toString()

Parameters:
initialSize - Initialize the output buffer to the specified size.
Returns:
new BlockWriter
See Also:
toString()

write

public void write(int c)
Overrides:
write in class PrintWriter

write

public void write(char[] cbuf,
                  int off,
                  int len)
Overrides:
write in class PrintWriter

write

public void write(String s,
                  int off,
                  int len)
Overrides:
write in class PrintWriter

println

public void println()
Overrides:
println in class PrintWriter

startBlock

public void startBlock()
Starts a new block. This is useful when streaming out content within a block. This method keeps track of the current block depth, so make sure that endBlock() is called when the block is completed.


endBlock

public void endBlock()
Ends a block. The depth counter is decreased, so we know when we are back at the root.


writeBlock

public void writeBlock(Object blockText)
Writes an entire block in one go. This method automatically handles starting and ending the block.

Parameters:
blockText - The text of the block.

printBlock

public void printBlock(Object blockText)
Alias for writeBlock(Object).

Parameters:
blockText - The text of the block.

getBlockDepth

public int getBlockDepth()
Returns how deep the number of blocks is. 0 means that no blocks are currently active.

Returns:
block depth

isEmpty

public boolean isEmpty()
Returns true if nothing has been written to the stream yet.

Returns:
true if nothing has been written yet.

getBuffer

public StringWriter getBuffer()
If this object has been created using create(), returns the StringWriter output buffer.

Returns:
the buffer for this BlockWriter

getPrependNewlineString

public String getPrependNewlineString()
Returns the string being prepended to new lines, if set.

Returns:
String that gets prepended before each new line, or null if not set.

setPrependNewlineString

public BlockWriter setPrependNewlineString(String prependNewLineString)
Sets the string to prepend to new lines. If set to null, this feature is disabled. By default, this starts adding the prepend immediately.

Parameters:
prependNewLineString - The string to prepend to each new line.
Returns:
This for chaining (especially after creation)

setPrependNewlineString

public BlockWriter setPrependNewlineString(String prependNewLineString,
                                           boolean skipFirstLine)
Sets the string to prepend to new lines. If set to null, this feature is disabled. The second parameter affects whether or not the next encountered new line gets the prepend string. If it is true, the first line won't be affected. Otherwise, this is the same as setPrependNewlineString(String).

Parameters:
prependNewLineString - The string to prepend to each new line.
skipFirstLine - If true, the first line won't be prepended.
Returns:
This for chaining (especially after creation)

toString

public String toString()
If this object has been created using create(), this will return the contents of the StringWriter buffer. Otherwise, this returns the default Object.toString() method.

Overrides:
toString in class Object
Returns:
The contents of the buffer, or a generic Object method.


© 2011 OverZealous Creations, LLC - BitBucket Repository