Package de.monticore.prettyprint
Class IndentPrinter
java.lang.Object
de.monticore.prettyprint.IndentPrinter
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
FieldsModifier and TypeFieldDescriptionprotected intprotected StringBuilderprotected intprotected booleanprotected intprotected Stringprotected Stringprotected StringBuilder -
Constructor Summary
ConstructorsConstructorDescriptionUses a new interal buffer for writingIndentPrinter(StringBuilder writtenbuffer) Uses this StringBuilder for appendingIndentPrinter(String startContent, int indention) Uses startcontent as start of buffer an sets the indention -
Method Summary
Modifier and TypeMethodDescriptionvoidadds 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.voidprotected voidThis method actually does the print.voidFlushes the internal buffer.Returns the content of the internal buffer Note: This method isn't side effect free: It flushes the internal buffer.intReturns the current indentation of the printerintReturns length of intendation: number of spaces per levelintReturns the maximum used line lengthReturns the WrittenBuffer without flushing the buffer.protected voidvoidindent()Indent one level This method call affects the current line, even if the first part was already printed.voidindent(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.booleanoptional break (makes additional line breal only after optionalBreak()-invocationbooleanReturns true if the current position is the beginning of a new linevoidvoidPrints the result of the toString() method of Object o or the string "null" if o has the null valuevoidprintln()Prints a newlinevoidprintln(int n) Prints n newlinesvoidPrints the result of the toString() method of Object o or the string "null" if o has the null value followed by a newlinevoidvoidsetIndentation(int indent) Set the current indentation of the printervoidsetIndentLength(int l) Set length of intendation: number of spaces per levelvoidsetMaxlinelength(int maxlinelength) Sets the maximum used line lengthvoidsetOptionalBreak(boolean optionBreak) optional break (makes additional line breal only after optionalBreak()-invocationvoidRemoves all trailing white spaces on the current linevoidunindent()Unindent one level This method call affects the current line, even if the first part was already printed.
-
Field Details
-
indent
protected int indent -
spacer
-
sp
-
maxlinelength
protected int maxlinelength -
optionalBreak
protected boolean optionalBreak -
optionalBreakPosition
protected int optionalBreakPosition -
linebuffer
-
writtenbuffer
-
-
Constructor Details
-
IndentPrinter
public IndentPrinter()Uses a new interal buffer for writing -
IndentPrinter
Uses this StringBuilder for appending- Parameters:
writtenbuffer- Buffer to use
-
IndentPrinter
Uses startcontent as start of buffer an sets the indention- Parameters:
startContent- first line of contentindention- first indention (e.g. 0 for classes, 1 for methods and attributes)
-
-
Method Details
-
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
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
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
-
println
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
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
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
-