org.jaffa.util
Class StringHelper

java.lang.Object
  extended byorg.jaffa.util.StringHelper

public class StringHelper
extends Object

Utility Class for Common String Manipulation routines.

Version:
1.0
Author:
PaulE

Nested Class Summary
static class StringHelper.Line
          This class contains the contents of a line and the EOL character.
 
Constructor Summary
StringHelper()
           
 
Method Summary
static String addCommentWithStamp(String originalComment, String additionalComment, boolean lifo, String userId)
          This is a helper for combining old and additional comments.
static String addHTMLLineBreak(String s, int lineLimit)
          This method will add HTML line breaks in the input String at the specified limits.
static String convertFromHTML(String s)
          Convert a string that has XML (and HTML) entities in it, into a regular string It will convert the following...
static String convertToHTML(String s)
          Convert a regular string into an XML (and HTML) based string It will convert the following
static ArrayList convertToList(String commaList)
          Converts a comma delimmitered string into an array of strings
static String findEol(String input)
          Returns the EOL character for the input String, if any.
static String formatDescription(Object field, String layout, String domainClassWithPackage, String domainField, boolean toHtml)
          This method is invoked in cases where 'description' field is appended to a 'code' field.
static String formatDescription(Object field, String layout, String domainClassWithPackage, String domainField, boolean toHtml, String beginMarker, String endMarker)
          This method is invoked in cases where 'description' field is appended to a 'code' field.
static String formatDescription(Object field, String layout, String domainClassWithPackage, String domainField, boolean toHtml, String beginMarker, String endMarker, int limit, String truncateIndicator)
          This method is invoked in cases where 'description' field is appended to a 'code' field.
static String getDefaultDescriptionBeginMarker()
          Returns the default String used in the formatDescription() methods placed at the beginning.
static String getDefaultDescriptionEndMarker()
          Returns the default String used in the formatDescription() methods placed at the end.
static String getLower(String input)
          A convenience method to translate the input String to LowerCase
static String getLower1(String input)
          A convenience method to translate the input String's 1st character to LowerCase, the rest of the string is left the same
static String getSpace(String input)
          A convenience method to translate the input String to words separated by spaces For eg: "abcDef" would be translated to "abc Def"
static String getStatic(String input)
          A convenience method to translate the input String to all UpperCase with underscores separating the words For eg: "abcDef" would be translated to "ABC_DEF"
static String getString(Reader reader)
          This returns the contents of a Reader as a String.
static StringBuffer getStringBuffer(Reader reader)
          This returns the contents of a Reader in a StringBuffer.
static String getUpper(String input)
          A convenience method to translate the input String to UpperCase
static String getUpper1(String input)
          A convenience method to translate the input String's 1st character to UpperCase, the rest of the string is left the same
static String linePad(String text, int indent)
          Pad each new line of the supplied string with the specified number of spaces
static String linePad(String text, int indent, String indentWith)
          Pad each new line of the supplied string with the specified number of 'indent Strings'
static String linePad(String text, int indent, String indentWith, boolean supressFirst)
          Pad each new line of the supplied string with the specified number of the indent string, but allows the suppression of the first line from indentation
static void main(String[] args)
          Test Routine
static String pad(int number)
          Pad a number to take up at least 2 characters
static String pad(int number, int length)
          Pad a number to take up a specified length
static String pad(int number, int len, String padVal)
          Pad a number to take up a specified length
static String[] parseString(String s)
          Tokenize the Input String with the default tokenizer (whitespace)
static String[] parseString(String s, String delim)
          Tokenize the Input String with the given delim
static StringHelper.Line readLine(PushbackReader reader)
          Reads a line from the input reader.
static String replace(String source, String find, String replace)
          Basic find and replace on a string, it will replace ALL occurences of the string
static String replicate(String text, int count)
          Produce a string by replicating the specific string the specified number of times
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StringHelper

public StringHelper()
Method Detail

pad

public static String pad(int number)
Pad a number to take up at least 2 characters

Parameters:
number - the number to pad
Returns:
string representation of number padded to 2 characters

pad

public static String pad(int number,
                         int length)
Pad a number to take up a specified length

Parameters:
number - the number to pad
length - length of resulting string
Returns:
string representation of number padded to specified length

pad

public static String pad(int number,
                         int len,
                         String padVal)
Pad a number to take up a specified length

Parameters:
number - the number to pad
len - length of resulting string
padVal - Character to be used for Padding
Returns:
string representation of number padded to specified length

parseString

public static String[] parseString(String s)
Tokenize the Input String with the default tokenizer (whitespace)

Parameters:
s - String to tokenize
Returns:
string[] representation tokenized String

parseString

public static String[] parseString(String s,
                                   String delim)
Tokenize the Input String with the given delim

Parameters:
s - String to tokenize
delim - Deliminator to use
Returns:
string[] representation tokenized String

