Class AutoIndentWriter
- java.lang.Object
-
- org.stringtemplate.v4.AutoIndentWriter
-
- All Implemented Interfaces:
STWriter
- Direct Known Subclasses:
NoIndentWriter
public class AutoIndentWriter extends Object implements STWriter
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.\nis the proper way to say newline for options and templates. Templates can mix\r\nand\nthem, but use\nin options likewrap="\n". This writer will render newline characters according tonewline. The default value is taken from theline.separatorsystem property, and can be overridden by passing in aStringto the appropriate constructor.
-
-
Field Summary
Fields Modifier and Type Field Description int[]anchorsStack of integer anchors (char positions in line); avoidIntegercreation overhead.intanchors_spbooleanatStartOfLineintcharIndexThe absolute char index into the output of the next char to be written.intcharPositionTrack char position in the line (later we can think about tabs).List<String>indentsStack of indents.intlineWidthStringnewline\nor\r\n?Writerout
-
Constructor Summary
Constructors Constructor Description AutoIndentWriter(Writer out)AutoIndentWriter(Writer out, String newline)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intindent()intindex()Return the absolute char index into the output of the char we're about to write.voidpopAnchorPoint()StringpopIndentation()voidpushAnchorPoint()voidpushIndentation(String indent)voidsetLineWidth(int lineWidth)intwrite(String str)Write out a string literal or attribute expression or expression element.intwrite(String str, String wrap)Write out a string literal or attribute expression or expression element.intwriteSeparator(String str)Write a separator.intwriteWrap(String wrap)Because we evaluate ST instance by invokingInterpreter.exec(STWriter, InstanceScope)again, we can't pass options in.
-
-
-
Field Detail
-
anchors
public int[] anchors
Stack of integer anchors (char positions in line); avoidIntegercreation overhead.
-
anchors_sp
public int anchors_sp
-
newline
public String newline
\nor\r\n?
-
out
public Writer out
-
atStartOfLine
public boolean atStartOfLine
-
charPosition
public int charPosition
Track char position in the line (later we can think about tabs). Indexed from 0. We want to keepcharPosition <=lineWidth. This is the position we are about to write, not the position last written to.
-
charIndex
public int charIndex
The absolute char index into the output of the next char to be written.
-
lineWidth
public int lineWidth
-
-
Method Detail
-
setLineWidth
public void setLineWidth(int lineWidth)
- Specified by:
setLineWidthin interfaceSTWriter
-
pushIndentation
public void pushIndentation(String indent)
- Specified by:
pushIndentationin interfaceSTWriter
-
popIndentation
public String popIndentation()
- Specified by:
popIndentationin interfaceSTWriter
-
pushAnchorPoint
public void pushAnchorPoint()
- Specified by:
pushAnchorPointin interfaceSTWriter
-
popAnchorPoint
public void popAnchorPoint()
- Specified by:
popAnchorPointin interfaceSTWriter
-
index
public int index()
Description copied from interface:STWriterReturn the absolute char index into the output of the char we're about to write. Returns 0 if no char written yet.
-
write
public int write(String str) throws IOException
Write out a string literal or attribute expression or expression element.- Specified by:
writein interfaceSTWriter- Throws:
IOException
-
writeSeparator
public int writeSeparator(String str) throws IOException
Description copied from interface:STWriterWrite a separator. Same asSTWriter.write(String)except that a"\n"cannot be inserted before emitting a separator.- Specified by:
writeSeparatorin interfaceSTWriter- Throws:
IOException
-
write
public int write(String str, String wrap) throws IOException
Write out a string literal or attribute expression or expression element.If doing line wrap, then check
wrapbefore emittingstr. If at or beyond desired line width then emit anewlineand any indentation before spitting outstr.- Specified by:
writein interfaceSTWriter- Throws:
IOException
-
writeWrap
public int writeWrap(String wrap) throws IOException
Description copied from interface:STWriterBecause we evaluate ST instance by invokingInterpreter.exec(STWriter, InstanceScope)again, we can't pass options in. So theBytecode.INSTR_WRITEinstruction of an applied template (such as when we wrap in between template applications like<data:{v|[<v>]}; wrap>) we need to write thewrapstring before callingInterpreter.exec(org.stringtemplate.v4.STWriter, org.stringtemplate.v4.InstanceScope). We expose just like for the separator. SeeInterpreter.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[])passeswraptoSTWriter.write(String str, String wrap). Can't pass toInterpreter.exec(org.stringtemplate.v4.STWriter, org.stringtemplate.v4.InstanceScope).- Specified by:
writeWrapin interfaceSTWriter- Throws:
IOException
-
indent
public int indent() throws IOException- Throws:
IOException
-
-