jcmdline
Class StringFormatHelper

java.lang.Object
  extended by jcmdline.StringFormatHelper

public class StringFormatHelper
extends java.lang.Object

A class used to facilitate common String formatting tasks.

Objects of this class contain no data. As such, this class is implemented as a Singleton.

Version:
jcmdline Rel. 2.0.0 $Id: StringFormatHelper.java,v 1.2 2002/12/07 14:22:06 lglawrence Exp $
Author:
Lynne Lawrence

Method Summary
protected  java.lang.String[] breakString(java.lang.String s, int maxLen)
          Breaks a String along word boundarys to the specified maximum length.
 java.lang.String formatBlockedText(java.lang.String s, int indent, int lineLen)
          Splits the specified String into lines that are indented by the specified indent and are of length less than or equal to the specified line length.
 java.lang.String formatHangingIndent(java.lang.String s, int indent, int lineLen)
          Formats a string with a hanging indent.
 java.lang.String formatLabeledList(java.lang.String[] labels, java.lang.String[] texts, java.lang.String divider, int maxIndent, int lineLen)
          Formats a "labeled list" (like a bullet or numbered list, only with labels for each item).
static StringFormatHelper getHelper()
          Gets the one and only instance of the StringFormatHelper
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getHelper

public static StringFormatHelper getHelper()
Gets the one and only instance of the StringFormatHelper

Returns:
the one and only instance of the StringFormatHelper

formatHangingIndent

public java.lang.String formatHangingIndent(java.lang.String s,
                                            int indent,
                                            int lineLen)
Formats a string with a hanging indent.

The returned String is formated such that:

Parameters:
indent - the number of spaces to indent all but the first line (may be 0)
lineLen - the maximum line length
s - the string to be formatted
Returns:
the formatted string
Throws:
java.lang.IllegalArgumentException - if lineLen is less than indent or if lineLen is less than 0.

formatBlockedText

public java.lang.String formatBlockedText(java.lang.String s,
                                          int indent,
                                          int lineLen)
Splits the specified String into lines that are indented by the specified indent and are of length less than or equal to the specified line length.

If s is null or empty, an empty String is returned.

Parameters:
s - the String to be formatted
indent - the length of the indent for the text block
lineLen - the maximum line length for the text block, including the indent
Returns:
the formatted text block
Throws:
java.lang.IllegalArgumentException - if lineLen is less than indent or if lineLen is less than 0.

breakString

protected java.lang.String[] breakString(java.lang.String s,
                                         int maxLen)
Breaks a String along word boundarys to the specified maximum length.

This method returns an array of two strings:

The first String is a line, of length less than maxLen. If this String is not the entire passed String, it will be terminated with a newline.

The second String is the remainder of the original String. If the original String had been broken on a space (as opposed to a newline that had been in the original String) all leading spaces will have been removed. If there is no remainder, null is returned as the second String and no newline will have been appended to the first String.

Parameters:
s - The String to be broken. If null, will be converted to an empty string.
maxLen - the maximum line length of the first returned string
Returns:
see the method description

formatLabeledList

public java.lang.String formatLabeledList(java.lang.String[] labels,
                                          java.lang.String[] texts,
                                          java.lang.String divider,
                                          int maxIndent,
                                          int lineLen)
Formats a "labeled list" (like a bullet or numbered list, only with labels for each item).

Example:

 System.out.println(formatLabeledList(
    new String[] { "old_file", "new_file" },
    new String[] { "the name of the file to copy - this file " +
                       "must already exist, be readable, and " +
                       "end with '.html'",
                   "the name of the file to receive the copy" },
    " = ", 20, 80));
 
produces....
 old_file = the name of the file to copy - this file must already exist, be
            readable, and end with '.html'
 new_file = the name of the file to copy to
 

Parameters:
labels - An array of labels.
texts - An array of texts to go with the labels.
divider - The divider to go between the labels and texts. This will be right-aligned against the texts.
maxIndent - Specifies the maximum indent for the text to be written out. If the combination of a label and divider is longer than maxIndent, the text will be written out in a block starting on the line following the label and divider, rather than on the same line.
lineLen - The maximum length of returned lines.
Returns:
The formatted list. It will be terminated with a newline.
Throws:
java.lang.IllegalArgumentException - if labels and text do not have the same number of elements.