de.enough.polish.util
Class TextUtil

java.lang.Object
  extended by de.enough.polish.util.TextUtil

public final class TextUtil
extends Object

Provides some useful String methods.

Copyright Enough Software 2004 - 2012

Author:
Robert Virkus, j2mepolish@enough.de

Field Summary
static String MAXLINES_APPENDIX
          the default appendix to attach to a truncated text
static int MAXLINES_APPENDIX_POSITION_AFTER
          Position of the appendix is at the end of the text.
static int MAXLINES_APPENDIX_POSITION_BEFORE
          Position of the appendix is at the beginning of the text.
static int MAXLINES_UNLIMITED
          standard maximum lines number for text wrapping
 
Constructor Summary
TextUtil()
           
 
Method Summary
static String encodeAsQuotedPrintable(String clearText, String enc)
          This method encodes a string to a quoted-printable string according to RFC 2045.
static String encodeForXmlParser(String text)
          Encodes a string for parsing using an XML parser.
static String encodeUrl(String url)
          Encodes a URL string.
static boolean equalsIgnoreCase(String str1, String str2)
          Compares two strings in a case-insensitive way.
static int lastIndexOf(String text, String match)
          Retrieves the last index of the given match in the specified text.
static String replace(String input, String search, String replacement)
          Replaces the all matches within a String.
static String replaceFirst(String input, String search, String replacement)
          Replaces the first match in a String.
static String replaceLast(String input, String search, String replacement)
          Replaces the last match in a String.
static char resolveNamedHtmlEntity(String name)
          Resolves the specified HTML entity such as "lt", "quot", "auml" and similar.
static String reverseForRtlLanguage(String input)
          Reverses the given text while keeping English texts and numbers in the normal position.
static String[] split(String value, char delimiter)
          Splits the given String around the matches defined by the given delimiter into an array.
static String[] split(String value, char delimiter, int numberOfChunks)
          Splits the given String around the matches defined by the given delimiter into an array.
static String[] split(String value, Font font, int firstLineWidth, int lineWidth)
          Deprecated. please use wrap instead
static void split(String value, Font font, int completeWidth, int firstLineWidth, int lineWidth, WrappedText result)
          Deprecated. please use wrap instead
static String[] splitAndTrim(String value, char delimiter)
          Splits the given String around the matches defined by the given delimiter into an array.
static String unescapeHtmlEntities(String input)
          Decodes text that contains HTML entities such as " or >.
static String[] wrap(String value, Font font, int firstLineWidth, int lineWidth)
          Wraps the given string so it fits on the specified lines.
static String[] wrap(String value, Font font, int firstLineWidth, int lineWidth, int maxLines, String maxLinesAppendix)
          Wraps the given string so it fits on the specified lines.
static String[] wrap(String value, Font font, int firstLineWidth, int lineWidth, int maxLines, String maxLinesAppendix, int maxLinesAppendixPosition)
          Wraps the given string so it fits on the specified lines.
static void wrap(String value, Font font, int firstLineWidth, int lineWidth, int maxLines, String maxLinesAppendix, int maxLinesAppendixPosition, WrappedText result)
          Wraps the given string so it fits on the specified lines.
static void wrap(String value, Font font, int completeWidth, int firstLineWidth, int lineWidth, WrappedText list, int maxLines, int maxLinesAppendixPosition)
          Wraps the given string so that the substrings fit into the the given line-widths.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAXLINES_UNLIMITED

public static final int MAXLINES_UNLIMITED
standard maximum lines number for text wrapping

See Also:
wrap(String, Font, int, int, int, String), Constant Field Values

MAXLINES_APPENDIX_POSITION_AFTER

public static final int MAXLINES_APPENDIX_POSITION_AFTER
Position of the appendix is at the end of the text.

See Also:
wrap(String, Font, int, int, int, String), Constant Field Values

MAXLINES_APPENDIX_POSITION_BEFORE

public static final int MAXLINES_APPENDIX_POSITION_BEFORE
Position of the appendix is at the beginning of the text.

See Also:
wrap(String, Font, int, int, int, String), Constant Field Values

MAXLINES_APPENDIX

public static final String MAXLINES_APPENDIX
the default appendix to attach to a truncated text

See Also:
wrap(String, Font, int, int, int, String), Constant Field Values
Constructor Detail

TextUtil

public TextUtil()
Method Detail

split

public static String[] split(String value,
                             char delimiter)
Splits the given String around the matches defined by the given delimiter into an array. Example: TextUtil.split("one;two;three", ';') results into the array {"one", "two", "three"}.

Parameters:
value - the String which should be split into an array
delimiter - the delimiter which marks the boundaries of the array
Returns:
an array, when the delimiter was not found, the array will only have a single element.

splitAndTrim

public static String[] splitAndTrim(String value,
                                    char delimiter)
Splits the given String around the matches defined by the given delimiter into an array. Example: TextUtil.splitAndTrim(" one; two; three", ';') results into the array {"one", "two", "three"}.

