net.sourceforge.java.util.text
Class StringUtils

java.lang.Object
  extended bynet.sourceforge.java.util.text.StringUtils

public class StringUtils
extends Object

Contains String related utility methods.

Author:
Sebastian Machhausen

Field Summary
static char DEFAULT_DELIMITER
           The default delimiter char to use
static String EMPTY_STRING
           Constant for the empty String
private static Map ENTITY_MAPPING
           Maps entity names to their corresponding unicode Characters.
private static String HTML_PATTERN
           Regular expression pattern to match any HTML tags.
static String LINE_SEPARATOR
           Constant for the line separator
private static int LONGEST_ENTITY
           The longest an entity can be 10, including the lead & and trail ;
private static int SHORTEST_ENTITY
           The shortest an entity can be 4, including the lead & and trailing ;
 
Constructor Summary
StringUtils()
           
 
Method Summary
static String abbreviate(String str, int maximumChars)
           Abbreviates the specified String if its length exceeds the value specified by maximumChars.
static String arrayToCSV(int[] input, char delimiter)
           Converts the specified input to a single String in which the input elements are separated by the given delimiter
static String arrayToCSV(Point[] input, char delimiter)
           Converts the specified input to a single String in which the input elements are separated by the given delimiter
static String arrayToCSV(String[] input, char delimiter)
           Converts the specified input to a single String in which the input elements are separated by the given delimiter
static String convertEntities(String text)
           Converts HTML to text converting entities such as " back to " and < back to < Ordinary text passes unchanged.
static String convertHTML(String text)
           Converts the specified HTML input String to pure raw text.
static String[] csvToArray(String input, char delimiter)
           Uses the specified delimiter to convert the input to an array of single String elements.
static int[] csvToIntArray(String input, char delimiter)
           Uses the specified delimiter to convert the input to an array of single int/code> values.
static Point[] csvToPointArray(String input, char delimiter)
           Uses the specified delimiter to convert the input to an array of single Point elements.
static char entityToChar(String entity)
           Converts the specified entity to a single char.
static boolean isEmpty(String str)
           Tests if the given String is empty.
static String nullValueToEmptyString(String str)
           Converts a possible null String object to an empty String object.
private static char potentialEntityToChar(String entity)
           Checks a number of gauntlet conditions to ensure this is a valid entity.
static String stripHTML(String text)
           Strips off all HTML tags from the specified text.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LONGEST_ENTITY

private static final int LONGEST_ENTITY

The longest an entity can be 10, including the lead & and trail ;

See Also:
SHORTEST_ENTITY, Constant Field Values

SHORTEST_ENTITY

private static final int SHORTEST_ENTITY

The shortest an entity can be 4, including the lead & and trailing ;

See Also:
LONGEST_ENTITY, Constant Field Values

ENTITY_MAPPING

private static Map ENTITY_MAPPING

Maps entity names to their corresponding unicode Characters.


HTML_PATTERN

private static final String HTML_PATTERN

Regular expression pattern to match any HTML tags.

See Also:
stripHTML(String), convertHTML(String), Constant Field Values

EMPTY_STRING

public static final String EMPTY_STRING

Constant for the empty String

See Also:
Constant Field Values

LINE_SEPARATOR

public static final String LINE_SEPARATOR

Constant for the line separator


DEFAULT_DELIMITER

public static final char DEFAULT_DELIMITER

The default delimiter char to use

See Also:
Constant Field Values
Constructor Detail

StringUtils

public StringUtils()
Method Detail

entityToChar

public static char entityToChar(String entity)

Converts the specified entity to a single char.

Parameters:
entity - the entity to convert; it must have lead & and trail ; stripped; may be a x#123 or #123 style entity; works faster if the entity is specified in lower case
Returns:
the equivalent character; 0 if the entity is unknown

convertEntities

public static String convertEntities(String text)

Converts HTML to text converting entities such as " back to " and < back to < Ordinary text passes unchanged.

