xtc.tree
Class Printer

java.lang.Object
  extended by xtc.tree.Utility
      extended by xtc.tree.Printer

public class Printer
extends Utility

A node pretty printing utility. This class helps with the pretty printing of syntax trees, including with the generation of source code. It provides facilities for indenting, escaping, aligning, and line-wrapping text. Note that, for the facilities of this class to work, newlines should never be printed through a character or string constant (e.g., by using '\n' or '\r') but always by calling the appropriate method.

Version:
$Revision: 1.62 $
Author:
Robert Grimm

Field Summary
protected  java.text.BreakIterator breaks
          The break iterator, if any.
protected  java.io.StringWriter bufferedOut
          The string writer, if output is currently being buffered.
protected  int buffering
          The number of outstanding invocations to buffer().
protected  int column
          The current column.
protected  java.io.PrintWriter directOut
          The original print writer.
protected  int indent
          The current indentation level.
protected  long line
          The current line.
protected  java.io.PrintWriter out
          The current print writer to print to.
 
Constructor Summary
Printer(java.io.OutputStream out)
          Create a new printer with the specified output stream.
Printer(java.io.PrintWriter out)
          Create a new printer with the specified print writer.
Printer(java.io.Writer out)
          Create a new printer with the specified writer.
 
Method Summary
 Printer align(int alignment)
          Print whitespace to align the output.
 Printer buffer()
          Start buffering the output.
 void close()
          Close this printer.
 int column()
          Get the current column number.
 Printer column(int column)
          Set the current column to the specified number.
 Printer decr()
          Decrease the current indentation level.
 Printer escape(char c)
          Print the specified character using C escapes.
 Printer escape(char c, int flags)
          Print the specified character with the specified escape sequences.
 Printer escape(java.lang.String s)
          Print the specified string using C escapes.
 Printer escape(java.lang.String s, int flags)
          Print the specified string with the specified escape sequences.
 Printer fit()
          Ensure that the buffer contents fit onto the current line.
 Printer fit(int align)
          Ensure that the buffer contents fit onto the current line.
 Printer fit(java.lang.String prefix)
          Ensure that the buffer contents fit onto the current line.
 Printer fitMore()
          Ensure that the buffer contents fit onto the current line.
 Printer flush()
          Flush the underlying print writer.
 Printer format(Node n)
          Format the specified node.
 Printer format(Node n, boolean locate)
          Format the specified node.
 Printer incr()
          Increase the current indentation level.
 Printer indent()
          Indent.
 Printer indentLess()
          Indent one tab stop less than the current indentation level.
 Printer indentMore()
          Indent one tab stop more than the current indentation level.
 int level()
          Get the current indentation level.
 long line()
          Get the current line number.
 Printer line(long line)
          Set the current line to the specified number.
 Printer lineUp(Locatable locatable)
          Line this printer up at the specified locatable object's location.
 Printer lineUp(Locatable locatable, int before)
          Line this printer up at the specified number of characters before the specified locatable object's location,
 Printer loc(Locatable locatable)
          Print the location for the specified locatable object.
 Printer p(Attribute attribute)
          Print the specified attribute.
 Printer p(char c)
          Print the specified character.
 Printer p(Comment comment)
          Print the specified comment.
 Printer p(double d)
          Print the specified double.
 Printer p(int i)
          Print the specified integer.
 Printer p(long l)
          Print the specified long.
 Printer p(Node node)
          Print the specified node.
 Printer p(java.lang.String s)
          Print the specified string.
 Printer pad(long l, int width)
          Print the specified long while also padding it to the specified width with leading spaces.
 Printer pln()
          Print a newline.
 Printer pln(char c)
          Print the specified character followed by a newline.
 Printer pln(double d)
          Print the specified double followed by a newline.
 Printer pln(int i)
          Print the specified integer followed by a newline.
 Printer pln(long l)
          Print the specified long followed by a newline.
 Printer pln(java.lang.String s)
          Print the specified string followed by a newline.
 Printer reset()
          Reset this printer.
 Printer sep()
          Print an indented separation comment.
 Printer setLevel(int level)
          Set the current indentation level.
protected  java.lang.String stopBuffering()
          Reset any buffering.
 Printer unbuffer()
          Stop buffering the output.
 Printer wrap(int alignment, java.lang.String text)
          Print the specified text.
 
Methods inherited from class xtc.tree.Utility
register, visitor
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

breaks

protected java.text.BreakIterator breaks
The break iterator, if any.


out

protected java.io.PrintWriter out
The current print writer to print to.


directOut

