com.compoze.collab.util
Class StringUtility

java.lang.Object
  extended by com.compoze.collab.util.StringUtility

public class StringUtility
extends Object

This class contains useful methods for working with strings.


Method Summary
static String bytesToHex(byte[] bytes)
          Gets a byte array as a string in hexadecimal with the characters returned in uppercase.
static String bytesToHex(byte[] bytes, boolean bUppercase)
          Gets a byte array as a string in hexadecimal with the characters returned in uppercase.
static String bytesToHex(byte[] bytes, int iOffset, int iLength, boolean bUppercase, boolean bFormatted)
          Gets a byte array as a string in hexadecimal.
static String byteToHex(byte data)
          Convenience method to convert a byte to a hex string.
static String byteToHex(byte data, boolean bUpperCase)
          Convenience method to convert a byte to a hex string.
static int compareVersions(int[] version1, int[] version2)
          Compare two version arrays as Comparator.compare(java.lang.Object, java.lang.Object).
static int[] crackVersionString(String sVersion)
          Crack a version number String like "3.12-4" into an array of ints {3,12,4}.
static boolean equalsWithNull(String s1, String s2)
          Determines if two strings are equal (either string may be null).
static String escapeAndProcessLinks(String sUnprocessed)
          HTML escapes a string and process its links for use in a JSP page.
static String escapeAndProcessLinks(String sUnprocessed, boolean bNewWindow)
          HTML escapes a string and process its links for use in a JSP page.
static String escapeQuotes(String sUnescapedString)
          This replaces the special quote characters with escaped value
static String[] getArrayFromSeparatedList(String sStr, String sDelim, boolean bTrim)
          Gets a String array from a separated list of Strings.
static String getCommaSeparatedList(Collection strs, boolean bSpaceAfterComma)
          Gets a comma separated list from a collection of strings or objects (for an object we call the toString method).
static void getCommaSeparatedList(Collection strs, boolean bSpaceAfterComma, StringBuffer buf)
          Gets a comma separated list from a collection of strings or objects (for an object we call the toString method).
static void getCommaSeparatedList(long[] longs, boolean bSpaceAfterComma, StringBuffer buf)
          Gets a comma separated list from an array of longs.
static String getCommaSeparatedList(Object[] strs, boolean bSpaceAfterComma)
          Gets a comma separated list from a collection of strings or objects (for an object we call the toString method).
static void getCommaSeparatedList(Object[] strs, boolean bSpaceAfterComma, StringBuffer buf)
          Gets a comma separated list from an array of strings or objects (for an object we call the toString method).
static byte[] getDigest(String sStr)
          Get an SHA digest of the specified string using the UTF-8 character encoding.
static byte[] getDigest(String sStr, String sAlgorithm)
          Get a digest of the specified string using the UTF-8 character encoding.
static byte[] getDigest(String sStr, String sAlgorithm, String sEncoding)
          Get a digest of the specified string.
static String getExceptionStackTrace(Throwable t)
          Gets the stack trace of a Throwable as a String.
static List getListFromSeparatedList(String sStr, String sDelim, boolean bTrim)
          Gets a list of Strings from a separated list of Strings.
static void getSeparatedList(Collection strs, String sSeparator, StringBuffer buf)
          Gets a separated list from a collection of strings or objects (for an object we call the toString method).
static void getSeparatedList(long[] longs, String sSeparator, StringBuffer buf)
          Gets a separated list from an array of longs.
static void getSeparatedList(Object[] strs, String sSeparator, StringBuffer buf)
          Gets a separated list from an array of strings or objects (for an object we call the toString method).
static byte hexToByte(String s)
          Convenience method to convert a hex string to a byte.
static byte[] hexToBytes(String sHex)
          Convenience method to convert a hex string to a byte array.
static String htmlEscape(String sUnescapedString)
          HTML escape a string.
static String htmlEscape(String sUnescapedString, String sEncoding)
          HTML escape a string.
static String htmlEscapeUnformatted(String sUnescapedString)
          Parially HMTL escapes a string for use in a JSP page.
static String htmlEscapeUnformatted(String sUnescapedString, String[] sTags)
          Partially HTML escapes a string for use in a JSP page.
static String indent(String s, int iColumns)
          Indents a String by the specified number of columns.
static boolean isBlank(String sStr)
          Determine if a string is null or empty.
static void main(String[] args)
          Main.
static String prettyTruncate(String sStr, int iLength)
          Truncate a string after a certain number of characters and append the specified string to it.
static String prettyTruncate(String sStr, String sPostfix, int iLength)
          Truncate a string after a certain number of characters and append the specified string to it.
static void printColumns(String[][] strs, int iPadding, PrintStream out)
          Prints strings in columns.
static String processLinks(String sUnprocessed)
          Processes a string, replacing any apparent URLs with the HTML code needed to generate an HREF link to that URL.
