polyglot.util
Class SimpleCodeWriter

java.lang.Object
  extended by polyglot.util.CodeWriter
      extended by polyglot.util.SimpleCodeWriter

public class SimpleCodeWriter
extends CodeWriter

SimpleCodeWriter is a simple, fast, bulletproof implementation of the CodeWriter interface. However, it does not try very hard to use vertical space optimally or to stay within the horizontal margins. If aesthetically appealing output is desired, use OptimalCodeWriter.


Nested Class Summary
protected  class SimpleCodeWriter.State
           
 
Field Summary
protected  boolean breakAll
           
protected  int lmargin
           
protected  Stack lmargins
           
protected  PrintWriter output
           
protected  int pos
           
protected  int rmargin
           
protected  int width
           
 
Constructor Summary
SimpleCodeWriter(OutputStream o, int width_)
           
SimpleCodeWriter(PrintWriter o, int width_)
           
SimpleCodeWriter(Writer o, int width_)
           
 
Method Summary
 void allowBreak(int n, int level, String alt, int altlen)
          Insert a break (an optional newline).
 void begin(int n)
          Start a new block with a relative indentation of n characters.
 void close()
          Flush all formatted text, reset formatter state, and close the underlying writer.
 void end()
          Terminate the most recent outstanding begin.
 boolean flush()
          Flush all formatted text to the underlying writer.
 boolean flush(boolean format)
          Like flush, but passing format=false causes output to be generated in the fastest way possible, with all breaks broken.
 void newline()
          Force a newline.
 void newline(int n, int level)
          newline with a specified indentation and level.
 String toString()
          toString is not really supported by this implementation.
 void unifiedBreak(int n, int level, String alt, int altlen)
          Insert a unified break.
 void write(String s)
          Print the string s verbatim on the output stream.
 void write(String s, int length)
          Print the string s on the output stream.
 
Methods inherited from class polyglot.util.CodeWriter
allowBreak, allowBreak, newline, unifiedBreak
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

output

protected PrintWriter output

width

protected int width

rmargin

protected int rmargin

lmargin

protected int lmargin

breakAll

protected boolean breakAll

lmargins

protected Stack lmargins

pos

protected int pos
Constructor Detail

SimpleCodeWriter

public SimpleCodeWriter(OutputStream o,
                        int width_)

SimpleCodeWriter

public SimpleCodeWriter(PrintWriter o,
                        int width_)

SimpleCodeWriter

public SimpleCodeWriter(Writer o,
                        int width_)
Method Detail

write

public void write(String s)
Description copied from class: CodeWriter
Print the string s verbatim on the output stream.

Specified by:
write in class CodeWriter
Parameters:
s - the string to print.

write

public void write(String s,
                  int length)
Description copied from class: CodeWriter
Print the string s on the output stream. Pretend that it has width length even if it has a different number of characters. This is useful when the string contains escape sequences, HTML character entity references, etc.

Specified by:
write in class CodeWriter

begin

public void begin(int n)
Description copied from class: CodeWriter
Start a new block with a relative indentation of n characters.

Specified by:
begin in class CodeWriter

end

public void end()
Description copied from class: CodeWriter
Terminate the most recent outstanding begin.

Specified by:
end in class CodeWriter

allowBreak

public void allowBreak(int n,
                       int level,
                       String alt,
                       int altlen)
Description copied from class: CodeWriter
Insert a break (an optional newline). Indentation will be preserved. Every break has a level. A level 0 break is always broken to form a newline. The codewriter tries to avoid breaking higher-level breaks, and the higher the level, the harder it tries.

Specified by:
allowBreak in class CodeWriter
Parameters:
n - indentation relative to the current block if the newline is inserted. Requires: n >= 0
level - the level of the break. Requires: level >= 0
alt - if no newline is inserted, the string alt is output instead. Requires: alt != null

unifiedBreak

public void unifiedBreak(int n,
                         int level,
                         String alt,
                         int altlen)
Description copied from class: CodeWriter
Insert a unified break. Unified breaks act like the breaks inserted by allowBreak, but unified breaks should also break if any break of the same level in the same block is broken, whereas ordinary breaks do not necessarily break in this case. That is, unified breaks act as if they were slightly lower level than other breaks of the same level (including other unified breaks!).

Specified by:
unifiedBreak in class CodeWriter
Parameters:
n - the relative indentation
level - the level of the break
alt - the alternative text
See Also:
CodeWriter.allowBreak(int, int, java.lang.String, int)

newline

public void newline()
Description copied from class: CodeWriter
Force a newline. Indentation will be preserved.

Overrides:
newline in class CodeWriter

newline

public void newline(int n,
                    int level)
Description copied from class: CodeWriter
newline with a specified indentation and level.

Specified by:
newline in class CodeWriter

flush

public boolean flush()
              throws IOException
Description copied from class: CodeWriter
Flush all formatted text to the underlying writer. Returns true if formatting was completely successful (the margins were obeyed).

Specified by:
flush in class CodeWriter
Throws:
IOException

flush

public boolean flush(boolean format)
              throws IOException
Description copied from class: CodeWriter
Like flush, but passing format=false causes output to be generated in the fastest way possible, with all breaks broken.

Specified by:
flush in class CodeWriter
Parameters:
format - whether to pretty-print the output
Returns:
whether formatting was completely successful.
Throws:
IOException

close

public void close()
           throws IOException
Description copied from class: CodeWriter
Flush all formatted text, reset formatter state, and close the underlying writer.

Specified by:
close in class CodeWriter
Throws:
IOException

toString

public String toString()
toString is not really supported by this implementation.

Specified by:
toString in class CodeWriter