protected java.io.PrintWriter directOut
The original print writer.


bufferedOut

protected java.io.StringWriter bufferedOut
The string writer, if output is currently being buffered.


buffering

protected int buffering
The number of outstanding invocations to buffer().


indent

protected int indent
The current indentation level.


column

protected int column
The current column.


line

protected long line
The current line.

Constructor Detail

Printer

public Printer(java.io.OutputStream out)
Create a new printer with the specified output stream. The printer does not flush the specified output stream on newlines.

Parameters:
out - The output stream.

Printer

public Printer(java.io.Writer out)
Create a new printer with the specified writer. The printer does not flush the specified writer on newlines.

Parameters:
out - The writer.

Printer

public Printer(java.io.PrintWriter out)
Create a new printer with the specified print writer.

Parameters:
out - The print writer to output to.
Method Detail

reset

public Printer reset()
Reset this printer. This method stops buffering (if this printer was buffering) and clears the current indentation level, column number, and line number.

Returns:
This printer.

column

public int column()
Get the current column number.

Returns:
The current column number.

column

public Printer column(int column)
Set the current column to the specified number.

Parameters:
column - The new column number.
Returns:
This printer.

line

public long line()
Get the current line number.

Returns:
The current line number.

line

public Printer line(long line)
Set the current line to the specified number.

Parameters:
line - The new line number.
Returns:
This printer.

buffer

public Printer buffer()
Start buffering the output. This method starts redirecting all output into a buffer, so that later invocations to fit(), fit(String), or fitMore() can ensure that the output fits onto the current line.

Note that invocations to this method are matched with invocations to the fit() and fitMore() methods. In other words, the fit() and fitMore() methods only have an effect, if they correspond to the first invocation of this method after (1) this printer has been created, (2) the last invocation of reset(), (3) the last invocation of unbuffer(), or (4) the last invocation of any methods printing a newline.

Returns:
This printer.

stopBuffering

protected java.lang.String stopBuffering()
Reset any buffering. If this printer is currently buffering the output, this method stops buffering and returns the buffer contents. Otherwise, it returns the empty string.

Returns:
The buffer contents.

fit

public Printer fit()
Ensure that the buffer contents fit onto the current line. This method writes the buffer contents out. If the contents do not fit onto the current line, it first writes a newline and then indents the output.

Returns:
This printer.
See Also:
buffer()

fit

public Printer fit(int align)
Ensure that the buffer contents fit onto the current line. This method writes the buffer contents out. If the contents do not fit onto the current line, it first writes a newline and then aligns the output with specified alignment.

Parameters:
align - The alignment.
Returns:
This printer.
See Also:
buffer()

fit

public Printer fit(java.lang.String prefix)
Ensure that the buffer contents fit onto the current line. This method writes the buffer contents out. If the contents do not fit onto the current line, it first writes a newline, then indents the output, and then writes the specified prefix.

Parameters:
prefix - The prefix.
Returns:
This printer.
See Also:
buffer()

fitMore

public Printer fitMore()
Ensure that the buffer contents fit onto the current line. This method writes the buffer contents out. If the contents do not fit onto the current line, it first writes a newline and then indents the output one tab stop more than the current indentation level.

Returns:
This printer.
See Also:
buffer()

unbuffer

public Printer unbuffer()
Stop buffering the output. If the output is currently being buffered, this method writes the buffer contents out and stops buffering. Otherwise, it has no effect.

Returns:
This printer.

align

public Printer align(int alignment)
Print whitespace to align the output. This method prints whitespace to cover the difference between the current column number and the specified, absolute alignment. If the column number is greater or equal the specified alignment, a single space character is printed.

Parameters:
alignment - The number of characters to align at.
Returns:
This printer.

level

public int level()
Get the current indentation level.

Returns:
The current indentation level.

setLevel

public Printer setLevel(int level)
Set the current indentation level.

Parameters:
level - The new indentation level.
Returns:
This printer.
Throws:
java.lang.IllegalArgumentException - Signals that the specified level is negative.

incr

public Printer incr()
Increase the current indentation level.

Returns:
This printer.

decr

public Printer decr()
Decrease the current indentation level.

Returns:
This printer.

indent

public Printer indent()
Indent.

Returns:
This printer.

indentLess

public Printer indentLess()
Indent one tab stop less than the current indentation level.

Returns:
This printer.

indentMore

public Printer indentMore()
Indent one tab stop more than the current indentation level.

Returns:
This printer.

p

public Printer p(char c)
Print the specified character.

Parameters:
c - The character to print.
Returns:
This printer.

p