Parameters:
value - the String which should be split into an array
delimiter - the delimiter which marks the boundaries of the array
Returns:
an array, when the delimiter was not found, the array will only have a single element.

split

public static String[] split(String value,
                             char delimiter,
                             int numberOfChunks)
Splits the given String around the matches defined by the given delimiter into an array. Example: TextUtil.split("one;two;three", ';', 3) results into the array {"one", "two", "three"}.
TextUtil.split("one;two;three", ';', 4) results into the array {"one", "two", "three", null}.
TextUtil.split("one;two;three", ';', 2) results into the array {"one", "two"}.
This method is less resource intensive compared to the other split method, since no temporary list needs to be created

Parameters:
value - the String which should be split into an array
delimiter - the delimiter which marks the boundaries of the array
numberOfChunks - the number of expected matches
Returns:
an array with the length of numberOfChunks, when not enough elements are found, the array will contain null elements

split

public static String[] split(String value,
                             Font font,
                             int firstLineWidth,
                             int lineWidth)
Deprecated. please use wrap instead

Wraps the given string so it fits on the specified lines. First of all it is split at the line-breaks ('\n'), subsequently the substrings are split when they do not fit on a single line.

Parameters:
value - the string which should be split
font - the font which is used to display the font
firstLineWidth - the allowed width for the first line
lineWidth - the allowed width for all other lines, lineWidth >= firstLineWidth
Returns:
the array containing the substrings
See Also:
wrap(String, Font, int, int)

wrap

public static String[] wrap(String value,
                            Font font,
                            int firstLineWidth,
                            int lineWidth)
Wraps the given string so it fits on the specified lines. First of all it is split at the line-breaks ('\n'), subsequently the substrings are split when they do not fit on a single line.

Parameters:
value - the string which should be split
font - the font which is used to display the font
firstLineWidth - the allowed width for the first line
lineWidth - the allowed width for all other lines, lineWidth >= firstLineWidth
Returns:
the array containing the substrings

wrap

public static String[] wrap(String value,
                            Font font,
                            int firstLineWidth,
                            int lineWidth,
                            int maxLines,
                            String maxLinesAppendix)
Wraps the given string so it fits on the specified lines. First of all the text is split at the line-breaks ('\n'), subsequently the substrings are split when they do not fit on a single line.

Parameters:
value - the string which should be wrapped
font - the font which is used to display the font
firstLineWidth - the allowed width for the first line
lineWidth - the allowed width for all other lines, lineWidth >= firstLineWidth
maxLines - the maximum number of lines
maxLinesAppendix - the appendix that should be added to the last line when the line number is greater than maxLines
Returns:
the array containing the substrings

wrap

public static String[] wrap(String value,
                            Font font,
                            int firstLineWidth,
                            int lineWidth,
                            int maxLines,
                            String maxLinesAppendix,
                            int maxLinesAppendixPosition)
Wraps the given string so it fits on the specified lines. First of all the text is split at the line-breaks ('\n'), subsequently the substrings are split when they do not fit on a single line.

Parameters:
value - the string which should be wrapped
font - the font which is used to display the font
firstLineWidth - the allowed width for the first line
lineWidth - the allowed width for all other lines, lineWidth >= firstLineWidth
maxLines - the maximum number of lines
maxLinesAppendix - the appendix that should be added to the last line when the line number is greater than maxLines
maxLinesAppendixPosition - either MAXLINES_APPENDIX_POSITION_AFTER or MAXLINES_APPENDIX_POSITION_BEFORE
Returns:
the array containing the substrings

wrap

public static void wrap(String value,
                        Font font,
                        int firstLineWidth,
                        int lineWidth,
                        int maxLines,
                        String maxLinesAppendix,
                        int maxLinesAppendixPosition,
                        WrappedText result)
Wraps the given string so it fits on the specified lines. First of all the text is split at the line-breaks ('\n'), subsequently the substrings are split when they do not fit on a single line.

Parameters:
value - the string which should be wrapped
font - the font which is used to display the font
firstLineWidth - the allowed width for the first line
lineWidth - the allowed width for all other lines, lineWidth >= firstLineWidth
maxLines - the maximum number of lines
maxLinesAppendix - the appendix that should be added to the last line when the line number is greater than maxLines
maxLinesAppendixPosition - either MAXLINES_APPENDIX_POSITION_AFTER or MAXLINES_APPENDIX_POSITION_BEFORE
result - the WrappedText that should be reused (should be cleared by caller in most cases)

split

public static void split(String value,
                         Font font,
                         int completeWidth,
                         int firstLineWidth,
                         int lineWidth,
                         WrappedText result)
Deprecated. please use wrap instead

Wraps the given string so that the substrings fit into the the given line-widths. It is expected that the specified lineWidth >= firstLineWidth. The resulting substrings will be added to the given ArrayList. When the complete string fits into the first line, it will be added to the list. When the string needs to be wrapped to fit on the lines, it is tried to split the string at a gap between words. When this is not possible, the given string will be wrapped in the middle of the corresponding word.

