HerdCharacterEscapeHandler implements CharacterEscapeHandler { @Override public void escape(char[] buffer, int start, int length, boolean isAttributeValue, Writer outputWriter) throws IOException { for (int i = start; i < start + length; i++) { char ch = buffer[i]; if (ch == '&') { outputWriter.write("&amp;"); continue; } if (ch == '<') { outputWriter.write("&lt;"); continue; } if (ch == '>') { outputWriter.write("&gt;"); continue; } if (ch == '"' && isAttributeValue) { outputWriter.write("&quot;"); continue; } if (ch == '\'' && isAttributeValue) { outputWriter.write("&apos;"); continue; } if (isXml11RestrictedCharacter(ch)) { outputWriter.write("&#x"); outputWriter.write(Integer.toHexString(ch)); outputWriter.write(";"); continue; } outputWriter.write(ch); } }  @Override void escape(char[] buffer, int start, int length, boolean isAttributeValue, Writer outputWriter); static boolean isXml11RestrictedCharacter(char ch);  }
@Test public void testEscapeNoAttribute() throws Exception { StringWriter writer = new StringWriter(); escapeHandler.escape(TEST_BUFFER.toCharArray(), 0, TEST_BUFFER.length(), false, writer); assertEquals("A&lt;&gt;&amp;\"'&#x1;\t", writer.toString()); }
@Test public void testEscapeAttribute() throws Exception { StringWriter writer = new StringWriter(); escapeHandler.escape(TEST_BUFFER.toCharArray(), 0, TEST_BUFFER.length(), true, writer); assertEquals("A&lt;&gt;&amp;&quot;&apos;&#x1;\t", writer.toString()); }
@Test public void testEscapeSubBuffer() throws Exception { StringWriter writer = new StringWriter(); escapeHandler.escape(TEST_BUFFER.toCharArray(), 1, 2, true, writer); assertEquals("&lt;&gt;", writer.toString()); }