Enum TextEncoding

java.lang.Object
java.lang.Enum<TextEncoding>
org.rapidcontext.core.data.TextEncoding
All Implemented Interfaces:
Serializable, Comparable<TextEncoding>

public enum TextEncoding extends Enum<TextEncoding>
A text encoding/escaping helper. Always encodes to printable ASCII (7-bit) characters, making it possible to use the output directly.
Version:
1.0
  • Enum Constant Details

    • NONE

      public static final TextEncoding NONE
      No encoding (i.e. raw pass-through)
    • ASCII

      public static final TextEncoding ASCII
      ASCII printable encoding. Replaces any non-printable ASCII (i.e not 7-bit ASCII) or control characters with spaces.
    • PROPERTIES

      public static final TextEncoding PROPERTIES
      Java properties encoding. Replaces any non-printable ASCII (i.e not 7-bit ASCII) characters with Unicode escape sequences.
    • JSON

      public static final TextEncoding 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

      public static final TextEncoding XML
      XML string encoding. This encoding escapes all non-printable ASCII (i.e not 7-bit ASCII) characters with numeric XML entities.
    • URL

      public static final TextEncoding 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

      public static TextEncoding[] values()
      Returns an array containing the constants of this enum type, in the order they are declared.
      Returns:
      an array containing the constants of this enum type, in the order they are declared
    • valueOf

      public static TextEncoding valueOf(String name)
      Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (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 type has no constant with the specified name
      NullPointerException - if the argument is null
    • encode

      public static String encode(TextEncoding encoding, String str)
      Encodes a string with the specified encoding.
      Parameters:
      encoding - the encoding to use
      str - the text to encode
      Returns:
      the encoded text string, or an empty string if input was null
    • encodeAscii

      public static String encodeAscii(String str, boolean linebreaks)
      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 encode
      linebreaks - the flag for preserving line breaks
      Returns:
      the encoded ASCII text string, or an empty string if the input was null
    • encodeProperty

      public static String encodeProperty(String str, boolean linebreaks)
      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 encode
      linebreaks - the flag for preserving line breaks
      Returns:
      the encoded property text string, or an empty string if the input was null
    • encodeJson

      public static String encodeJson(String str)
      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

      public static String encodeJsonString(String str)
      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

      public static String encodeXml(String str, boolean linebreaks)
      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 encode
      linebreaks - the flag for preserving line breaks
      Returns:
      the encoded XML string, or an empty string if the input was null
    • encodeUrl

      public static String encodeUrl(String str)
      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