public class AutoIndentWriter extends java.lang.Object implements STWriter
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.Modifier and Type | Field and Description |
---|---|
int[] |
anchors
Stack of integer anchors (char positions in line); avoid
Integer
creation overhead. |
int |
anchors_sp |
boolean |
atStartOfLine |
int |
charIndex
The absolute char index into the output of the next char to be written.
|
int |
charPosition
Track char position in the line (later we can think about tabs).
|
java.util.List<java.lang.String> |
indents
Stack of indents.
|
int |
lineWidth |
java.lang.String |
newline
\n or \r\n ? |
java.io.Writer |
out |
Constructor and Description |
---|
AutoIndentWriter(java.io.Writer out) |
AutoIndentWriter(java.io.Writer out,
java.lang.String newline) |
Modifier and Type | Method and Description |
---|---|
int |
indent() |
int |
index()
Return the absolute char index into the output of the char
we're about to write.
|
void |
popAnchorPoint() |
java.lang.String |
popIndentation() |
void |
pushAnchorPoint() |
void |
pushIndentation(java.lang.String indent) |
void |
setLineWidth(int lineWidth) |
int |
write(java.lang.String str)
Write out a string literal or attribute expression or expression element.
|
int |
write(java.lang.String str,
java.lang.String wrap)
Write out a string literal or attribute expression or expression element.
|
int |
writeSeparator(java.lang.String str)
Write a separator.
|
int |
writeWrap(java.lang.String wrap)
Because we evaluate ST instance by invoking
Interpreter.exec(STWriter, InstanceScope) again, we can't pass options in. |
public java.util.List<java.lang.String> indents
List
as it's much faster than Stack
. Grows
from 0..n-1.public int[] anchors
Integer
creation overhead.public int anchors_sp
public java.lang.String newline
\n
or \r\n
?public java.io.Writer out
public boolean atStartOfLine
public int charPosition
charPosition <=
lineWidth
.
This is the position we are about to write, not the position
last written to.public int charIndex
public int lineWidth
public AutoIndentWriter(java.io.Writer out, java.lang.String newline)
public AutoIndentWriter(java.io.Writer out)
public void setLineWidth(int lineWidth)
setLineWidth
in interface STWriter
public void pushIndentation(java.lang.String indent)
pushIndentation
in interface STWriter
public java.lang.String popIndentation()
popIndentation
in interface STWriter
public void pushAnchorPoint()
pushAnchorPoint
in interface STWriter
public void popAnchorPoint()
popAnchorPoint
in interface STWriter
public int index()
STWriter
public int write(java.lang.String str) throws java.io.IOException
public int writeSeparator(java.lang.String str) throws java.io.IOException
STWriter
STWriter.write(String)
except that a "\n"
cannot be inserted before emitting a separator.writeSeparator
in interface STWriter
java.io.IOException
public int write(java.lang.String str, java.lang.String wrap) throws java.io.IOException
wrap
before emitting str
.
If at or beyond desired line width then emit a newline
and any
indentation before spitting out str
.public int writeWrap(java.lang.String wrap) throws java.io.IOException
STWriter
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(org.stringtemplate.v4.STWriter, org.stringtemplate.v4.InstanceScope)
. We expose just like for the
separator. See Interpreter.writeObject(org.stringtemplate.v4.STWriter, org.stringtemplate.v4.InstanceScope, java.lang.Object, java.lang.String[])
where it checks for ST
instance. If POJO, Interpreter.writePOJO(org.stringtemplate.v4.STWriter, org.stringtemplate.v4.InstanceScope, java.lang.Object, java.lang.String[])
passes wrap
to
STWriter.write(String str, String wrap)
. Can't pass to
Interpreter.exec(org.stringtemplate.v4.STWriter, org.stringtemplate.v4.InstanceScope)
.public int indent() throws java.io.IOException
java.io.IOException