jj2000.j2k.util
Class MsgPrinter

java.lang.Object
  |
  +--jj2000.j2k.util.MsgPrinter

public class MsgPrinter
extends java.lang.Object

This utility class formats messages to the specified line width, by inserting line-breaks between words, and printing the resulting lines.


Field Summary
private static int IS_EOS
          Signals that the end-of-string was reached
private static int IS_NEWLINE
          Signals that a newline was found
 int lw
          The line width to use
 
Constructor Summary
MsgPrinter(int linewidth)
          Creates a new message printer with the specified line width and with the default locale.
 
Method Summary
 int getLineWidth()
          Returns the line width that is used for formatting.
private  int nextLineEnd(java.lang.String str, int from)
          Returns the index of the last character of the next word, plus 1, or IS_NEWLINE if a newline character is encountered before the next word, or IS_EOS if the end of the string is ecnounterd before the next word.
private  int nextWord(java.lang.String str, int from)
          Returns the position of the first character in the next word, starting from 'from', if a newline is encountered first then the index of the newline character plus 1 is returned.
 void print(java.io.PrintWriter out, int flind, int ind, java.lang.String msg)
          Formats the message to print in the current line width, by breaking the message into lines between words.
 void setLineWidth(int linewidth)
          Sets the line width to the specified value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

lw

public int lw
The line width to use


IS_NEWLINE

private static final int IS_NEWLINE
Signals that a newline was found

See Also:
Constant Field Values

IS_EOS

private static final int IS_EOS
Signals that the end-of-string was reached

See Also:
Constant Field Values
Constructor Detail

MsgPrinter

public MsgPrinter(int linewidth)
Creates a new message printer with the specified line width and with the default locale.

Parameters:
linewidth - The line width for which to format (in characters)
Method Detail

getLineWidth

public int getLineWidth()
Returns the line width that is used for formatting.

Returns:
The line width used for formatting

setLineWidth

public void setLineWidth(int linewidth)
Sets the line width to the specified value. This new value will be used in subsequent calls to the print() message.

Parameters:
linewidth - The new line width to use (in cahracters)

print

public void print(java.io.PrintWriter out,
                  int flind,
                  int ind,
                  java.lang.String msg)
Formats the message to print in the current line width, by breaking the message into lines between words. The number of spaces to indent the first line is specified by 'flind' and the number of spaces to indent each of the following lines is specified by 'ind'. Newlines in 'msg' are respected. A newline is always printed at the end.

Parameters:
out - Where to print the message.
flind - The indentation for the first line.
ind - The indentation for the other lines.
msg - The message to format and print.

nextLineEnd

private int nextLineEnd(java.lang.String str,
                        int from)
Returns the index of the last character of the next word, plus 1, or IS_NEWLINE if a newline character is encountered before the next word, or IS_EOS if the end of the string is ecnounterd before the next word. The method first skips all whitespace characters at or after 'from', except newlines. If a newline is found IS_NEWLINE is returned. Then it skips all non-whitespace characters and returns the position of the last non-whitespace character, plus 1. The returned index may be greater than the last valid index in the tsring, but it is always suitable to be used in the String.substring() method.

Non-whitespace characters are defined as in the Character.isWhitespace method (that method is used).

Parameters:
str - The string to parse
from - The index of the first position to search from
Returns:
The index of the last character in the next word, plus 1, IS_NEWLINE, or IS_EOS if there are no more words.

nextWord

private int nextWord(java.lang.String str,
                     int from)
Returns the position of the first character in the next word, starting from 'from', if a newline is encountered first then the index of the newline character plus 1 is returned. If the end of the string is encountered then IS_EOS is returned. Words are defined as any concatenation of 1 or more characters which are not whitespace. Whitespace characters are those for which Character.isWhitespace() returns true (that method is used).

Non-whitespace characters are defined as in the Character.isWhitespace method (that method is used).

Parameters:
str - The string to parse
from - The index where to start parsing
Returns:
The index of the first character of the next word, or the index of the newline plus 1, or IS_EOS.