Class TextUtils

java.lang.Object
org.archive.util.TextUtils

public class TextUtils extends Object
  • Constructor Details

    • TextUtils

      public TextUtils()
  • Method Details

    • getMatcher

      public static Matcher getMatcher(String pattern, CharSequence input)
      Get a matcher object for a precompiled regex pattern. This method tries to reuse Matcher objects for efficiency. It can hold for recycling one Matcher per pattern per thread. Matchers retrieved should be returned for reuse via the recycleMatcher() method, but no errors will occur if they are not. This method is a hotspot frequently accessed.
      Parameters:
      pattern - the string pattern to use
      input - the character sequence the matcher should be using
      Returns:
      a matcher object loaded with the submitted character sequence
    • recycleMatcher

      public static void recycleMatcher(Matcher m)
    • replaceAll

      public static String replaceAll(String pattern, CharSequence input, String replacement)
      Utility method using a precompiled pattern instead of using the replaceAll method of the String class. This method will also be reusing Matcher objects.
      Parameters:
      pattern - precompiled Pattern to match against
      input - the character sequence to check
      replacement - the String to substitute every match with
      Returns:
      the String with all the matches substituted
      See Also:
    • replaceFirst

      public static String replaceFirst(String pattern, CharSequence input, String replacement)
      Utility method using a precompiled pattern instead of using the replaceFirst method of the String class. This method will also be reusing Matcher objects.
      Parameters:
      pattern - precompiled Pattern to match against
      input - the character sequence to check
      replacement - the String to substitute the first match with
      Returns:
      the String with the first match substituted
      See Also:
    • matches

      public static boolean matches(String pattern, CharSequence input)
      Utility method using a precompiled pattern instead of using the matches method of the String class. This method will also be reusing Matcher objects.
      Parameters:
      pattern - precompiled Pattern to match against
      input - the character sequence to check
      Returns:
      true if character sequence matches
      See Also:
    • split

      public static String[] split(String pattern, CharSequence input)
      Utility method using a precompiled pattern instead of using the split method of the String class.
      Parameters:
      pattern - precompiled Pattern to split by
      input - the character sequence to split
      Returns:
      array of Strings split by pattern
      See Also:
    • getFirstWord

      public static String getFirstWord(String s)
      Parameters:
      s - String to find first word in (Words are delimited by whitespace).
      Returns:
      First word in the passed string else null if no word found.
    • escapeForHTMLJavascript

      public static String escapeForHTMLJavascript(String s)
      Escapes a string so that it can be passed as an argument to a javscript in a JSP page. This method takes a string and returns the same string with any single quote escaped by prepending the character with a backslash. Linebreaks are also replaced with '\n'. Also, less-than signs and ampersands are replaced with HTML entities.
      Parameters:
      s - The string to escape
      Returns:
      The same string escaped.
    • escapeForMarkupAttribute

      public static String escapeForMarkupAttribute(String s)
      Escapes a string so that it can be placed inside XML/HTML attribute. Replaces ampersand, less-than, greater-than, single-quote, and double-quote with escaped versions.
      Parameters:
      s - The string to escape
      Returns:
      The same string escaped.
    • escapeForHTML

      public static String escapeForHTML(String s)
      Minimally escapes a string so that it can be placed inside XML/HTML attribute. Escapes lt and amp.
      Parameters:
      s - The string to escape
      Returns:
      The same string escaped.
    • writeEscapedForHTML

      public static void writeEscapedForHTML(String s, Writer w) throws IOException
      Utility method for writing a (potentially large) String to a JspWriter, escaping it for HTML display, without constructing another large String of the whole content.
      Parameters:
      s - String to write
      w - destination Writer
      Throws:
      IOException
    • unescapeHtml

      public static CharSequence unescapeHtml(CharSequence cs)
      Replaces HTML Entity Encodings.
      Parameters:
      cs - The CharSequence to remove html codes from
      Returns:
      the same CharSequence or an escaped String.
    • exceptionToString

      public static String exceptionToString(String message, Throwable e)
      Parameters:
      message - Message to put at top of the string returned. May be null.
      e - Exception to write into a string.
      Returns:
      Return formatted string made of passed message and stack trace of passed exception.
    • urlEscape

      public static String urlEscape(String s)
      Exception- and warning-free URL-escaping utility method.
      Parameters:
      s - String to escape
      Returns:
      URL-escaped string
    • urlUnescape

      public static String urlUnescape(String s)
      Exception- and warning-free URL-unescaping utility method.
      Parameters:
      s - String do unescape
      Returns:
      URL-unescaped String