linePad

public static String linePad(String text,
                             int indent)
Pad each new line of the supplied string with the specified number of spaces

Parameters:
text - String to pad
indent - number of spaces to indent by
Returns:
the indented string

linePad

public static String linePad(String text,
                             int indent,
                             String indentWith)
Pad each new line of the supplied string with the specified number of 'indent Strings'

Parameters:
text - String to pad
indent - number of spaces to indent by
indentWith - String to use for indenting
Returns:
the indented string

linePad

public static String linePad(String text,
                             int indent,
                             String indentWith,
                             boolean supressFirst)
Pad each new line of the supplied string with the specified number of the indent string, but allows the suppression of the first line from indentation

Parameters:
text - String to pad
indent - number of spaces to indent by
indentWith - String to use for indenting
supressFirst - true, if the first line should be skipped when indenting
Returns:
the indented string

replicate

public static String replicate(String text,
                               int count)
Produce a string by replicating the specific string the specified number of times

Parameters:
text - String to replicate
count - number of times to replicate string
Returns:
final replicated string

convertToList

public static ArrayList convertToList(String commaList)
Converts a comma delimmitered string into an array of strings

Parameters:
commaList - Source string
Returns:
List of strings

convertFromHTML

public static String convertFromHTML(String s)
Convert a string that has XML (and HTML) entities in it, into a regular string It will convert the following... & to & < to < > to > ' to ' " to "

Parameters:
s - String to convert
Returns:
converted string

convertToHTML

public static String convertToHTML(String s)
Convert a regular string into an XML (and HTML) based string It will convert the following... & to & < to < > to > ' to ' " to "

Parameters:
s - String to convert
Returns:
converted string

addHTMLLineBreak

public static String addHTMLLineBreak(String s,
                                      int lineLimit)
This method will add HTML line breaks in the input String at the specified limits. This is useful for wrapping long lines of text.

Parameters:
s - String to modify
lineLimit - The interval at which the line breaks will be added.

replace

public static String replace(String source,
                             String find,
                             String replace)
Basic find and replace on a string, it will replace ALL occurences of the string

Parameters:
source - original string
find - string to search for
replace - string to replace found strings with
Returns:
resultant string from the replace

getLower1

public static String getLower1(String input)
A convenience method to translate the input String's 1st character to LowerCase, the rest of the string is left the same

Parameters:
input - the string to process
Returns:
the modified string

getUpper1

public static String getUpper1(String input)
A convenience method to translate the input String's 1st character to UpperCase, the rest of the string is left the same

Parameters:
input - the string to process
Returns:
the modified string

getLower

public static String getLower(String input)
A convenience method to translate the input String to LowerCase

Parameters:
input - the string to process
Returns:
the modified string

getUpper

public static String getUpper(String input)
A convenience method to translate the input String to UpperCase

Parameters:
input - the string to process
Returns:
the modified string

getStatic

public static String getStatic(String input)
A convenience method to translate the input String to all UpperCase with underscores separating the words For eg: "abcDef" would be translated to "ABC_DEF"

Parameters:
input - the string to process
Returns:
the modified string

getSpace

public static String getSpace(String input)
A convenience method to translate the input String to words separated by spaces For eg: "abcDef" would be translated to "abc Def"

Parameters:
input - the string to process
Returns:
the modified string

getStringBuffer

public static StringBuffer getStringBuffer(Reader reader)
                                    throws IOException
This returns the contents of a Reader in a StringBuffer.

Parameters:
reader - the input source.
Returns:
a StringBuffer with the contents of the source. A null is returned if the input is null.
Throws:
IOException - if any error occurs in reading the contents of the input.

getString

public static String getString(Reader reader)
                        throws IOException
This returns the contents of a Reader as a String.

Parameters:
reader - the input source.
Returns:
a String with the contents of the source. A null is returned if the input is null.
Throws:
IOException - if any error occurs in reading the contents of the input.

formatDescription

public static String formatDescription(Object field,
                                       String layout,
                                       String domainClassWithPackage,
                                       String domainField,
                                       boolean toHtml)
This method is invoked in cases where 'description' field is appended to a 'code' field. This method formats the input dexcriptionField based on the passed layout. The layout can be passed directly or can be determined from the parameters domainClassWithPackage and domainField, through the appropriate FieldMetaData object. It then truncates the formatted String to the input limit. If the String is truncated, then the truncateIndicator will be appended. Finally the String will be packaged between the beginMarker and endMarker. If the toHtml flag is true, then the result will made HTML safe. This method will use the default values beginMarker=' (', endMarker=')', limit=25, truncateIndicator='...'

Parameters:
field - The description of the field.
layout - The layout, if any to be used for formatting
domainClassWithPackage - The domainClass to determine the FieldMetaData object, to get a handle on the layout.
domainField - The domainField to determine the FieldMetaData object, to get a handle on the layout.
toHtml - if true, then the output will be converted to HTML.
Returns:
the formatted string packed between the markers. An empty String will be returned, in case the input field is null.

