org.jboss.dna.common.util
Class StringUtil

java.lang.Object
  extended by org.jboss.dna.common.util.StringUtil

public class StringUtil
extends java.lang.Object

Utilities for string processing and manipulation.


Field Summary
static java.lang.String[] EMPTY_STRING_ARRAY
           
 
Method Summary
static java.lang.String createString(char charToRepeat, int numberOfRepeats)
          Create a new string containing the specified character repeated a specific number of times.
static java.lang.String createString(java.lang.String pattern, java.lang.Object... parameters)
          Create a string by substituting the parameters into all key occurrences in the supplied format.
static java.lang.String getStackTrace(java.lang.Throwable t)
          Get the stack trace of the supplied exception.
static java.lang.String justifyCenter(java.lang.String str, int width, char padWithChar)
          Center the contents of the string.
static java.lang.String justifyLeft(java.lang.String str, int width, char padWithChar)
          Left justify the contents of the string, ensuring that the supplied string begins at the first character and that the resulting string is of the desired length.
static java.lang.String justifyRight(java.lang.String str, int width, char padWithChar)
          Right justify the contents of the string, ensuring that the string ends at the last character.
static java.lang.String read(java.io.InputStream stream)
          Read and return the entire contents of the supplied InputStream.
static java.lang.String read(java.io.Reader reader)
          Read and return the entire contents of the supplied Reader.
static java.lang.String readableString(java.lang.Object obj)
          Create a human-readable form of the supplied object by choosing the representation format based upon the object type.
static java.lang.String setLength(java.lang.String original, int length, char padChar)
          Set the length of the string, padding with the supplied character if the supplied string is shorter than desired, or truncating the string if it is longer than desired.
static java.util.List<java.lang.String> splitLines(java.lang.String content)
          Split the supplied content into lines, returning each line as an element in the returned list.
static java.lang.String truncate(java.lang.Object obj, int maxLength)
          Truncate the supplied string to be no more than the specified length.
static java.lang.String truncate(java.lang.Object obj, int maxLength, java.lang.String suffix)
          Truncate the supplied string to be no more than the specified length.
static void write(java.lang.String content, java.io.OutputStream stream)
          Write the entire contents of the supplied string to the given stream.
static void write(java.lang.String content, java.io.Writer writer)
          Write the entire contents of the supplied string to the given writer.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_STRING_ARRAY

public static final java.lang.String[] EMPTY_STRING_ARRAY
Method Detail

splitLines

public static java.util.List<java.lang.String> splitLines(java.lang.String content)
Split the supplied content into lines, returning each line as an element in the returned list.

Parameters:
content - the string content that is to be split
Returns:
the list of lines; never null but may be an empty (unmodifiable) list if the supplied content is null or empty

createString

public static java.lang.String createString(java.lang.String pattern,
                                            java.lang.Object... parameters)
Create a string by substituting the parameters into all key occurrences in the supplied format. The pattern consists of zero or more keys of the form {n}, where n is an integer starting at 1. Therefore, the first parameter replaces all occurrences of "{1}", the second parameter replaces all occurrences of "{2}", etc.

If any parameter is null, the corresponding key is replaced with the string "null". Therefore, consider using an empty string when keys are to be removed altogether.

If there are no parameters, this method does nothing and returns the supplied pattern as is.

Parameters:
pattern - the pattern
parameters - the parameters used to replace keys
Returns:
the string with all keys replaced (or removed)

createString

public static java.lang.String createString(char charToRepeat,
                                            int numberOfRepeats)
Create a new string containing the specified character repeated a specific number of times.

Parameters:
charToRepeat - the character to repeat
numberOfRepeats - the number of times the character is to repeat in the result; must be greater than 0
Returns:
the resulting string

setLength

public static java.lang.String setLength(java.lang.String original,
                                         int length,
                                         char padChar)
Set the length of the string, padding with the supplied character if the supplied string is shorter than desired, or truncating the string if it is longer than desired. Unlike justifyLeft(String, int, char), this method does not remove leading and trailing whitespace.

Parameters:
original - the string for which the length is to be set; may not be null
length - the desired length; must be positive
padChar - the character to use for padding, if the supplied string is not long enough
Returns:
the string of the desired length
See Also:
justifyLeft(String, int, char)

justifyRight

public static java.lang.String justifyRight(java.lang.String str,
                                            int width,
                                            char padWithChar)
Right justify the contents of the string, ensuring that the string ends at the last character. If the supplied string is longer than the desired width, the leading characters are removed so that the last character in the supplied string at the last position. If the supplied string is shorter than the desired width, the padding character is inserted one or more times such that the last character in the supplied string appears as the last character in the resulting string and that the length matches that specified.

