de.hunsicker.jalopy.printer
Class NodeWriter

java.lang.Object
  |
  +--java.io.Writer
        |
        +--de.hunsicker.jalopy.printer.NodeWriter

public class NodeWriter
extends java.io.Writer

The writer to be used to print a Java AST. This class contains some basic support methods to be used by printers.

Version:
$Revision: 1.8.2.4 $
Author:
Marco Hunsicker, David Beutel
See Also:
Printer

Field Summary
protected  int column
          Current column position.
protected  boolean continuation
          Should indenting use an added contination amount?
protected  int continuationIndentSize
          Number of spaces to use for continuation indentation.
protected  Environment environment
          The envrionment to use.
protected  boolean footer
          Should a footer be inserted at the end of every file?
static boolean INDENT_NO
          Indicates that no indentation should be performed.
static boolean INDENT_YES
          Indicates that indentation should be performed.
protected  int indentSize
          Number of spaces to take for one indent.
protected  boolean insertTrailingEmpty
          Indicates wether a trailing empty line should be inserted at the end of a file.
protected  int last
          Holds the type of the last printed token.
protected  int leftBraceIndent
          Number of spaces to print before left curly braces.
protected  boolean leftBraceNewline
          Print left curly braces on a new line?
protected  int line
          Current line number.
protected  java.lang.String lineSeparator
          Used line separator.
protected  int mode
          Printing mode.
protected  boolean newline
          Indicates whether we're at the beginning of a new line (column == 1).
static boolean NEWLINE_NO
          Print NO newline after a token.
static boolean NEWLINE_YES
          Print a newline after a token.
protected  boolean nextNewline
          Indicates that the line following this is a new line Used for printing commas in blocks without white space after the new line
protected  java.lang.String originalLineSeparator
          The original line separator of the file as reported by the lexer.
protected  Convention settings
          The code convention settings that controls the output style.
protected  boolean useLeadingTabs
          Should tabs only be used to print leading indentation?
protected  boolean useTabs
          Should tabs be used to print indentation?
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
protected NodeWriter(CompositeFactory factory)
          Creates a new NodeWriter object.
  NodeWriter(java.io.Writer out, CompositeFactory factory, java.lang.String filename, java.util.Map issues, java.lang.String lineSeparator, java.lang.String originalLineSeparator)
          Creates a new NodeWriter object with the given file output format.
 
Method Summary
 void close()
          Closes the stream, flushing it first.
 void flush()
          Flushes the stream.
 int getColumn()
          Returns the line column position of the last written character.
 CompositeFactory getCompositeFactory()
           
 java.lang.String getFilename()
          Returns the name of the parsed file.
 int getIndentLength()
          Returns the length of the current indent string.
 int getIndentLevel()
          Returns the current indent level.
 int getIndentSize()
          Returns the number of spaces to use for indentation.
 JavaNodeFactory getJavaNodeFactory()
           
 int getLast()
          Returns the type of the token last printed.
 int getLine()
          Returns the current line number.
 java.lang.String getLineSeparator()
          Returns the current line separator.
 java.lang.String getString(int length)
          Returns a string of the given length.
 void indent()
          Increases the current indent level one level.
 int print(java.lang.String string, int type)
          Outputs the given string of the given type to the underlying writer.
 void printBlankLines(int amount)
          Prints the given number of blank lines.
 int printLeftBrace()
          Outputs a left curly brace.
 int printLeftBrace(boolean newlineBefore, boolean newlineAfter)
          Outputs a leftcurly brace.
 int printLeftBrace(boolean newlineBefore, boolean newlineAfter, boolean indent)
          Outputs a left curly brace.
 void printNewline()
          Outputs a line break.
 int printRightBrace()
          Outputs a closing curly brace.
 int printRightBrace(boolean newlineAfter)
          Outputs a right curly brace.
 int printRightBrace(int type, boolean newlineAfter)
          Outputs a right curly brace.
 int printRightBrace(int type, boolean whitespaceBefore, boolean newlineAfter)
          Outputs a right curly brace.
 void setEnvironment(Environment environment)
          Sets the environment to use.
 void setFilename(java.lang.String filename)
          Sets the filename of the file beeing printed.
 void setIndentLevel(int level)
          Sets the current indent level.
 void setLast(int type)
          Sets the type of the token last printed.
 void setLineSeparator(java.lang.String lineSeparator)
          Sets the separator string to use for newlines.
 void setTracking(boolean tracking)
          Sets whether the tree that is to be printed contains nodes that needs their positions tracked.
 void setWriter(java.io.Writer out)
          Sets the underlying writer to actually write to.
 void unindent()
          Decreases the current indent level one level.
 void write(char[] cbuf, int off, int len)
          Write a portion of an array of characters.
 
