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.ObjectThis class provides utilities for string formatting.- Author:
- jagander
 
- 
- 
Field SummaryFields Modifier and Type Field Description static java.lang.StringASCII_CHAR_RANGEstatic java.util.regex.PatternASCII_CHARS_REGEXstatic intDEFAULT_STACK_LINESstatic intDEFAULT_TRUNCATE_LENGTHstatic java.lang.StringHTML_NEWLINEstatic java.lang.StringJS_NEWLINEstatic java.util.regex.PatternNORMALIZE_WHITE_SPACE_REGEXstatic java.util.regex.PatternNUMERIC_REGEXstatic java.lang.String[]REGEX_SPECIAL_CHARSSpecial characters within a regular expression.static java.util.regex.PatternSIMPLE_NUMERIC_REGEXstatic java.util.regex.PatternSTATEMENT_REGEXstatic java.util.regex.PatternSTRIP_ASCII_REGEXstatic java.util.regex.PatternSTRIP_PADDINGstatic java.util.regex.PatternURL_BASE_REGEX
 - 
Constructor SummaryConstructors Constructor Description StrUtils()
 - 
Method SummaryAll Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.StringaddWildcards(java.lang.String string)static java.lang.Stringdequote(java.lang.String string)Remove quotes surrounding a string.static java.lang.StringescapeRegex(java.lang.String regexSource)Escape a string for use in a regular expression.static java.lang.StringgetAlternateCaseString(java.lang.String string)static java.lang.StringgetNice(java.lang.String string)static java.lang.StringgetNice(java.lang.String string, java.lang.Boolean capitalize, java.lang.Boolean replaceUnderscore)static java.lang.StringgetNonASCII(java.lang.String string)Get any and all characters that are not ASCII.static java.lang.StringgetNonNull(java.lang.Object... objects)static java.lang.NumbergetNumber(java.lang.String string)static java.lang.NumbergetNumber(java.lang.String string, java.util.Locale locale)static java.lang.NumbergetNumberLoose(java.lang.String string)static java.lang.StringgetURIFilename(java.net.URI uri)static java.lang.StringgetURLBase(java.lang.String string)Get the base of a url such that you have a url string to the same folder.static java.lang.StringgetURLFilename(java.lang.String url)static java.lang.StringgetURLFilename(java.net.URL url)static booleanisASCII(java.lang.String string)Test to see if string contains only ASCII characters.static booleanisNumber(java.lang.String string)static java.lang.Stringnormalize(java.lang.String string)Normalize a string by trimming and compacting whitespace.static booleannullOrEmpty(java.lang.String string)static java.lang.StringsafeJS(java.lang.String string)Scrub a String for things that make it unsuitable to insert into a JavaScript string.static java.lang.StringstripPadding(java.lang.String string)static java.lang.StringtoSeparatedString(java.lang.Object[] array)static java.lang.StringtoSeparatedString(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.StringtoString(java.io.InputStream is)static java.lang.StringtoString(java.lang.Exception e, java.lang.Boolean isJS)static java.lang.StringtoString(java.lang.Exception e, java.lang.Boolean isJS, java.lang.Integer lines)Get a reasonable string representation of an exception.static java.lang.StringtoString(java.lang.Object object)static java.lang.StringtoString(java.lang.Object[] list)static java.lang.StringtoString(java.lang.Object[] list, java.lang.Boolean isJS)static java.lang.StringtoString(java.lang.String[] list, java.lang.Boolean isJS)static java.lang.StringtoString(java.util.List<?> list)The default usage oftoString(List, Boolean), outputting in xhtml format.static java.lang.StringtoString(java.util.List<?> list, java.lang.Boolean isJS)A generic string representation of a list.static java.lang.StringtoUpper(java.lang.String string)static java.lang.Stringtruncate(java.lang.String string)Truncate a given string to a default length.static java.lang.Stringtruncate(java.lang.String string, java.lang.Integer length)Truncate a given string to a provided length.
 
- 
- 
- 
Field Detail- 
DEFAULT_TRUNCATE_LENGTHpublic static final int DEFAULT_TRUNCATE_LENGTH - See Also:
- Constant Field Values
 
 - 
JS_NEWLINEpublic static final java.lang.String JS_NEWLINE - See Also:
- Constant Field Values
 
 - 
HTML_NEWLINEpublic static final java.lang.String HTML_NEWLINE - See Also:
- Constant Field Values
 
 - 
DEFAULT_STACK_LINESpublic static final int DEFAULT_STACK_LINES - See Also:
- Constant Field Values
 
 - 
NORMALIZE_WHITE_SPACE_REGEXpublic static final java.util.regex.Pattern NORMALIZE_WHITE_SPACE_REGEX 
 - 
URL_BASE_REGEXpublic static final java.util.regex.Pattern URL_BASE_REGEX 
 - 
ASCII_CHAR_RANGEpublic static final java.lang.String ASCII_CHAR_RANGE - See Also:
- Constant Field Values
 
 - 
ASCII_CHARS_REGEXpublic static final java.util.regex.Pattern ASCII_CHARS_REGEX 
 - 
STRIP_ASCII_REGEXpublic static final java.util.regex.Pattern STRIP_ASCII_REGEX 
 - 
NUMERIC_REGEXpublic static final java.util.regex.Pattern NUMERIC_REGEX 
 - 
SIMPLE_NUMERIC_REGEXpublic static final java.util.regex.Pattern SIMPLE_NUMERIC_REGEX 
 - 
STATEMENT_REGEXpublic static final java.util.regex.Pattern STATEMENT_REGEX 
 - 
STRIP_PADDINGpublic static final java.util.regex.Pattern STRIP_PADDING 
 - 
REGEX_SPECIAL_CHARSpublic 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- 
safeJSpublic 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.
 
 - 
dequotepublic 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
 
 - 
toStringpublic 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 object
- isJS- - flag for whether the output is for javascript or xhtml markup
- Returns:
- a string representation of the list
 
 - 
toStringpublic 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 string
- isJS- - flag to indicate if you want javascript newlines or html newlines
- lines- - number of stacktrace lines to add to representation
- Returns:
- a string representation of an exception
 
 - 
toStringpublic static java.lang.String toString(java.lang.Exception e, java.lang.Boolean isJS)
 - 
toStringpublic 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
 
 - 
toStringpublic static java.lang.String toString(java.lang.Object[] list) 
 - 
toStringpublic static java.lang.String toString(java.lang.Object[] list, java.lang.Boolean isJS)
 - 
toStringpublic static java.lang.String toString(java.lang.String[] list, java.lang.Boolean isJS)
 - 
toStringpublic static java.lang.String toString(java.io.InputStream is) throws java.io.IOException- Throws:
- java.io.IOException
 
 - 
toStringpublic static java.lang.String toString(java.lang.Object object) 
 - 
escapeRegexpublic 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
 
 - 
truncatepublic 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)
 
 - 
truncatepublic 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 necessary
- length- - length to truncate to
- Returns:
- truncated string if longer than specified length + an elipses else the original string
- See Also:
- DEFAULT_TRUNCATE_LENGTH
 
 - 
normalizepublic 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
 
 - 
getURLBasepublic 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
 
 - 
getURLFilenamepublic static java.lang.String getURLFilename(java.lang.String url) 
 - 
getURIFilenamepublic static java.lang.String getURIFilename(java.net.URI uri) 
 - 
getURLFilenamepublic static java.lang.String getURLFilename(java.net.URL url) 
 - 
isASCIIpublic 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
 
 - 
getNonASCIIpublic 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
 
 - 
toSeparatedStringpublic 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
 
 - 
toSeparatedStringpublic static java.lang.String toSeparatedString(java.lang.Object[] array) 
 - 
isNumberpublic static boolean isNumber(java.lang.String string) 
 - 
getNumberpublic static java.lang.Number getNumber(java.lang.String string, java.util.Locale locale)
 - 
getNumberpublic static java.lang.Number getNumber(java.lang.String string) 
 - 
getNumberLoosepublic static java.lang.Number getNumberLoose(java.lang.String string) 
 - 
toStatementspublic static java.util.List<java.lang.String> toStatements(java.lang.String sql) 
 - 
nullOrEmptypublic static boolean nullOrEmpty(java.lang.String string) 
 - 
stripPaddingpublic static java.lang.String stripPadding(java.lang.String string) 
 - 
toUpperpublic static java.lang.String toUpper(java.lang.String string) 
 - 
getNicepublic static java.lang.String getNice(java.lang.String string, java.lang.Boolean capitalize, java.lang.Boolean replaceUnderscore)
 - 
getNicepublic static java.lang.String getNice(java.lang.String string) 
 - 
addWildcardspublic static java.lang.String addWildcards(java.lang.String string) 
 - 
getAlternateCaseStringpublic static java.lang.String getAlternateCaseString(java.lang.String string) 
 - 
getNonNullpublic static java.lang.String getNonNull(java.lang.Object... objects) 
 
- 
 
-