Class StringEncoder
-
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
Decode a string encoded byencode(java.lang.String, boolean)
.static String
Dequote a string previously enquoted byenquote(java.lang.String)
.static String
Encode a string, escaping control and XML-invalid characters.static String
enquote
(byte[] data, int off, int len) Enquote bytes, treating them as an ASCII string.static String
Enquote a string.static int
enquotedLength
(String string) Determine the length of a string previously enquoted byenquote(java.lang.String)
when it appears as the prefix of a longer string.static boolean
isValidXMLChar
(int codepoint) Determine if the given character is a valid XML character according to the XML 1.0 specification.
-
Field Details
-
Method Details
-
encode
Encode a string, escaping control and XML-invalid characters. Whether tab, newline, and carriage return are escaped is optional; these are the three control characters that are valid inside XML documents.Characters are escaped using
\uNNNN
notation like Java unicode characters, e.g.,0x001f
would appear in the encoded string as\u001f
. Normal Java backslash escapes are used for tab, newline, carriage return, backspace, and formfeed. Backslash characters are themselves encoded with a double backslash.- Parameters:
value
- string to encode (possibly null)escapeTABNLCR
- escape tab, newline, and carriage return characters as well- Returns:
- the encoded version of
value
, ornull
ifvalue
wasnull
- See Also:
-
decode
Decode a string encoded byencode(java.lang.String, boolean)
.The parsing is strict; any ill-formed backslash escape sequence (i.e., not of the form
\uNNNN
,\b
,\t
,\n
,\f
,\r
or\\
) will cause an exception to be thrown.- Parameters:
text
- string to decode (possibly null)- Returns:
- the decoded version of
text
, ornull
iftext
wasnull
- Throws:
IllegalArgumentException
- iftext
contains an invalid escape sequence- See Also:
-
enquote
Enquote a string. Functions likeencode(string, true)
but in addition the resulting string is surrounded by double quotes and double quotes in the string are backslash-escaped.Note: the strings returned by this method are not suitable for decoding by
decode(java.lang.String)
. Usedequote(java.lang.String)
instead.- Parameters:
string
- string to enquote- Returns:
- enquoted string, or the string
null
(not quoted) ifstring
is null - Throws:
IllegalArgumentException
- ifstring
is null
-
enquote
Enquote bytes, treating them as an ASCII string.- Parameters:
data
- ascii character bufferoff
- starting offset indata
len
- number of characters indata
- Returns:
- enquoted string
- Throws:
IllegalArgumentException
- ifdata
is nullIndexOutOfBoundsException
- ifoff
and/orlen
is invalid- See Also:
-
dequote
Dequote a string previously enquoted byenquote(java.lang.String)
.- Parameters:
quotedString
- a string returned byenquote(java.lang.String)
- Returns:
- original unquoted string
- Throws:
IllegalArgumentException
- ifquotedString
has an invalid format (i.e., it could not have ever been returned byenquote(java.lang.String)
)
-
enquotedLength
Determine the length of a string previously enquoted byenquote(java.lang.String)
when it appears as the prefix of a longer string. This method assumes that the prefix is a valid quoted string; usedequote(java.lang.String)
to verify.- Parameters:
string
- a string containing a prefix returned byenquote(java.lang.String)
- Returns:
- length of
string
when enquoted - Throws:
IllegalArgumentException
- if a starting or terminating quote character is not found
-
isValidXMLChar
public static boolean isValidXMLChar(int codepoint) Determine if the given character is a valid XML character according to the XML 1.0 specification.Valid characters are tab, newline, carriage return, and characters in the ranges
0x0020 - 0xd7ff
,0xe000 - 0xffdf
, and0x10000 - 0x10ffff
.- Parameters:
codepoint
- character codepoint- Returns:
- true if
codepoint
is a valid XML character - See Also:
-