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