org.lateralnz.common.util
Class StringUtils

java.lang.Object
  extended by org.lateralnz.common.util.StringUtils
All Implemented Interfaces:
Constants

public final class StringUtils
extends java.lang.Object
implements Constants

common string utility functions


Field Summary
 
Fields inherited from interface org.lateralnz.common.util.Constants
AMPERSAND, AND, APPLICATION_X_WWW_FORM_URLENCODED, ASTERISK, AT, BACK_SLASH, CHAR_LEFT_CHEV, CHAR_RIGHT_CHEV, COLON, COMMA, CRLF, DASH, DELETE, DOLLAR, DOT, DOT_CLASS, DOT_JAVA, EIGHT, EMPTY, EQUALS, FALSE, FILE_SEPARATOR, FIVE, FORWARD_SLASH, FOUR, FULLSTOP, HASH, HAT, INSERT, LEFT_BRACE, LEFT_BRACKET, LEFT_CHEV, MAX_LEN_APPCODE, MAX_LEN_APPGROUP, MAX_LEN_MSGCODE, MAX_LEN_MSGTEXT, MINUS_ONE, N, NEWLINE, NINE, NO, NULL_STRING, OFF, ON, ONE, ONE_DAY, ONE_HOUR, ONE_MINUTE, ONE_WEEK, PATH_SEPARATOR, PERCENT, PIPE, PLUS, QUESTION_MARK, QUOTE, REQUIRED, REQUIRES_NEW, RETURN, RIGHT_BRACE, RIGHT_BRACKET, RIGHT_CHEV, SELECT, SEMICOLON, SEVEN, SINGLE_QUOTE, SIX, SPACE, SQ_LEFT_BRACKET, SQ_RIGHT_BRACKET, SUPPORTS, TEXT_HTML, TEXT_PLAIN, TEXT_XML, THREE, TRUE, TWO, UNDERSCORE, UPDATE, UTF_8, Y, YES, ZERO
 
Constructor Summary
StringUtils()
           
 
Method Summary
static int countOccurrences(java.lang.StringBuffer sb, char c)
           
static int countOccurrences(java.lang.String s, char c)
          count the occurrences of a character in a string
static java.lang.String degzip(byte[] b)
           
static java.lang.String escape(java.lang.String s)
          escape a string
static java.util.List findRegex(java.lang.String s, java.lang.String pattern)
          return a list of regular expression groups given a string and a regex grouping pattern
static java.lang.String format(java.lang.String pattern, java.lang.Object[] arguments)
          a quick and dirty wrapper over MessageFormat.format, so we don't need to import it into JSPs as well as org.lateralnz.util.*
static java.lang.String format(java.lang.String pattern, java.lang.String argument)
          a wrapper for MessageFormat.format that takes a single argument rather than an array
static java.lang.String fromArray(java.lang.String[] s, java.lang.String delim)
           
static java.lang.String fromArray(java.lang.String[] s, java.lang.String delim, int start, int len)
          turn an array of strings into a single delimited string
static java.lang.String fromList(java.util.List l, java.lang.String delim)
          turn a list of strings into a single delimited string
static java.lang.String fromMap(java.util.Map map, char delim)
          create a string based upon the contents of a map.
static java.lang.String getFromDelimitedString(java.lang.String s, java.lang.String delim, int index)
          parse a delimited string returning the element at the specified index
static java.util.regex.Pattern getPattern(java.lang.String pattern)
           
static java.lang.String getRandomString(int len)
          create a random string of a specified length
static java.lang.String getTagValue(java.lang.String s, java.lang.String tagStart, java.lang.String tagEnd)
          given a string and the start and end of a tag return the contents of the tag.
static byte[] gzip(java.lang.String s)
           
static boolean isEmpty(java.lang.Object o)
          return true if an object is null, or it is a string and empty
