StringEscaper { @SuppressWarnings("checkstyle:magicnumber") public String escape(CharSequence text) { final StringBuilder result = new StringBuilder(); for (int i = 0; i < text.length(); ++i) { final char c = text.charAt(i); final String cs = Character.toString(c); if (this.escapeCharacters.contains(cs)) { result.append(this.toEscapeCharacter); result.append(cs); } else { final String special = this.specialChars.get(cs); if (special != null) { result.append(special); } else if (c < this.minValidChar || c > this.maxValidChar) { if (this.maxValidChar > 0) { result.append("\\u"); result.append(formatHex(c, 4)); } } else { result.append(cs); } } } return result.toString(); } StringEscaper();  StringEscaper(CharSequence toEscapeCharacter, CharSequence... escapeCharacters); void setSpecialChars(String[][] chars); void setValidCharRange(int minValidChar, int maxValidChar); @SuppressWarnings("checkstyle:magicnumber") String escape(CharSequence text); @Pure static String formatHex(int amount, int digits); static final String[][] JAVA_SPECIAL_CHARS; static final String JAVA_ESCAPE_CHAR; static final String JAVA_STRING_CHAR; static final String JSON_SPECIAL_ESCAPED_CHAR; static final int JAVA_MIN_CHAR; static final int JAVA_MAX_CHAR; }
@Test public void escape_01() { StringEscaper escaper = new StringEscaper(); assertEquals("abc", escaper.escape("abc")); }
@Test public void escape_02() { StringEscaper escaper = new StringEscaper(); assertEquals("a\\nb\\tc", escaper.escape("a\nb\tc")); }
@Test public void escape_03() { StringEscaper escaper = new StringEscaper(); assertEquals("a\\\\nbc\\\\T", escaper.escape("a\\nbc\\T")); }
@Test public void escape_04() { StringEscaper escaper = new StringEscaper(); assertEquals("ab/c", escaper.escape("ab/c")); }
@Test public void escape_05() { StringEscaper escaper = new StringEscaper(); assertEquals("ab\\\"c", escaper.escape("ab\"c")); }
@Test public void escape_06() { StringEscaper escaper = new StringEscaper(); assertEquals("ab'c", escaper.escape("ab'c")); }
@Test public void escape_07() { StringEscaper escaper = new StringEscaper(); assertEquals("ab\\u0004c", escaper.escape("ab\u0004c")); }
@Test public void escape_08() { StringEscaper escaper = new StringEscaper(); assertEquals("ab\\\\u0004c", escaper.escape("ab\\u0004c")); }