Parameters:
str - the string to be right justified; if null, an empty string is used
width - the desired width of the string; must be positive
padWithChar - the character to use for padding, if needed
Returns:
the right justified string

justifyLeft

public static java.lang.String justifyLeft(java.lang.String str,
                                           int width,
                                           char padWithChar)
Left justify the contents of the string, ensuring that the supplied string begins at the first character and that the resulting string is of the desired length. If the supplied string is longer than the desired width, it is truncated to the specified length. If the supplied string is shorter than the desired width, the padding character is added to the end of the string one or more times such that the length is that specified. All leading and trailing whitespace is removed.

Parameters:
str - the string to be left justified; if null, an empty string is used
width - the desired width of the string; must be positive
padWithChar - the character to use for padding, if needed
Returns:
the left justified string
See Also:
setLength(String, int, char)

justifyCenter

public static java.lang.String justifyCenter(java.lang.String str,
                                             int width,
                                             char padWithChar)
Center the contents of the string. If the supplied string is longer than the desired width, it is truncated to the specified length. If the supplied string is shorter than the desired width, padding characters are added to the beginning and end of the string such that the length is that specified; one additional padding character is prepended if required. All leading and trailing whitespace is removed before centering.

Parameters:
str - the string to be left justified; if null, an empty string is used
width - the desired width of the string; must be positive
padWithChar - the character to use for padding, if needed
Returns:
the left justified string
See Also:
setLength(String, int, char)

truncate

public static java.lang.String truncate(java.lang.Object obj,
                                        int maxLength)
Truncate the supplied string to be no more than the specified length. This method returns an empty string if the supplied object is null.

Parameters:
obj - the object from which the string is to be obtained using Object.toString().
maxLength - the maximum length of the string being returned
Returns:
the supplied string if no longer than the maximum length, or the supplied string truncated to be no longer than the maximum length (including the suffix)
Throws:
java.lang.IllegalArgumentException - if the maximum length is negative

truncate

public static java.lang.String truncate(java.lang.Object obj,
                                        int maxLength,
                                        java.lang.String suffix)
Truncate the supplied string to be no more than the specified length. This method returns an empty string if the supplied object is null.

Parameters:
obj - the object from which the string is to be obtained using Object.toString().
maxLength - the maximum length of the string being returned
suffix - the suffix that should be added to the content if the string must be truncated, or null if the default suffix of "..." should be used
Returns:
the supplied string if no longer than the maximum length, or the supplied string truncated to be no longer than the maximum length (including the suffix)
Throws:
java.lang.IllegalArgumentException - if the maximum length is negative

read

public static java.lang.String read(java.io.Reader reader)
                             throws java.io.IOException
Read and return the entire contents of the supplied Reader. This method always closes the reader when finished reading.

Parameters:
reader - the reader of the contents; may be null
Returns:
the contents, or an empty string if the supplied reader is null
Throws:
java.io.IOException - if there is an error reading the content

read

public static java.lang.String read(java.io.InputStream stream)
                             throws java.io.IOException
Read and return the entire contents of the supplied InputStream. This method always closes the stream when finished reading.

Parameters:
stream - the streamed contents; may be null
Returns:
the contents, or an empty string if the supplied stream is null
Throws:
java.io.IOException - if there is an error reading the content

write

public static void write(java.lang.String content,
                         java.io.OutputStream stream)
                  throws java.io.IOException
Write the entire contents of the supplied string to the given stream. This method always flushes and closes the stream when finished.

Parameters:
content - the content to write to the stream; may be null
stream - the stream to which the content is to be written
Throws:
java.io.IOException
java.lang.IllegalArgumentException - if the stream is null

write

public static void write(java.lang.String content,
                         java.io.Writer writer)
                  throws java.io.IOException
Write the entire contents of the supplied string to the given writer. This method always flushes and closes the writer when finished.

Parameters:
content - the content to write to the writer; may be null
writer - the writer to which the content is to be written
Throws:
java.io.IOException
java.lang.IllegalArgumentException - if the writer is null

readableString

public static java.lang.String readableString(java.lang.Object obj)
Create a human-readable form of the supplied object by choosing the representation format based upon the object type.

This method is capable of generating strings for nested objects. For example, a Map would be written in the form:

    { 2008-02-03T14:22:49 => [ "description", 3, [ 003459de7389g23aef, true ] ] }
 

Parameters:
obj - the object that is to be converted to a string.
Returns:
the string representation that is to be human readable

getStackTrace

public static java.lang.String getStackTrace(java.lang.Throwable t)
Get the stack trace of the supplied exception.

Parameters:
t - the exception for which the stack trace is to be returned
Returns:
the stack trace, or null if the supplied exception is null


Copyright © 2008. All Rights Reserved.