Class HtmlEscaper

java.lang.Object
org.cthing.escapers.HtmlEscaper

public final class HtmlEscaper extends Object
Escapes string and character arrays using numeric and named HTML character entities. Markup-significant characters (e.g. <) are always escaped. Options are provided to escape additional characters using numeric or named character entities. 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 &quot; Double quote
0x23 - 0x25 Unchanged Printable ASCII characters
0x26 &amp; Ampersand
0x27 &#x27; Single quote
0x28 - 0x3B Unchanged Printable ASCII characters
0x3C &lt; Less than
0x3D Unchanged Printable ASCII characters
0x3E &gt; Greater than
0x3F - 0x7E Unchanged Printable ASCII characters
0x7F Numeric character entity (e.g. &#x7F;) ASCII control character
0x80 - 0xFF Unchanged by default or numeric character entity (e.g. &#x84;) if HtmlEscaper.Option.ESCAPE_NON_ASCII specified, or named character entity (e.g. &reg;) if HtmlEscaper.Option.USE_ISO_LATIN_1_ENTITIES specified. ISO Latin-1
0x100 - 0xD7FF Unchanged by default or numeric character entity (e.g. &#x7F;) if HtmlEscaper.Option.ESCAPE_NON_ASCII specified, or named character entity (e.g. &delta;) if HtmlEscaper.Option.USE_HTML4_EXTENDED_ENTITIES specified. Unicode BMP
0xE000 - 0xFFFD Unchanged by default or numeric character entity (e.g. &#xE000;) if HtmlEscaper.Option.ESCAPE_NON_ASCII specified Unicode BMP
0x10000 - 0x10FFFF Unchanged by default or numeric character entity (e.g. &#x10000;) if HtmlEscaper.Option.ESCAPE_NON_ASCII specified Unicode surrogate pairs

Characters not listed in the above table are not included in the output (e.g. the null character 0x00 is dropped).

  • Method Details

    • escape

      public static String escape(CharSequence charSequence, HtmlEscaper.Option... options)
      Applies HTML 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<HtmlEscaper.Option> options)
      Applies HTML 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, HtmlEscaper.Option... options) throws IOException
      Applies HTML 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<HtmlEscaper.Option> options) throws IOException
      Applies HTML 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, HtmlEscaper.Option... options)
      Applies HTML 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.
    • escape

      public static String escape(char[] charArr, int offset, int length, HtmlEscaper.Option... options)
      Applies HTML 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.
    • escape

      public static String escape(char[] charArr, Set<HtmlEscaper.Option> options)
      Applies HTML 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.
    • escape

      public static String escape(char[] charArr, int offset, int length, Set<HtmlEscaper.Option> options)
      Applies HTML 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.
    • escape

      @WillNotClose public static void escape(char[] charArr, Writer writer, HtmlEscaper.Option... options) throws IOException
      Applies HTML 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, HtmlEscaper.Option... options) throws IOException
      Applies HTML 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<HtmlEscaper.Option> options) throws IOException
      Applies HTML 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<HtmlEscaper.Option> options) throws IOException
      Applies HTML 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