static boolean isEmpty(java.lang.String s)
          return true if a string is null or empty (in other words equals(\"\"))
static boolean isEqual(java.lang.String s1, java.lang.String s2)
          check for equality between two strings, taking nulls into account
static java.lang.String isNull(java.lang.String s, java.lang.String replacement)
          if a string is null, then return the specified replacement
static boolean isNumeric(java.lang.String s)
          Check if a string is numeric (digit chars only)
static java.lang.String lpad(java.lang.String text, char pad, int length)
          left pad a string with a certain character so that it equals the specified length
static boolean matches(java.lang.String s, java.lang.String pattern)
          return true if a particular string matches the regular expression
static java.lang.String readFrom(java.io.Reader r)
          read the contents of the specified reader
static java.lang.String readFrom(java.io.Reader r, int size)
          read the contents of the specified reader up to the specified size
static java.lang.String readFromFile(java.lang.String filename)
          read the contents of a file and return as a string
static java.lang.String remove(java.lang.String s, java.lang.String chars)
          remove all occurences of a list of characters from a string.
static void replace(java.lang.StringBuffer sb, java.lang.String oldstr, java.lang.String newstr)
          replace all references of a string in a stringbuffer with the contents of a replacement string
static java.lang.String replace(java.lang.String s, java.lang.String oldstr, java.lang.String newstr)
          replace all occurrences of a string with the contents of another string
static java.lang.String replaceChars(java.lang.String s, java.lang.String chars, char newChar)
           
static java.lang.String replaceTag(java.lang.String s, java.lang.String tagStart, java.lang.String tagEnd, java.lang.String replace)
          replace all references to a 'tag' within a string.
static java.lang.String rpad(java.lang.String text, char pad, int length)
          right pad a string with a character so that the length is the same as that specified by the length param
static java.util.List splitRegex(java.lang.String s, java.lang.String pattern)
          split a string based upon a regular expression pattern For example: splitRegex("a,b,c,d,e", ",") would return a list containing a, b, c, d, and e as the elements
static java.lang.String strip(java.lang.String s, java.lang.String chars)
          strip all occurrences of characters in a string from a specified string
static java.lang.String stripLTSpaces(java.lang.String s)
          strip leading a trailing spaces from a string based upon line delimiters
static java.lang.String[] toArray(java.lang.String s, java.lang.String delim)
          turn a string into an array of strings (delimiters are not returned as part of the array)
static java.lang.String[] toArray(java.lang.String s, java.lang.String delim, boolean returnDelims)
          turn a string into a string array based upon a delimiter
static java.lang.String toDirectory(java.lang.String filename)
          makes sure that a directory filename ends with the file separator.
static java.lang.String toHex(byte b)
           
static java.lang.String toHex(byte[] b)
           
static java.lang.String toHTML(java.lang.String s)
          convert a string into an 'HTML-ready' string, handling special characters and newlines
static java.lang.String toHTML(java.lang.String s, boolean includeBR)
          convert a string into an 'HTML-ready' string, handling special characters.
static java.util.List toList(java.lang.String s, java.lang.String delim)
          turn a string into a list based upon a delimiter
static java.util.List toList(java.lang.String s, java.lang.String delim, java.util.List list)
          chop a string up into tokens based upon the delimiter and add the tokens to a list
static void toMap(java.util.Map m, java.lang.String s, java.lang.String delim)
          turn a string into a map of param=value objects based upon a delimiter and using the specified map object for the result
static java.util.Map toMap(java.lang.String s, java.lang.String delim)
          turn a string into a map of param=value objects based upon a delimiter.
static java.lang.String toMD5Digest(java.lang.String s)
          return an MD5 digest of a specified string
static java.lang.String toSHADigest(java.lang.String s)
           
static java.lang.String unencode(java.lang.String s)
          unencode a string containing html escape codes (in the form &#...;)
static java.lang.String unescape(java.lang.String s)
          take a string that contains escaped values \\n \\t \\r and returns it with the actual escape codes (\n, \t, \r)
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StringUtils

public StringUtils()
Method Detail

countOccurrences

public static final int countOccurrences(java.lang.String s,
                                         char c)
count the occurrences of a character in a string


countOccurrences

public static final int countOccurrences(java.lang.StringBuffer sb,
                                         char c)

degzip

public static final java.lang.String degzip(byte[] b)
                                     throws java.io.IOException
Throws:
java.io.IOException

escape

public static final java.lang.String escape(java.lang.String s)
escape a string


findRegex

public static final java.util.List findRegex(java.lang.String s,
                                             java.lang.String pattern)
return a list of regular expression groups given a string and a regex grouping pattern


format

public static final java.lang.String format(java.lang.String pattern,
                                            java.lang.Object[] arguments)
a quick and dirty wrapper over MessageFormat.format, so we don't need to import it into JSPs as well as org.lateralnz.util.*


format

public static final java.lang.String format(java.lang.String pattern,
                                            java.lang.String argument)
a wrapper for MessageFormat.format that takes a single argument rather than an array


fromArray

public static final java.lang.String fromArray(java.lang.String[] s,
                                               java.lang.String delim)

fromArray

public static final java.lang.String fromArray(java.lang.String[] s,
                                               java.lang.String delim,
                                               int start,
                                               int len)
turn an array of strings into a single delimited string

Parameters:
s - the array of strings to use
delim - the delimiter to use between each value
start - the start position of the array
len - the number of elements to use from the array

fromList

public static final java.lang.String fromList(java.util.List l,
                                              java.lang.String delim)
turn a list of strings into a single delimited string


fromMap

public static final java.lang.String fromMap(java.util.Map map,
                                             char delim)
create a string based upon the contents of a map. The string will be constructed as key=value[DELIM]key=value[DELIM]..., where [DELIM] is specified in the call to this method. For example: fromMap(m, ',') would return: key1=value1,key2=value2,key3=value3,etc


getFromDelimitedString

public static final java.lang.String getFromDelimitedString(java.lang.String s,
                                                            java.lang.String delim,
                                                            int index)
parse a delimited string returning the element at the specified index

Parameters:
s - the string to parse
delim - the delimiters
index - the index value to return

getPattern

public static final java.util.regex.Pattern getPattern(java.lang.String pattern)
                                                throws java.util.regex.PatternSyntaxException
Throws:
java.util.regex.PatternSyntaxException

getRandomString

public static java.lang.String getRandomString(int len)
create a random string of a specified length


getTagValue

public static final java.lang.String getTagValue(java.lang.String s,
                                                 java.lang.String tagStart,
                                                 java.lang.String tagEnd)
given a string and the start and end of a tag return the contents of the tag. For example, given the string: "hello this <!-- is a test -->" and a tag start of "<!--" and a tag end of "-->", this method should return " is a test "


gzip

public static final byte[] gzip(java.lang.String s)
                         throws java.io.IOException
Throws:
java.io.IOException

isEmpty

public static final boolean isEmpty(java.lang.String s)
return true if a string is null or empty (in other words equals(\"\"))


isEmpty

public static final boolean isEmpty(java.lang.Object o)
return true if an object is null, or it is a string and empty


isEqual

public static final boolean isEqual(java.lang.String s1,
                                    java.lang.String s2)
check for equality between two strings, taking nulls into account


isNumeric

public static final boolean isNumeric(java.lang.String s)
Check if a string is numeric (digit chars only)


isNull

public static final java.lang.String isNull(java.lang.String s,
                                            java.lang.String replacement)
if a string is null, then return the specified replacement

Parameters:
s - the string to check for null
replacement - the string to return if s is null

lpad

public static final java.lang.String lpad(java.lang.String text,
                                          char pad,
                                          int length)
left pad a string with a certain character so that it equals the specified length


matches

public static final boolean matches(java.lang.String s,
                                    java.lang.String pattern)
                             throws java.util.regex.PatternSyntaxException
return true if a particular string matches the regular expression

Parameters:
s - the string to check
pattern - the regular expression to look for
Throws:
java.util.regex.PatternSyntaxException

readFromFile

public static final java.lang.String readFromFile(java.lang.String filename)
read the contents of a file and return as a string


readFrom

public static final java.lang.String readFrom(java.io.Reader r)
read the contents of the specified reader


readFrom

public static final java.lang.String readFrom(java.io.Reader r,
                                              int size)
read the contents of the specified reader up to the specified size


remove

public static final java.lang.String remove(java.lang.String s,
                                            java.lang.String chars)
remove all occurences of a list of characters from a string. for example:
 remove("This is a test", " ");
 
should return "Thisisatest".

Parameters:
s - the string to remove characters from
chars - the list of characters to remove

replace

public static final void replace(java.lang.StringBuffer sb,
                                 java.lang.String oldstr,
                                 java.lang.String newstr)
replace all references of a string in a stringbuffer with the contents of a replacement string


replace

public static final java.lang.String replace(java.lang.String s,
                                             java.lang.String oldstr,
                                             java.lang.String newstr)
replace all occurrences of a string with the contents of another string

Parameters:
s - the string to search
oldstr - the string pattern to look for
newstr - the string pattern to replace with

replaceChars

public static final java.lang.String replaceChars(java.lang.String s,
                                                  java.lang.String chars,
                                                  char newChar)

replaceTag

public static final java.lang.String replaceTag(java.lang.String s,
                                                java.lang.String tagStart,
                                                java.lang.String tagEnd,
                                                java.lang.String replace)
replace all references to a 'tag' within a string. A tag is a section of text defined with a start string and and end string. For example:
 replaceTag("this is a test  blah blah", "", "");
 
Would be expected to return "this is a test blah blah"


rpad

public static final java.lang.String rpad(java.lang.String text,
                                          char pad,
                                          int length)
right pad a string with a character so that the length is the same as that specified by the length param


splitRegex

public static final java.util.List splitRegex(java.lang.String s,
                                              java.lang.String pattern)
                                       throws java.util.regex.PatternSyntaxException
split a string based upon a regular expression pattern For example: splitRegex("a,b,c,d,e", ",") would return a list containing a, b, c, d, and e as the elements

Throws:
java.util.regex.PatternSyntaxException

strip

public static final java.lang.String strip(java.lang.String s,
                                           java.lang.String chars)
strip all occurrences of characters in a string from a specified string

Parameters:
s - the string to search
chars - a string of characters to remove from s

stripLTSpaces

public static final java.lang.String stripLTSpaces(java.lang.String s)
strip leading a trailing spaces from a string based upon line delimiters


toArray

public static final java.lang.String[] toArray(java.lang.String s,
                                               java.lang.String delim)
turn a string into an array of strings (delimiters are not returned as part of the array)

Parameters:
s - the string to convert
delim - the delimiters to use in conversion

toArray

public static final java.lang.String[] toArray(java.lang.String s,
                                               java.lang.String delim,
                                               boolean returnDelims)
turn a string into a string array based upon a delimiter

Parameters:
s - the string to convert
delim - the delimiters to use in conversion
returnDelimiters - if true, then delimiters are included in the array

toDirectory

public static final java.lang.String toDirectory(java.lang.String filename)
makes sure that a directory filename ends with the file separator. eg: /usr/local/lib becomes /usr/local/lib/


toHex

public static final java.lang.String toHex(byte b)

toHex

public static final java.lang.String toHex(byte[] b)

toHTML

public static final java.lang.String toHTML(java.lang.String s)
convert a string into an 'HTML-ready' string, handling special characters and newlines


toHTML

public static final java.lang.String toHTML(java.lang.String s,
                                            boolean includeBR)
convert a string into an 'HTML-ready' string, handling special characters. This will convert newlines to html breaks if specified


toList

public static final java.util.List toList(java.lang.String s,
                                          java.lang.String delim,
                                          java.util.List list)
chop a string up into tokens based upon the delimiter and add the tokens to a list


toList

public static final java.util.List toList(java.lang.String s,
                                          java.lang.String delim)
turn a string into a list based upon a delimiter


toMap

public static final java.util.Map toMap(java.lang.String s,
                                        java.lang.String delim)
turn a string into a map of param=value objects based upon a delimiter. The string should be something like: "a=100,b=hello,c=a203" where the delimiter is obviously ","


toMap

public static final void toMap(java.util.Map m,
                               java.lang.String s,
                               java.lang.String delim)
turn a string into a map of param=value objects based upon a delimiter and using the specified map object for the result


unescape

public static final java.lang.String unescape(java.lang.String s)
take a string that contains escaped values \\n \\t \\r and returns it with the actual escape codes (\n, \t, \r)


unencode

public static final java.lang.String unencode(java.lang.String s)
                                       throws java.lang.NumberFormatException
unencode a string containing html escape codes (in the form &#...;)

Throws:
java.lang.NumberFormatException

toMD5Digest

public static final java.lang.String toMD5Digest(java.lang.String s)
                                          throws java.lang.Exception
return an MD5 digest of a specified string

Throws:
java.lang.Exception

toSHADigest

public static final java.lang.String toSHADigest(java.lang.String s)
                                          throws java.lang.Exception
Throws:
java.lang.Exception