private String escapeJava(String str, boolean unicode) { CharSequenceTranslator tr = new LookupTranslator(new String[][] { { "\"", "\\\"" }, { "\\", "\\\\" } }) .with(new CharSequenceTranslator[] { new LookupTranslator( EntityArrays.JAVA_CTRL_CHARS_ESCAPE()) }); if (unicode) tr = tr.with(new CharSequenceTranslator[] { UnicodeEscaper .outsideOf(32, 127) }); return tr.translate(str); }
/** * Unescapes special entity char sequences like < to its UTF-8 representation. * All ISO-8859-1, HTML4 and Basic entities will be translated. * * @param text the text that will be unescaped * @return the unescaped version of the string text */ public static String unescapeEntities(String text) { CharSequenceTranslator iso = new LookupTranslator(EntityArrays.ISO8859_1_UNESCAPE()); CharSequenceTranslator basic = new LookupTranslator(EntityArrays.BASIC_UNESCAPE()); //CharSequenceTranslator html4 = new LookupTranslator(EntityArrays.HTML40_EXTENDED_UNESCAPE()); return StringEscapeUtils.unescapeHtml4(iso.translate(basic.translate(text))); }