com.ashridgetech.jamda.code
Class CodeWriter

java.lang.Object
  |
  +--com.ashridgetech.jamda.code.CodeWriter
Direct Known Subclasses:
JavaWriter

public class CodeWriter
extends java.lang.Object

Provides services for writing program code to a Writer.

Author:
Paul Boocock version 1.0

Field Summary
protected  java.io.Writer underlyingWriter
          The Writer with which this instance was constructed
 
Constructor Summary
CodeWriter(java.io.Writer writer)
          Construct a CodeWriter attached to a Writer.
 
Method Summary
 CodeWriter begin()
          Write the start of a new block.
 CodeWriter commaDoubleList(java.util.Collection tokens1, java.util.Collection tokens2)
          Write a list pairs of tokens, separated by commas.
 CodeWriter commaList(java.util.Collection tokens)
          Write a list of tokens, separated by commas.
 void copyNoIndent(java.io.Reader r)
          Insert a piece of text from a Reader into the code, without any extra indenting, but keeping any indenting in the lines read from the Reader.
 void copyWithIndent(java.io.Reader r)
          Insert a piece of text from a Reader into the code, placing the current indent before each line, in addition to any indenting in the lines read from the Reader.
 CodeWriter end()
          Write the end of a block, followed by a blank line.
 CodeWriter end(java.lang.String comment)
          Decrease the indent level.
 CodeWriter endLine(java.lang.String code)
          Write a piece of code to the output Writer with a new line after it.
 void flush()
          Flush the Writer used by this CodeWriter.
 java.io.PrintWriter getWriter()
          Get the PrintWriter to which this CodeWriter outputs code.
 CodeWriter in()
          Increase the indent level by one.
 CodeWriter line(java.lang.String line)
          Write a line of code, indented to the current indent level.
 CodeWriter newline()
          End the current line on the output Writer.
 CodeWriter newline(int nLines)
          Write zero or more newlines, the first of which will end the current line on the output Writer.
 CodeWriter out()
          Decrease the indent level by one.
static java.lang.String quote(java.lang.String s)
          Surround a string with double quotes, escaping any double quotes within it.
 CodeWriter space()
          Insert a space into the current line.
 CodeWriter spaceWrite(java.lang.String code)
          Write a space to the output CodeWriter, followed by a piece of code.
 CodeWriter spaceWrite(java.lang.String code1, java.lang.String code2)
          Write two pieces of code to the output Writer, with a space before each one.
 CodeWriter spaceWrite(java.lang.String code1, java.lang.String code2, java.lang.String code3)
          Write three pieces of code to the output Writer, with a space before each one.
 CodeWriter startLine()
          Start a line of code, indented to the current indent level, leaving the current position on the same line.
 CodeWriter startLine(java.lang.String code)
          Start a line of code, indented to the current indent level, write a piece of code, leaving the current position on the same line.
 CodeWriter write(java.lang.String code)
          Write a piece of code to the output Writer.
 CodeWriter write(java.lang.String code1, java.lang.String code2)
          Write two pieces of code to the output Writer.
 CodeWriter write(java.lang.String code1, java.lang.String code2, java.lang.String code3)
          Write three pieces of code to the output Writer.
 CodeWriter writeSpace(java.lang.String code)
          Write a piece of code to the output CodeWriter, followed by a space
 CodeWriter writeSpace(java.lang.String code1, java.lang.String code2)
          Write two pieces of code to the output Writer, with a space after each one.
 CodeWriter writeSpace(java.lang.String code1, java.lang.String code2, java.lang.String code3)
          Write three pieces of code to the output Writer, with a space after each one.
 CodeWriter writeSpaced(java.lang.String code)
          Write a piece of code to the output CodeWriter, preceded and followed by a space
 CodeWriter writeSpaced(java.lang.String code1, java.lang.String code2)
          Write two pieces of code to the output Writer, with a space before the first, between each and after the last.
 CodeWriter writeSpaced(java.lang.String code1, java.lang.String code2, java.lang.String code3)
          Write three pieces of code to the output Writer, with a space before the first, between each and after the last.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

underlyingWriter

protected final java.io.Writer underlyingWriter
The Writer with which this instance was constructed

Constructor Detail

CodeWriter

public CodeWriter(java.io.Writer writer)
Construct a CodeWriter attached to a Writer.

Parameters:
writer - the Writer to which the code is written - stored in underlyingWriter.
Method Detail

getWriter

public java.io.PrintWriter getWriter()
Get the PrintWriter to which this CodeWriter outputs code.

Returns:
the writer

write

public CodeWriter write(java.lang.String code)
Write a piece of code to the output Writer.

Parameters:
code - the code to write
Returns:
this object

write

public CodeWriter write(java.lang.String code1,
                        java.lang.String code2)
Write two pieces of code to the output Writer.

Parameters:
code1 - the first piece of code to write
code2 - the second piece of code to write
Returns:
this object

write

public CodeWriter write(java.lang.String code1,
                        java.lang.String code2,
                        java.lang.String code3)
Write three pieces of code to the output Writer.

Parameters:
code1 - the first piece of code to write
code2 - the second piece of code to write
code3 - the third piece of code to write
Returns:
this object

space

public CodeWriter space()
Insert a space into the current line.

Returns:
this object

writeSpace

public CodeWriter writeSpace(java.lang.String code)
Write a piece of code to the output CodeWriter, followed by a space