static String processLinks(String sUnprocessed, boolean bNewWindow)
          Processes a string, replacing any apparent URLs with the HTML code needed to generate an HREF link to that URL.
static String safeHtmlEscape(Object obj)
          HTML escapes a object for use in a JSP page.
static String safeHtmlEscape(String sUnescapedString)
          HTML escapes a string for use in a JSP page.
static String safeHtmlEscape(String sUnescapedString, String sEncoding)
          HTML escapes a string for use in a JSP page.
static String substituteAll(String sSource, String sToBeReplaced, String sReplacement)
          Substitutes all occurrences of a string.
static char toHexChar(int i)
          Convenience method to convert an int to a hex char.
static String trimChar(String str, char c)
          Trim the given character from either end of a String.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

safeHtmlEscape

public static String safeHtmlEscape(Object obj)
HTML escapes a object for use in a JSP page. The object to string representation is used. This uses the default Character Encoding of the Virtual Machine

Parameters
obj - object to escape (may not be wnull)
Returns
the escaped string

safeHtmlEscape

public static String safeHtmlEscape(String sUnescapedString)
HTML escapes a string for use in a JSP page. This uses the default Character Encoding of the Virtual Machine

Parameters
sUnescapedString - string to escape
Returns
the escaped string

safeHtmlEscape

public static String safeHtmlEscape(String sUnescapedString,
                                    String sEncoding)
                             throws UnsupportedEncodingException
HTML escapes a string for use in a JSP page. A Character Encoding is used to determine how the characters are escaped. This should be used if the desired encoding is different than the default.

Parameters
sUnescapedString - string to escape
sEncoding - character encoding
Returns
the escaped string
Throws
UnsupportedEncodingException

htmlEscapeUnformatted

public static String htmlEscapeUnformatted(String sUnescapedString)
Parially HMTL escapes a string for use in a JSP page. This method escapes all but the following HTML formatting tags: <br><i><b><u><font> <ol><ul><li><pre><code>.

Parameters
sUnescapedString - the unescaped string

htmlEscapeUnformatted

public static String htmlEscapeUnformatted(String sUnescapedString,
                                           String[] sTags)
Partially HTML escapes a string for use in a JSP page.

Parameters
sUnescapedString - the unescaped string
sTags - the tags that are not to be escaped, as their localized name. For example new String[] { "b", "i", "font" }

processLinks

public static String processLinks(String sUnprocessed,
                                  boolean bNewWindow)
Processes a string, replacing any apparent URLs with the HTML code needed to generate an HREF link to that URL.

Parameters
sUnprocessed - the unprocessed string
bNewWindow - if true, link should open new browser window
Returns
the processed string

processLinks

public static String processLinks(String sUnprocessed)
Processes a string, replacing any apparent URLs with the HTML code needed to generate an HREF link to that URL. By default, link appears in current browser window.

Parameters
sUnprocessed - the unprocessed string
Returns
the processed string

escapeAndProcessLinks

public static String escapeAndProcessLinks(String sUnprocessed)
HTML escapes a string and process its links for use in a JSP page. By default, link appears in current browser window.

Parameters
sUnprocessed - the unprocessed string
Returns
the processed string
See Also
htmlEscape(String), processLinks(String)

escapeAndProcessLinks

public static String escapeAndProcessLinks(String sUnprocessed,
                                           boolean bNewWindow)
HTML escapes a string and process its links for use in a JSP page.

Parameters
sUnprocessed - the unprocessed string
bNewWindow - if true, link should open new browser window
Returns
the processed string
See Also
htmlEscape(String), processLinks(String)

getDigest

public static byte[] getDigest(String sStr,
                               String sAlgorithm,
                               String sEncoding)
                        throws NoSuchAlgorithmException,
                               UnsupportedEncodingException
Get a digest of the specified string.

Parameters
sStr - the string to digest
sAlgorithm - use the specified MessageDigest algorithm
sEncoding - use the specified character encoding
Returns
the digest
Throws
NoSuchAlgorithmException - if the specified algorithm could not be found
UnsupportedEncodingException - if the specified character encoding is not supported

getDigest

public static byte[] getDigest(String sStr)
                        throws NoSuchAlgorithmException,
                               UnsupportedEncodingException
Get an SHA digest of the specified string using the UTF-8 character encoding.

Parameters
sStr - the string to digest
Returns
the SHA digest
Throws
NoSuchAlgorithmException - if the specified algorithm could not be found
UnsupportedEncodingException - if the specified character encoding is not supported

getDigest

public static byte[] getDigest(String sStr,
                               String sAlgorithm)
                        throws NoSuchAlgorithmException,
                               UnsupportedEncodingException
Get a digest of the specified string using the UTF-8 character encoding.