Parameters:
value - the string which should be wrapped
font - the font which is used to display the font
completeWidth - the complete width of the given string for the specified font.
firstLineWidth - the allowed width for the first line
lineWidth - the allowed width for all other lines, lineWidth >= firstLineWidth
result - the WrappedText to which the substrings will be added.
See Also:
wrap(String, Font, int, int, int, WrappedText, int, int)

wrap

public static void wrap(String value,
                        Font font,
                        int completeWidth,
                        int firstLineWidth,
                        int lineWidth,
                        WrappedText list,
                        int maxLines,
                        int maxLinesAppendixPosition)
Wraps the given string so that the substrings fit into the the given line-widths. It is expected that the specified lineWidth >= firstLineWidth. The resulting substrings will be added to the given ArrayList. When the complete string fits into the first line, it will be added to the list. When the string needs to be wrapped to fit on the lines, it is tried to split the string at a gap between words. When this is not possible, the given string will be wrapped in the middle of the corresponding word.

Parameters:
value - the string which should be wrapped
font - the font which is used to display the font
completeWidth - the complete width of the given string for the specified font.
firstLineWidth - the allowed width for the first line
lineWidth - the allowed width for all other lines, lineWidth >= firstLineWidth
maxLines - the maximum number of lines
maxLinesAppendixPosition - either MAXLINES_APPENDIX_POSITION_AFTER or MAXLINES_APPENDIX_POSITION_BEFORE
list - the list to which the substrings will be added.

encodeUrl

public static String encodeUrl(String url)
Encodes a URL string. This method assumes UTF-8 usage.

Parameters:
url - URL to encode
Returns:
the encoded URL

encodeForXmlParser

public static String encodeForXmlParser(String text)
Encodes a string for parsing using an XML parser. &, < and > are replaced.

Parameters:
text - the text
Returns:
the cleaned text

replace

public static String replace(String input,
                             String search,
                             String replacement)
Replaces the all matches within a String.

Parameters:
input - the input string
search - the string that should be replaced
replacement - the replacement
Returns:
the input string where the search string has been replaced
Throws:
NullPointerException - when one of the specified strings is null

replaceFirst

public static String replaceFirst(String input,
                                  String search,
                                  String replacement)
Replaces the first match in a String.

Parameters:
input - the input string
search - the string that should be replaced
replacement - the replacement
Returns:
the input string where the first match of the search string has been replaced
Throws:
NullPointerException - when one of the specified strings is null

replaceLast

public static String replaceLast(String input,
                                 String search,
                                 String replacement)
Replaces the last match in a String.

Parameters:
input - the input string
search - the string that should be replaced
replacement - the replacement
Returns:
the input string where the last match of the search string has been replaced
Throws:
NullPointerException - when one of the specified strings is null

lastIndexOf

public static int lastIndexOf(String text,
                              String match)
Retrieves the last index of the given match in the specified text.

Parameters:
text - the text in which the match is given
match - the match within the text
Returns:
the last index of the match or -1 when the match is not found in the given text
Throws:
NullPointerException - when text or match is null

equalsIgnoreCase

public static boolean equalsIgnoreCase(String str1,
                                       String str2)
Compares two strings in a case-insensitive way. Both strings are lower cased and then compared. If both are equal this method returns true, false otherwise.

Parameters:
str1 - the string to compare
str2 - the string to compare to
Returns:
true if both strings are equals except case, false
Throws:
NullPointerException - if str1 is null
See Also:
String.equals(Object), String.equalsIgnoreCase(String)

reverseForRtlLanguage

public static String reverseForRtlLanguage(String input)
Reverses the given text while keeping English texts and numbers in the normal position.

Parameters:
input - the text
Returns:
the reversed text

encodeAsQuotedPrintable

public static String encodeAsQuotedPrintable(String clearText,
                                             String enc)
                                      throws UnsupportedEncodingException
This method encodes a string to a quoted-printable string according to RFC 2045. All five rules are implemented.

Parameters:
clearText - the string to encode
enc - The encoding which should be used to interpret the cleartext.
Returns:
the encoded string
Throws:
UnsupportedEncodingException - if the given encoding is not supported

resolveNamedHtmlEntity

public static char resolveNamedHtmlEntity(String name)
Resolves the specified HTML entity such as "lt", "quot", "auml" and similar. Please note that right now no Greek nor mathematical symbols are supported. Compare http://www.w3.org/TR/WD-html40-970708/sgml/entities.html for known HTML entities.

Parameters:
name - the name of the entity
Returns:
the corresponding character, '?' when the entity could not be resolved.

unescapeHtmlEntities

public static String unescapeHtmlEntities(String input)
Decodes text that contains HTML entities such as &quot; or &#62;.

Parameters:
input - the text input that might contain HTML entities
Returns:
the cleaned text. When an HTML entity could not be resolved, it will be replaced with a question mark '?'.