Parameters:
code - the code to write
Returns:
this object

writeSpace

public CodeWriter writeSpace(java.lang.String code1,
                             java.lang.String code2)
Write two pieces of code to the output Writer, with a space after each one.

Parameters:
code1 - the first piece of code to write
code2 - the second piece of code to write
Returns:
this object

writeSpace

public CodeWriter writeSpace(java.lang.String code1,
                             java.lang.String code2,
                             java.lang.String code3)
Write three pieces of code to the output Writer, with a space after each one.

Parameters:
code1 - the first piece of code to write
code2 - the second piece of code to write
code3 - the third piece of code to write
Returns:
this object

spaceWrite

public CodeWriter spaceWrite(java.lang.String code)
Write a space to the output CodeWriter, followed by a piece of code.

Parameters:
code - the code to write after the space
Returns:
this object

spaceWrite

public CodeWriter spaceWrite(java.lang.String code1,
                             java.lang.String code2)
Write two pieces of code to the output Writer, with a space before each one.

Parameters:
code1 - the first piece of code to write
code2 - the second piece of code to write
Returns:
this object

spaceWrite

public CodeWriter spaceWrite(java.lang.String code1,
                             java.lang.String code2,
                             java.lang.String code3)
Write three pieces of code to the output Writer, with a space before each one.

Parameters:
code1 - the first piece of code to write
code2 - the second piece of code to write
code3 - the third piece of code to write
Returns:
this object

writeSpaced

public CodeWriter writeSpaced(java.lang.String code)
Write a piece of code to the output CodeWriter, preceded and followed by a space

Parameters:
code - the code to write
Returns:
this object

writeSpaced

public CodeWriter writeSpaced(java.lang.String code1,
                              java.lang.String code2)
Write two pieces of code to the output Writer, with a space before the first, between each and after the last.

Parameters:
code1 - the first piece of code to write
code2 - the second piece of code to write
Returns:
this object

writeSpaced

public CodeWriter writeSpaced(java.lang.String code1,
                              java.lang.String code2,
                              java.lang.String code3)
Write three pieces of code to the output Writer, with a space before the first, between each and after the last.

Parameters:
code1 - the first piece of code to write
code2 - the second piece of code to write
code3 - the third piece of code to write
Returns:
this object

newline

public CodeWriter newline()
End the current line on the output Writer.

Returns:
this object

newline

public CodeWriter newline(int nLines)
Write zero or more newlines, the first of which will end the current line on the output Writer.

Parameters:
nLines - the number of newlines - must be zero or more
Returns:
this object

line

public CodeWriter line(java.lang.String line)
Write a line of code, indented to the current indent level.

Parameters:
line - the line of code
Returns:
this object

startLine

public CodeWriter startLine()
Start a line of code, indented to the current indent level, leaving the current position on the same line.

Returns:
this object

startLine

public CodeWriter startLine(java.lang.String code)
Start a line of code, indented to the current indent level, write a piece of code, leaving the current position on the same line.

Returns:
this object

endLine

public CodeWriter endLine(java.lang.String code)
Write a piece of code to the output Writer with a new line after it.

Parameters:
code - the code to write
Returns:
this object

begin

public CodeWriter begin()
Write the start of a new block.

Returns:
this object

end

public CodeWriter end()
Write the end of a block, followed by a blank line.

Returns:
this object
See Also:
end(String)

end

public CodeWriter end(java.lang.String comment)
Decrease the indent level. Write the end of a block, indented to the new indent level, with single line comment after it, followed by a blank line.

Parameters:
comment - the text of the comment - may be null if no comment is needed
Returns:
this object
See Also:
out()

in

public CodeWriter in()
Increase the indent level by one.

Returns:
this object

out

public CodeWriter out()
Decrease the indent level by one.

Returns:
this object
Throws:
java.lang.IllegalStateException - if the indent level is already zero

commaList

public CodeWriter commaList(java.util.Collection tokens)
Write a list of tokens, separated by commas. A space is added before and after unless the list is empty.

Parameters:
tokens - the list of tokens whose string values are written
Returns:
this object

quote

public static java.lang.String quote(java.lang.String s)
Surround a string with double quotes, escaping any double quotes within it.

Parameters:
s - the String to quote
Returns:
the quoted string

commaDoubleList

public CodeWriter commaDoubleList(java.util.Collection tokens1,
                                  java.util.Collection tokens2)
Write a list pairs of tokens, separated by commas. The first token from tokens1 is written, then a space, then the first token from tokens2, and so on until the end of both collections. A space is added before and after unless the collections are empty.

Parameters:
tokens1 - the first list of tokens whose string values are written - a null is treated as an empty collection
Returns:
this object

copyWithIndent

public void copyWithIndent(java.io.Reader r)
                    throws java.io.IOException
Insert a piece of text from a Reader into the code, placing the current indent before each line, in addition to any indenting in the lines read from the Reader.

Parameters:
r - the source of the text
java.io.IOException

copyNoIndent

public void copyNoIndent(java.io.Reader r)
                  throws java.io.IOException
Insert a piece of text from a Reader into the code, without any extra indenting, but keeping any indenting in the lines read from the Reader.

Parameters:
r - the source of the text
java.io.IOException

flush

public void flush()
Flush the Writer used by this CodeWriter.



Copyright © 2003 Ashridge Technologies Ltd. All Rights Reserved.