Class 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
    • Constructor Detail

      • BlockWriter

        public BlockWriter​(Writer out)
      • BlockWriter

        public BlockWriter​(Writer out,
                           boolean autoFlush)
      • 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​(char[] cbuf,
                          int off,
                          int len)
        Overrides:
        write 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)
        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.