Class IndentPrinter

java.lang.Object
de.monticore.prettyprint.IndentPrinter

public class IndentPrinter extends Object
This class can be used as a printer to files or StringBuilders. It adds indentation by using the special methods indent(), unindent(). The method calls indent() and reindent() affect the current line, even if the first part was already printed. Note: Call flushBuffer() at the end in order to flush the internal buffer to retrieve the correct output. STATE Ok; Useful class for indent strings
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected int
     
    protected StringBuilder
     
    protected int
     
    protected boolean
     
    protected int
     
    protected String
     
    protected String
     
    protected StringBuilder
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Uses a new interal buffer for writing
    Uses this StringBuilder for appending
    IndentPrinter(String startContent, int indention)
    Uses startcontent as start of buffer an sets the indention
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addLine(Object newContent)
    adds a line and sets the indention automatically as following: if more "}" than "{" in newContent, the next line will be unindented by the difference, otherwise the current line will be indented by the difference.
    void
     
    protected void
    This method actually does the print.
    void
    Flushes the internal buffer.
    Returns the content of the internal buffer Note: This method isn't side effect free: It flushes the internal buffer.
    int
    Returns the current indentation of the printer
    int
    Returns length of intendation: number of spaces per level
    int
    Returns the maximum used line length
    Returns the WrittenBuffer without flushing the buffer.
    protected void
     
    void
    Indent one level This method call affects the current line, even if the first part was already printed.
    void
    indent(int i)
    For positive values of i: indent i levels For negative values of i: unindent -i levels This method call affects the current line, even if the first part was already printed.
    boolean
    optional break (makes additional line breal only after optionalBreak()-invocation
    boolean
    Returns true if the current position is the beginning of a new line
    void
     
    void
    Prints the result of the toString() method of Object o or the string "null" if o has the null value
    void
    Prints a newline
    void
    println(int n)
    Prints n newlines
    void
    Prints the result of the toString() method of Object o or the string "null" if o has the null value followed by a newline
    void
     
    void
    setIndentation(int indent)
    Set the current indentation of the printer
    void
    Set length of intendation: number of spaces per level
    void
    setMaxlinelength(int maxlinelength)
    Sets the maximum used line length
    void
    setOptionalBreak(boolean optionBreak)
    optional break (makes additional line breal only after optionalBreak()-invocation
    void
    Removes all trailing white spaces on the current line
    void
    Unindent one level This method call affects the current line, even if the first part was already printed.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • indent

      protected int indent
    • spacer

      protected String spacer
    • sp

      protected String sp
    • maxlinelength

      protected int maxlinelength
    • optionalBreak

      protected boolean optionalBreak
    • optionalBreakPosition

      protected int optionalBreakPosition
    • linebuffer

      protected StringBuilder linebuffer
    • writtenbuffer

      protected StringBuilder writtenbuffer
  • Constructor Details

    • IndentPrinter

      public IndentPrinter()
      Uses a new interal buffer for writing
    • IndentPrinter

      public IndentPrinter(StringBuilder writtenbuffer)
      Uses this StringBuilder for appending
      Parameters:
      writtenbuffer - Buffer to use
    • IndentPrinter

      public IndentPrinter(String startContent, int indention)
      Uses startcontent as start of buffer an sets the indention
      Parameters:
      startContent - first line of content
      indention - first indention (e.g. 0 for classes, 1 for methods and attributes)
  • Method Details

    • getContent

      public String getContent()
      Returns the content of the internal buffer Note: This method isn't side effect free: It flushes the internal buffer. After calling this method, new text added by print/println is automaticly starting in a new line
      Returns:
      Content of buffer as String
    • setIndentLength

      public void setIndentLength(int l)
      Set length of intendation: number of spaces per level
      Parameters:
      l - number of spaces per level (default is 2)
    • getIndentLength

      public int getIndentLength()
      Returns length of intendation: number of spaces per level
      Returns:
      number of spaces per level
    • setIndentation

      public void setIndentation(int indent)
      Set the current indentation of the printer
    • getIndentation

      public int getIndentation()
      Returns the current indentation of the printer
      Returns:
    • doPrint

      protected void doPrint(String s)
      This method actually does the print. It deals with "\r""\n","\r","\n" in the string. This method is not meant for external use.
      Parameters:
      s - String to be
    • handleOptionalBreak

      protected void handleOptionalBreak()
    • flushBuffer

      public void flushBuffer()
      Flushes the internal buffer. After calling this method, new text added by print/println is automaticly starting in a new line
    • indent

      public void indent(int i)
      For positive values of i: indent i levels For negative values of i: unindent -i levels This method call affects the current line, even if the first part was already printed.
      Parameters:
      i - Number of levels to indent/unindent
    • indent

      public void indent()
      Indent one level This method call affects the current line, even if the first part was already printed.
    • unindent

      public void unindent()
      Unindent one level This method call affects the current line, even if the first part was already printed.
    • print

      public void print(Object o)
      Prints the result of the toString() method of Object o or the string "null" if o has the null value
      Parameters:
      o - Object to be printed
    • printWithoutProcessing

      public void printWithoutProcessing(Object o)
    • println

      public void println(Object o)
      Prints the result of the toString() method of Object o or the string "null" if o has the null value followed by a newline
      Parameters:
      o - Object to be printed
    • println

      public void println()
      Prints a newline
    • println

      public void println(int n)
      Prints n newlines
      Parameters:
      n - Number of newlines
    • getWrittenbuffer

      public StringBuilder getWrittenbuffer()
      Returns the WrittenBuffer without flushing the buffer. This methdo call is side effect free, but might not contain the whole buffer. To retrieve the complete buffer call flushBuffer() before getWrtttenBuffer()
      Returns:
      Buffer with already written lines
    • isStartOfLine

      public boolean isStartOfLine()
      Returns true if the current position is the beginning of a new line
      Returns:
      true if current line is empty
    • addLine

      public void addLine(Object newContent)
      adds a line and sets the indention automatically as following: if more "}" than "{" in newContent, the next line will be unindented by the difference, otherwise the current line will be indented by the difference. If the difference is 0, no indention will be changed
      Parameters:
      newContent - content to add
    • getMaxlinelength

      public int getMaxlinelength()
      Returns the maximum used line length
      Returns:
    • setMaxlinelength

      public void setMaxlinelength(int maxlinelength)
      Sets the maximum used line length
      Parameters:
      maxlinelength -
    • isOptionalBreak

      public boolean isOptionalBreak()
      optional break (makes additional line breal only after optionalBreak()-invocation
      Returns:
    • setOptionalBreak

      public void setOptionalBreak(boolean optionBreak)
      optional break (makes additional line breal only after optionalBreak()-invocation
      Parameters:
      optionBreak -
    • optionalBreak

      public void optionalBreak()
    • clearBuffer

      public void clearBuffer()
    • stripTrailing

      public void stripTrailing()
      Removes all trailing white spaces on the current line