Methods inherited from class java.io.Writer
write, write, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INDENT_NO

public static final boolean INDENT_NO
Indicates that no indentation should be performed.

See Also:
Constant Field Values

INDENT_YES

public static final boolean INDENT_YES
Indicates that indentation should be performed.

See Also:
Constant Field Values

NEWLINE_NO

public static final boolean NEWLINE_NO
Print NO newline after a token.

See Also:
Constant Field Values

NEWLINE_YES

public static final boolean NEWLINE_YES
Print a newline after a token.

See Also:
Constant Field Values

settings

protected Convention settings
The code convention settings that controls the output style.


environment

protected Environment environment
The envrionment to use.


lineSeparator

protected java.lang.String lineSeparator
Used line separator. Defaults to the platform standard.


originalLineSeparator

protected java.lang.String originalLineSeparator
The original line separator of the file as reported by the lexer.


continuation

protected boolean continuation
Should indenting use an added contination amount?


footer

protected boolean footer
Should a footer be inserted at the end of every file?


insertTrailingEmpty

protected boolean insertTrailingEmpty
Indicates wether a trailing empty line should be inserted at the end of a file.


leftBraceNewline

protected boolean leftBraceNewline
Print left curly braces on a new line?


newline

protected boolean newline
Indicates whether we're at the beginning of a new line (column == 1).


nextNewline

protected boolean nextNewline
Indicates that the line following this is a new line Used for printing commas in blocks without white space after the new line


useLeadingTabs

protected boolean useLeadingTabs
Should tabs only be used to print leading indentation?


useTabs

protected boolean useTabs
Should tabs be used to print indentation?


column

protected int column
Current column position.


continuationIndentSize

protected int continuationIndentSize
Number of spaces to use for continuation indentation.


indentSize

protected int indentSize
Number of spaces to take for one indent.


last

protected int last
Holds the type of the last printed token.


leftBraceIndent

protected int leftBraceIndent
Number of spaces to print before left curly braces.


line

protected int line
Current line number.


mode

protected int mode
Printing mode.

Constructor Detail

NodeWriter

public NodeWriter(java.io.Writer out,
                  CompositeFactory factory,
                  java.lang.String filename,
                  java.util.Map issues,
                  java.lang.String lineSeparator,
                  java.lang.String originalLineSeparator)
Creates a new NodeWriter object with the given file output format.

Parameters:
out - the output stream to write to.
filename - name of the parsed file.
issues - holds the issues found during a run.
lineSeparator - the lineSeparator to use.
originalLineSeparator - the original line separator of the file.

NodeWriter

protected NodeWriter(CompositeFactory factory)
Creates a new NodeWriter object.

Method Detail

getCompositeFactory

public CompositeFactory getCompositeFactory()

getJavaNodeFactory

public JavaNodeFactory getJavaNodeFactory()

getColumn

public int getColumn()
Returns the line column position of the last written character.

Returns:
line position of the last written character.

setEnvironment

public void setEnvironment(Environment environment)
Sets the environment to use.

Parameters:
environment - environment.
Since:
1.0b9

setFilename

public void setFilename(java.lang.String filename)
Sets the filename of the file beeing printed.

Parameters:
filename - filename of the source files beeing printed.

getFilename

public java.lang.String getFilename()
Returns the name of the parsed file.

Returns:
filename.

getIndentLength

public int getIndentLength()
Returns the length of the current indent string.

Returns:
indent string length.

setIndentLevel

public void setIndentLevel(int level)
Sets the current indent level.

Parameters:
level - new indent level.

getIndentLevel

public int getIndentLevel()
Returns the current indent level.

Returns:
indent level.

getIndentSize

public int getIndentSize()
Returns the number of spaces to use for indentation.

Returns:
the indentation size.
Since:
1.0b8

setLast

public void setLast(int type)
Sets the type of the token last printed.

Parameters:
type - type of the token

getLast

public int getLast()
Returns the type of the token last printed.

Returns:
type of the token that was printed last. Returns JavaTokenTypes.BOF if nothing was printed yet.

getLine

public int getLine()
Returns the current line number.

Returns:
the current line number.

setLineSeparator

public void setLineSeparator(java.lang.String lineSeparator)
Sets the separator string to use for newlines.

Parameters:
lineSeparator - separator string. Either "\n", "\r" or "\r\n".

getLineSeparator

public java.lang.String getLineSeparator()
Returns the current line separator.

