org.clapper.util.io
Class JustifyTextWriter

java.lang.Object
  extended by java.io.Writer
      extended by java.io.PrintWriter
          extended by org.clapper.util.io.JustifyTextWriter
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable, java.lang.Appendable

public class JustifyTextWriter
extends java.io.PrintWriter

The JustifyTextWriter class is a filter class. A JustifyTextWriter object wraps a Writer or OutputStream object, filtering output to the wrapped object so that output lines are right-justified, left-justified or centered within a field. (Strictly speaking, there's no need for this class to support left-justifying lines, since that's the default. It's supported here solely for completeness.)

For instance, given the following input:

 This is the first line.
 This, a longer line, is the second.
 

A JustifyTextWriter that's right-justifying lines in a 50-character field would produce:

                           This is the first line.| <-- 
               This, a longer line, is the second.| <-- 
 

(The arrows and vertical bars would obviously not be output. They're there to show where the field ends.)

A JustifyTextWriter that's centering lines in a 50-character field would produce:

              This is the first line.             | <--
        This, a longer line, is the second.       | <--
 

For an interesting effect, consider wrapping a JustifyTextWriter inside a WordWrapWriter to get word-wrapped, justified output. For instance, to wrap words on a 60-character boundary, and then center them in the same field, use code like this:

 final int WIDTH = 60;
 WordWrapWriter out = new WordWrapWriter (new JustifyWriter (JustifyWriter.CENTER, WIDTH), WIDTH);
 

Notes

  1. The class does not do any special processing of tab characters. Embedded tab characters and newline characters can have surprising (and unwanted) effects on the rendered output.
  2. Technically, this class probably ought to extend the java.io.FilterWriter class, since performs filtering of output written to another underlying Writer or OutputStream. However, I wanted JustifyTextWriter to be polymorphically compatible with java.io.PrintWriter, so it could be passed to methods expecting a PrintWriter object; there are more methods that expect a PrintWriter than there are methods that expect a FilterWriter.

Version:
$Revision: 6735 $
Author:
Copyright © 2004-2007 Brian M. Clapper
See Also:
WordWrapWriter, TextUtil.rightJustifyString(String,int), TextUtil.leftJustifyString(String,int), TextUtil.centerString(String,int), Writer, PrintWriter

Field Summary
static int DEFAULT_LINE_LENGTH
          The default line length.
 
Fields inherited from class java.io.PrintWriter
out
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
JustifyTextWriter(java.io.OutputStream output, JustifyStyle justification)
          Build an JustifyTextWriter object that will write its output to the specified OutputStream object, using the default line length of 79.
JustifyTextWriter(java.io.OutputStream output, JustifyStyle justification, int lineLength)
          Build an JustifyTextWriter object that will write its output to the specified OutputStream object, using the specified line length.
JustifyTextWriter(java.io.PrintWriter output, JustifyStyle justification)
          Build an JustifyTextWriter object that will write its output to the specified PrintWriter object, using the default line length of 79.
JustifyTextWriter(java.io.PrintWriter output, JustifyStyle justification, int lineLength)
          Build an JustifyTextWriter object that will write its output to the specified PrintWriter object, using the specified line length.
JustifyTextWriter(java.io.Writer output, JustifyStyle justification)
          Build an JustifyTextWriter object that will write its output to the specified Writer object, using the default line length of 79.
JustifyTextWriter(java.io.Writer output, JustifyStyle justification, int lineLength)
          Build an JustifyTextWriter object that will write its output to the specified Writer object, using the specified line length.
 
Method Summary
 boolean checkError()
          Flush the stream and check its error state.
 void close()
          Close the stream, flushing it first.
 void flush()
          Flush the stream.
 JustifyStyle getJustification()
          Retrieve the current justification style.
 int getLineLength()
          Retrieve the current line length.
 void print(boolean b)
          Print a boolean.
 void print(char c)
          Print a character.
 void print(char[] s)
          Print an array of characters.
 void print(double d)
          Print a double.
 void print(float f)
          Print a float.
 void print(int i)
          Print an integer.
 void print(long l)
          Print a long.
 void print(java.lang.Object x)
          Print an Object.
 void print(short s)
          Print a short.
 void print(java.lang.String s)
          Print a String.
 void println()
          End the current line.
 void println(boolean b)
          Print a boolean and finish the line.
 void println(char c)
          Print a character and finish the line.
 void println(char[] s)
          Print an array of characters.
 void println(double d)
          Print a double and finish the line.
 void println(float f)
          Print a float and finish the line.
 void println(int i)
          Print an integer.
 void println(long l)
          Print a long and finish the line.
 void println(java.lang.Object x)
          Print an Object and finish the line.
 void println(short s)
          Print a short and finish the line.
 void println(java.lang.String s)
          Print a String and finish the line.
 void setJustification(JustifyStyle style)
          Set or change the current justification style.
 void setLineLength(int newLineLength)
          Set the line length.
 void write(char[] cbuf)
          Write an array of characters.
 void write(char[] cbuf, int off, int len)
          Write a portion of an array of characters to the underlying output object.
 void write(int c)
          Write a single character.
 void write(java.lang.String s)
          Write a string.
 void write(java.lang.String s, int off, int len)
          Write a portion of a String of characters to the underlying output object.
 
Methods inherited from class java.io.PrintWriter
append, append, append, clearError, format, format, printf, printf, setError
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_LINE_LENGTH

public static final int DEFAULT_LINE_LENGTH
The default line length.

See Also:
Constant Field Values
Constructor Detail

JustifyTextWriter

public JustifyTextWriter(java.io.Writer output,
                         JustifyStyle justification)
Build an JustifyTextWriter object that will write its output to the specified Writer object, using the default line length of 79.

Parameters:
output - Where the output goes.
justification - How to justify the output
See Also:
DEFAULT_LINE_LENGTH, JustifyTextWriter(Writer,JustifyStyle,int), JustifyTextWriter(PrintWriter,JustifyStyle), JustifyTextWriter(OutputStream,JustifyStyle), JustifyStyle, Writer

JustifyTextWriter

public JustifyTextWriter(java.io.PrintWriter output,
                         JustifyStyle justification)
Build an JustifyTextWriter object that will write its output to the specified PrintWriter object, using the default line length of 79.

Parameters:
output - Where the output goes.
justification - How to justify the output
See Also:
DEFAULT_LINE_LENGTH, JustifyTextWriter(Writer,JustifyStyle), JustifyTextWriter(PrintWriter,JustifyStyle,int), JustifyTextWriter(OutputStream,JustifyStyle), JustifyStyle, Writer

JustifyTextWriter

public JustifyTextWriter(java.io.OutputStream output,
                         JustifyStyle justification)
Build an JustifyTextWriter object that will write its output to the specified OutputStream object, using the default line length of 79.

Parameters:
output - Where the output goes.
justification - How to justify the output
See Also:
DEFAULT_LINE_LENGTH, JustifyTextWriter(OutputStream,JustifyStyle,int), JustifyTextWriter(Writer,JustifyStyle), JustifyTextWriter(PrintWriter,JustifyStyle), JustifyStyle, OutputStream

JustifyTextWriter

public JustifyTextWriter(java.io.Writer output,
                         JustifyStyle justification,
                         int lineLength)
Build an JustifyTextWriter object that will write its output to the specified Writer object, using the specified line length.

Parameters:
output - Where the output goes.
justification - How to justify the output
lineLength - The desired line length.
See Also:
DEFAULT_LINE_LENGTH, JustifyTextWriter(Writer,JustifyStyle), JustifyTextWriter(PrintWriter,JustifyStyle,int), JustifyTextWriter(OutputStream,JustifyStyle,int), JustifyStyle, Writer

JustifyTextWriter

public JustifyTextWriter(java.io.PrintWriter output,
                         JustifyStyle justification,
                         int lineLength)
Build an JustifyTextWriter object that will write its output to the specified PrintWriter object, using the specified line length.

Parameters:
output - Where the output goes.
justification - How to justify the output
lineLength - The desired line length.
See Also:
DEFAULT_LINE_LENGTH, JustifyTextWriter(PrintWriter,JustifyStyle), JustifyTextWriter(Writer,JustifyStyle,int), JustifyTextWriter(OutputStream,JustifyStyle,int), JustifyStyle, Writer

JustifyTextWriter

public JustifyTextWriter(java.io.OutputStream output,
                         JustifyStyle justification,
                         int lineLength)
Build an JustifyTextWriter object that will write its output to the specified OutputStream object, using the specified line length.

Parameters:
output - Where the output goes.
justification - How to justify the output
lineLength - The desired line length.
See Also:
DEFAULT_LINE_LENGTH, JustifyTextWriter(OutputStream,JustifyStyle), JustifyTextWriter(Writer,JustifyStyle,int), JustifyTextWriter(PrintWriter,JustifyStyle,int), JustifyStyle, Writer
Method Detail

checkError

public boolean checkError()
Flush the stream and check its error state.

Overrides:
checkError in class java.io.PrintWriter

close

public void close()
Close the stream, flushing it first. Closing a previously-closed stream has no effect.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.PrintWriter

flush

public void flush()
Flush the stream. If the stream has saved any characters from the various write() methods in a buffer, write them immediately to their intended destination. Then, if that destination is another character or byte stream, flush it. Thus one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams.

Specified by:
flush in interface java.io.Flushable
Overrides:
flush in class java.io.PrintWriter

getJustification

public JustifyStyle getJustification()
Retrieve the current justification style.

Returns:
The current justification style
See Also:
setJustification(org.clapper.util.io.JustifyStyle)

setJustification

public void setJustification(JustifyStyle style)
Set or change the current justification style.

Parameters:
style - The new justification style
See Also:
setJustification(org.clapper.util.io.JustifyStyle)

getLineLength

public int getLineLength()
Retrieve the current line length.

Returns:
The line length.

setLineLength

public void setLineLength(int newLineLength)
                   throws java.lang.IndexOutOfBoundsException
Set the line length.

Parameters:
newLineLength - The new line length to use. A value of 0 disables wrapping.
Throws:
java.lang.IndexOutOfBoundsException - the value is negative

print

public void print(boolean b)
Print a boolean.

Overrides:
print in class java.io.PrintWriter
Parameters:
b - The boolean to print

print

public void print(char c)
Print a character.

Overrides:
print in class java.io.PrintWriter
Parameters:
c - The character to print

print

public void print(char[] s)
Print an array of characters.

Overrides:
print in class java.io.PrintWriter
Parameters:
s - The array of characters to print

print

public void print(double d)
Print a double.

Overrides:
print in class java.io.PrintWriter
Parameters:
d - The double floating point number to print

print

public void print(float f)
Print a float.

Overrides:
print in class java.io.PrintWriter
Parameters:
f - The floating point number to print

print

public void print(int i)
Print an integer.

Overrides:
print in class java.io.PrintWriter
Parameters:
i - The integer to print

print

public void print(long l)
Print a long.

Overrides:
print in class java.io.PrintWriter
Parameters:
l - The long to print

print

public void print(short s)
Print a short.

Parameters:
s - The short to print

print

public void print(java.lang.String s)
Print a String.

Overrides:
print in class java.io.PrintWriter
Parameters:
s - The String to print.

print

public void print(java.lang.Object x)
Print an Object.

Overrides:
print in class java.io.PrintWriter
Parameters:
x - The object to print.

println

public void println()
End the current line.

Overrides:
println in class java.io.PrintWriter

println

public void println(boolean b)
Print a boolean and finish the line.

Overrides:
println in class java.io.PrintWriter
Parameters:
b - The boolean to print

println

public void println(char c)
Print a character and finish the line.

Overrides:
println in class java.io.PrintWriter
Parameters:
c - The character to print

println

public void println(char[] s)
Print an array of characters.

Overrides:
println in class java.io.PrintWriter
Parameters:
s - The array of characters to print

println

public void println(double d)
Print a double and finish the line.

Overrides:
println in class java.io.PrintWriter
Parameters:
d - The double floating point number to print

println

public void println(float f)
Print a float and finish the line.

Overrides:
println in class java.io.PrintWriter
Parameters:
f - The floating point number to print

println

public void println(int i)
Print an integer.

Overrides:
println in class java.io.PrintWriter
Parameters:
i - The integer to print

println

public void println(long l)
Print a long and finish the line.

Overrides:
println in class java.io.PrintWriter
Parameters:
l - The long to print

println

public void println(short s)
Print a short and finish the line.

Parameters:
s - The short to print

println

public void println(java.lang.String s)
Print a String and finish the line.

Overrides:
println in class java.io.PrintWriter
Parameters:
s - The String to print.

println

public void println(java.lang.Object x)
Print an Object and finish the line.

Overrides:
println in class java.io.PrintWriter
Parameters:
x - The object to print.

write

public void write(int c)
Write a single character. This method is called by all other write(), print() and println() methods. Thus, subclasses that wish to preprocess (or intercept) the output only have to override this method.

Overrides:
write in class java.io.PrintWriter
Parameters:
c - Character to write

write

public void write(char[] cbuf,
                  int off,
                  int len)
Write a portion of an array of characters to the underlying output object. Assumes the characters represent the start of a new line. Each line is indented according to this object's defined indentation level.

Overrides:
write in class java.io.PrintWriter
Parameters:
cbuf - Array of characters
off - Offset from which to start writing characters
len - Number of characters to write

write

public void write(java.lang.String s,
                  int off,
                  int len)
Write a portion of a String of characters to the underlying output object. Assumes the characters represent the start of a new line. Each line is indented according to this object's defined indentation level.

Overrides:
write in class java.io.PrintWriter
Parameters:
s - String from which to write
off - Offset from which to start writing characters
len - Number of characters to write

write

public void write(java.lang.String s)
Write a string.

Overrides:
write in class java.io.PrintWriter
Parameters:
s - String to write

write

public void write(char[] cbuf)
Write an array of characters.

Overrides:
write in class java.io.PrintWriter
Parameters:
cbuf - Array of characters to write


Copyright © 2004-2007 Brian M. Clapper. All Rights Reserved.