Class IndentingWriter


  • public class IndentingWriter
    extends Object
    A writer that inserts the configured indent unit at the start of each line.
    • Constructor Detail

      • IndentingWriter

        public IndentingWriter​(Writer writer)
        Creates a writer indenting with tabs.
        Parameters:
        writer - the underlying writer
      • IndentingWriter

        public IndentingWriter​(Writer writer,
                               String indentString)
        Creates a writer with the given indent unit.
        Parameters:
        writer - the underlying writer
        indentString - the string written once per indent level
    • Method Detail

      • toStringWriter

        public static IndentingWriter toStringWriter()
        Creates a writer accumulating into a StringWriter, readable via toString().
        Returns:
        the string-backed writer
      • indent

        public void indent()
        Increases the indentation level.
      • dedent

        public void dedent()
        Decreases the indentation level, never going below zero.
      • write

        public void write​(String text)
        Writes text, emitting the indent before the first character of each line.
        Parameters:
        text - the text to write
        Throws:
        RuntimeException - if the underlying writer fails
      • writeLine

        public void writeLine​(String text)
        Writes text followed by a newline.
        Parameters:
        text - the text to write
      • newLine

        public void newLine()
        Writes a newline.
        Throws:
        RuntimeException - if the underlying writer fails
      • blankLine

        public void blankLine()
        Writes an empty line.
      • getIndentLevel

        public int getIndentLevel()
        Returns:
        the current indentation level
      • writeRaw

        public void writeRaw​(String text)
        Writes already-formatted text without adding indentation, still tracking lines.
        Parameters:
        text - the text to write
        Throws:
        RuntimeException - if the underlying writer fails
      • getCurrentLine

        public int getCurrentLine()
        Returns:
        the 1-based line number the next character will be written to
      • flush

        public void flush()
        Flushes the underlying writer.
        Throws:
        RuntimeException - if flushing fails
      • toString

        public String toString()
        Flushes and returns the accumulated output.
        Overrides:
        toString in class Object
        Returns:
        the underlying writer's string form; the emitted source when backed by a StringWriter