Class XmlEscaper

java.lang.Object
org.cthing.escapers.XmlEscaper

public final class XmlEscaper extends Object
Escapes strings and character arrays using XML 1.0 entities. For example, calling the escape(CharSequence, Option...) with the string "this & that" produces the string "this & that". Characters are escaped according to the following table:
Character Escaping
Code Point Escape Description
0x09 Unchanged Tab
0x0A Unchanged Line feed
0x0D Unchanged Carriage return
0x20 - 0x21 Unchanged Printable ASCII characters
0x22 " Double quote
0x23 - 0x25 Unchanged Printable ASCII characters
0x26 & Ampersand
0x27 ' Single quote
0x28 - 0x3B Unchanged Printable ASCII characters
0x3C < Less than
0x3D Unchanged Printable ASCII characters
0x3E > Greater than
0x3F - 0x7E Unchanged Printable ASCII characters
0x7F Numeric character entity (e.g. ) ASCII control character
0x80 - 0xFF Unchanged by default or numeric character entity (e.g. „) if XmlEscaper.Option.ESCAPE_NON_ASCII specified ISO Latin-1
0x100 - 0xD7FF Unchanged by default or numeric character entity (e.g. ) if XmlEscaper.Option.ESCAPE_NON_ASCII specified Unicode BMP
0xE000 - 0xFFFD Unchanged by default or numeric character entity (e.g. ) if XmlEscaper.Option.ESCAPE_NON_ASCII specified Unicode BMP
0x10000 - 0x10FFFF Unchanged by default or numeric character entity (e.g. 𐀀) if XmlEscaper.Option.ESCAPE_NON_ASCII specified Unicode surrogate pairs

The characters in the above table correspond to the valid XML characters. Invalid characters are not included in the output (e.g. the null character 0x00 is dropped).

  • Method Details

    • escape

      public static String escape(CharSequence charSequence, XmlEscaper.Option... options)
      Applies XML escaping to the specified string.
      Parameters:
      charSequence - String 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(CharSequence charSequence, Set<XmlEscaper.Option> options)
      Applies XML escaping to the specified string.
      Parameters:
      charSequence - String 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

      @WillNotClose public static void escape(CharSequence charSequence, Writer writer, XmlEscaper.Option... options) throws IOException
      Applies XML escaping to the specified string and writes the result to the specified writer. Note that invalid XML characters are not included in the output.
      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<XmlEscaper.Option> options) throws IOException
      Applies XML escaping to the specified string and writes the result to the specified writer. Note that invalid XML characters are not included in the output.
      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, XmlEscaper.Option... options)
      Applies XML 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, XmlEscaper.Option... options)
      Applies XML 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<XmlEscaper.Option> options)
      Applies XML 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<XmlEscaper.Option> options)
      Applies XML 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, XmlEscaper.Option... options) throws IOException
      Applies XML escaping to the specified character array and writes the result to the specified writer. Note that invalid XML characters are not included in the output.
      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, XmlEscaper.Option... options) throws IOException
      Applies XML escaping to the specified character array and writes the result to the specified writer. Note that invalid XML characters are not included in the output.
      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<XmlEscaper.Option> options) throws IOException
      Applies XML escaping to the specified character array and writes the result to the specified writer. Note that invalid XML characters are not included in the output.
      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<XmlEscaper.Option> options) throws IOException
      Applies XML escaping to the specified character array and writes the result to the specified writer. Note that invalid XML characters are not included in the output.
      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