Package ca.cgjennings.io
Class EscapedTextCodec
- java.lang.Object
-
- ca.cgjennings.io.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 tos
, 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.
-
-
-
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 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
s
but will appropriate escapes - Throws:
java.lang.NullPointerException
- iss
isnull
- 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 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|BACKSLASH
would produced escaped output similar to a.properties
file. IfescapeFlags
is zero, the original input is returned.- Parameters:
s
- the string to escapeUnicodeescapeFlags
- optional flags for the escapeUnicode process- Returns:
- a string equivalent to
s
but will appropriate escapes - Throws:
java.lang.NullPointerException
- iss
isnull
-
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
- ifs
isnull
-
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
- ifs
isnull
-
main
public static void main(java.lang.String[] args)
-
-