Returns:
the current line separator.

getString

public java.lang.String getString(int length)
Returns a string of the given length.

Parameters:
length - length of the string to return.
Returns:
string with the given length.

setTracking

public void setTracking(boolean tracking)
Sets whether the tree that is to be printed contains nodes that needs their positions tracked.

Parameters:
tracking - true to indicate that certain nodes needs their positions tracked.
Since:
1.0b9

setWriter

public void setWriter(java.io.Writer out)
Sets the underlying writer to actually write to.

Parameters:
out - writer to write to.

close

public void close()
           throws java.io.IOException
Closes the stream, flushing it first.

Specified by:
close in class java.io.Writer
Throws:
java.io.IOException - if an I/O error occured.

flush

public void flush()
           throws java.io.IOException
Flushes the stream.

Specified by:
flush in class java.io.Writer
Throws:
java.io.IOException - if an I/O error occured.

indent

public void indent()
Increases the current indent level one level.


print

public int print(java.lang.String string,
                 int type)
          throws java.io.IOException
Outputs the given string of the given type to the underlying writer.

Parameters:
string - string to write.
type - type of the string.
Returns:
the column offset were the string started.
Throws:
java.io.IOException - if an I/O error occured.

printBlankLines

public void printBlankLines(int amount)
                     throws java.io.IOException
Prints the given number of blank lines.

Parameters:
amount - number of blank lines to print.
Throws:
java.io.IOException - if an I/O error occured.

printLeftBrace

public int printLeftBrace()
                   throws java.io.IOException
Outputs a left curly brace.

Returns:
the column offset where the brace was printed.
Throws:
java.io.IOException - if an I/O error occured.

printLeftBrace

public int printLeftBrace(boolean newlineBefore,
                          boolean newlineAfter)
                   throws java.io.IOException
Outputs a leftcurly brace.

Parameters:
newlineBefore - true if a newline should be printed before the brace.
newlineAfter - true if a newline should be printed after the brace.
Returns:
the column offset where the brace was printed.
Throws:
java.io.IOException - if an I/O error occured.

printLeftBrace

public int printLeftBrace(boolean newlineBefore,
                          boolean newlineAfter,
                          boolean indent)
                   throws java.io.IOException
Outputs a left curly brace.

Parameters:
newlineBefore - true if a newline should be printed before the brace.
newlineAfter - true if a newline should be printed after the brace.
indent - if true the brace will be indented relative to the current indentation level.
Returns:
the column offset where the brace was printed.
Throws:
java.io.IOException - if an I/O error occured.

printNewline

public void printNewline()
                  throws java.io.IOException
Outputs a line break.

Throws:
java.io.IOException - if an I/O error occured.

printRightBrace

public int printRightBrace()
                    throws java.io.IOException
Outputs a closing curly brace. Prints a newline after the brace.

Returns:
the column offset where the brace was printed.
Throws:
java.io.IOException - if an I/O error occured.

printRightBrace

public int printRightBrace(boolean newlineAfter)
                    throws java.io.IOException
Outputs a right curly brace.

Parameters:
newlineAfter - true if a newline should be printed after the brace.
Returns:
the column offset where the brace was printed.
Throws:
java.io.IOException - if an I/O error occured.

printRightBrace

public int printRightBrace(int type,
                           boolean newlineAfter)
                    throws java.io.IOException
Outputs a right curly brace.

Parameters:
type - the type of the brace. Either RCURLY or OBJBLOCK.
newlineAfter - if true a newline will be printed after the brace.
Returns:
the column offset where the brace was printed.
Throws:
java.io.IOException - if an I/O error occured.

printRightBrace

public int printRightBrace(int type,
                           boolean whitespaceBefore,
                           boolean newlineAfter)
                    throws java.io.IOException
Outputs a right curly brace.

Parameters:
type - the type of the brace. Either RCURLY or OBJBLOCK.
whitespaceBefore - if true outputs indentation whitespace (depending on the code convention setting).
newlineAfter - if true a newline will be printed after the brace.
Returns:
the column offset where the brace was printed.
Throws:
java.io.IOException - if an I/O error occured.

unindent

public void unindent()
Decreases the current indent level one level.


write

public void write(char[] cbuf,
                  int off,
                  int len)
           throws java.io.IOException
Write a portion of an array of characters.

Specified by:
write in class java.io.Writer
Parameters:
cbuf - array of characters.
off - offset from which to start writing characters.
len - number of characters to write.
Throws:
java.io.IOException - if an I/O error occured.


Copyright © 1997-2005 Jalopy. All Rights Reserved.