/** * Unescapes any Java literals found in the <code>String</code>. For example, it will turn a * sequence of <code>'\'</code> and <code>'n'</code> into a newline character, unless the <code> * '\'</code> is preceded by another <code>'\'</code>. * * @param str the <code>String</code> to unescape, may be null * @return a new unescaped <code>String</code>, <code>null</code> if null string input */ public static String unescapeJava(String str) { if (str == null) { return null; } try { StringPrintWriter writer = new StringPrintWriter(str.length()); unescapeJava(writer, str); return writer.getString(); } catch (IOException ioe) { // this should never ever happen while writing to a StringWriter ioe.printStackTrace(); return null; } }
private static String escapeJavaStyleString(String str, boolean escapeSingleQuotes) { if (str == null) { return null; } try { StringPrintWriter writer = new StringPrintWriter(str.length() * 2); escapeJavaStyleString(writer, str, escapeSingleQuotes); return writer.getString(); } catch (IOException ioe) { // this should never ever happen while writing to a StringWriter ioe.printStackTrace(); return null; } }