grace.io
Class PrintWriter

java.lang.Object
  |
  +--java.io.Writer
        |
        +--java.io.PrintWriter
              |
              +--grace.io.PrintWriter
Direct Known Subclasses:
FormattedPrintWriter, JavaPrintWriter, LispPrintWriter

public abstract class PrintWriter
extends java.io.PrintWriter

This class is similar to the java.io.PrintWriter class except it intelligently prints objects as well as simple types. It also abstracts some of the formatting features such that derived versions of this class can print various styles of formatting the objects.

For example, the LispPrintWriter class will print basic types and objects with a lisp like appearance. This is useful for the CommandLineSessionManager because it needs to present AreaPoolServer data in a lisp like format.

PrintHandlers may be added to the system that print additional types that are not handled correctly by the default printing scheme. Simply derive from PrintHandler and add an instance of it using the addPrintHandler function.

Alternatively, you can create the derived PrintHandler class in the directory grace.io.handlers.... Then, when an object of the type named by the handler is printed, this class will be automatically loaded, instantiated, and used to print the value. For example, the JDK 1.2 java.util.Collection class uses this scheme. So, a JDK 1.2 specific class called grace.io.handlers.java.util.Collection class exists that is used only when a Collection instance is printed. This keeps the PrintWriter clases independent of JDK 1.2.

To do:


Field Summary
protected  java.text.DateFormat dateFormat
           
 
Fields inherited from class java.io.PrintWriter
out
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
PrintWriter(java.io.OutputStream stream)
           
PrintWriter(java.io.OutputStream stream, boolean autoFlush)
           
PrintWriter(java.io.Writer writer)
           
PrintWriter(java.io.Writer writer, boolean autoFlush)
           
 
Method Summary
protected static java.util.Vector add(java.lang.Object[] first, java.util.Vector second)
          Just a utility function to add an array of objects to a vector of objects.
static void addPrintHandler(java.lang.Class type, PrintHandler handler)
          Caches the handler for the given class so that it can be printed by calling the "handle" function on the given PrintHandler.
protected  boolean alreadyPrinted(java.lang.Object object)
          This indicates that the given object has been printed in the PrintStream and should not be printed again, presumably.
protected static java.lang.String arrayTypeName(java.lang.Class type, boolean shorten, boolean shortenAll)
           
protected  void autoFlush()
           
abstract  void beginList()
           
abstract  void beginObject()
           
protected  void beginString()
           
abstract  void endList()
           
abstract  void endObject()
           
protected  void endString()
           
protected static java.util.Vector fieldAccessors(java.lang.Class clazz)
          Tries to find and remember the accessors for the given class.
protected static PrintHandler getPrintHandler(java.lang.Class toFind)
          This finds a PrintHandler by first looking in the handlers subdirectory for the type and then by iterating through the list of added PrintHandlers (added using addPrintHandler) to find a PrintHandler that will handle the requested subtype.
protected static java.lang.reflect.Method getPrintMethod(java.lang.Class objectClass)
          This returns the reflected method named "printTo(PrintWriter)" on the given clazz.
 boolean getPrintObjectsOnlyOnce()
           
protected static PrintHandler getPrintToHandler(java.lang.Class clazz)
          This finds and adds a PrintMethodHandler for the given class that invokes the classes printTo method if available it has one.
 boolean prettyOutput()
           
protected  void print(java.util.Enumeration elements)
           
protected  void print(java.util.Hashtable table)
           
protected  void print(java.lang.Object[] array)
           
 void print(grace.io.PrintWriter.ResultSetRow row)
           
protected  void print(java.sql.ResultSet resultSet)
           
 void print(java.lang.String name, boolean value)
          This is the function a user should call to cause the named object to printed.
 void print(java.lang.String name, byte value)
          This is the function a user should call to cause the named object to printed.
 void print(java.lang.String name, char value)
          This is the function a user should call to cause the named object to printed.
 void print(java.lang.String name, double value)
          This is the function a user should call to cause the named object to printed.
 void print(java.lang.String name, float value)
          This is the function a user should call to cause the named object to printed.
 void print(java.lang.String name, int value)
          This is the function a user should call to cause the named object to printed.
 void print(java.lang.String name, java.lang.Object object)
          This is the function a user should call to cause the named object to printed.
protected  void print(java.util.Vector vector)
           
protected abstract  void printField(Value value)
          This meant to be overridden to provide a meaningful way to print the value of a field;
protected  void printListElementValue(java.lang.Object value, int index, int numElements)
          This method is not meant to be called by the general public.
protected  void printName(java.lang.Object name)
          This is called when the name of an object is printed.
