Package com.overzealous.remark.util
Class BlockWriter
- java.lang.Object
-
- java.io.Writer
-
- java.io.PrintWriter
-
- com.overzealous.remark.util.BlockWriter
-
- All Implemented Interfaces:
Closeable,Flushable,Appendable,AutoCloseable
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
-
-
Constructor Summary
Constructors Constructor Description BlockWriter(OutputStream out)BlockWriter(OutputStream out, boolean autoFlush)BlockWriter(Writer out)BlockWriter(Writer out, boolean autoFlush)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static BlockWritercreate()Creates a new, empty BlockWriter with a StringWriter as the buffer.static BlockWritercreate(int initialSize)Creates a new, empty BlockWriter with a StringWriter as the buffer.voidendBlock()Ends a block.intgetBlockDepth()Returns how deep the number of blocks is.StringWritergetBuffer()If this object has been created usingcreate(), returns the StringWriter output buffer.StringgetPrependNewlineString()Returns the string being prepended to new lines, if set.booleanisEmpty()Returns true if nothing has been written to the stream yet.voidprintBlock(Object blockText)Alias forwriteBlock(Object).voidprintln()BlockWritersetPrependNewlineString(String prependNewLineString)Sets the string to prepend to new lines.BlockWritersetPrependNewlineString(String prependNewLineString, boolean skipFirstLine)Sets the string to prepend to new lines.voidstartBlock()Starts a new block.StringtoString()If this object has been created usingcreate(), this will return the contents of the StringWriter buffer.voidwrite(char[] cbuf, int off, int len)voidwrite(int c)voidwrite(String s, int off, int len)voidwriteBlock(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
-
-
-
-
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:
writein classPrintWriter
-
write
public void write(char[] cbuf, int off, int len)- Overrides:
writein classPrintWriter
-
write
public void write(String s, int off, int len)
- Overrides:
writein classPrintWriter
-
println
public void println()
- Overrides:
printlnin classPrintWriter
-
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 thatendBlock()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 forwriteBlock(Object).- Parameters:
blockText- The text of the block.
-
getBlockDepth
public int getBlockDepth()
Returns how deep the number of blocks is.0means 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 usingcreate(), 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 assetPrependNewlineString(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)
-
-