Parameters
sStr - the string to digest
sAlgorithm - use the specified MessageDigest algorithm
Returns
the SHA digest
Throws
NoSuchAlgorithmException - if the specified algorithm could not be found
UnsupportedEncodingException - if the specified character encoding is not supported

byteToHex

public static String byteToHex(byte data)
Convenience method to convert a byte to a hex string.

Parameters
data - the byte to convert
Returns
the converted byte

byteToHex

public static String byteToHex(byte data,
                               boolean bUpperCase)
Convenience method to convert a byte to a hex string.

Parameters
data - the byte to convert
Returns
the converted byte

bytesToHex

public static String bytesToHex(byte[] bytes)
Gets a byte array as a string in hexadecimal with the characters returned in uppercase.

Parameters
bytes - the byte array
Returns
the hex string

bytesToHex

public static String bytesToHex(byte[] bytes,
                                boolean bUppercase)
Gets a byte array as a string in hexadecimal with the characters returned in uppercase.

Parameters
bytes - the byte array
bUppercase - if true,
Returns
the hex string

bytesToHex

public static String bytesToHex(byte[] bytes,
                                int iOffset,
                                int iLength,
                                boolean bUppercase,
                                boolean bFormatted)
Gets a byte array as a string in hexadecimal.

Parameters
bytes - the byte array
iOffset - the offset in the byte array to start
iLength - the number of bytes to convert
bUppercase - if true, characters are returned in uppercase
bFormatted - if true, format hexadecimal in a multiline easily readable way
Returns
the hex string

getCommaSeparatedList

public static void getCommaSeparatedList(Collection strs,
                                         boolean bSpaceAfterComma,
                                         StringBuffer buf)
Gets a comma separated list from a collection of strings or objects (for an object we call the toString method). Null objects are represented by the empty string.

Parameters
strs - the strings or objects (not null)
bSpaceAfterComma - if true, include a space after each comma that separates the elements in the list
buf - the buffer to write to

getSeparatedList

public static void getSeparatedList(Collection strs,
                                    String sSeparator,
                                    StringBuffer buf)
Gets a separated list from a collection of strings or objects (for an object we call the toString method). Null objects are represented by the empty string.

Parameters
strs - the strings or objects (not null)
sSeparator - the String to use to separate the elements
buf - the buffer to write to

getCommaSeparatedList

public static String getCommaSeparatedList(Collection strs,
                                           boolean bSpaceAfterComma)
Gets a comma separated list from a collection of strings or objects (for an object we call the toString method). Null objects are represented by the empty string.

Parameters
strs - the strings or objects (not null)
bSpaceAfterComma - if true, include a space after each comma that separates the elements in the list
Returns
the comma separated list

getCommaSeparatedList

public static void getCommaSeparatedList(Object[] strs,
                                         boolean bSpaceAfterComma,
                                         StringBuffer buf)
Gets a comma separated list from an array of strings or objects (for an object we call the toString method). Null objects are represented by the empty string.

Parameters
strs - the strings or objects (not null)
bSpaceAfterComma - if true, include a space after each comma that separates the elements in the list
buf - the buffer to write to

getCommaSeparatedList

public static void getCommaSeparatedList(long[] longs,
                                         boolean bSpaceAfterComma,
                                         StringBuffer buf)
Gets a comma separated list from an array of longs.

Parameters
longs - the longs
bSpaceAfterComma - if true, include a space after each comma that separates the elements in the list
buf - the buffer to write to

getSeparatedList

public static void getSeparatedList(Object[] strs,
                                    String sSeparator,
                                    StringBuffer buf)
Gets a separated list from an array of strings or objects (for an object we call the toString method). Null objects are represented by the empty string.

Parameters
strs - the strings or objects (not null)
sSeparator - the separator between list objects
buf - the buffer to write to

getSeparatedList

public static void getSeparatedList(long[] longs,
                                    String sSeparator,
                                    StringBuffer buf)
Gets a separated list from an array of longs.

Parameters
longs - the strings or objects (not null)
sSeparator - the separator between list objects
buf - the buffer to write to

getCommaSeparatedList

public static String getCommaSeparatedList(Object[] strs,
                                           boolean bSpaceAfterComma)
Gets a comma separated list from a collection of strings or objects (for an object we call the toString method). Null objects are represented by the empty string.

Parameters
strs - the strings or objects (not null)
bSpaceAfterComma - if true, include a space after each comma that separates the elements in the list
Returns
the comma separated list

getExceptionStackTrace

public static String getExceptionStackTrace(Throwable t)
Gets the stack trace of a Throwable as a String.

Parameters
t - the throwable
Returns
the stack trace

isBlank

public static boolean isBlank(String sStr)
Determine if a string is null or empty.

Parameters
sStr - the string to evaluate
Returns
true if a string is null or empty

