Class IndentationCorrectingWriter
- java.lang.Object
-
- java.io.Writer
-
- org.mapstruct.ap.internal.writer.IndentationCorrectingWriter
-
- All Implemented Interfaces:
java.io.Closeable,java.io.Flushable,java.lang.Appendable,java.lang.AutoCloseable
class IndentationCorrectingWriter extends java.io.WriterEXPERIMENTAL: 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classIndentationCorrectingWriter.StateA state of parsing a given character buffer.private static classIndentationCorrectingWriter.StateContextKeeps the current context of parsing the given character buffer.
-
Field Summary
Fields Modifier and Type Field Description private IndentationCorrectingWriter.StateContextcontextprivate IndentationCorrectingWriter.StatecurrentStateprivate static booleanDEBUGSet to true to enable output of written characters on the console.private static booleanIS_WINDOWSprivate static java.lang.StringLINE_SEPARATOR
-
Constructor Summary
Constructors Constructor Description IndentationCorrectingWriter(java.io.Writer out)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()voidflush()private static char[]getIndentation(int indentationLevel)private static booleanisWindows()voidwrite(char[] cbuf, int off, int len)
-
-
-
Field Detail
-
DEBUG
private static final boolean DEBUG
Set to true to enable output of written characters on the console.- See Also:
- Constant Field Values
-
LINE_SEPARATOR
private static final java.lang.String LINE_SEPARATOR
-
IS_WINDOWS
private static final boolean IS_WINDOWS
-
currentState
private IndentationCorrectingWriter.State currentState
-
context
private final IndentationCorrectingWriter.StateContext context
-
-
Method Detail
-
write
public void write(char[] cbuf, int off, int len) throws java.io.IOException- Specified by:
writein classjava.io.Writer- Throws:
java.io.IOException
-
flush
public void flush() throws java.io.IOException- Specified by:
flushin interfacejava.io.Flushable- Specified by:
flushin classjava.io.Writer- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Specified by:
closein classjava.io.Writer- Throws:
java.io.IOException
-
isWindows
private static boolean isWindows()
-
getIndentation
private static char[] getIndentation(int indentationLevel)
-
-