Class StringEncoder
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic StringDecode a string encoded byencode(java.lang.String, boolean).static StringDequote a string previously enquoted byenquote(java.lang.String).static StringEncode a string, escaping control and XML-invalid characters.static Stringenquote(byte[] data, int off, int len) Enquote bytes, treating them as an ASCII string.static StringEnquote a string.static intenquotedLength(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 booleanisValidXMLChar(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
\uNNNNnotation like Java unicode characters, e.g.,0x001fwould 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, ornullifvaluewasnull - 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,\ror\\) will cause an exception to be thrown.- Parameters:
text- string to decode (possibly null)- Returns:
- the decoded version of
text, ornulliftextwasnull - Throws:
IllegalArgumentException- iftextcontains 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) ifstringis null - Throws:
IllegalArgumentException- ifstringis null
-
enquote
Enquote bytes, treating them as an ASCII string.- Parameters:
data- ascii character bufferoff- starting offset indatalen- number of characters indata- Returns:
- enquoted string
- Throws:
IllegalArgumentException- ifdatais nullIndexOutOfBoundsException- ifoffand/orlenis 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- ifquotedStringhas 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
stringwhen 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
codepointis a valid XML character - See Also:
-