formatDescription

public static String formatDescription(Object field,
                                       String layout,
                                       String domainClassWithPackage,
                                       String domainField,
                                       boolean toHtml,
                                       String beginMarker,
                                       String endMarker)
This method is invoked in cases where 'description' field is appended to a 'code' field. This method formats the input dexcriptionField based on the passed layout. The layout can be passed directly or can be determined from the parameters domainClassWithPackage and domainField, through the appropriate FieldMetaData object. It then truncates the formatted String to the input limit. If the String is truncated, then the truncateIndicator will be appended. Finally the String will be packaged between the beginMarker and endMarker. If the toHtml flag is true, then the result will made HTML safe. This method will use the default values limit=25, truncateIndicator='...'

Parameters:
field - The description of the field.
layout - The layout, if any to be used for formatting
domainClassWithPackage - The domainClass to determine the FieldMetaData object, to get a handle on the layout.
domainField - The domainField to determine the FieldMetaData object, to get a handle on the layout.
toHtml - if true, then the output will be converted to HTML.
beginMarker - The marker at the start of the output. Default is ' ('
endMarker - The marker at the end of the output. Default is ')'
Returns:
the formatted string packed between the markers. An empty String will be returned, in case the input field is null.

formatDescription

public static String formatDescription(Object field,
                                       String layout,
                                       String domainClassWithPackage,
                                       String domainField,
                                       boolean toHtml,
                                       String beginMarker,
                                       String endMarker,
                                       int limit,
                                       String truncateIndicator)
This method is invoked in cases where 'description' field is appended to a 'code' field. This method formats the input dexcriptionField based on the passed layout. The layout can be passed directly or can be determined from the parameters domainClassWithPackage and domainField, through the appropriate FieldMetaData object. It then truncates the formatted String to the input limit. If the String is truncated, then the truncateIndicator will be appended. No truncation will be performed if the limit <= 0 Finally the String will be packaged between the beginMarker and endMarker. If the toHtml flag is true, then the result will made HTML safe.

Parameters:
field - The description of the field.
layout - The layout, if any to be used for formatting
domainClassWithPackage - The domainClass to determine the FieldMetaData object, to get a handle on the layout.
domainField - The domainField to determine the FieldMetaData object, to get a handle on the layout.
toHtml - if true, then the output will be converted to HTML.
beginMarker - The marker at the start of the output. Default is ' ('
endMarker - The marker at the end of the output. Default is ')'
limit - The limit for the formatted decription. Default is 25.
truncateIndicator - The string to append tot he formatted description, if exceeds the limit and is truncated.
Returns:
the formatted string packed between the markers. An empty String will be returned, in case the input field is null.

getDefaultDescriptionBeginMarker

public static String getDefaultDescriptionBeginMarker()
Returns the default String used in the formatDescription() methods placed at the beginning.

Returns:
the default String used in the formatDescription() methods placed at the beginning.

getDefaultDescriptionEndMarker

public static String getDefaultDescriptionEndMarker()
Returns the default String used in the formatDescription() methods placed at the end.

Returns:
the default String used in the formatDescription() methods placed at the end.

addCommentWithStamp

public static String addCommentWithStamp(String originalComment,
                                         String additionalComment,
                                         boolean lifo,
                                         String userId)
This is a helper for combining old and additional comments. A stamp containing the userId and current time will be inserted before the additional comment. The additional comment will be inserted before the old comment if 'lifo' is true, otherwise it'll be appended. If the input userId is null, then the userId will be obtained from the SecurityManager.

Parameters:
originalComment - The original comment.
additionalComment - The additional comment.
lifo - This determines if the additional comment is inserted before or appended after the old comment.
userId - The userId.
Returns:
The combination of the old comment and the additional comment.

findEol

public static String findEol(String input)
Returns the EOL character for the input String, if any. The EOL character can be '\r', '\n' or '\r\n'.

Returns:
the EOL character for the input String. A null string will be returned in case no EOL charcater is found.

readLine

public static StringHelper.Line readLine(PushbackReader reader)
                                  throws IOException
Reads a line from the input reader. It'll return an instance of StringHelper.Line object, which has the contents and EOL of the line. The EOL character can be '\r', '\n' or '\r\n'. This method is used instead of the readLine() method of the BufferedReader, since there is no other way of finding out if the last line in a file ends with an EOL. This also has the added benefit of knowing the exact EOL character. A null will be returned if the EOF is reached.

Returns:
a StringHelper.Line object containing the line contents and the EOL character. A null is returned if the end of file is reached.
Throws:
IOException - if any IO error occurs.

main

public static void main(String[] args)
Test Routine

Parameters:
args - no args required


Copyright © 2002-2004 JAFFA Project.