Class EscapedTextCodec


  • public final class EscapedTextCodec
    extends java.lang.Object
    This class provides static methods for encoding/decoding Unicode, newline, return, and tab escapes using the same format as property files. The class neither inserts nor removes the possible trailing backslash that indicates that a line should be concatenated with the following line. Valid escapeUnicode sequences consist of a backslash (\) followed by any of:
     uXXXX   insert Unicode character U+XXXX (where XXXX is a 16-bit hexadecimal number)
     n       newline
     r       return
     f       form feed
     t       tab
     s       space
             (a slash followed by a space) space
     \       backslash
     :       :
     =       =
     #       #
     !       !
     
    Since:
    3.0
    Author:
    Chris Jennings
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int ASSIGNMENT
      Escape the key/value assignment characters '=' and ':'.
      static int BACKSLASH
      Escape the back slash character.
      static int INITIAL_COMMENT
      Escape an initial '#' or '!'.
      static int SPACE
      Escape spaces (just space characters, not other whitespace).
      static int UNICODE
      Use Unicode escapes for characters outside of ISO8859-1.
      static int USE_S_ESCAPE
      When spaces are escaped, this uses "\s" for space instead of "\ "; this is not valid for .properties files, but for other types of files it produces clearer output.
      static int WHITESPACE
      Escape the non-space whitespace characters of newline, return, tab, and form feed.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String escape​(java.lang.CharSequence s, int escapeFlags)
      Escape a string by inserting backslash escapeUnicode sequences similar to the handling of escapes in .properties files.
      static java.lang.String escapeUnicode​(java.lang.CharSequence s)
      Escape a string by inserting Unicode escapeUnicode sequences for characters outside of the printable ISO 8859-1 range.
      static boolean isWrappedLine​(java.lang.CharSequence s)
      Tests whether a line of text ends with a line wrapping back slash.
      static void main​(java.lang.String[] args)  
      static java.lang.String unescape​(java.lang.CharSequence s)
      Returns a string equivalent to s, but with all escapeUnicode sequences converted back to regular (unescaped) characters.
      static java.lang.String unescapeUnicode​(java.lang.CharSequence s)
      Returns a string with Unicode escapes converted to normal characters.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • UNICODE

        public static final int UNICODE
        Use Unicode escapes for characters outside of ISO8859-1.
        See Also:
        Constant Field Values
      • SPACE

        public static final int SPACE
        Escape spaces (just space characters, not other whitespace).
        See Also:
        Constant Field Values
      • USE_S_ESCAPE

        public static final int USE_S_ESCAPE
        When spaces are escaped, this uses "\s" for space instead of "\ "; this is not valid for .properties files, but for other types of files it produces clearer output.
        See Also:
        Constant Field Values
      • ASSIGNMENT

        public static final int ASSIGNMENT
        Escape the key/value assignment characters '=' and ':'.
        See Also:
        Constant Field Values
      • INITIAL_COMMENT

        public static final int INITIAL_COMMENT
        Escape an initial '#' or '!'.
        See Also:
        Constant Field Values
      • WHITESPACE

        public static final int WHITESPACE
        Escape the non-space whitespace characters of newline, return, tab, and form feed.
        See Also:
        SPACE, Constant Field Values
      • BACKSLASH

        public static final int BACKSLASH
        Escape the back slash character.
        See Also:
        Constant Field Values
    • Method Detail

      • isWrappedLine

        public static boolean isWrappedLine​(java.lang.CharSequence s)
        Tests whether a line of text ends with a line wrapping back slash. Some file formats indicate that a long line is wrapped onto the next line by ending the line with a back slash. This method will check for this character. It correctly handles cases where the line actually ends in an escaped back slash.
        Parameters:
        s - the line to test
        Returns:
        true if the line ends in a line wrapping escapeUnicode
      • escapeUnicode

        public static java.lang.String escapeUnicode​(java.lang.CharSequence s)
        Escape a string by inserting Unicode escapeUnicode sequences for characters outside of the printable ISO 8859-1 range. This is equivalent to escapeUnicode( s, UNICODE ). If no escapeUnicode codes are inserted, the original input is returned. (If it is a String, the same string is returned; otherwise it is converted to a string by calling its toString() method.)
        Parameters:
        s - the string to escapeUnicode
        Returns:
        a string equivalent to s but will appropriate escapes
        Throws:
        java.lang.NullPointerException - is s is null
        See Also:
        escapeUnicode(java.lang.CharSequence)
      • escape

        public static java.lang.String escape​(java.lang.CharSequence s,
                                              int escapeFlags)
        Escape a string by inserting backslash escapeUnicode sequences similar to the handling of escapes in .properties files. If no escapeUnicode codes are inserted, the original input is returned. (If it is a String, the same string is returned; otherwise it is converted to a string by calling its toString() method.)

        The set of characters to be escaped is controlled by escapeFlags, which should be a binary or of some combination of null null null ASSIGNMENT, BACKSLASH, INITIAL_COMMENT, SPACE, USE_S_ESCAPE, UNICODE, and WHITESPACE. For example, the combination UNICODE|WHITESPACE|BACKSLASH would produced escaped output similar to a .properties file. If escapeFlags is zero, the original input is returned.

        Parameters:
        s - the string to escapeUnicode
        escapeFlags - optional flags for the escapeUnicode process
        Returns:
        a string equivalent to s but will appropriate escapes
        Throws:
        java.lang.NullPointerException - is s is null
      • unescapeUnicode

        public static java.lang.String unescapeUnicode​(java.lang.CharSequence s)
        Returns a string with Unicode escapes converted to normal characters. If the input does not contain any Unicode escapeUnicode sequences, it is returned unchanged.
        Parameters:
        s - the string to convert
        Returns:
        the unescaped string
        Throws:
        java.lang.NullPointerException - if s is null
      • unescape

        public static java.lang.String unescape​(java.lang.CharSequence s)
        Returns a string equivalent to s, but with all escapeUnicode sequences converted back to regular (unescaped) characters. If the input does not contain any escapeUnicode sequences, it is returned unchanged.
        Parameters:
        s - the string to convert
        Returns:
        the unescaped string
        Throws:
        java.lang.NullPointerException - if s is null
      • main

        public static void main​(java.lang.String[] args)