maspack.util
Class IndentingPrintWriter

java.lang.Object
  extended by java.io.Writer
      extended by java.io.PrintWriter
          extended by maspack.util.IndentingPrintWriter
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable, java.lang.Appendable

public class IndentingPrintWriter
extends java.io.PrintWriter


Constructor Summary
IndentingPrintWriter(java.io.File file)
          Creates a new IndentingPrintWriter, without automatic line flushing, with the specified file.
IndentingPrintWriter(java.io.File file, java.lang.String csn)
          Creates a new IndentingPrintWriter, without automatic line flushing, with the specified file and charset.
IndentingPrintWriter(java.io.OutputStream out)
          Create a new IndentingPrintWriter, without automatic line flushing, from an existing OutputStream.
IndentingPrintWriter(java.io.OutputStream out, boolean autoFlush)
          Create a new IndentingPrintWriter from an existing OutputStream.
IndentingPrintWriter(java.lang.String fileName)
          Creates a new IndentingPrintWriter, without automatic line flushing, with the specified file name.
IndentingPrintWriter(java.lang.String fileName, java.lang.String csn)
          Creates a new IndentingPrintWriter, without automatic line flushing, with the specified file name and charset.
IndentingPrintWriter(java.io.Writer out)
          Create a new IndentingPrintWriter, without automatic line flushing.
IndentingPrintWriter(java.io.Writer out, boolean autoFlush)
          Create a new IndentingPrintWriter.
 
Method Summary
 void addIndentation(int inc)
           
static void addIndentation(java.io.PrintWriter pw, int inc)
          Tests a PrintWriter to see if it is an instance of and IndentingPrintWriter, and if it is, add a specified amount of indentation.
 int getIndentation()
           
 int getTabSize()
           
 java.lang.String indentationString()
           
static int justWroteNewline(java.io.PrintWriter pw)
          Tests a PrintWriter to see if it is an instance of IndentingPrintWriter, and if so, returns 1 if a newline was the last character written and 0 otherwise.
static void main(java.lang.String[] args)
           
 void println()
           
static int printOpening(java.io.PrintWriter pw, java.lang.String str)
          Prints a string to a PrintWriter.
 void removeIndentation(int inc)
           
static void removeIndentation(java.io.PrintWriter pw, int inc)
          Tests a PrintWriter to see if it is an instance of and IndentingPrintWriter, and if it is, add a specified amount of indentation.
 void setIndentation(int col)
           
 void setTabSize(int size)
           
 void write(char[] buf, int off, int len)
           
 void write(int c)
           
 void write(java.lang.String str, int off, int len)
           
 
Methods inherited from class java.io.PrintWriter
append, append, append, checkError, close, flush, format, format, print, print, print, print, print, print, print, print, print, printf, printf, println, println, println, println, println, println, println, println, println, write, write
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IndentingPrintWriter

public IndentingPrintWriter(java.io.Writer out)
Create a new IndentingPrintWriter, without automatic line flushing.

Parameters:
out - A character-output stream

IndentingPrintWriter

public IndentingPrintWriter(java.io.Writer out,
                            boolean autoFlush)
Create a new IndentingPrintWriter.

Parameters:
out - A character-output stream
autoFlush - A boolean; if true, the println, printf, or format methods will flush the output buffer

IndentingPrintWriter

public IndentingPrintWriter(java.io.OutputStream out)
Create a new IndentingPrintWriter, without automatic line flushing, from an existing OutputStream. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will convert characters into bytes using the default character encoding.

Parameters:
out - An output stream
See Also:
OutputStreamWriter.OutputStreamWriter(java.io.OutputStream)

IndentingPrintWriter

public IndentingPrintWriter(java.io.OutputStream out,
                            boolean autoFlush)
Create a new IndentingPrintWriter from an existing OutputStream. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will convert characters into bytes using the default character encoding.

Parameters:
out - An output stream
autoFlush - A boolean; if true, the println, printf, or format methods will flush the output buffer
See Also:
OutputStreamWriter.OutputStreamWriter(java.io.OutputStream)

IndentingPrintWriter

public IndentingPrintWriter(java.lang.String fileName)
                     throws java.io.FileNotFoundException
Creates a new IndentingPrintWriter, without automatic line flushing, with the specified file name. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will encode characters using the default charset for this instance of the Java virtual machine.

Parameters:
fileName - The name of the file to use as the destination of this writer. If the file exists then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.
Throws:
java.io.FileNotFoundException - If the given string does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
java.lang.SecurityException - If a security manager is present and checkWrite(fileName) denies write access to the file
Since:
1.5

