Package org.rapidcontext.core.data
Enum Class TextEncoding
- All Implemented Interfaces:
Serializable
,Comparable<TextEncoding>
,Constable
A text encoding/escaping helper. Always encodes to printable ASCII
(7-bit) characters, making it possible to use the output directly.
- Version:
- 1.0
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Enum
Enum.EnumDesc<E extends Enum<E>>
-
Enum Constant Summary
Enum Constants -
Method Summary
Modifier and TypeMethodDescriptionstatic String
encode
(TextEncoding encoding, String str) Encodes a string with the specified encoding.static String
encodeAscii
(String str, boolean linebreaks) Encodes a string to ASCII text.static String
encodeJson
(String str) Encodes a string into a JSON string literal.static String
encodeJsonString
(String str) Encodes a string into a JSON string literal.static String
encodeProperty
(String str, boolean linebreaks) Encodes a string to a escaped property value.static String
Encodes a string for URLs or form data, i.e.static String
Encodes a string to properly escaped XML.static TextEncoding
Returns the enum constant of this class with the specified name.static TextEncoding[]
values()
Returns an array containing the constants of this enum class, in the order they are declared.
-
Enum Constant Details
-
NONE
No encoding (i.e. raw pass-through) -
ASCII
ASCII printable encoding. Replaces any non-printable ASCII (i.e not 7-bit ASCII) or control characters with spaces. -
PROPERTIES
Java properties encoding. Replaces any non-printable ASCII (i.e not 7-bit ASCII) characters with Unicode escape sequences. -
JSON
JSON string encoding. Escapes newlines, tabs, backslashes and double quotes. Also, any character outside the range 0x20-0x7f will be replaced with a Unicode escape sequence. -
XML
XML string encoding. This encoding escapes all non-printable ASCII (i.e not 7-bit ASCII) characters with numeric XML entities. -
URL
The "application/x-www-form-urlencoded" encoding, i.e. for URLs and form data. Replaces any non-printable ASCII (i.e not 7-bit ASCII) characters with numeric UTF-8 %-sequences.
-
-
Method Details
-
values
Returns an array containing the constants of this enum class, in the order they are declared.- Returns:
- an array containing the constants of this enum class, in the order they are declared
-
valueOf
Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)- Parameters:
name
- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
IllegalArgumentException
- if this enum class has no constant with the specified nameNullPointerException
- if the argument is null
-
encode
Encodes a string with the specified encoding.- Parameters:
encoding
- the encoding to usestr
- the text to encode- Returns:
- the encoded text string, or an empty string if input was null
-
encodeAscii
Encodes a string to ASCII text. The output will only contain printable ASCII with an option to keep newlines. All other characters will be converted to spaces.- Parameters:
str
- the text to encodelinebreaks
- the flag for preserving line breaks- Returns:
- the encoded ASCII text string, or an empty string if the input was null
-
encodeProperty
Encodes a string to a escaped property value. The output will only contain printable ASCII with an option to keep newlines. All other characters will be converted to Unicode escape sequences.- Parameters:
str
- the text to encodelinebreaks
- the flag for preserving line breaks- Returns:
- the encoded property text string, or an empty string if the input was null
-
encodeJson
Encodes a string into a JSON string literal. The output will only contain printable ASCII, with other characters converted to Unicode escape sequences.- Parameters:
str
- the text to encode- Returns:
- the encoded JSON string
-
encodeJsonString
Encodes a string into a JSON string literal. The output will only contain printable ASCII, with other characters converted to Unicode escape sequences.- Parameters:
str
- the text to encode, or null- Returns:
- the encoded JSON string, or "null" if null
-
encodeXml
Encodes a string to properly escaped XML. The output will only contain printable ASCII with an option to keep newlines. All other characters will be converted to numeric XML entities.- Parameters:
str
- the text to encodelinebreaks
- the flag for preserving line breaks- Returns:
- the encoded XML string, or an empty string if the input was null
-
encodeUrl
Encodes a string for URLs or form data, i.e. the MIME type "application/x-www-form-urlencoded". The output will only contain printable ASCII. All other characters will be converted to numeric UTF-8 %-sequences.- Parameters:
str
- the text to encode- Returns:
- the encoded string, or an empty string if the input was null
-