/** * <p>Escapes the characters in a <code>String</code> using Java String rules to * a <code>Writer</code>.</p> * <p/> * <p>A <code>null</code> string input has no effect.</p> * * @param out Writer to write escaped string into * @param str String to escape values in, may be null * @throws IllegalArgumentException if the Writer is <code>null</code> * @throws IOException if error occurs on underlying Writer * @see #escapeJava(java.lang.String) */ public static void escapeJava(Writer out, String str) throws IOException { escapeJavaStyleString(out, str, false, false); }
/** * <p>Escapes the characters in a <code>String</code> using JavaScript String rules * to a <code>Writer</code>.</p> * <p/> * <p>A <code>null</code> string input has no effect.</p> * * @param out Writer to write escaped string into * @param str String to escape values in, may be null * @throws IllegalArgumentException if the Writer is <code>null</code> * @throws IOException if error occurs on underlying Writer * @see #escapeJavaScript(java.lang.String) */ public static void escapeJavaScript(Writer out, String str) throws Exception { escapeJavaStyleString(out, str, true, true); }
/** * <p>Escapes the characters in a <code>String</code> using Java String rules.</p> * <p/> * <p>Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.) </p> * <p/> * <p>So a tab becomes the characters <code>'\\'</code> and * <code>'t'</code>.</p> * <p/> * <p>The only difference between Java strings and JavaScript strings * is that in JavaScript, a single quote must be escaped.</p> * <p/> * <p>Example: * <pre> * input string: He didn't say, "Stop!" * output string: He didn't say, \"Stop!\" * </pre> * </p> * * @param str String to escape values in, may be null * @return String with escaped values, <code>null</code> if null string input */ public static String escapeJava(String str) throws Exception { return escapeJavaStyleString(str, false, false); }
/** * <p>Escapes the characters in a <code>String</code> using JavaScript String rules.</p> * <p>Escapes any values it finds into their JavaScript String form. * Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.) </p> * <p/> * <p>So a tab becomes the characters <code>'\\'</code> and * <code>'t'</code>.</p> * <p/> * <p>The only difference between Java strings and JavaScript strings * is that in JavaScript, a single quote must be escaped.</p> * <p/> * <p>Example: * <pre> * input string: He didn't say, "Stop!" * output string: He didn\'t say, \"Stop!\" * </pre> * </p> * * @param str String to escape values in, may be null * @return String with escaped values, <code>null</code> if null string input */ public static String escapeJavaScript(String str) throws Exception { return escapeJavaStyleString(str, true, true); }
/** * <p>Worker method for the {@link #escapeJavaScript(String)} method.</p> * * @param str String to escape values in, may be null * @param escapeSingleQuotes escapes single quotes if <code>true</code> * @param escapeForwardSlash TODO * @return the escaped string */ private static String escapeJavaStyleString(String str, boolean escapeSingleQuotes, boolean escapeForwardSlash) throws Exception { if (str == null) { return null; } StringWriter writer = new StringWriter(str.length() * 2); escapeJavaStyleString(writer, str, escapeSingleQuotes, escapeForwardSlash); return writer.toString(); }