IndentingPrintWriter

public IndentingPrintWriter(java.lang.String fileName,
                            java.lang.String csn)
                     throws java.io.FileNotFoundException,
                            java.io.UnsupportedEncodingException
Creates a new IndentingPrintWriter, without automatic line flushing, with the specified file name and charset. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will encode characters using the provided charset.

Parameters:
fileName - The name of the file to use as the destination of this writer. If the file exists then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.
csn - The name of a supported charset
Throws:
java.io.FileNotFoundException - If the given string does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
java.lang.SecurityException - If a security manager is present and checkWrite(fileName) denies write access to the file
java.io.UnsupportedEncodingException - If the named charset is not supported
Since:
1.5

IndentingPrintWriter

public IndentingPrintWriter(java.io.File file)
                     throws java.io.FileNotFoundException
Creates a new IndentingPrintWriter, without automatic line flushing, with the specified file. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will encode characters using the default charset for this instance of the Java virtual machine.

Parameters:
file - The file to use as the destination of this writer. If the file exists then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.
Throws:
java.io.FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
java.lang.SecurityException - If a security manager is present and checkWrite(file.getPath()) denies write access to the file
Since:
1.5

IndentingPrintWriter

public IndentingPrintWriter(java.io.File file,
                            java.lang.String csn)
                     throws java.io.FileNotFoundException,
                            java.io.UnsupportedEncodingException
Creates a new IndentingPrintWriter, without automatic line flushing, with the specified file and charset. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will encode characters using the provided charset.

Parameters:
file - The file to use as the destination of this writer. If the file exists then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.
csn - The name of a supported charset
Throws:
java.io.FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
java.lang.SecurityException - If a security manager is present and checkWrite(file.getPath()) denies write access to the file
java.io.UnsupportedEncodingException - If the named charset is not supported
Since:
1.5
Method Detail

setIndentation

public void setIndentation(int col)

addIndentation

public void addIndentation(int inc)

removeIndentation

public void removeIndentation(int inc)

getIndentation

public int getIndentation()

indentationString

public java.lang.String indentationString()

setTabSize

public void setTabSize(int size)

getTabSize

public int getTabSize()

write

public void write(char[] buf,
                  int off,
                  int len)
Overrides:
write in class java.io.PrintWriter

write

public void write(int c)
Overrides:
write in class java.io.PrintWriter

println

public void println()
Overrides:
println in class java.io.PrintWriter

write

public void write(java.lang.String str,
                  int off,
                  int len)
Overrides:
write in class java.io.PrintWriter

main

public static void main(java.lang.String[] args)

addIndentation

public static void addIndentation(java.io.PrintWriter pw,
                                  int inc)
Tests a PrintWriter to see if it is an instance of and IndentingPrintWriter, and if it is, add a specified amount of indentation.

Parameters:
pw - PrintWriter to test
inc - indentation to add

removeIndentation

public static void removeIndentation(java.io.PrintWriter pw,
                                     int inc)
Tests a PrintWriter to see if it is an instance of and IndentingPrintWriter, and if it is, add a specified amount of indentation.

Parameters:
pw - PrintWriter to test
inc - indentation to add

justWroteNewline

public static int justWroteNewline(java.io.PrintWriter pw)
Tests a PrintWriter to see if it is an instance of IndentingPrintWriter, and if so, returns 1 if a newline was the last character written and 0 otherwise. If the PrintWriter is not an IndentingPrintWriter, then -1 is written.

Parameters:
pw - PrintWriter to test
Returns:
true if pw is an indenting print writer and just wrote a newline.

printOpening

public static int printOpening(java.io.PrintWriter pw,
                               java.lang.String str)
Prints a string to a PrintWriter. This string is then followed by a newline character if (a) the PrintWriter is not an instance of IndentingPrintWriter, or (b) the PrintWriter is an instance of IndentingPrintWriter and the last previously written character was not a newline. The method returns -1 if (a) applies, 1 if (b) applies, and 0 otherwise.

The purpose of this method is to facilitate the cuddling of openning delimiters. Suppose we want to write a block of items delimited by '[' and ']'. Then to keep everything aligned, we may want the openning string to be simply "[ " if there was nothing written before it, as in

 [ name="joe"
   age=23
 ]
 
while if something was written before it (such as field name information), then we will want the openning string to be "[ \n", as in
 personField=[
   name="joe"
   age=23
 ]