Package gov.nasa.arc.pds.tools.util
Class StrUtils
java.lang.Object
gov.nasa.arc.pds.tools.util.StrUtils
public class StrUtils extends Object
This class provides utilities for string formatting.
- Author:
- jagander
-
Field Summary
Fields Modifier and Type Field Description static StringASCII_CHAR_RANGEstatic PatternASCII_CHARS_REGEXstatic intDEFAULT_STACK_LINESstatic intDEFAULT_TRUNCATE_LENGTHstatic StringHTML_NEWLINEstatic StringJS_NEWLINEstatic PatternNORMALIZE_WHITE_SPACE_REGEXstatic PatternNUMERIC_REGEXstatic String[]REGEX_SPECIAL_CHARSSpecial characters within a regular expression.static PatternSIMPLE_NUMERIC_REGEXstatic PatternSTATEMENT_REGEXstatic PatternSTRIP_ASCII_REGEXstatic PatternSTRIP_PADDINGstatic PatternURL_BASE_REGEX -
Constructor Summary
Constructors Constructor Description StrUtils() -
Method Summary
Modifier and Type Method Description static StringaddWildcards(String string)static Stringdequote(String string)Remove quotes surrounding a string.static StringescapeRegex(String regexSource)Escape a string for use in a regular expression.static StringgetAlternateCaseString(String string)static StringgetNice(String string)static StringgetNice(String string, Boolean capitalize, Boolean replaceUnderscore)static StringgetNonASCII(String string)Get any and all characters that are not ASCII.static StringgetNonNull(Object... objects)static NumbergetNumber(String string)static NumbergetNumber(String string, Locale locale)static NumbergetNumberLoose(String string)static StringgetURIFilename(URI uri)static StringgetURLBase(String string)Get the base of a url such that you have a url string to the same folder.static StringgetURLFilename(String url)static StringgetURLFilename(URL url)static booleanisASCII(String string)Test to see if string contains only ASCII characters.static booleanisNumber(String string)static Stringnormalize(String string)Normalize a string by trimming and compacting whitespace.static booleannullOrEmpty(String string)static StringsafeJS(String string)Scrub a String for things that make it unsuitable to insert into a JavaScript string.static StringstripPadding(String string)static StringtoSeparatedString(Object[] array)static StringtoSeparatedString(List<?> list)A generic comma separated string representation of a list.static List<String>toStatements(String sql)static StringtoString(InputStream is)static StringtoString(Exception e, Boolean isJS)static StringtoString(Exception e, Boolean isJS, Integer lines)Get a reasonable string representation of an exception.static StringtoString(Object object)static StringtoString(Object[] list)static StringtoString(Object[] list, Boolean isJS)static StringtoString(String[] list, Boolean isJS)static StringtoString(List<?> list)The default usage oftoString(List, Boolean), outputting in xhtml format.static StringtoString(List<?> list, Boolean isJS)A generic string representation of a list.static StringtoUpper(String string)static Stringtruncate(String string)Truncate a given string to a default length.static Stringtruncate(String string, Integer length)Truncate a given string to a provided length.
-
Field Details
-
DEFAULT_TRUNCATE_LENGTH
public static final int DEFAULT_TRUNCATE_LENGTH- See Also:
- Constant Field Values
-
JS_NEWLINE
- See Also:
- Constant Field Values
-
HTML_NEWLINE
- See Also:
- Constant Field Values
-
DEFAULT_STACK_LINES
public static final int DEFAULT_STACK_LINES- See Also:
- Constant Field Values
-
NORMALIZE_WHITE_SPACE_REGEX
-
URL_BASE_REGEX
-
ASCII_CHAR_RANGE
- See Also:
- Constant Field Values
-
ASCII_CHARS_REGEX
-
STRIP_ASCII_REGEX
-
NUMERIC_REGEX
-
SIMPLE_NUMERIC_REGEX
-
STATEMENT_REGEX
-
STRIP_PADDING
-
REGEX_SPECIAL_CHARS
Special characters within a regular expression. Listed here so that variable strings that may be used in a regular expression match can be escaped.- See Also:
escapeRegex(String)
-
-
Constructor Details
-
StrUtils
public StrUtils()
-
-
Method Details
-
safeJS
Scrub a String for things that make it unsuitable to insert into a JavaScript string. Modify the string as necessary to make it safe but preserve display.Currently this only addresses non-escaped single quotes, double quotes and non-meaningful backslashes. Other issues will be addressed as discovered.
- Parameters:
string- - the string to be cleaned.- Returns:
- The cleaned string.
-
dequote
Remove quotes surrounding a string. This is useful in cases that you are retrieving strings from a file and each value is quoted.- Parameters:
string- - the string to remove surrounding quotes from- Returns:
- original string minus surrounding quotes
-
toString
A generic string representation of a list. It just calls the toString method on each item and inserts a xhtml break or newline between the items. What type of newline to use is dependent on the flag for whether the output is for javascript.- Parameters:
list- - a list of objects with a usable toString() on each objectisJS- - flag for whether the output is for javascript or xhtml markup- Returns:
- a string representation of the list
-
toString
Get a reasonable string representation of an exception. "Reasonable" is either the exception message or exception name plus some number of lines of the stacktrace.- Parameters:
e- - exception to convert to stringisJS- - flag to indicate if you want javascript newlines or html newlineslines- - number of stacktrace lines to add to representation- Returns:
- a string representation of an exception
-
toString
-
toString
The default usage oftoString(List, Boolean), outputting in xhtml format.- Parameters:
list- - a list of objects with a usable toString() on each object- Returns:
- a string representation of the list
-
toString
-
toString
-
toString
-
toString
- Throws:
IOException
-
toString
-
escapeRegex
Escape a string for use in a regular expression. This is useful when a block of text is to be used in a regular expression match.- Parameters:
regexSource- - source string to escape- Returns:
- an escaped version of the string for use in a regular expression
- See Also:
REGEX_SPECIAL_CHARS
-
truncate
Truncate a given string to a default length.- Parameters:
string- - string to truncate if necessary- Returns:
- truncated string if longer than specified length + an elipses else the original string
- See Also:
DEFAULT_TRUNCATE_LENGTH,truncate(String, Integer)
-
truncate
Truncate a given string to a provided length.Note that the returned string may be longer than the specified length due to the addition of an elipses.
Note that truncate length is defaulted if null
- Parameters:
string- - string to truncate if necessarylength- - length to truncate to- Returns:
- truncated string if longer than specified length + an elipses else the original string
- See Also:
DEFAULT_TRUNCATE_LENGTH
-
normalize
Normalize a string by trimming and compacting whitespace.- Parameters:
string- - string to normalize- Returns:
- a trimmed and whitespace-compressed version of the string
- See Also:
NORMALIZE_WHITE_SPACE_REGEX
-
getURLBase
Get the base of a url such that you have a url string to the same folder. This is not particularly useful to a restful interface.Note that this requires the url string passed in to be a valid url. Typical usage is to get the base from the current URL. In that case, the url will always be valid.
- Parameters:
string- - url to get base of.- Returns:
- The base of the passed in url string.
- See Also:
URL_BASE_REGEX
-
getURLFilename
-
getURIFilename
-
getURLFilename
-
isASCII
Test to see if string contains only ASCII characters.Note that '^', '*' and '$' match the empty string in the regex and the single occurrence of the empty string token can't do triple duty. We return true for an empty string explicitly rather than using the regex.
- Parameters:
string- - string to test for non-ascii chars.- Returns:
- boolean indicating if the string is all ASCII chars
- See Also:
ASCII_CHARS_REGEX
-
getNonASCII
Get any and all characters that are not ASCII.- Parameters:
string- - string to pull non-ASCII characters from- Returns:
- string containing all non-ASCII chars
- See Also:
STRIP_ASCII_REGEX
-
toSeparatedString
A generic comma separated string representation of a list. It just calls the toString method on each item and inserts a comma between the items.- Parameters:
list- - a list of objects with a usable toString() on each object- Returns:
- a comma separated string representation of the list
-
toSeparatedString
-
isNumber
-
getNumber
-
getNumber
-
getNumberLoose
-
toStatements
-
nullOrEmpty
-
stripPadding
-
toUpper
-
getNice
-
getNice
-
addWildcards
-
getAlternateCaseString
-
getNonNull
-