public static String escapeJavaString(String s, boolean nullMeansNull) { if(s == null) { return nullMeansNull ? "null" : "\"\""; } else { String s1 = Util.replace(s, "\\", "\\\\"); String s2 = Util.replace(s1, "\"", "\\\""); String s3 = Util.replace(s2, "\n\r", "\\n"); String s4 = Util.replace(s3, "\n", "\\n"); String s5 = Util.replace(s4, "\r", "\\r"); return "\"" + s5 + "\""; } }
/** * Converts double-quoted Java strings to their contents. For example, * <code>"foo\"bar"</code> becomes <code>foo"bar</code>. */ public static String stripDoubleQuotes(String value) { assert value.charAt(0) == '"'; assert value.charAt(value.length() - 1) == '"'; String s5 = value.substring(1, value.length() - 1); String s4 = Util.replace(s5, "\\r", "\r"); String s3 = Util.replace(s4, "\\n", "\n"); String s2 = Util.replace(s3, "\\\"", "\""); String s1 = Util.replace(s2, "\\\\", "\\"); return s1; }
/** * Converts double-quoted Java strings to their contents. For example, * <code>"foo\"bar"</code> becomes <code>foo"bar</code>. */ public static String stripDoubleQuotes(String value) { assert value.charAt(0) == '"'; assert value.charAt(value.length() - 1) == '"'; String s5 = value.substring(1, value.length() - 1); String s4 = Util.replace(s5, "\\r", "\r"); String s3 = Util.replace(s4, "\\n", "\n"); String s2 = Util.replace(s3, "\\\"", "\""); String s1 = Util.replace(s2, "\\\\", "\\"); return s1; }
/** * Prints a string, enclosing in double quotes (") and escaping if * necessary. For examples, <code>printDoubleQuoted(w,"x\"y",false)</code> * prints <code>"x\"y"</code>. */ public static void printJavaString( PrintWriter pw, String s, boolean nullMeansNull) { if (s == null) { if (nullMeansNull) { pw.print("null"); } } else { String s1 = replace(s, "\\", "\\\\"); String s2 = replace(s1, "\"", "\\\""); String s3 = replace(s2, "\n\r", "\\n"); String s4 = replace(s3, "\n", "\\n"); String s5 = replace(s4, "\r", "\\r"); pw.print("\""); pw.print(s5); pw.print("\""); } }
/** * Prints a string, enclosing in double quotes (") and escaping if * necessary. For examples, <code>printDoubleQuoted(w,"x\"y",false)</code> * prints <code>"x\"y"</code>. */ public static void printJavaString( PrintWriter pw, String s, boolean nullMeansNull) { if (s == null) { if (nullMeansNull) { pw.print("null"); } } else { String s1 = replace(s, "\\", "\\\\"); String s2 = replace(s1, "\"", "\\\""); String s3 = replace(s2, "\n\r", "\\n"); String s4 = replace(s3, "\n", "\\n"); String s5 = replace(s4, "\r", "\\r"); pw.print("\""); pw.print(s5); pw.print("\""); } }
/** * Returns the string quoted for SQL, for example <code>_ISO-8859-1'is it a * plane? no it''s superman!'</code>. * * @param prefix if true, prefix the character set name * @param suffix if true, suffix the collation clause * @return the quoted string */ public String asSql( boolean prefix, boolean suffix) { StringBuilder ret = new StringBuilder(); if (prefix && (null != charsetName)) { ret.append("_"); ret.append(charsetName); } ret.append("'"); ret.append(Util.replace(value, "'", "''")); ret.append("'"); // NOTE jvs 3-Feb-2005: see FRG-78 for why this should go away if (false) { if (suffix && (null != collation)) { ret.append(" "); ret.append(collation.toString()); } } return ret.toString(); }
/** * Returns the string quoted for SQL, for example <code>_ISO-8859-1'is it a * plane? no it''s superman!'</code>. * * @param prefix if true, prefix the character set name * @param suffix if true, suffix the collation clause * @return the quoted string */ public String asSql( boolean prefix, boolean suffix) { StringBuilder ret = new StringBuilder(); if (prefix && (null != charsetName)) { ret.append("_"); ret.append(charsetName); } ret.append("'"); ret.append(Util.replace(getValue(), "'", "''")); ret.append("'"); // NOTE jvs 3-Feb-2005: see FRG-78 for why this should go away if (false) { if (suffix && (null != collation)) { ret.append(" "); ret.append(collation.toString()); } } return ret.toString(); }
/** * Converts a string (which may contain quotes and newlines) into a java * literal. * * <p>For example, * <pre><code>string with "quotes" split * across lines</code></pre> * * <p>becomes * * <blockquote><pre><code>"string with \"quotes\" split" + NL + * "across lines"</code></pre></blockquote> */ public static String quoteForJava(String s) { s = Util.replace(s, "\\", "\\\\"); s = Util.replace(s, "\"", "\\\""); s = LINE_BREAK_PATTERN.matcher(s).replaceAll(LINE_BREAK); s = TAB_PATTERN.matcher(s).replaceAll("\\\\t"); s = "\"" + s + "\""; final String spurious = " + \n\"\""; if (s.endsWith(spurious)) { s = s.substring(0, s.length() - spurious.length()); } return s; }
/** * Converts a string (which may contain quotes and newlines) into a java * literal. * * <p>For example, * <pre><code>string with "quotes" split * across lines</code></pre> * * <p>becomes * * <blockquote><pre><code>"string with \"quotes\" split" + NL + * "across lines"</code></pre></blockquote> */ public static String quoteForJava(String s) { s = Util.replace(s, "\\", "\\\\"); s = Util.replace(s, "\"", "\\\""); s = LINE_BREAK_PATTERN.matcher(s).replaceAll(LINE_BREAK); s = TAB_PATTERN.matcher(s).replaceAll("\\\\t"); s = "\"" + s + "\""; final String spurious = " + \n\"\""; if (s.endsWith(spurious)) { s = s.substring(0, s.length() - spurious.length()); } return s; }
s = Util.replace(s, "\"", "\\\""); s = LINE_BREAK_PATTERN.matcher(s).replaceAll(LINE_BREAK); s = TAB_PATTERN.matcher(s).replaceAll("\\\\t");
s = Util.replace(s, "\"", "\\\""); s = LINE_BREAK_PATTERN.matcher(s).replaceAll(LINE_BREAK); s = TAB_PATTERN.matcher(s).replaceAll("\\\\t");