Top Description Fields Constructors Methods
org.codehaus.janino.util

public Class AutoIndentWriter

extends FilterWriter
Class Inheritance
Imports
java.io.FilterWriter, .IOException, .Writer, java.util.ArrayList, .Arrays, .Iterator, .List, org.codehaus.commons.nullanalysis.Nullable

A java.io.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

Modifier and TypeField and Description
public static final char
CLEAR_TABULATORS

Special character at the beginning of a line that flushes a tabular layout.

public static final 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 final StringBuilder
lineBuffer

Buffer for the "current line", including the trailing line break (CR, LF or CRLF).

public static final char
TABULATOR

Special character indicating a tabular layout of all following lines until UNINDENT.

private List<StringBuilder>
tabulatorBuffer

Iff non-null, then we are in "tab mode".

private int
tabulatorIndentation

The indentation when tab mode started.

public static final char
UNINDENT

Special character at the beginning of a line that unindents the following text by one position.

Inherited from java.io.FilterWriter:
out

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
close()

Overrides java.io.FilterWriter.close.

Implements java.io.Closeable.close.

Closes the stream, flushing it first.
public void
flush()

Overrides java.io.FilterWriter.flush.

Implements java.io.Flushable.flush.

Flushes the stream.
private void
private void
line(String line)

private static void
resolveTabs(List<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
private static String

Returns:

a String of n spaces
spaces
(int n)

public void
write(char[]
Buffer of characters to be written
cbuf
,
int
Offset from which to start reading characters
off
,
int
Number of characters to be written
len
)

Overrides java.io.FilterWriter.write.

Writes a portion of an array of characters.
public void
write(String
String to be written
str
,
int
Offset from which to start reading characters
off
,
int
Number of characters to be written
len
)

Overrides java.io.FilterWriter.write.

Writes a portion of a string.
public void
write(int
int specifying a character to be written
c
)

Overrides java.io.FilterWriter.write.

Writes a single character.

Field Detail

CLEAR_TABULATORSback to summary
public static final char CLEAR_TABULATORS

Special character at the beginning of a line that flushes a tabular layout.

INDENTback to summary
public static final char INDENT

Special character at the beginning of a line that indents the following text by one position.

indentationback to summary
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.

lineBufferback to summary
private final StringBuilder lineBuffer

Buffer for the "current line", including the trailing line break (CR, LF or CRLF).

TABULATORback to summary
public static final char TABULATOR

Special character indicating a tabular layout of all following lines until UNINDENT.

tabulatorBufferback to summary
private List<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.

Annotations
@Nullable
tabulatorIndentationback to summary
private int tabulatorIndentation

The indentation when tab mode started.

UNINDENTback to summary
public static final char UNINDENT

Special character at the beginning of a line that unindents the following text by one position.

Constructor Detail

AutoIndentWriterback to summary
public AutoIndentWriter(Writer out)

Method Detail

closeback to summary
public void close() throws IOException

Overrides java.io.FilterWriter.close.

Implements java.io.Closeable.close.

Doc from java.io.Writer.close.

Closes the stream, flushing it first. Once the stream has been closed, further write() or flush() invocations will cause an IOException to be thrown. Closing a previously closed stream has no effect.

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs
flushback to summary
public void flush() throws IOException

Overrides java.io.FilterWriter.flush.

Implements java.io.Flushable.flush.

Doc from java.io.FilterWriter.flush.

Flushes the stream.

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs
flushTabulatorBufferback to summary
private void flushTabulatorBuffer() throws IOException
lineback to summary
private void line(String line) throws IOException
resolveTabsback to summary
private static void resolveTabs(List<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
spacesback to summary
private static String spaces(int n)
Returns:String

a String of n spaces

writeback to summary
public void write(char[] cbuf, int off, int len) throws IOException

Overrides java.io.FilterWriter.write.

Doc from java.io.FilterWriter.write.

Writes a portion of an array of characters.

Parameters
cbuf:char[]

Buffer of characters to be written

off:int

Offset from which to start reading characters

len:int

Number of characters to be written

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs
writeback to summary
public void write(String str, int off, int len) throws IOException

Overrides java.io.FilterWriter.write.

Doc from java.io.FilterWriter.write.

Writes a portion of a string.

Parameters
str:String

String to be written

off:int

Offset from which to start reading characters

len:int

Number of characters to be written

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs
writeback to summary
public void write(int c) throws IOException

Overrides java.io.FilterWriter.write.

Doc from java.io.FilterWriter.write.

Writes a single character.

Parameters
c:int

Doc from java.io.Writer.write.

int specifying a character to be written

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs