Compoze Software, Inc.

com.compoze.util
Class StringUtility

java.lang.Object
  extended bycom.compoze.util.StringUtility
All Implemented Interfaces:
java.io.Serializable

public class StringUtility
extends java.lang.Object
implements java.io.Serializable

This class contains utility methods that operate on strings.

See Also:
Serialized Form

Constructor Summary
StringUtility()
           
 
Method Summary
static java.lang.String bytesToHex(byte[] data)
          Convenience method to convert a byte array to a hex string.
static java.lang.String byteToHex(byte data)
          Convenience method to convert a byte to a hex string.
static java.lang.String encode(java.lang.String sUnencodedString, java.lang.String sFromEncoding, java.lang.String sToEncoding)
          Method description.
static java.lang.String escapeQuotes(java.lang.String sUnescapedString)
          This replaces the special quote characters with escaped value
static byte[] getDigest(java.lang.String sStr)
          Get an SHA digest of the specified string using the UTF-8 character encoding.
static byte[] getDigest(java.lang.String sStr, java.lang.String sAlgorithm)
          Get a digest of the specified string using the UTF-8 character encoding.
static byte[] getDigest(java.lang.String sStr, java.lang.String sAlgorithm, java.lang.String sEncoding)
          Get a digest of the specified string.
static long getDigestLong(java.lang.String sStr)
          Get an SHA digest of the specified string, folding over the byte array into a long.
static java.lang.String getListString(java.util.Collection items, java.lang.String sDelim)
          Creates a comma seperated list of items from a collection
static java.util.LinkedList getSeparatedList(java.lang.String sListOfStrings, java.lang.String sDelim)
          From a list of strings separated by some separation character, get a linked list of the strings.
static java.lang.String getStringFromInputStream(java.io.InputStream is)
          Get a string from an input stream.
static byte hexToByte(java.lang.String s)
          Convenience method to convert a hex string to a byte.
static byte[] hexToBytes(java.lang.String sHex)
          Convenience method to convert a hex string to a byte array.
static java.lang.String htmlEscape(java.lang.String sUnescapedString)
          HTML escape a string.
static java.lang.String htmlEscape(java.lang.String sUnescapedString, java.lang.String sEncoding)
          HTML escape a string.
static boolean isBlank(java.lang.String sStr)
          Determine if a string is null and empty.
static void main(java.lang.String[] args)
          Exerciser.
static java.lang.String mapReplace(java.lang.String sString, java.util.Map replacementMap, java.lang.String sEscape)
          Replace occurrences of escaped strings (variables) within a string using a hash map that maps keys to their values.
static java.lang.String mapReplace(java.lang.String sString, java.util.Map replacementMap, java.lang.String sEscapeStart, java.lang.String sEscapeEnd)
          Replace occurrences of escaped strings (variables) within a string using a hash map that maps keys to their values.
static java.lang.String mapReplace(java.lang.String sString, java.util.Map replacementMap, java.lang.String sEscapeStart, java.lang.String sEscapeEnd, boolean bDefaultToSystem)
          Replace occurrences of escaped strings (variables) within a string using a hash map that maps keys to their values.
static java.lang.String mapReplaceFully(java.lang.String sString, java.util.Map replacementMap, java.lang.String sEscapeStart, java.lang.String sEscapeEnd, boolean bDefaultToSystem)
          Replace occurrences of escaped strings (variables) within a string using a hash map that maps keys to their values.
static java.lang.Object objectFromBase64(java.lang.String str)
          Gets an object from a string.
static java.lang.Object objectFromBase64(java.lang.String str, boolean bCompress)
          Gets an object from a string.
static java.lang.String objectToBase64(java.lang.Object serializableObj)
          Gets a string from an object.
static java.lang.String objectToBase64(java.lang.Object serializableObj, boolean bCompress)
          Gets a string from an object.
static java.lang.String objectToString(java.lang.Object o)
          Convert an object to a string using the object toString.
static java.lang.String prettyTruncate(java.lang.String sStr, int iLength)
          Truncate a string after a certain number of characters and append the specified string to it.
static java.lang.String prettyTruncate(java.lang.String sStr, java.lang.String sPostfix, int iLength)
          Truncate a string after a certain number of characters and append the specified string to it.
static java.lang.String replaceString(java.lang.String sOriginalString, java.lang.String sOldString, java.lang.String sNewString)
          Replace all occurrences of a string in a string.
static java.lang.String substituteAll(java.lang.String source, java.lang.String toBeReplaced, java.lang.String replacement)
          Substitutes all occurrences of a string.
static java.lang.String substituteFirst(java.lang.String source, java.lang.String toBeReplaced, java.lang.String replacement)
          Substitutes first occurrence of a string.
static char toHexChar(int i)
          Convenience method to convert an int to a hex char.
static java.lang.String trimChar(java.lang.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
 

Constructor Detail

StringUtility

public StringUtility()
Method Detail

objectToBase64

public static java.lang.String objectToBase64(java.lang.Object serializableObj)
                                       throws java.io.IOException
Gets a string from an object. This converts a Serializable object to a string by base64 encoding its serialized data. The object's data is compressed.

Parameters:
serializableObj - the object to convert
Returns:
a UTF-8 string containing the serialized, base64 encoded version of the object
Throws:
java.io.IOException - if the object could not be converted to a String

objectToBase64

public static java.lang.String objectToBase64(java.lang.Object serializableObj,
                                              boolean bCompress)
                                       throws java.io.IOException
Gets a string from an object. This converts a Serializable object to a string by base64 encoding its serialized data.

Parameters:
serializableObj - the object to convert
bCompress - if true, compress the output
Returns:
a UTF-8 string containing the serialized, base64 encoded version of the object
Throws:
java.io.IOException - if the object could not be converted to a String

objectFromBase64

public static java.lang.Object objectFromBase64(java.lang.String str)
                                         throws java.io.IOException,
                                                java.lang.ClassNotFoundException
Gets an object from a string. This converts a base64 encoded string with a serialized object in it to an Object. Compression is used.

Parameters:
str - the string containing the base64 encoded, serialized version of the object
Returns:
the object
Throws:
java.io.IOException - if the String could not be converted to an object
java.lang.ClassNotFoundException - if the class in the String could not be found

objectFromBase64

public static java.lang.Object objectFromBase64(java.lang.String str,
                                                boolean bCompress)
                                         throws java.io.IOException,
                                                java.lang.ClassNotFoundException
Gets an object from a string. This converts a base64 encoded string with a serialized object in it to an Object.

Parameters:
str - the string containing the base64 encoded, serialized version of the object
bCompress - if true, decompress the object's data as it is read
Returns:
the object
Throws:
java.io.IOException - if the String could not be converted to an object
java.lang.ClassNotFoundException - if the class in the String could not be found

encode

public static java.lang.String encode(java.lang.String sUnencodedString,
                                      java.lang.String sFromEncoding,
                                      java.lang.String sToEncoding)
Method description.


byteToHex

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

Parameters:
data - the byte to convert
Returns:
the converted byte

bytesToHex

public static java.lang.String bytesToHex(byte[] data)
Convenience method to convert a byte array to a hex string.

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

hexToByte

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

Parameters:
s - the string to convert
Returns:
the converted byte

hexToBytes

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

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

htmlEscape

public static java.lang.String htmlEscape(java.lang.String sUnescapedString,
                                          java.lang.String sEncoding)
                                   throws java.io.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:
java.io.UnsupportedEncodingException

htmlEscape

public static java.lang.String htmlEscape(java.lang.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 java.lang.String escapeQuotes(java.lang.String sUnescapedString)
This replaces the special quote characters with escaped value

Parameters:
sUnescapedString - the unescaped string
Returns:
String the escaped string

getStringFromInputStream

public static java.lang.String getStringFromInputStream(java.io.InputStream is)
                                                 throws java.io.IOException
Get a string from an input stream.

Parameters:
is - the input stream to some text
Returns:
the string, or null if not found
Throws:
java.io.IOException - if an i/o error occurred

trimChar

public static java.lang.String trimChar(java.lang.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

replaceString

public static java.lang.String replaceString(java.lang.String sOriginalString,
                                             java.lang.String sOldString,
                                             java.lang.String sNewString)
                                      throws java.io.IOException
Replace all occurrences of a string in a string.

Parameters:
sOriginalString - the string to do the replacement in
sOldString - the string to search for
sNewString - the replacement
Returns:
the original string with replacement made
Throws:
java.io.IOException

substituteAll

public static final java.lang.String substituteAll(java.lang.String source,
                                                   java.lang.String toBeReplaced,
                                                   java.lang.String replacement)
Substitutes all occurrences of a string.


substituteFirst

public static final java.lang.String substituteFirst(java.lang.String source,
                                                     java.lang.String toBeReplaced,
                                                     java.lang.String replacement)
Substitutes first occurrence of a string.


mapReplace

public static java.lang.String mapReplace(java.lang.String sString,
                                          java.util.Map replacementMap,
                                          java.lang.String sEscape)
Replace occurrences of escaped strings (variables) within a string using a hash map that maps keys to their values.

Parameters:
sString - the string to replace variables in
replacementMap - the key/value pairs for replacement
sEscape - the string that appears before and after variables
Returns:
the original string with variables replaced by their values

mapReplaceFully

public static java.lang.String mapReplaceFully(java.lang.String sString,
                                               java.util.Map replacementMap,
                                               java.lang.String sEscapeStart,
                                               java.lang.String sEscapeEnd,
                                               boolean bDefaultToSystem)
Replace occurrences of escaped strings (variables) within a string using a hash map that maps keys to their values. Keep processing string until all nested replacements have been made.

Parameters:
sString - the string to replace variables in
replacementMap - the key/value pairs for replacement
sEscapeStart - the string that appears before variables
sEscapeEnd - the string that appears after variables
bDefaultToSystem - if a property cannot be found, default to the system property
Returns:
the original string with variables replaced by their values

mapReplace

public static java.lang.String mapReplace(java.lang.String sString,
                                          java.util.Map replacementMap,
                                          java.lang.String sEscapeStart,
                                          java.lang.String sEscapeEnd)
Replace occurrences of escaped strings (variables) within a string using a hash map that maps keys to their values.

Parameters:
sString - the string to replace variables in
replacementMap - the key/value pairs for replacement
sEscapeStart - the string that appears before variables
sEscapeEnd - the string that appears after variables
Returns:
the original string with variables replaced by their values

mapReplace

public static java.lang.String mapReplace(java.lang.String sString,
                                          java.util.Map replacementMap,
                                          java.lang.String sEscapeStart,
                                          java.lang.String sEscapeEnd,
                                          boolean bDefaultToSystem)
Replace occurrences of escaped strings (variables) within a string using a hash map that maps keys to their values.

Parameters:
sString - the string to replace variables in
replacementMap - the key/value pairs for replacement
sEscapeStart - the string that appears before variables
sEscapeEnd - the string that appears after variables
bDefaultToSystem - if a property cannot be found, default to the system property
Returns:
the original string with variables replaced by their values

getSeparatedList

public static java.util.LinkedList getSeparatedList(java.lang.String sListOfStrings,
                                                    java.lang.String sDelim)
From a list of strings separated by some separation character, get a linked list of the strings.

Parameters:
sListOfStrings - the separated list of strings
sDelim - the delimiter
Returns:
the list of strings
See Also:
StringTokenizer

getListString

public static java.lang.String getListString(java.util.Collection items,
                                             java.lang.String sDelim)
Creates a comma seperated list of items from a collection

Parameters:
items - collection of items
sDelim - delimiter
Returns:
comma seperated list of items

objectToString

public static java.lang.String objectToString(java.lang.Object o)
Convert an object to a string using the object toString.

Parameters:
o - the object to convert to string
Returns:
the object to string or empty string if null
See Also:
Object.toString()

getDigest

public static byte[] getDigest(java.lang.String sStr,
                               java.lang.String sAlgorithm,
                               java.lang.String sEncoding)
                        throws java.security.NoSuchAlgorithmException,
                               java.io.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:
java.security.NoSuchAlgorithmException - if the specified algorithm could not be found
java.io.UnsupportedEncodingException - if the specified character encoding is not supported

getDigest

public static byte[] getDigest(java.lang.String sStr)
                        throws java.security.NoSuchAlgorithmException,
                               java.io.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:
java.security.NoSuchAlgorithmException - if the specified algorithm could not be found
java.io.UnsupportedEncodingException - if the specified character encoding is not supported

getDigest

public static byte[] getDigest(java.lang.String sStr,
                               java.lang.String sAlgorithm)
                        throws java.security.NoSuchAlgorithmException,
                               java.io.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:
java.security.NoSuchAlgorithmException - if the specified algorithm could not be found
java.io.UnsupportedEncodingException - if the specified character encoding is not supported

getDigestLong

public static long getDigestLong(java.lang.String sStr)
                          throws java.security.NoSuchAlgorithmException,
                                 java.io.UnsupportedEncodingException
Get an SHA digest of the specified string, folding over the byte array into a long.

Parameters:
sStr - the string to digest
Returns:
the string's SHA digest folded into a long
Throws:
java.security.NoSuchAlgorithmException - if the specified algorithm could not be found
java.io.UnsupportedEncodingException - if the specified character encoding is not supported

isBlank

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

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

prettyTruncate

public static java.lang.String prettyTruncate(java.lang.String sStr,
                                              java.lang.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 java.lang.String prettyTruncate(java.lang.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)

main

public static void main(java.lang.String[] args)
Exerciser.

Parameters:
args - arguments (none taken)

Compoze Software, Inc.

Copyright ©1999-2003 Compoze Software, Inc. All rights reserved.