org.stringtemplate.v4
Class AutoIndentWriter

java.lang.Object
  extended by 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. \n is the proper way to say newline for options and templates. Templates can mix \r\n and \n them but use \n for sure in options like wrap="\n". ST will generate the right thing. Override the default (locale) newline by passing in a string to the constructor.


Field Summary
 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).
 List<String> indents
          stack of indents; use List as it's much faster than Stack.
 int lineWidth
           
 String newline
          \n or \r\n?
 Writer out
           
 
Fields inherited from interface org.stringtemplate.v4.STWriter
NO_WRAP
 
Constructor Summary
AutoIndentWriter(Writer out)
           
AutoIndentWriter(Writer out, String newline)
           
 
Method Summary
 int indent()
           
 int index()
          Return the absolute char index into the output of the char we're about to write.
 void popAnchorPoint()
           
 String popIndentation()
           
 void pushAnchorPoint()
           
 void pushIndentation(String indent)
           
 void setLineWidth(int lineWidth)
           
 int write(String str)
          Write out a string literal or attribute expression or expression element.
 int write(String str, String wrap)
          Write out a string literal or attribute expression or expression element.
 int writeSeparator(String str)
          Write a separator.
 int writeWrap(String wrap)
          Because we evaluate ST instance by invoking exec() again, we can't pass options in.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

indents

public List<String> indents
stack of indents; use List as it's much faster than Stack. Grows from 0..n-1.


anchors

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


anchors_sp

public int anchors_sp

newline

public String newline
\n or \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 keep charPosition <= 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
Constructor Detail

AutoIndentWriter

public AutoIndentWriter(Writer out,
                        String newline)

AutoIndentWriter

public AutoIndentWriter(Writer out)
Method Detail

setLineWidth

public void setLineWidth(int lineWidth)
Specified by:
setLineWidth in interface STWriter

pushIndentation

public void pushIndentation(String indent)
Specified by:
pushIndentation in interface STWriter

popIndentation

public String popIndentation()
Specified by:
popIndentation in interface STWriter

pushAnchorPoint

public void pushAnchorPoint()
Specified by:
pushAnchorPoint in interface STWriter

popAnchorPoint

public void popAnchorPoint()
Specified by:
popAnchorPoint in interface STWriter

index

public int index()
Description copied from interface: STWriter
Return the absolute char index into the output of the char we're about to write. Returns 0 if no char written yet.

Specified by:
index in interface STWriter

write

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

Specified by:
write in interface STWriter
Throws:
IOException

writeSeparator

public int writeSeparator(String str)
                   throws IOException
Description copied from interface: STWriter
Write a separator. Same as write() except that a \n cannot be inserted before emitting a separator.

Specified by:
writeSeparator in interface STWriter
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 wrap before emitting this str. If at or beyond desired line width then emit a \n and any indentation before spitting out this str.

Specified by:
write in interface STWriter
Throws:
IOException

writeWrap

public int writeWrap(String wrap)
              throws IOException
Description copied from interface: STWriter
Because we evaluate ST instance by invoking exec() again, we can't pass options in. So the WRITE instruction of an applied template (such as when we wrap in between template applications like ]}; wrap>) we need to write the wrap string before calling exec(). We expose just like for the separator. See Interpreter.writeObject where it checks for ST instance. If POJO, writePOJO passes wrap to STWriter's write(String str, String wrap) method. Can't pass to exec().

Specified by:
writeWrap in interface STWriter
Throws:
IOException

indent

public int indent()
           throws IOException
Throws:
IOException


Copyright © 2011. All Rights Reserved.