public Printer p(int i)
Print the specified integer.

Parameters:
i - The integer to print.
Returns:
This printer.

p

public Printer p(long l)
Print the specified long.

Parameters:
l - The long to print.
Returns:
This printer.

p

public Printer p(double d)
Print the specified double.

Parameters:
d - The double to print.
Returns:
This printer.

p

public Printer p(java.lang.String s)
Print the specified string.

Parameters:
s - The string to print.
Returns:
This printer.

pln

public Printer pln(char c)
Print the specified character followed by a newline.

Parameters:
c - The character to print.
Returns:
This printer.

pln

public Printer pln(int i)
Print the specified integer followed by a newline.

Parameters:
i - The integer to print.
Returns:
This printer.

pln

public Printer pln(long l)
Print the specified long followed by a newline.

Parameters:
l - The long to print.
Returns:
This printer.

pln

public Printer pln(double d)
Print the specified double followed by a newline.

Parameters:
d - The double to print.
Returns:
This printer.

pln

public Printer pln(java.lang.String s)
Print the specified string followed by a newline.

Parameters:
s - The string to print.
Returns:
This printer.

pln

public Printer pln()
Print a newline.

Returns:
This printer.

escape

public Printer escape(char c)
Print the specified character using C escapes.

Parameters:
c - The character to print.
Returns:
This printer.

escape

public Printer escape(char c,
                      int flags)
Print the specified character with the specified escape sequences.

Parameters:
c - The character to print.
flags - The escape flags.
Returns:
This printer.
See Also:
Utilities

escape

public Printer escape(java.lang.String s)
Print the specified string using C escapes.

Parameters:
s - The string to print.
Returns:
This printer.

escape

public Printer escape(java.lang.String s,
                      int flags)
Print the specified string with the specified escape sequences.

Parameters:
s - The string to print.
flags - The escape flags.
Returns:
This printer.
See Also:
Utilities

pad

public Printer pad(long l,
                   int width)
Print the specified long while also padding it to the specified width with leading spaces.

Parameters:
l - The long to print.
width - The width.
Returns:
This printer.

sep

public Printer sep()
Print an indented separation comment.

Returns:
This printer.

wrap

public Printer wrap(int alignment,
                    java.lang.String text)
Print the specified text. This method line-wraps the specified text with the specified per-line alignment. It does, however, not print the initial alignment or the final end-of-line.

Parameters:
alignment - The per-line alignment.
text - The text.

p

public Printer p(Node node)
Print the specified node. If the specified node is null, nothing is printed.

Parameters:
node - The node to print.
Returns:
This printer.

p

public Printer p(Attribute attribute)
Print the specified attribute.

Parameters:
attribute - The attribute.
Returns:
This printer.

p

public Printer p(Comment comment)
Print the specified comment. Note that this method does not indent the first line, but it does indent all following lines for comments spanning multiple lines. Further note that this method does not dispatch this printer's visitor on the node contained in the comment.

Parameters:
comment - The comment to print.
Returns:
This printer.

format

public Printer format(Node n)
Format the specified node. Instead of using this printer's visitor to print the specified node, this method emits a general representation of the node, using the node's generic traversal methods if available.

Parameters:
n - The node.
Returns:
This printer.

format

public Printer format(Node n,
                      boolean locate)
Format the specified node. Instead of using this printer's visitor to print the specified node, this method emits a general representation of the node, using the node's generic traversal methods if available.

Parameters:
n - The node.
locate - The flag for printing a node's location.
Returns:
This printer.

loc

public Printer loc(Locatable locatable)
Print the location for the specified locatable object. First, if the locatable is a node and has a Constants.ORIGINAL property, that property's locatable value replaces the specified locatable object. Second, if the actual locatable has a location, this method prints the file name, a colon, the line number, another colon, and the column number. If the actual locatable does not have a location, nothing is printed.

Parameters:
locatable - The locatable object.
Returns:
This printer.

lineUp

public Printer lineUp(Locatable locatable)
Line this printer up at the specified locatable object's location.

Parameters:
locatable - The locatable object.
Returns:
This printer.

lineUp

public Printer lineUp(Locatable locatable,
                      int before)
Line this printer up at the specified number of characters before the specified locatable object's location,

Parameters:
locatable - The locatable object.
before - The number of characters before the object.
Returns:
This printer.

flush

public Printer flush()
Flush the underlying print writer.

Returns:
This printer.

close

public void close()
Close this printer. This method stops buffering (if this printer was buffering) and then closes the underlying print writer.



Copyright © 2012. All Rights Reserved.