protected  void printNullObject()
           
protected  void printNullString()
           
protected  void printQuoted(java.lang.String string)
           
protected  void printType(Value value, boolean shorten, boolean shortenAll)
          Prints the type of the given object shortened to the rightmost name if desired.
protected  void printValue(Value value)
          This prints the given object in one of many ways.
 void printWithFieldAccessors(Value parent, java.util.Vector accessors)
          This uses the given Vector to print the fields of the given object using the printField function.
protected  java.lang.String referenceNameOf(java.lang.Object object)
           
protected static java.util.Vector rememberAccessorsUsingFields(java.lang.Class clazz)
          This function tries to gain access to the fields of the object using reflection and creates a FieldAccessor for each field depending on if it can be accessed publicly or by a get method.
protected static java.util.Vector rememberAccessorsUsingGetMethods(java.lang.Class clazz)
          This reflects on the get functions of the given class, finds all of the get functions that look like follow the bean get pattern, and stores them as FieldAccessors in the classFieldAccessors.
protected  void rememberPrinted(java.lang.Object object)
          This records the fact the given object has been printed in the PrintStream and should not be printed again, presumably.
protected static java.lang.String rightmostName(java.lang.String name)
          This returns the rightmost name in a dot '.' separated list of names.
 void setDateFormat(java.text.DateFormat format)
          Sets the DateFormat used for printing all dates.
 void setPrettyOutput(boolean pretty)
           
 void setPrintObjectsOnlyOnce(boolean value)
           
protected static java.lang.String shortenedTypeName(java.lang.Class type, boolean shortenNonJDKTypes)
          This returns the abreviated name of the given class.
 
Methods inherited from class java.io.PrintWriter
checkError, close, flush, print, print, print, print, print, print, print, print, print, println, println, println, println, println, println, println, println, println, println, setError, write, write, write, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

dateFormat

protected java.text.DateFormat dateFormat
Constructor Detail

PrintWriter

public PrintWriter(java.io.OutputStream stream)

PrintWriter

public PrintWriter(java.io.OutputStream stream,
                   boolean autoFlush)

PrintWriter

public PrintWriter(java.io.Writer writer)

PrintWriter

public PrintWriter(java.io.Writer writer,
                   boolean autoFlush)
Method Detail

print

public void print(java.lang.String name,
                  java.lang.Object object)
This is the function a user should call to cause the named object to printed. Two types of users call this method. The grace.log.EventFormat class calls this to print a top level object and the code in a printTo method calls this to print a field. This is why it must keep track of whether this is the first call to it or not. If it is the first call, it must set the object as the top level object.

print

public void print(java.lang.String name,
                  int value)
This is the function a user should call to cause the named object to printed.

print

public void print(java.lang.String name,
                  byte value)
This is the function a user should call to cause the named object to printed.

print

public void print(java.lang.String name,
                  char value)
This is the function a user should call to cause the named object to printed.

print

public void print(java.lang.String name,
                  float value)
This is the function a user should call to cause the named object to printed.

print

public void print(java.lang.String name,
                  double value)
This is the function a user should call to cause the named object to printed.

print

public void print(java.lang.String name,
                  boolean value)
This is the function a user should call to cause the named object to printed.

printField

protected abstract void printField(Value value)
This meant to be overridden to provide a meaningful way to print the value of a field;
Parameters:
value - of object

printNullString

protected void printNullString()

printNullObject

protected void printNullObject()

printQuoted

protected void printQuoted(java.lang.String string)

printValue

protected void printValue(Value value)
This prints the given object in one of many ways. In order, it tries to print the object as a primitive type, print it as a type for which a handler has been added to this PrintWriter, print it as a class that has implemented the printTo function, print the result of calling get functions for each field, or print by calling the toString method.

printWithFieldAccessors

public void printWithFieldAccessors(Value parent,
                                    java.util.Vector accessors)
This uses the given Vector to print the fields of the given object using the printField function.

addPrintHandler

public static void addPrintHandler(java.lang.Class type,
                                   PrintHandler handler)
Caches the handler for the given class so that it can be printed by calling the "handle" function on the given PrintHandler.
Parameters:
type - of object to print
handler - on which the "handle" function will be called with a value of type 'type'.

getPrintHandler

protected static PrintHandler getPrintHandler(java.lang.Class toFind)
This finds a PrintHandler by first looking in the handlers subdirectory for the type and then by iterating through the list of added PrintHandlers (added using addPrintHandler) to find a PrintHandler that will handle the requested subtype. If it still can't find one, it makes a final attempt at looking for a printTo pattern in the class.

shortenedTypeName