hexToBytes

public static byte[] hexToBytes(String sHex)
Convenience method to convert a hex string to a byte array.

Parameters
data - the byte[] to convert
Returns
the converted byte[]

hexToByte

public static byte hexToByte(String s)
Convenience method to convert a hex string to a byte.

Parameters
s - the string to convert
Returns
the converted byte

toHexChar

public static char toHexChar(int i)
Convenience method to convert an int to a hex char.

Parameters
i - the int to convert
Returns
the converted char

crackVersionString

public static int[] crackVersionString(String sVersion)
                                throws NumberFormatException
Crack a version number String like "3.12-4" into an array of ints {3,12,4}. Valid delimiters between numbers are "." and "-". Two delimiters in a row will cause a NumberFormatException. A trailing delimiter is ignored.

Parameters
sVersion - the version number String
Returns
the corresponding int[]
Throws
NumberFormatException - if sVersion isn't of the form #(-#)*-? where '#' represents a number and '-' represents a delimiter

compareVersions

public static int compareVersions(int[] version1,
                                  int[] version2)
Compare two version arrays as Comparator.compare(java.lang.Object, java.lang.Object). If the arrays are different lengths with one a prefix of the other, the longer array will be considered greater.

Parameters
version1 - the first version array
version2 - the second version array
Returns
a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second

equalsWithNull

public static boolean equalsWithNull(String s1,
                                     String s2)
Determines if two strings are equal (either string may be null).

Parameters
s1 - the first string (may be null)
s2 - the second string (may be null)
Returns
true if the strings are equal or both are null

htmlEscape

public static String htmlEscape(String sUnescapedString,
                                String sEncoding)
                         throws UnsupportedEncodingException
HTML escape a string. This implementation uses a static buffer to write output to and as such is synchronized.

Parameters
sUnescapedString - the unescaped string
sEncoding - the character encoding
Returns
String the escaped string
Throws
UnsupportedEncodingException

htmlEscape

public static String htmlEscape(String sUnescapedString)
HTML escape a string. This implementation uses a static buffer to write output to and as such is synchronized. This uses the default encoding of the Virtual Machine.

Parameters
sUnescapedString - the unescaped string
Returns
String the escaped string

escapeQuotes

public static String escapeQuotes(String sUnescapedString)
This replaces the special quote characters with escaped value

Parameters
sUnescapedString - the unescaped string
Returns
String the escaped string

trimChar

public static String trimChar(String str,
                              char c)
Trim the given character from either end of a String.

Parameters
str - the string to trim
c - the character to trim from either end
Returns
the trimmed string

substituteAll

public static final String substituteAll(String sSource,
                                         String sToBeReplaced,
                                         String sReplacement)
Substitutes all occurrences of a string.

Parameters
sSource - the string to do the replacement in
sToBeReplaced - the string to search for
sReplacement - the replacement
Returns
the original string with replacement made

prettyTruncate

public static String prettyTruncate(String sStr,
                                    String sPostfix,
                                    int iLength)
Truncate a string after a certain number of characters and append the specified string to it. This is useful for taking a string and appending "..." if the string becomes too long.

Parameters
sStr - the original string
sPostfix - the string to put at the end of the original string
iLength - the maximum length of the result (string plus postfix)

prettyTruncate

public static String prettyTruncate(String sStr,
                                    int iLength)
Truncate a string after a certain number of characters and append the specified string to it. This is useful for taking a string and appending "..." if the string becomes too long.

Parameters
sStr - the original string
iLength - the maximum length of the result (string plus postfix)

getArrayFromSeparatedList

public static String[] getArrayFromSeparatedList(String sStr,
                                                 String sDelim,
                                                 boolean bTrim)
Gets a String array from a separated list of Strings.

Parameters
sStr - the original string (not null)
sDelim - the delimiter (not null)
bTrim - if true, do a trim() on each String
Returns
the String array (not null)

getListFromSeparatedList

public static List getListFromSeparatedList(String sStr,
                                            String sDelim,
                                            boolean bTrim)
Gets a list of Strings from a separated list of Strings.

Parameters
sStr - the original string (not null)
sDelim - the delimiter (not null)
bTrim - if true, do a trim() on each String
Returns
the list (not null)

printColumns

public static void printColumns(String[][] strs,
                                int iPadding,
                                PrintStream out)
Prints strings in columns.

Parameters
strs - the strings (first dimension is rows, second dimension is columns, not null)
iWidth - if negative, the number of spaces between columns, if positive, the actual column width

indent

public static String indent(String s,
                            int iColumns)
Indents a String by the specified number of columns.

Parameters
s - the String to indent (not null)
iColumns - the number of columns to indent
Returns
the indented String

main

public static void main(String[] args)
Main.



Copyright © 2006 BEA Systems, Inc. All Rights Reserved