Class YamlEscaper

java.lang.Object
org.cthing.escapers.YamlEscaper

public final class YamlEscaper extends Object
Escapes strings and character arrays according to the Escaped Characters section of the YAML Ain’t Markup Language specification. The returned string may be surrounded by single or double quotes depending on the characters found in the string.

The returned string will be surrounded by double quotes under the following conditions:

  • The string contains a character that requires escaping
  • The string contains a double quote, single quote or backslash

The returned string will be surrounded by single quotes under the following conditions

  • The string does not require double quotes
  • The string is empty
  • The string contains leading or trailing spaces
  • The string begins with the document start character sequence
  • The string end with the document end character sequence
  • The string contains any of the following characters: # , [ ] { } & * ! | > % @ ? : - /

If none of the above conditions apply, the string is returned without quotes.

Characters are escaped according to the following table:

Character Escaping
Code Point Escape Description
0x00 \0 Null
0x01 - 0x06 \xXX ASCII control characters
0x07 \a Bell
0x08 \b Backspace
0x09 \t Tab
0x0A \n Line feed
0x0B \v Vertical tab
0x0C \f Form feed
0x0D \r Carriage return
0x0E - 0x1F \xXX ASCII control characters
0x20 - 0x21 Unchanged Printable ASCII characters
0x22 \" Double quote
0x23 - 0x2E Unchanged Printable ASCII characters
0x2F \/ Forward slash
0x30 - 0x5B Unchanged Printable ASCII characters
0x5C \\ Backslash
0x5D - 0x7E Unchanged Printable ASCII characters
0x7F \xXX ASCII control characters
0x80 - 0x84 \xXX Escaped 8-bit hex code
0x85 \N Next line
0x86 - 0x9F \xXX ISO Latin-1
0xA0 \_ Non-breaking space
0xA1 - 0xFF \xXX ISO Latin-1
0x100 - 0x2027 \uXXXX Unicode BMP
0x2028 \L Line separator
0x2029 \P Paragraph separator
0x202A - 0xFFFF \uXXXX Unicode BMP
0x10000 - 0x10FFFF \UXXXXXXXX Unicode surrogate pairs
  • Method Details

    • escape

      public static String escape(CharSequence charSequence, YamlEscaper.Option... options)
      Applies YAML escaping to the specified string.
      Parameters:
      charSequence - String to escape
      options - Escaping options
      Returns:
      Escaped string or null if null was passed in.
    • escape

      public static String escape(CharSequence charSequence, Set<YamlEscaper.Option> options)
      Applies YAML escaping to the specified string.
      Parameters:
      charSequence - String to escape
      options - Escaping options
      Returns:
      Escaped string or null if null was passed in.
    • escape

      @WillNotClose public static void escape(CharSequence charSequence, Writer writer, YamlEscaper.Option... options) throws IOException
      Applies YAML escaping to the specified string and writes the result to the specified writer.
      Parameters:
      charSequence - String to escape
      writer - Writer to which the escaped string is written
      options - Escaping options
      Throws:
      IOException - if there was a problem writing the escaped string
      IllegalArgumentException - if the writer is null
    • escape

      @WillNotClose public static void escape(CharSequence charSequence, Writer writer, Set<YamlEscaper.Option> options) throws IOException
      Applies YAML escaping to the specified string and writes the result to the specified writer.
      Parameters:
      charSequence - String to escape
      writer - Writer to which the escaped string is written
      options - Escaping options
      Throws:
      IOException - if there was a problem writing the escaped string
      IllegalArgumentException - if the writer is null
    • escape

      public static String escape(char[] charArr, YamlEscaper.Option... options)
      Applies YAML escaping to the specified character array.
      Parameters:
      charArr - Character array to escape
      options - Escaping options
      Returns:
      Escaped string or null if null was passed in. Note that invalid XML characters are not included in the output.
    • escape

      public static String escape(char[] charArr, int offset, int length, YamlEscaper.Option... options)
      Applies YAML escaping to the specified character array.
      Parameters:
      charArr - Character array to escape
      offset - Start index in array
      length - Number of characters in array
      options - Escaping options
      Returns:
      Escaped string or null if null was passed in. Note that invalid XML characters are not included in the output.
    • escape

      public static String escape(char[] charArr, Set<YamlEscaper.Option> options)
      Applies YAML escaping to the specified character array.
      Parameters:
      charArr - Character array to escape
      options - Escaping options
      Returns:
      Escaped string or null if null was passed in. Note that invalid XML characters are not included in the output.
    • escape

      public static String escape(char[] charArr, int offset, int length, Set<YamlEscaper.Option> options)
      Applies YAML escaping to the specified character array.
      Parameters:
      charArr - Character array to escape
      offset - Start index in array
      length - Number of characters in array
      options - Escaping options
      Returns:
      Escaped string or null if null was passed in. Note that invalid XML characters are not included in the output.
    • escape

      @WillNotClose public static void escape(char[] charArr, Writer writer, YamlEscaper.Option... options) throws IOException
      Applies YAML escaping to the specified character array and writes the result to the specified writer.
      Parameters:
      charArr - Character array to escape
      writer - Writer to which the escaped string is written
      options - Escaping options
      Throws:
      IOException - if there was a problem writing the escaped string
      IllegalArgumentException - if the writer is null
    • escape

      @WillNotClose public static void escape(char[] charArr, int offset, int length, Writer writer, YamlEscaper.Option... options) throws IOException
      Applies YAML escaping to the specified character array and writes the result to the specified writer.
      Parameters:
      charArr - Character array to escape
      offset - Start index in array
      length - Number of characters in array
      writer - Writer to which the escaped string is written
      options - Escaping options
      Throws:
      IOException - if there was a problem writing the escaped string
      IllegalArgumentException - if the writer is null
    • escape

      @WillNotClose public static void escape(char[] charArr, Writer writer, Set<YamlEscaper.Option> options) throws IOException
      Applies YAML escaping to the specified character array and writes the result to the specified writer.
      Parameters:
      charArr - Character array to escape
      writer - Writer to which the escaped string is written
      options - Escaping options
      Throws:
      IOException - if there was a problem writing the escaped string
      IllegalArgumentException - if the writer is null
    • escape

      @WillNotClose public static void escape(char[] charArr, int offset, int length, Writer writer, Set<YamlEscaper.Option> options) throws IOException
      Applies YAML escaping to the specified character array and writes the result to the specified writer.
      Parameters:
      charArr - Character array to escape
      offset - Start index in array
      length - Number of characters in array
      writer - Writer to which the escaped string is written
      options - Escaping options
      Throws:
      IOException - if there was a problem writing the escaped string
      IllegalArgumentException - if the writer is null