Package ca.cgjennings.io
Class EscapedTextCodec
- java.lang.Object
-
- ca.cgjennings.io.EscapedTextCodec
-
public final class EscapedTextCodec extends java.lang.ObjectThis 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 intASSIGNMENTEscape the key/value assignment characters '=' and ':'.static intBACKSLASHEscape the back slash character.static intINITIAL_COMMENTEscape an initial '#' or '!'.static intSPACEEscape spaces (just space characters, not other whitespace).static intUNICODEUse Unicode escapes for characters outside of ISO8859-1.static intUSE_S_ESCAPEWhen 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 intWHITESPACEEscape 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.Stringescape(java.lang.CharSequence s, int escapeFlags)Escape a string by inserting backslash escapeUnicode sequences similar to the handling of escapes in.propertiesfiles.static java.lang.StringescapeUnicode(java.lang.CharSequence s)Escape a string by inserting Unicode escapeUnicode sequences for characters outside of the printable ISO 8859-1 range.static booleanisWrappedLine(java.lang.CharSequence s)Tests whether a line of text ends with a line wrapping back slash.static voidmain(java.lang.String[] args)static java.lang.Stringunescape(java.lang.CharSequence s)Returns a string equivalent tos, but with all escapeUnicode sequences converted back to regular (unescaped) characters.static java.lang.StringunescapeUnicode(java.lang.CharSequence s)Returns a string with Unicode escapes converted to normal characters.
-
-
-
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:
trueif 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 toescapeUnicode( s, UNICODE ). If no escapeUnicode codes are inserted, the original input is returned. (If it is aString, the same string is returned; otherwise it is converted to a string by calling itstoString()method.)- Parameters:
s- the string to escapeUnicode- Returns:
- a string equivalent to
sbut will appropriate escapes - Throws:
java.lang.NullPointerException- issisnull- 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.propertiesfiles. If no escapeUnicode codes are inserted, the original input is returned. (If it is aString, the same string is returned; otherwise it is converted to a string by calling itstoString()method.)The set of characters to be escaped is controlled by
escapeFlags, which should be a binary or of some combination of null null nullASSIGNMENT,BACKSLASH,INITIAL_COMMENT,SPACE,USE_S_ESCAPE,UNICODE, andWHITESPACE. For example, the combinationUNICODE|WHITESPACE|BACKSLASHwould produced escaped output similar to a.propertiesfile. IfescapeFlagsis zero, the original input is returned.- Parameters:
s- the string to escapeUnicodeescapeFlags- optional flags for the escapeUnicode process- Returns:
- a string equivalent to
sbut will appropriate escapes - Throws:
java.lang.NullPointerException- issisnull
-
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- ifsisnull
-
unescape
public static java.lang.String unescape(java.lang.CharSequence s)
Returns a string equivalent tos, 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- ifsisnull
-
main
public static void main(java.lang.String[] args)
-
-