ppg.util
Class CodeWriter

java.lang.Object
  extended by ppg.util.CodeWriter

public class CodeWriter
extends java.lang.Object

A CodeWriter is a pretty-printing engine. It formats structured text onto an output stream o in the minimum number of lines, while keeping the width of the output within width characters if possible.


Constructor Summary
CodeWriter(java.io.OutputStream o, int width_)
          Create a CodeWriter object with output stream o and width width_.
CodeWriter(java.io.Writer w, int width_)
          Create a CodeWriter object with output w and width width_.
 
Method Summary
 void allowBreak(int n)
          Allow a newline.
 void allowBreak(int n, java.lang.String alt)
          Allow a newline.
 void begin(int n)
          Start a new block with a relative indentation of n characters.
 void end()
          Terminate the most recent outstanding begin.
 boolean flush()
          Send out the current batch of text to be formatted.
 void newline()
          Force a newline with no added indentation.
 void newline(int n)
          Force a newline.
 void write(java.lang.String s)
          Print the string s verbatim on the output stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CodeWriter

public CodeWriter(java.io.OutputStream o,
                  int width_)
Create a CodeWriter object with output stream o and width width_.


CodeWriter

public CodeWriter(java.io.Writer w,
                  int width_)
Create a CodeWriter object with output w and width width_.

Method Detail

write

public void write(java.lang.String s)
Print the string s verbatim on the output stream.


newline

public void newline()
Force a newline with no added indentation.


begin

public void begin(int n)
Start a new block with a relative indentation of n characters.
A block is a formatting unit. The formatting algorithm will try to put the whole block in one line unless If either of the two conditions is satisfied, the formatting algorithm will break the block into lines: every allowBreak will cause a line change, the first line is printed at the current cursor position pos, all the following lines are printed at the position pos+n.

Parameters:
n - the number of characters increased on indentation (relative to the current position) for all lines in the block.

end

public void end()
Terminate the most recent outstanding begin.


allowBreak

public void allowBreak(int n)
Allow a newline. Indentation will be preserved. If no newline is inserted, a single space character is output instead.

Parameters:
n - the amount of increase in indentation if the newline is inserted.

allowBreak

public void allowBreak(int n,
                       java.lang.String alt)
Allow a newline. Indentation will be preserved.

Parameters:
n - the amount of increase in indentation if the newline is inserted.
alt - if no newline is inserted, the string alt is output instead.

newline

public void newline(int n)
Force a newline. Indentation will be preserved. This method should be used sparingly; usually a call to allowBreak is preferable because forcing a newline also causes all breaks in containing blocks to be broken.

Parameters:
n - the amount of increase in indentation after the newline.

flush

public boolean flush()
              throws java.io.IOException
Send out the current batch of text to be formatted. All outstanding begin's are closed and the current indentation level is reset to 0. Returns true if formatting was completely successful (the margins were obeyed).

Throws:
java.io.IOException