Class IndentationCorrectingWriter

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

    class IndentationCorrectingWriter
    extends java.io.Writer
    EXPERIMENTAL: A writer used to write processed templates which corrects line indentation based on the nesting level as implied by (normal and curly) braces.

    This writer discards any leading whitespace characters following to a line break character. When the first non-whitespace character is written after a line break, the correct indentation characters are added, which is four whitespace characters per indentation level.

    The state pattern is line oriented. It starts by writing text. Indentation is increased if a brace '('or brace '{' is encountered in the code to be generated and written out in state: IN_TEXT_START_OF_LINE. Whenever a line end occurs (PC or Linux style) the amount of enters is checked and at max set to 2. Whenever a string definition is encountered in the code that should be generated, increasing the indentation is stopped (so `{` and '(' are ignored) until the end of the string is encountered ('"'). To avoid writing a new indentation, the state then returns to IN_TEXT.

    This is a very basic implementation which does not take into account comments, escaping etc.

    • Constructor Detail

      • IndentationCorrectingWriter

        IndentationCorrectingWriter​(java.io.Writer out)
    • Method Detail

      • write

        public void write​(char[] cbuf,
                          int off,
                          int len)
                   throws java.io.IOException
        Specified by:
        write in class java.io.Writer
        Throws:
        java.io.IOException
      • flush

        public void flush()
                   throws java.io.IOException
        Specified by:
        flush in interface java.io.Flushable
        Specified by:
        flush in class java.io.Writer
        Throws:
        java.io.IOException
      • close

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

        private static boolean isWindows()
      • getIndentation

        private static char[] getIndentation​(int indentationLevel)