Top Description Fields Constructors Methods
org.stringtemplate.v4

public Class AutoIndentWriter

extends Object
implements STWriter
Class Inheritance
All Implemented Interfaces
org.stringtemplate.v4.STWriter
Known Direct Subclasses
org.stringtemplate.v4.NoIndentWriter
Imports
java.io.IOException, .Writer, java.util.ArrayList, .List, .Stack

Essentially a char filter that knows how to auto-indent output by maintaining a stack of indent levels.

The indent stack is a stack of strings so we can repeat original indent not just the same number of columns (don't have to worry about tabs vs spaces then). Anchors are char positions (tabs won't work) that indicate where all future wraps should justify to. The wrap position is actually the larger of either the last anchor or the indentation level.

This is a filter on a Writer.

\n is the proper way to say newline for options and templates. Templates can mix \r\n and \n them, but use \n in options like wrap="\n". This writer will render newline characters according to newline. The default value is taken from the line.separator system property, and can be overridden by passing in a String to the appropriate constructor.

Field Summary

Modifier and TypeField and Description
public int[]
anchors

Stack of integer anchors (char positions in line); avoid Integer creation overhead.

public int
public boolean
public int
charIndex

The absolute char index into the output of the next char to be written.

public int
charPosition

Track char position in the line (later we can think about tabs).

public List<String>
indents

Stack of indents.

public int
public String
newline

\n or \r\n?

public Writer

Constructor Summary

AccessConstructor and Description
public
public

Method Summary

Modifier and TypeMethod and Description
public int
public int
index()

Implements org.stringtemplate.v4.STWriter.index.

Return the absolute char index into the output of the char we're about to write.
public void
public String
public void
public void
public void
public int
write(String str)

Implements org.stringtemplate.v4.STWriter.write.

Write out a string literal or attribute expression or expression element.
public int
write(String str, String wrap)

Implements org.stringtemplate.v4.STWriter.write.

Write out a string literal or attribute expression or expression element.
public int
public int
writeWrap(String wrap)

Implements org.stringtemplate.v4.STWriter.writeWrap.

Because we evaluate ST instance by invoking Interpreter#exec(STWriter, InstanceScope) again, we can't pass options in. So the Bytecode#INSTR_WRITE instruction of an applied template (such as when we wrap in between template applications like <data:{v|[<v>]}; wrap>) we need to write the wrap string before calling Interpreter#exec.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

anchorsback to summary
public int[] anchors

Stack of integer anchors (char positions in line); avoid Integer creation overhead.

anchors_spback to summary
public int anchors_sp
atStartOfLineback to summary
public boolean atStartOfLine
charIndexback to summary
public int charIndex

The absolute char index into the output of the next char to be written.

charPositionback to summary
public int charPosition

Track char position in the line (later we can think about tabs). Indexed from 0. We want to keep charPosition <= lineWidth. This is the position we are about to write, not the position last written to.

indentsback to summary
public List<String> indents

Stack of indents. Use List as it's much faster than Stack. Grows from 0..n-1.

lineWidthback to summary
public int lineWidth
newlineback to summary
public String newline

\n or \r\n?

outback to summary
public Writer out

Constructor Detail

AutoIndentWriterback to summary
public AutoIndentWriter(Writer out, String newline)
AutoIndentWriterback to summary
public AutoIndentWriter(Writer out)

Method Detail

indentback to summary
public int indent() throws IOException
indexback to summary
public int index()

Implements org.stringtemplate.v4.STWriter.index.

Doc from org.stringtemplate.v4.STWriter.index.

Return the absolute char index into the output of the char we're about to write. Returns 0 if no char written yet.

Annotations
@Override
popAnchorPointback to summary
public void popAnchorPoint()

Implements org.stringtemplate.v4.STWriter.popAnchorPoint.

Annotations
@Override
popIndentationback to summary
public String popIndentation()

Implements org.stringtemplate.v4.STWriter.popIndentation.

Annotations
@Override
pushAnchorPointback to summary
public void pushAnchorPoint()

Implements org.stringtemplate.v4.STWriter.pushAnchorPoint.

Annotations
@Override
pushIndentationback to summary
public void pushIndentation(String indent)

Implements org.stringtemplate.v4.STWriter.pushIndentation.

Annotations
@Override
setLineWidthback to summary
public void setLineWidth(int lineWidth)

Implements org.stringtemplate.v4.STWriter.setLineWidth.

Annotations
@Override
writeback to summary
public int write(String str) throws IOException

Implements org.stringtemplate.v4.STWriter.write.

Write out a string literal or attribute expression or expression element.

Annotations
@Override
writeback to summary
public int write(String str, String wrap) throws IOException

Implements org.stringtemplate.v4.STWriter.write.

Write out a string literal or attribute expression or expression element.

If doing line wrap, then check wrap before emitting str. If at or beyond desired line width then emit a newline and any indentation before spitting out str.

Annotations
@Override
writeSeparatorback to summary
public int writeSeparator(String str) throws IOException

Implements org.stringtemplate.v4.STWriter.writeSeparator.

Doc from org.stringtemplate.v4.STWriter.writeSeparator.

Write a separator. Same as write(String) except that a "\n" cannot be inserted before emitting a separator.

Annotations
@Override
writeWrapback to summary
public int writeWrap(String wrap) throws IOException

Implements org.stringtemplate.v4.STWriter.writeWrap.

Doc from org.stringtemplate.v4.STWriter.writeWrap.

Because we evaluate ST instance by invoking Interpreter#exec(STWriter, InstanceScope) again, we can't pass options in. So the Bytecode#INSTR_WRITE instruction of an applied template (such as when we wrap in between template applications like <data:{v|[<v>]}; wrap>) we need to write the wrap string before calling Interpreter#exec. We expose just like for the separator. See Interpreter#writeObject where it checks for ST instance. If POJO, Interpreter#writePOJO passes wrap to STWriter#write(String str, String wrap). Can't pass to Interpreter#exec.

Annotations
@Override