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 Details

    • 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)
  • Constructor Details

    • StrUtils

      public StrUtils()
  • Method Details

    • 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 object
      isJS - - 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 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
    • toString

      public static String toString​(Exception e, Boolean isJS)
    • toString

      public static String toString​(List<?> list)
      The default usage of toString(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​(Object[] list)
    • toString

      public static String toString​(Object[] list, Boolean isJS)
    • toString

      public static String toString​(String[] list, Boolean isJS)
    • toString

      public static String toString​(InputStream is) throws IOException
      Throws:
      IOException
    • toString

      public static String toString​(Object object)
    • 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 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
    • 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
    • getURLFilename

      public static String getURLFilename​(String url)
    • getURIFilename

      public static String getURIFilename​(URI uri)
    • getURLFilename

      public static String getURLFilename​(URL url)
    • 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
    • toSeparatedString

      public static String toSeparatedString​(Object[] array)
    • isNumber

      public static boolean isNumber​(String string)
    • getNumber

      public static Number getNumber​(String string, Locale locale)
    • getNumber

      public static Number getNumber​(String string)
    • getNumberLoose

      public static Number getNumberLoose​(String string)
    • toStatements

      public static List<String> toStatements​(String sql)
    • nullOrEmpty

      public static boolean nullOrEmpty​(String string)
    • stripPadding

      public static String stripPadding​(String string)
    • toUpper

      public static String toUpper​(String string)
    • getNice

      public static String getNice​(String string, Boolean capitalize, Boolean replaceUnderscore)
    • getNice

      public static String getNice​(String string)
    • addWildcards

      public static String addWildcards​(String string)
    • getAlternateCaseString

      public static String getAlternateCaseString​(String string)
    • getNonNull

      public static String getNonNull​(Object... objects)