Class StringTools

java.lang.Object
io.goobi.viewer.controller.StringTools

public final class StringTools extends Object

StringTools class.

  • Field Details

    • REGEX_QUOTATION_MARKS

      public static final String REGEX_QUOTATION_MARKS
      Constant REGEX_QUOTATION_MARKS="\"[^()]*?\"".
      See Also:
    • REGEX_PARENTHESES

      public static final String REGEX_PARENTHESES
      Constant REGEX_PARENTHESES="\\([^()]*\\)".
      See Also:
    • REGEX_PARENTESES_DATES

      public static final String REGEX_PARENTESES_DATES
      Constant REGEX_PARENTESES_DATES="\\([\\w|\\s|\\-|\\.|\\?]+\\)".
      See Also:
    • REGEX_BRACES

      public static final String REGEX_BRACES
      Constant REGEX_BRACES="\\{(\\w+)\\}".
      See Also:
    • REGEX_WORDS

      public static final String REGEX_WORDS
      Constant REGEX_WORDS="[a-zäáàâöóòôüúùûëéèêßñ0123456789]+".
      See Also:
    • DEFAULT_ENCODING

      public static final String DEFAULT_ENCODING
      Constant DEFAULT_ENCODING="UTF-8".
    • SLASH_REPLACEMENT

      public static final String SLASH_REPLACEMENT
      Constant SLASH_REPLACEMENT="U002F".
      See Also:
    • BACKSLASH_REPLACEMENT

      public static final String BACKSLASH_REPLACEMENT
      Constant BACKSLASH_REPLACEMENT="U005C".
      See Also:
    • PIPE_REPLACEMENT

      public static final String PIPE_REPLACEMENT
      Constant PIPE_REPLACEMENT="U007C".
      See Also:
    • QUESTION_MARK_REPLACEMENT

      public static final String QUESTION_MARK_REPLACEMENT
      Constant QUESTION_MARK_REPLACEMENT="U003F".
      See Also:
    • PERCENT_REPLACEMENT

      public static final String PERCENT_REPLACEMENT
      Constant PERCENT_REPLACEMENT="U0025".
      See Also:
    • PLUS_REPLACEMENT

      public static final String PLUS_REPLACEMENT
      Constant PLUS_REPLACEMENT="U0025".
      See Also:
  • Method Details

    • encodeUrl

      public static String encodeUrl(String string)
      Escape url for submitted form data. A space is encoded as '+'.
      Parameters:
      string - String to encode
      Returns:
      URL-encoded string
    • encodeUrl

      public static String encodeUrl(String string, boolean escapeCriticalUrlCharacters)

      encodeUrl.

      Parameters:
      string - String to encode
      escapeCriticalUrlCharacters - If true, slashes etc. will be manually escaped prior to URL encoding
      Returns:
      URL-encoded string
    • decodeUrl

      public static String decodeUrl(String string)

      decodeUrl.

      Parameters:
      string - a String object.
      Returns:
      a String object.
    • findFirstMatch

      public static Optional<String> findFirstMatch(String text, String regex, int group)
      Finds the first String matching a regex within another string and return it as an Optional.
      Parameters:
      text - The String in which to search
      regex - The regex to search for
      group - a int.
      Returns:
      An optional containing the first String within the text matched by regex, or an empty optional if no match was found
    • findBestMatch

      public static String findBestMatch(String s, List<String> candidates, String language)
      Parameters:
      s - String to match
      candidates - List of Strings with possible matches
      language - Language for scoring; English will be used, if no Locale can be matched for given language
      Returns:
      Best matching string; null if none found
    • escapeHtmlChars

      public static String escapeHtmlChars(String str)
      Escapes special HTML characters in the given string.
      Parameters:
      str - a String object.
      Returns:
      a String object.
    • escapeHtmlLtGt

      public static String escapeHtmlLtGt(String str)
      Escapes <> in the given string.
      Parameters:
      str - a String object.
      Returns:
      a String object.
    • removeDiacriticalMarks

      public static String removeDiacriticalMarks(String s)
      Removed diacritical marks from each letter in the given String.
      Parameters:
      s - a String object.
      Returns:
      String without diacritical marks
    • replaceCharacterVariants

      public static String replaceCharacterVariants(String text)
    • removeLineBreaks

      public static String removeLineBreaks(String s, String replaceWith)
      Removes regular and HTML line breaks from the given String.
      Parameters:
      s - a String object.
      replaceWith - a String object.
      Returns:
      String without line breaks
    • stripJS

      public static String stripJS(String s)

      stripJS.

      Parameters:
      s -
      Returns:
      String sans any script-tag blocks
    • stripPatternBreakingChars

      public static String stripPatternBreakingChars(String s)
      Use this method to log user-controller variables that may contain pattern-breaking characters such as line breaks and tabs.
      Parameters:
      s - String to clean
      Returns:
      String sans any logger pattern-breaking characters
    • getLength

      public static int getLength(String s)
      Return the length of the given string, or 0 if the string is null.
      Parameters:
      s - a String object.
      Returns:
      the length of the string if it exists, 0 otherwise
    • escapeHtml

      public static String escapeHtml(String text)
      Escapes the given string. Uses StringEscapeUtils.escapeHtml4(String) and additionally converts all line breaks (\r\n, \r, \n) to html line breaks (<br/> )
      Parameters:
      text - the text to escape
      Returns:
      the escaped string
    • escapeQuotes

      public static String escapeQuotes(String s)

      escapeQuotes.

      Parameters:
      s - a String object.
      Returns:
      a String object.
    • getCharset

      public static String getCharset(String input)
      Parameters:
      input -
      Returns:
      Charset of the given input
    • convertStringEncoding

      public static String convertStringEncoding(String string, String from, String to)
      Converts a String from one given encoding to the other.
      Parameters:
      string - The string to convert.
      from - Source encoding.
      to - Destination encoding.
      Returns:
      The converted string.
    • isStringUrlEncoded

      public static boolean isStringUrlEncoded(String s, String charset) throws UnsupportedEncodingException
      Checks whether given string already contains URL-encoded characters.
      Parameters:
      s - String to check
      charset - Charset for URL decoding
      Returns:
      true if decoded string differs from original; false otherwise
      Throws:
      UnsupportedEncodingException
    • escapeCriticalUrlChracters

      public static String escapeCriticalUrlChracters(String value, boolean escapePercentCharacters)

      escapeCriticalUrlChracters.

      Parameters:
      value - a String object.
      escapePercentCharacters - a boolean.
      Returns:
      a String object.
    • unescapeCriticalUrlChracters

      public static String unescapeCriticalUrlChracters(String value)

      unescapeCriticalUrlChracters.

      Parameters:
      value - a String object.
      Returns:
      a String object.
    • isImageUrl

      public static boolean isImageUrl(String url)

      isImageUrl.

      Parameters:
      url - a String object.
      Returns:
      true if this is an image URL; false otherwise
    • renameIncompatibleCSSClasses

      public static String renameIncompatibleCSSClasses(String html)
      Renames CSS classes that start with digits in the given html code due to Chrome ignoring such classes.
      Parameters:
      html - The HTML to fix
      Returns:
      Same HTML document but with Chrome-compatible CSS class names
    • getHierarchyForCollection

      public static List<String> getHierarchyForCollection(String collection, String split)

      getHierarchyForCollection.

      Parameters:
      collection - a String object.
      split - a String object.
      Returns:
      List of string containing every (sub-)collection name
    • normalizeWebAnnotationCoordinates

      public static String normalizeWebAnnotationCoordinates(String coords)
      Normalizes WebAnnotation coordinates for rectangle rendering (x,y,w,h -> minX,minY,maxX,maxY).
      Parameters:
      coords - a String object.
      Returns:
      Legacy format coordinates
    • getMatch

      public static String getMatch(String s, String pattern)

      getMatch.

      Parameters:
      s - a String object.
      pattern - a String object.
      Returns:
      a String object.
    • intern

      public static String intern(String string)

      intern.

      Parameters:
      string - a String object.
      Returns:
      a String object.
    • generateHash

      public static String generateHash(String myString)
      Creates an hash of the given String using SHA-256.
      Parameters:
      myString - a String object.
      Returns:
      generated hash
    • appendTrailingSlash

      public static String appendTrailingSlash(String path)
      Parameters:
      path -
      Returns:
      Given path with a trailing slash, if not yet present
    • removeTrailingSlashes

      public static String removeTrailingSlashes(String path)
    • removeQuotations

      public static String removeQuotations(String s)
      Parameters:
      s -
      Returns:
      Given string without quotation marks, or same string if not in quotation marks
    • checkValueEmptyOrInverted

      public static boolean checkValueEmptyOrInverted(String value)
      Parameters:
      value -
      Returns:
      true if value null, empty or starts with 0x1; false otherwise
    • filterStringsViaRegex

      public static List<String> filterStringsViaRegex(List<String> values, String regex)
      Parameters:
      values - All values to check
      regex -
      Returns:
      List of values that match regex
    • parseInt

      public static Optional<Integer> parseInt(String s)
      Try to parse the given string as integer.
      Parameters:
      s - the string to parse
      Returns:
      An Optional containing the parsed int. If the string is blank or cannot be parsed to an integer, an empty Optional is returned
    • getIntegerRange

      public static int[] getIntegerRange(String inRange)
    • cleanUserGeneratedData

      public static String cleanUserGeneratedData(String data)
      Clean a String from any malicious content like script tags, line breaks and backtracking filepaths. TODO InvalidPathException in Windows
      Parameters:
      data -
      Returns:
      a cleaned up string which can be savely used
    • sortByList

      public static int sortByList(String v1, String v2, List<String> sorting)
    • convertToSingleWord

      public static String convertToSingleWord(String text, int maxLength, String whitespaceReplacement)
    • replaceAllMatches

      public static String replaceAllMatches(String string, String matchRegex, Function<List<String>,String> replacer)
    • truncateText

      public static String truncateText(String text, int maxLength)
      Returns a truncated version of the given text which is no longer that maxLength. If possible the truncated text ends between words. If it is shorter than the original text, '...' is appended at the end (these count towards the maxLength, i.e. the actual text has at most maxLength-3 characters)
      Parameters:
      text - the text to truncate
      maxLength - maximal length of the returned text
      Returns:
      the truncated text or null if the input was null