protected static java.lang.String shortenedTypeName(java.lang.Class type,
                                                    boolean shortenNonJDKTypes)
This returns the abreviated name of the given class. That is, the leading path is stripped off. It also converts the object versions of the primitive type names into the primitive type names. If the shortenNonJDKTypes flag is set, every class is converted.
Parameters:
type - whose type name to shorten
convertNonJDKTypes - shorten every type's name rather than JDK's.

rightmostName

protected static java.lang.String rightmostName(java.lang.String name)
This returns the rightmost name in a dot '.' separated list of names.

printType

protected void printType(Value value,
                         boolean shorten,
                         boolean shortenAll)
Prints the type of the given object shortened to the rightmost name if desired.
Parameters:
object - whose type to print
shorten - shorten type names to the rightmost name
shortenAll - shorten all type names instead of just JDK names

arrayTypeName

protected static java.lang.String arrayTypeName(java.lang.Class type,
                                                boolean shorten,
                                                boolean shortenAll)

printName

protected void printName(java.lang.Object name)
This is called when the name of an object is printed. Currently, this merely prints the object as a string. This can be overrided to print the name of an object in any way.
Parameters:
name - of object to print

getPrintMethod

protected static java.lang.reflect.Method getPrintMethod(java.lang.Class objectClass)
This returns the reflected method named "printTo(PrintWriter)" on the given clazz.

getPrintToHandler

protected static PrintHandler getPrintToHandler(java.lang.Class clazz)
This finds and adds a PrintMethodHandler for the given class that invokes the classes printTo method if available it has one.
Parameters:
clazz - on which to add PrintMethodHandler
Returns:
PrintHandler that was added

fieldAccessors

protected static java.util.Vector fieldAccessors(java.lang.Class clazz)
Tries to find and remember the accessors for the given class. It first tries to find the accessors in the classFieldAccessors hashtable. Then it calls the remmeberAccessorsUsingFields. If this vector of accessors is zero length, then it calls rememberAccessorsUsingGetMethods.
Parameters:
clazz - for which to remember accessors
Returns:
Vector

rememberAccessorsUsingGetMethods

protected static java.util.Vector rememberAccessorsUsingGetMethods(java.lang.Class clazz)
This reflects on the get functions of the given class, finds all of the get functions that look like follow the bean get pattern, and stores them as FieldAccessors in the classFieldAccessors.
Returns:
Vector

rememberAccessorsUsingFields

protected static java.util.Vector rememberAccessorsUsingFields(java.lang.Class clazz)
This function tries to gain access to the fields of the object using reflection and creates a FieldAccessor for each field depending on if it can be accessed publicly or by a get method.
Parameters:
clazz - to reflect and discover fields and get functions
Returns:
Vector

setDateFormat

public void setDateFormat(java.text.DateFormat format)
Sets the DateFormat used for printing all dates.

add

protected static java.util.Vector add(java.lang.Object[] first,
                                      java.util.Vector second)
Just a utility function to add an array of objects to a vector of objects.
Parameters:
first - array to add to second vector
second - vector appended to first
Returns:
first + second

prettyOutput

public boolean prettyOutput()

setPrettyOutput

public void setPrettyOutput(boolean pretty)

getPrintObjectsOnlyOnce

public boolean getPrintObjectsOnlyOnce()

setPrintObjectsOnlyOnce

public void setPrintObjectsOnlyOnce(boolean value)

beginList

public abstract void beginList()

endList

public abstract void endList()

beginObject

public abstract void beginObject()

endObject

public abstract void endObject()

beginString

protected void beginString()

endString

protected void endString()

rememberPrinted

protected void rememberPrinted(java.lang.Object object)
This records the fact the given object has been printed in the PrintStream and should not be printed again, presumably.

referenceNameOf

protected java.lang.String referenceNameOf(java.lang.Object object)

alreadyPrinted

protected boolean alreadyPrinted(java.lang.Object object)
This indicates that the given object has been printed in the PrintStream and should not be printed again, presumably.

autoFlush

protected void autoFlush()

printListElementValue

protected void printListElementValue(java.lang.Object value,
                                     int index,
                                     int numElements)
This method is not meant to be called by the general public. It is public only because it needs to be called by various PrintHandlers in the system. It is a convience function for printing items of a collection that is indexed.

print

protected void print(java.lang.Object[] array)

print

protected void print(java.util.Vector vector)

print

protected void print(java.util.Hashtable table)

print

protected void print(java.util.Enumeration elements)

print

public void print(grace.io.PrintWriter.ResultSetRow row)

print

protected void print(java.sql.ResultSet resultSet)