|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectgroovy.util.IndentPrinter
public class IndentPrinter
A helper class for printing indented text. This can be used stand-alone or, more commonly, from Builders.
By default, a PrintWriter to System.out is used as the Writer, but it is possible to change the Writer by passing a new one as a constructor argument.
Indention by default is 2 characters but can be changed by passing a different value as a constructor argument.
The following is an example usage. Note that within a "with" block you need to specify a parameter name so that this.println is not called instead of IndentPrinter.println:
new IndentPrinter(new PrintWriter(out)).with { p -> p.printIndent() p.println('parent1') p.incrementIndent() p.printIndent() p.println('child 1') p.printIndent() p.println('child 2') p.decrementIndent() p.printIndent() p.println('parent2') p.flush() }
The above example prints this to standard output:
parent1 child 1 child 2 parent2
Constructor Summary | |
---|---|
IndentPrinter()
Creates an IndentPrinter backed by a PrintWriter pointing to System.out, with an indent of two spaces. |
|
IndentPrinter(Writer out)
Creates an IndentPrinter backed by the supplied Writer, with an indent of two spaces. |
|
IndentPrinter(Writer out,
String indent)
Creates an IndentPrinter backed by the supplied Writer, with a user-supplied String to be used for indenting. |
|
IndentPrinter(Writer out,
String indent,
boolean addNewlines)
Creates an IndentPrinter backed by the supplied Writer, with a user-supplied String to be used for indenting and the ability to override newline handling. |
Method Summary | |
---|---|
void |
decrementIndent()
|
void |
flush()
|
int |
getIndentLevel()
|
void |
incrementIndent()
|
void |
print(char c)
Prints a character. |
void |
print(String text)
Prints a string. |
void |
printIndent()
Prints the current indent level. |
void |
println()
Prints an end-of-line character (if enabled via addNewLines property). |
void |
println(String text)
Prints a string followed by an end of line character. |
void |
setIndentLevel(int indentLevel)
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public IndentPrinter()
IndentPrinter(Writer, String)
public IndentPrinter(Writer out)
out
- Writer to output toIndentPrinter(Writer, String)
public IndentPrinter(Writer out, String indent)
out
- Writer to output toindent
- character(s) used to indent each linepublic IndentPrinter(Writer out, String indent, boolean addNewlines)
out
- Writer to output toindent
- character(s) used to indent each lineaddNewlines
- set to false to gobble all new lines (default true)Method Detail |
---|
public void println(String text)
text
- String to be writtenpublic void print(String text)
text
- String to be writtenpublic void print(char c)
c
- char to be writtenpublic void printIndent()
public void println()
IndentPrinter(Writer, String, boolean)
public void incrementIndent()
public void decrementIndent()
public int getIndentLevel()
public void setIndentLevel(int indentLevel)
public void flush()
|
Copyright © 2003-2012 The Codehaus. All rights reserved. | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |