Package org.cthing.escapers
Class YamlEscaper
java.lang.Object
org.cthing.escapers.YamlEscaper
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:
| 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 |
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic voidescape(char[] charArr, int offset, int length, Writer writer, Set<YamlEscaper.Option> options) Applies YAML escaping to the specified character array and writes the result to the specified writer.static voidescape(char[] charArr, int offset, int length, Writer writer, YamlEscaper.Option... options) Applies YAML escaping to the specified character array and writes the result to the specified writer.static Stringescape(char[] charArr, int offset, int length, Set<YamlEscaper.Option> options) Applies YAML escaping to the specified character array.static Stringescape(char[] charArr, int offset, int length, YamlEscaper.Option... options) Applies YAML escaping to the specified character array.static voidescape(char[] charArr, Writer writer, Set<YamlEscaper.Option> options) Applies YAML escaping to the specified character array and writes the result to the specified writer.static voidescape(char[] charArr, Writer writer, YamlEscaper.Option... options) Applies YAML escaping to the specified character array and writes the result to the specified writer.static Stringescape(char[] charArr, Set<YamlEscaper.Option> options) Applies YAML escaping to the specified character array.static Stringescape(char[] charArr, YamlEscaper.Option... options) Applies YAML escaping to the specified character array.static voidescape(CharSequence charSequence, Writer writer, Set<YamlEscaper.Option> options) Applies YAML escaping to the specified string and writes the result to the specified writer.static voidescape(CharSequence charSequence, Writer writer, YamlEscaper.Option... options) Applies YAML escaping to the specified string and writes the result to the specified writer.static Stringescape(CharSequence charSequence, Set<YamlEscaper.Option> options) Applies YAML escaping to the specified string.static Stringescape(CharSequence charSequence, YamlEscaper.Option... options) Applies YAML escaping to the specified string.
-
Method Details
-
escape
Applies YAML escaping to the specified string.- Parameters:
charSequence- String to escapeoptions- Escaping options- Returns:
- Escaped string or
nullifnullwas passed in.
-
escape
Applies YAML escaping to the specified string.- Parameters:
charSequence- String to escapeoptions- Escaping options- Returns:
- Escaped string or
nullifnullwas 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 escapewriter- Writer to which the escaped string is writtenoptions- Escaping options- Throws:
IOException- if there was a problem writing the escaped stringIllegalArgumentException- if the writer isnull
-
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 escapewriter- Writer to which the escaped string is writtenoptions- Escaping options- Throws:
IOException- if there was a problem writing the escaped stringIllegalArgumentException- if the writer isnull
-
escape
Applies YAML escaping to the specified character array.- Parameters:
charArr- Character array to escapeoptions- Escaping options- Returns:
- Escaped string or
nullifnullwas passed in. Note that invalid XML characters are not included in the output.
-
escape
Applies YAML escaping to the specified character array.- Parameters:
charArr- Character array to escapeoffset- Start index in arraylength- Number of characters in arrayoptions- Escaping options- Returns:
- Escaped string or
nullifnullwas passed in. Note that invalid XML characters are not included in the output.
-
escape
Applies YAML escaping to the specified character array.- Parameters:
charArr- Character array to escapeoptions- Escaping options- Returns:
- Escaped string or
nullifnullwas 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 escapeoffset- Start index in arraylength- Number of characters in arrayoptions- Escaping options- Returns:
- Escaped string or
nullifnullwas 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 escapewriter- Writer to which the escaped string is writtenoptions- Escaping options- Throws:
IOException- if there was a problem writing the escaped stringIllegalArgumentException- if the writer isnull
-
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 escapeoffset- Start index in arraylength- Number of characters in arraywriter- Writer to which the escaped string is writtenoptions- Escaping options- Throws:
IOException- if there was a problem writing the escaped stringIllegalArgumentException- if the writer isnull
-
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 escapewriter- Writer to which the escaped string is writtenoptions- Escaping options- Throws:
IOException- if there was a problem writing the escaped stringIllegalArgumentException- if the writer isnull
-
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 escapeoffset- Start index in arraylength- Number of characters in arraywriter- Writer to which the escaped string is writtenoptions- Escaping options- Throws:
IOException- if there was a problem writing the escaped stringIllegalArgumentException- if the writer isnull
-