Package gov.nasa.arc.pds.tools.util
Class StrUtils
- java.lang.Object
-
- gov.nasa.arc.pds.tools.util.StrUtils
-
public class StrUtils extends java.lang.Object
This class provides utilities for string formatting.- Author:
- jagander
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
ASCII_CHAR_RANGE
static java.util.regex.Pattern
ASCII_CHARS_REGEX
static int
DEFAULT_STACK_LINES
static int
DEFAULT_TRUNCATE_LENGTH
static java.lang.String
HTML_NEWLINE
static java.lang.String
JS_NEWLINE
static java.util.regex.Pattern
NORMALIZE_WHITE_SPACE_REGEX
static java.util.regex.Pattern
NUMERIC_REGEX
static java.lang.String[]
REGEX_SPECIAL_CHARS
Special characters within a regular expression.static java.util.regex.Pattern
SIMPLE_NUMERIC_REGEX
static java.util.regex.Pattern
STATEMENT_REGEX
static java.util.regex.Pattern
STRIP_ASCII_REGEX
static java.util.regex.Pattern
STRIP_PADDING
static java.util.regex.Pattern
URL_BASE_REGEX
-
Constructor Summary
Constructors Constructor Description StrUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.String
addWildcards(java.lang.String string)
static java.lang.String
dequote(java.lang.String string)
Remove quotes surrounding a string.static java.lang.String
escapeRegex(java.lang.String regexSource)
Escape a string for use in a regular expression.static java.lang.String
getAlternateCaseString(java.lang.String string)
static java.lang.String
getNice(java.lang.String string)
static java.lang.String
getNice(java.lang.String string, java.lang.Boolean capitalize, java.lang.Boolean replaceUnderscore)
static java.lang.String
getNonASCII(java.lang.String string)
Get any and all characters that are not ASCII.static java.lang.String
getNonNull(java.lang.Object... objects)
static java.lang.Number
getNumber(java.lang.String string)
static java.lang.Number
getNumber(java.lang.String string, java.util.Locale locale)
static java.lang.Number
getNumberLoose(java.lang.String string)
static java.lang.String
getURIFilename(java.net.URI uri)
static java.lang.String
getURLBase(java.lang.String string)
Get the base of a url such that you have a url string to the same folder.static java.lang.String
getURLFilename(java.lang.String url)
static java.lang.String
getURLFilename(java.net.URL url)
static boolean
isASCII(java.lang.String string)
Test to see if string contains only ASCII characters.static boolean
isNumber(java.lang.String string)
static java.lang.String
normalize(java.lang.String string)
Normalize a string by trimming and compacting whitespace.static boolean
nullOrEmpty(java.lang.String string)
static java.lang.String
safeJS(java.lang.String string)
Scrub a String for things that make it unsuitable to insert into a JavaScript string.static java.lang.String
stripPadding(java.lang.String string)
static java.lang.String
toSeparatedString(java.lang.Object[] array)
static java.lang.String
toSeparatedString(java.util.List<?> list)
A generic comma separated string representation of a list.static java.util.List<java.lang.String>
toStatements(java.lang.String sql)
static java.lang.String
toString(java.io.InputStream is)
static java.lang.String
toString(java.lang.Exception e, java.lang.Boolean isJS)
static java.lang.String
toString(java.lang.Exception e, java.lang.Boolean isJS, java.lang.Integer lines)
Get a reasonable string representation of an exception.static java.lang.String
toString(java.lang.Object object)
static java.lang.String
toString(java.lang.Object[] list)
static java.lang.String
toString(java.lang.Object[] list, java.lang.Boolean isJS)
static java.lang.String
toString(java.lang.String[] list, java.lang.Boolean isJS)
static java.lang.String
toString(java.util.List<?> list)
The default usage oftoString(List, Boolean)
, outputting in xhtml format.static java.lang.String
toString(java.util.List<?> list, java.lang.Boolean isJS)
A generic string representation of a list.static java.lang.String
toUpper(java.lang.String string)
static java.lang.String
truncate(java.lang.String string)
Truncate a given string to a default length.static java.lang.String
truncate(java.lang.String string, java.lang.Integer length)
Truncate a given string to a provided length.
-
-
-
Field Detail
-
DEFAULT_TRUNCATE_LENGTH
public static final int DEFAULT_TRUNCATE_LENGTH
- See Also:
- Constant Field Values
-
JS_NEWLINE
public static final java.lang.String JS_NEWLINE
- See Also:
- Constant Field Values
-
HTML_NEWLINE
public static final java.lang.String 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
public static final java.util.regex.Pattern NORMALIZE_WHITE_SPACE_REGEX
-
URL_BASE_REGEX
public static final java.util.regex.Pattern URL_BASE_REGEX
-
ASCII_CHAR_RANGE
public static final java.lang.String ASCII_CHAR_RANGE
- See Also:
- Constant Field Values
-
ASCII_CHARS_REGEX
public static final java.util.regex.Pattern ASCII_CHARS_REGEX
-
STRIP_ASCII_REGEX
public static final java.util.regex.Pattern STRIP_ASCII_REGEX
-
NUMERIC_REGEX
public static final java.util.regex.Pattern NUMERIC_REGEX
-
SIMPLE_NUMERIC_REGEX
public static final java.util.regex.Pattern SIMPLE_NUMERIC_REGEX
-
STATEMENT_REGEX
public static final java.util.regex.Pattern STATEMENT_REGEX
-
STRIP_PADDING
public static final java.util.regex.Pattern STRIP_PADDING
-
REGEX_SPECIAL_CHARS
public static final java.lang.String[] 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)
-
-
Method Detail
-
safeJS
public static java.lang.String safeJS(java.lang.String string)
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
public static java.lang.String dequote(java.lang.String string)
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
public static java.lang.String toString(java.util.List<?> list, java.lang.Boolean isJS)
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
public static java.lang.String toString(java.lang.Exception e, java.lang.Boolean isJS, java.lang.Integer lines)
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
public static java.lang.String toString(java.lang.Exception e, java.lang.Boolean isJS)
-
toString
public static java.lang.String toString(java.util.List<?> list)
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
public static java.lang.String toString(java.lang.Object[] list)
-
toString
public static java.lang.String toString(java.lang.Object[] list, java.lang.Boolean isJS)
-
toString
public static java.lang.String toString(java.lang.String[] list, java.lang.Boolean isJS)
-
toString
public static java.lang.String toString(java.io.InputStream is) throws java.io.IOException
- Throws:
java.io.IOException
-
toString
public static java.lang.String toString(java.lang.Object object)
-
escapeRegex
public static java.lang.String escapeRegex(java.lang.String regexSource)
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
public static java.lang.String truncate(java.lang.String string)
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
public static java.lang.String truncate(java.lang.String string, java.lang.Integer length)
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
public static java.lang.String normalize(java.lang.String string)
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
public static java.lang.String getURLBase(java.lang.String string)
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
public static java.lang.String getURLFilename(java.lang.String url)
-
getURIFilename
public static java.lang.String getURIFilename(java.net.URI uri)
-
getURLFilename
public static java.lang.String getURLFilename(java.net.URL url)
-
isASCII
public static boolean isASCII(java.lang.String string)
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
public static java.lang.String getNonASCII(java.lang.String string)
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
public static java.lang.String toSeparatedString(java.util.List<?> list)
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
public static java.lang.String toSeparatedString(java.lang.Object[] array)
-
isNumber
public static boolean isNumber(java.lang.String string)
-
getNumber
public static java.lang.Number getNumber(java.lang.String string, java.util.Locale locale)
-
getNumber
public static java.lang.Number getNumber(java.lang.String string)
-
getNumberLoose
public static java.lang.Number getNumberLoose(java.lang.String string)
-
toStatements
public static java.util.List<java.lang.String> toStatements(java.lang.String sql)
-
nullOrEmpty
public static boolean nullOrEmpty(java.lang.String string)
-
stripPadding
public static java.lang.String stripPadding(java.lang.String string)
-
toUpper
public static java.lang.String toUpper(java.lang.String string)
-
getNice
public static java.lang.String getNice(java.lang.String string, java.lang.Boolean capitalize, java.lang.Boolean replaceUnderscore)
-
getNice
public static java.lang.String getNice(java.lang.String string)
-
addWildcards
public static java.lang.String addWildcards(java.lang.String string)
-
getAlternateCaseString
public static java.lang.String getAlternateCaseString(java.lang.String string)
-
getNonNull
public static java.lang.String getNonNull(java.lang.Object... objects)
-
-