Class CompilerIO

java.lang.Object
  extended by CompilerIO

public class CompilerIO
extends java.lang.Object

This class handles all IO for the compiler application. Several streams are managed by this class.

$Id$


Field Summary
private  java.io.PrintWriter buf
          the temporary buffer
private  java.io.PrintWriter dst
          the current output Writer, either the obj file or the buffer.
private  boolean echoing
           
private  java.lang.String echoPrefix
           
private  java.io.PrintWriter obj
          the output file
private  java.io.BufferedReader src
          the input file
private  java.io.StringWriter sw
          the underlying StringWriter used in implementing the buffer
 
Constructor Summary
CompilerIO(java.lang.String srcName, java.lang.String objX)
          Create a new CompilerIO object, using the file name given for the input file, and creating the output file name based on the input file name.
CompilerIO(java.lang.String srcName, java.lang.String objName, java.lang.String objX)
          Create a new CompilerIO object, using the file names given for the input file and the output file.
 
Method Summary
 void closeAll()
          Close the input, output, and temporary streams.
 void closeBuffer()
          Close the temporary Writer that we've been using to buffer the output and switch back to the output file as the target of all the emit methods.
 void emit(java.lang.String s)
          Write a string to the currently active output Writer.
 void emitBuffer()
          Write the contents of the temporary buffer to the output file.
 void emitFile(java.lang.String fileName)
          Copy an entire file to the currently active output Writer.
 void emitWithPrefix(java.lang.String s)
          Write the prefix and then the given string to the currently active output Writer.
 boolean getEchoing()
          Get the current state of the echoing property.
 java.lang.String getEchoPrefix()
          Get the echo prefix string.
static void main(java.lang.String[] arg)
          Run a simple test of this CompilerIO class.
 void openBuffer()
          Create a new temporary Writer for buffering and set it to be the target of all the emit methods.
 java.lang.String readSrcLine()
          Read one line of input from the input file.
 void setEchoing(boolean e)
          Set the echoing property.
 void setEchoPrefix(java.lang.String p)
          Set the echo prefix that is written out before each echoed line or other String written with emitWithPrefix.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

src

private java.io.BufferedReader src
the input file


dst

private java.io.PrintWriter dst
the current output Writer, either the obj file or the buffer.


obj

private java.io.PrintWriter obj
the output file


buf

private java.io.PrintWriter buf
the temporary buffer


sw

private java.io.StringWriter sw
the underlying StringWriter used in implementing the buffer


echoing

private boolean echoing

echoPrefix

private java.lang.String echoPrefix
Constructor Detail

CompilerIO

public CompilerIO(java.lang.String srcName,
                  java.lang.String objX)
           throws java.io.IOException
Create a new CompilerIO object, using the file name given for the input file, and creating the output file name based on the input file name. To create the output file name, the extension is removed from the input file name, and replaced with the given extension.

Parameters:
srcName - the name of the input file
objX - the extension to use for the output file. Does not include the dot that separates the name from the extension.
Throws:
java.io.IOException - If any input or output exception occurs while opening the files.

CompilerIO

public CompilerIO(java.lang.String srcName,
                  java.lang.String objName,
                  java.lang.String objX)
           throws java.io.IOException
Create a new CompilerIO object, using the file names given for the input file and the output file. If the extension is not null, then the given extension is added to the output file name after a period.

Parameters:
srcName - the name of the input file
objName - the name of the output file
objX - the extension to use for the output file. Does not include the dot that separates the name from the extension.
Throws:
java.io.IOException - If any input or output exception occurs while opening the files.
Method Detail

readSrcLine

public java.lang.String readSrcLine()
                             throws java.io.IOException
Read one line of input from the input file.

Returns:
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached.
Throws:
java.io.IOException - If an I/O error occurs while reading

emit

public void emit(java.lang.String s)
Write a string to the currently active output Writer.

Parameters:
s - The String to write out.

emitBuffer

public void emitBuffer()
Write the contents of the temporary buffer to the output file. This can only be called when the buffer is closed. This method will return silently without doing anything if the buffer is the currently active output Writer.


emitWithPrefix

public void emitWithPrefix(java.lang.String s)
Write the prefix and then the given string to the currently active output Writer.

Parameters:
s - The String to write out.

emitFile

public void emitFile(java.lang.String fileName)
              throws java.io.IOException
Copy an entire file to the currently active output Writer. This is a direct copy and nothing is done to the file on the way out, ie, no prefix is applied.

Parameters:
fileName - the name of the file to copy
Throws:
java.io.IOException - If an IO error occurs

setEchoing

public void setEchoing(boolean e)
Set the echoing property. If true, every line read from src stream is copied to the output stream.

Parameters:
e - true or false.

getEchoing

public boolean getEchoing()
Get the current state of the echoing property.

Returns:
the current echoing state.

setEchoPrefix

public void setEchoPrefix(java.lang.String p)
Set the echo prefix that is written out before each echoed line or other String written with emitWithPrefix.

Parameters:
p - the String prefix

getEchoPrefix

public java.lang.String getEchoPrefix()
Get the echo prefix string.

Returns:
the current value of the echo prefix.

openBuffer

public void openBuffer()
Create a new temporary Writer for buffering and set it to be the target of all the emit methods.


closeBuffer

public void closeBuffer()
Close the temporary Writer that we've been using to buffer the output and switch back to the output file as the target of all the emit methods. This method should be called before using emitBuffer to copy the contents of the temporary buffer to the output file.


closeAll

public void closeAll()
              throws java.io.IOException
Close the input, output, and temporary streams.

Throws:
java.io.IOException - If an I/O error occurs while closing

main

public static void main(java.lang.String[] arg)
                 throws java.io.IOException
Run a simple test of this CompilerIO class.

Parameters:
arg -
pathname to read. If not specified, then the program tries to read "CompilerIO.java". The CompilerIO constructor builds the output name from the input name.
Throws:
java.io.IOException - If an IO error occurs while processing