Class AutoIndentWriter

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.Appendable, java.lang.AutoCloseable

    public class AutoIndentWriter
    extends java.io.FilterWriter
    A FilterWriter that indents lines by processing some control characters in the character stream.

    INDENT or UNINDENT may precede lines and indicate that the line and all following lines should be (un)indented by one position.

    TABULATORs may appear anywhere in lines and dictate that portions of all following lines should be vertically aligned (see resolveTabs(List)).

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static char CLEAR_TABULATORS
      Special character at the beginning of a line that flushes a tabular layout.
      static char INDENT
      Special character at the beginning of a line that indents the following text by one position.
      private int indentation
      The current indentation level; incremented by a INDENT char at the beginning of a line, and decremented by a UNINDENT char at the beginning of a line.
      private java.lang.StringBuilder lineBuffer
      Buffer for the "current line", including the trailing line break (CR, LF or CRLF).
      static char TABULATOR
      Special character indicating a tabular layout of all following lines until UNINDENT.
      private java.util.List<java.lang.StringBuilder> tabulatorBuffer
      Iff non-null, then we are in "tab mode".
      private int tabulatorIndentation
      The indentation when tab mode started.
      static char UNINDENT
      Special character at the beginning of a line that unindents the following text by one position.
      • Fields inherited from class java.io.FilterWriter

        out
      • Fields inherited from class java.io.Writer

        lock
    • Constructor Summary

      Constructors 
      Constructor Description
      AutoIndentWriter​(java.io.Writer out)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      void flush()  
      private void flushTabulatorBuffer()  
      private void line​(java.lang.String line)  
      private static void resolveTabs​(java.util.List<java.lang.StringBuilder> lineGroup)
      Expands all TABULATORs in the given List of StringBuilders with spaces, so that the characters immediately following the TABULATORs are vertically aligned, like this:
      private static java.lang.String spaces​(int n)  
      void write​(char[] cbuf, int off, int len)  
      void write​(int c)  
      void write​(java.lang.String str, int off, int len)  
      • Methods inherited from class java.io.Writer

        append, append, append, nullWriter, write, write
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • TABULATOR

        public static final char TABULATOR
        Special character indicating a tabular layout of all following lines until UNINDENT.
        See Also:
        Constant Field Values
      • CLEAR_TABULATORS

        public static final char CLEAR_TABULATORS
        Special character at the beginning of a line that flushes a tabular layout.
        See Also:
        Constant Field Values
      • INDENT

        public static final char INDENT
        Special character at the beginning of a line that indents the following text by one position.
        See Also:
        Constant Field Values
      • UNINDENT

        public static final char UNINDENT
        Special character at the beginning of a line that unindents the following text by one position.
        See Also:
        Constant Field Values
      • lineBuffer

        private final java.lang.StringBuilder lineBuffer
        Buffer for the "current line", including the trailing line break (CR, LF or CRLF).
      • indentation

        private int indentation
        The current indentation level; incremented by a INDENT char at the beginning of a line, and decremented by a UNINDENT char at the beginning of a line.
      • tabulatorBuffer

        @Nullable
        private java.util.List<java.lang.StringBuilder> tabulatorBuffer
        Iff non-null, then we are in "tab mode". While in tab mode, lines are not printed immediately, but stored in this buffer. Tab mode starts when a line contains a TABULATOR. Tab mode ends when output is unindented beyond the level when tab mode started, or when this writer is closed.

        When tab mode ends, all buffered lines are vertically aligned at the TABULATORs and printed.

      • tabulatorIndentation

        private int tabulatorIndentation
        The indentation when tab mode started.
    • Constructor Detail

      • AutoIndentWriter

        public AutoIndentWriter​(java.io.Writer out)
    • Method Detail

      • write

        public void write​(@Nullable
                          char[] cbuf,
                          int off,
                          int len)
                   throws java.io.IOException
        Overrides:
        write in class java.io.FilterWriter
        Throws:
        java.io.IOException
      • write

        public void write​(@Nullable
                          java.lang.String str,
                          int off,
                          int len)
                   throws java.io.IOException
        Overrides:
        write in class java.io.FilterWriter
        Throws:
        java.io.IOException
      • write

        public void write​(int c)
                   throws java.io.IOException
        Overrides:
        write in class java.io.FilterWriter
        Throws:
        java.io.IOException
      • line

        private void line​(java.lang.String line)
                   throws java.io.IOException
        Throws:
        java.io.IOException
      • flushTabulatorBuffer

        private void flushTabulatorBuffer()
                                   throws java.io.IOException
        Throws:
        java.io.IOException
      • resolveTabs

        private static void resolveTabs​(java.util.List<java.lang.StringBuilder> lineGroup)
        Expands all TABULATORs in the given List of StringBuilders with spaces, so that the characters immediately following the TABULATORs are vertically aligned, like this:

        Input:

           a @b @c\r\n
           aa @bb @cc\r\n
           aaa @bbb @ccc\r\n
         

        Output:

           a   b   c\r\n
           aa  bb  cc\r\n
           aaa bbb ccc\r\n
         
      • spaces

        private static java.lang.String spaces​(int n)
        Returns:
        a String of n spaces
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.FilterWriter
        Throws:
        java.io.IOException
      • flush

        public void flush()
                   throws java.io.IOException
        Specified by:
        flush in interface java.io.Flushable
        Overrides:
        flush in class java.io.FilterWriter
        Throws:
        java.io.IOException