StringUtils { public static String escapeHtml(String str) { int len = str.length(); char[] s = str.toCharArray(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < len; i += 1) { char c = s[i]; if ((c == '\\') || (c == '"') || (c == '/')) { sb.append('\\'); sb.append(c); } else if (c == '\b') { sb.append("\\b"); } else if (c == '\t') { sb.append("\\t"); } else if (c == '\n') { sb.append("<br>"); } else if (c == '\f') { sb.append("\\f"); } else if (c == '\r') { sb.append("\\r"); } else if (c == '>') { sb.append("&gt;"); } else if (c == '<') { sb.append("&lt;"); } else if (c == '&') { sb.append("&amp;"); } else if (c < ' ') { } else { sb.append(c); } } return sb.toString(); } private  StringUtils(); static String stringifyException(final Throwable e); static String byteToHexString(final byte[] bytes, final int start, final int end); static String byteToHexString(final byte[] bytes); static byte[] hexStringToByte(final String hex); static String escapeHtml(String str); static String arrayAwareToString(Object o); static String arrayToString(Object array); static String showControlCharacters(String str); static String getRandomString(Random rnd, int minLength, int maxLength); static String getRandomString(Random rnd, int minLength, int maxLength, char minValue, char maxValue); static void writeNullableString(String str, DataOutputView out); static String readNullableString(DataInputView in);  }
@Test public void testEscapeHTML() { String testString = "\b \t / \n \f \r <default>"; String controlString = StringUtils.escapeHtml(testString); assertEquals("\\b \\t \\/ <br> \\f \\r &lt;default&gt;", controlString); }