sofya.viewers
Class Viewer

java.lang.Object
  extended by sofya.viewers.Viewer
Direct Known Subclasses:
ByteSourceViewer, CFViewer, MapViewer, MutationTableViewer, SourceViewer, TestHistoryViewer, TestSelectionViewer, TraceViewer

public abstract class Viewer
extends java.lang.Object

Abstract superclass of all viewers. Defines the contract for viewers and encapsulates data members and methods shared by all classes of viewer.

Since viewer outputs are expected to be human-readable character data, all viewer classes use java.io.Writer objects internally to write data to the streams. Reading of the streams should be handled accordingly.

Version:
12/02/2005
Author:
Alex Kinneer

Field Summary
protected  java.io.PrintWriter destStream
          Stream which will currently be used if no output file is specified.
protected  java.lang.String inputFile
          Name of the file being viewed.
protected static java.lang.String LINE_SEP
          The system dependent newline character sequence.
protected  java.io.File outputFile
          Name of the output file, if any.
protected static java.io.BufferedReader stdin
          Buffered character reader attached to System.in, provided for convenience.
 
Constructor Summary
protected Viewer()
          Protected default constructor to prevent unsafe instantiation of viewers.
  Viewer(java.lang.String inputFile)
          Standard constructor, creates a viewer which displays its output to the system console (System.out).
  Viewer(java.lang.String inputFile, java.io.OutputStream stream)
          Standard constructor, creates a viewer which prints its output to the specified stream.
  Viewer(java.lang.String inputFile, java.lang.String outputFile)
          Standard constructor, creates a viewer which prints its output to the specified file.
 
Method Summary
 void print()
          Prints the viewer outputs to the stream with highest precedence, ordered as follows: The specified output file, if any The specified stream, if any The System.out stream
abstract  void print(java.io.PrintWriter stream)
          Prints the viewer output to the specified stream - subclasses must provide the implementation for this method.
static void printMethodName(java.lang.String methodName, java.io.PrintWriter stream)
          Prints a method name in a normalized human-readable format.
static java.lang.String rightJust(int value, int width)
          Converts an integer to a string padded to a specified width.
 void setInputFile(java.lang.String fileName)
          Sets the file to be viewed by this viewer.
 void setOutputFile(java.lang.String fileName)
          Sets the name of the file to which viewer outputs should be written.
 void setOutputStream(java.io.OutputStream stream)
          Sets the stream to which viewer outputs should be written.
static java.lang.String sizeString(java.lang.String s, int size)
          Sets a string to a fixed size.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

stdin

protected static final java.io.BufferedReader stdin
Buffered character reader attached to System.in, provided for convenience.


LINE_SEP

protected static final java.lang.String LINE_SEP
The system dependent newline character sequence.


destStream

protected java.io.PrintWriter destStream
Stream which will currently be used if no output file is specified.


inputFile

protected java.lang.String inputFile
Name of the file being viewed.


outputFile

protected java.io.File outputFile
Name of the output file, if any.

Constructor Detail

Viewer

protected Viewer()
Protected default constructor to prevent unsafe instantiation of viewers.


Viewer

public Viewer(java.lang.String inputFile)
Standard constructor, creates a viewer which displays its output to the system console (System.out).

Parameters:
inputFile - Name of the input file whose contents are being displayed by the viewer.

Viewer

public Viewer(java.lang.String inputFile,
              java.lang.String outputFile)
       throws SameFileNameException,
              java.io.IOException
Standard constructor, creates a viewer which prints its output to the specified file.

Parameters:
inputFile - Name of the input file whose contents are being displayed by the viewer.
outputFile - Name of the file to which the viewer output should be written.
Throws:
SameFileNameException - If the specified output file and input file are the same file.
java.io.IOException - If there is an error creating the output file.

Viewer

public Viewer(java.lang.String inputFile,
              java.io.OutputStream stream)
Standard constructor, creates a viewer which prints its output to the specified stream.

Note: When using print(), an output file specified using setOutputFile(java.lang.String) takes precedence over the specified stream.

Parameters:
inputFile - Name of the input file whose contents are being displayed by the viewer.
stream - Stream to which the viewer output should be written.
Method Detail

print

public abstract void print(java.io.PrintWriter stream)
                    throws java.io.IOException
Prints the viewer output to the specified stream - subclasses must provide the implementation for this method.

The stream passed to this method takes highest precedence, it need not necessarily be any stream maintained or created internally by the viewer. Use print() to have the viewer select or create a stream based on specified settings and an order of precedence.

Parameters:
stream - Stream to which the viewer outputs are to be written.
Throws:
java.io.IOException - If there is an error writing to the stream.

setInputFile

public void setInputFile(java.lang.String fileName)
Sets the file to be viewed by this viewer.

Parameters:
fileName - Name of the input file whose contents are being displayed by the viewer.

setOutputFile

public void setOutputFile(java.lang.String fileName)
                   throws SameFileNameException,
                          java.io.IOException
Sets the name of the file to which viewer outputs should be written.

Note: When using print(), output to file takes precedence over any stream specified in a constructor or with setOutputStream(java.io.OutputStream).

Parameters:
fileName - Name of the file to which the viewer output should be written. If null, the viewer will use the last specified stream, or System.out by default.
Throws:
SameFileNameException - If the specified output file and input file are the same file.
java.io.IOException - If there is an error creating the output file.

setOutputStream

public void setOutputStream(java.io.OutputStream stream)
Sets the stream to which viewer outputs should be written.

Note: When using print(), an output file specified using setOutputFile(java.lang.String) takes precedence over the specified stream.

Parameters:
stream - Stream to which the viewer output should be written.

print

public final void print()
                 throws java.io.IOException
Prints the viewer outputs to the stream with highest precedence, ordered as follows:

Throws:
java.io.IOException - If there is an error writing to the stream.

printMethodName

public static void printMethodName(java.lang.String methodName,
                                   java.io.PrintWriter stream)
Prints a method name in a normalized human-readable format. Used to guarantee a consistent display format.

Parameters:
methodName - Method name to be formatted, as returned by a BCEL method.
stream - Stream to which the normalized form is to be printed.

sizeString

public static java.lang.String sizeString(java.lang.String s,
                                          int size)
Sets a string to a fixed size.

If the string is shorter than the requested size, it is padded with spaces (ASCII 32). If it is longer than the requested size, it is truncated.

Parameters:
s - String to be resized.
size - New length to which the string is to be sized.
Returns:
The provided string, padded or truncated to the specified size.

rightJust

public static java.lang.String rightJust(int value,
                                         int width)
Converts an integer to a string padded to a specified width.

If the length of the integer string after conversion is less than the requested length, it is padded with spaces (ASCII 32). Otherwise it is left unchanged (it will not be truncated).

Parameters:
value - Integer value to be converted to a string.
width - Required length of the string after conversion.
Returns:
The string representation of the provided integer value, padded to the specified size if necessary.