Parameters:
text - raw text to be processed; must not be null
Returns:
converted text; HTML 4.0 entities such as ♥ { and &x#123;   -> 16 are also handled; null input also returns null

stripHTML

public static String stripHTML(String text)

Strips off all HTML tags from the specified text.

Parameters:
text - the raw text to strip HTML tags from
Returns:
the text with all HTML tags stripped of; if null is specified null is also returned

convertHTML

public static String convertHTML(String text)

Converts the specified HTML input String to pure raw text. All HTML entities are converted and all HTML tags stripped off.

Parameters:
text - the HTML input String to convert; a null value is converted to an empty String
Returns:
the converted raw text
See Also:
convertEntities(String), stripHTML(String)

nullValueToEmptyString

public static String nullValueToEmptyString(String str)

Converts a possible null String object to an empty String object. If the specified value is non null the value is just being trimmed using String.trim()

Parameters:
str - the String object to convert
Returns:
an empty String if the specified value is set to null; otherwise the specified value is being trimmed using String.trim()

isEmpty

public static boolean isEmpty(String str)

Tests if the given String is empty. A String object is considered empty if it is either null or its trimmed length is zero.

Parameters:
str - the String to test
Returns:
true if the given String is empty; false otherwise

abbreviate

public static String abbreviate(String str,
                                int maximumChars)

Abbreviates the specified String if its length exceeds the value specified by maximumChars. The resulting String will then be a substring of str beginning at index 0 and ending at index maximumChars-1 plus a concatenated "..." to show that an abbreviation has been done. If the length of str is below the value specified by maximumChars the input String remains untouched.

Parameters:
str - the String to abbreviate
maximumChars - the maximum characters to allow in the specified input String
Returns:
the abbreviated String

arrayToCSV

public static String arrayToCSV(String[] input,
                                char delimiter)

Converts the specified input to a single String in which the input elements are separated by the given delimiter

Parameters:
input - a String array holding the elements
delimiter - the delimiter char to use
Returns:
a single String that separates the input elements by the given delimiter
See Also:
arrayToCSV(int[], char), arrayToCSV(Point[], char)

arrayToCSV

public static String arrayToCSV(int[] input,
                                char delimiter)

Converts the specified input to a single String in which the input elements are separated by the given delimiter

Parameters:
input - an int array holding the elements
delimiter - the delimiter char to use
Returns:
a single String that separates the input elements by the given delimiter
See Also:
arrayToCSV(String[], char), arrayToCSV(Point[], char)

arrayToCSV

public static String arrayToCSV(Point[] input,
                                char delimiter)

Converts the specified input to a single String in which the input elements are separated by the given delimiter

Parameters:
input - a Point array holding the elements
delimiter - the delimiter char to use
Returns:
a single String that separates the input elements by the given delimiter
See Also:
arrayToCSV(String[], char), arrayToCSV(int[], char)

csvToArray

public static String[] csvToArray(String input,
                                  char delimiter)

Uses the specified delimiter to convert the input to an array of single String elements.

Parameters:
input - a String in which the elements are separated by the given delimiter
delimiter - the delimiter char to use
Returns:
an array of String holding all elements from the given input
See Also:
csvToIntArray(String, char), csvToPointArray(String, char)

csvToIntArray

public static int[] csvToIntArray(String input,
                                  char delimiter)

Uses the specified delimiter to convert the input to an array of single int/code> values.

Parameters:
input - a String in which the elements are separated by the given delimiter
delimiter - the delimiter char to use
Returns:
an array of int values from the given input
See Also:
csvToArray(String, char), csvToPointArray(String, char)

csvToPointArray

public static Point[] csvToPointArray(String input,
                                      char delimiter)

Uses the specified delimiter to convert the input to an array of single Point elements.

Parameters:
input - a String in which the elements are separated by the given delimiter
delimiter - the delimiter char to use
Returns:
an array of Point objects holding all elements from the given input
See Also:
csvToArray(String, char), csvToIntArray(String, char)

potentialEntityToChar

private static char potentialEntityToChar(String entity)

Checks a number of gauntlet conditions to ensure this is a valid entity. Converts the entity to the corresponding char.

Parameters:
entity - String that may hold an entity; lead & must be stripped, but may contain text past the ;
Returns:
corresponding unicode character, or 0 if the entity is invalid