List of usage examples for org.apache.commons.lang3 StringEscapeUtils escapeJava
public static final String escapeJava(final String input)
Escapes the characters in a String using Java String rules.
Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.)
So a tab becomes the characters '\\' and 't' .
The only difference between Java strings and JavaScript strings is that in JavaScript, a single quote and forward-slash (/) are escaped.
Example:
input string: He didn't say, "Stop!"Usage
From source file:org.siphon.db2js.EmbedSqlTranslator.java
private void appendSqlToCode(CodePart sql) { boolean isFollow = sql.isFollowSql; boolean dirty = false; for (String line : sql.lines) { if (line.trim().isEmpty()) { code.currentLine.append(line); code.lines.add(code.currentLine.toString()); code.currentLine.setLength(0); } else {//from ww w .j av a 2 s . com if (!isFollow) { line = line.replaceFirst("^\\s+", ""); // trim left code.currentLine.append("\"" + StringEscapeUtils.escapeJava(line) + "\""); isFollow = true; } else { line = line.replaceFirst("^\\s+", " "); if (sql.isInCode && dirty == false) { code.currentLine.append(sql.name + " += \"").append(StringEscapeUtils.escapeJava(line)) .append("\""); } else { code.currentLine.append("+ \"").append(StringEscapeUtils.escapeJava(line)).append("\""); } } dirty = true; code.lines.add(code.currentLine.toString()); code.currentLine.setLength(0); } } String line = sql.currentLine.toString(); if (line.trim().isEmpty()) { if (!isFollow) { code.currentLine.append("''"); dirty = true; } else { code.currentLine.append(line); } code.lines.add(code.currentLine.toString()); code.currentLine.setLength(0); } else { if (!isFollow) { line = line.replaceFirst("^\\s+", ""); // trim left code.currentLine.append("\"" + StringEscapeUtils.escapeJava(line) + "\""); isFollow = true; dirty = true; } else { if (sql.isInCode && dirty == false) { code.currentLine.append(sql.name + " += \"").append(StringEscapeUtils.escapeJava(line)) .append("\""); } else { code.currentLine.append("+ \"").append(StringEscapeUtils.escapeJava(line)).append("\""); } dirty = true; } } if (dirty) { code.currentLine.append(";"); } sql.currentLine.setLength(0); sql.lines.clear(); }From source file:org.slc.sli.dal.init.AggregationLoader.java
/** * Executes a command given in a string//from www . j av a 2 s . c o m * @param command The command to execute against the mongo template * @return true if command was executed successfully, false otherwise */ private boolean executeString(String command) { return template.executeCommand("{\"$eval\":\"" + StringEscapeUtils.escapeJava(command) + "\"}").ok(); }From source file:org.structr.csv.test.CsvTest.java
/** * Test CSV output/* w w w . ja v a2 s.c o m*/ */ public void test02CsvOutput() { // create some objects String resource = "/test_one"; String test0Id = getUuidFromLocation(RestAssured.given().contentType("application/json; charset=UTF-8") .filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)) .body(" { 'name' : 'TestOne-0', 'anInt' : 0, 'aLong' : 0, 'aDate' : '2012-09-18T00:33:12+0200' } ") .expect().statusCode(201).when().post(resource).getHeader("Location")); String test1Id = getUuidFromLocation(RestAssured.given().contentType("application/json; charset=UTF-8") .body(" { 'name' : 'TestOne-1', 'anInt' : 1, 'aLong' : 10, 'aDate' : '2012-09-18T01:33:12+0200' } ") .expect().statusCode(201).when().post(resource).getHeader("Location")); String test2Id = getUuidFromLocation(RestAssured.given().contentType("application/json; charset=UTF-8") .body(" { 'name' : 'TestOne-2', 'anInt' : 2, 'aLong' : 20, 'aDate' : '2012-09-18T02:33:12+0200' } ") .expect().statusCode(201).when().post(resource).getHeader("Location")); String test3Id = getUuidFromLocation(RestAssured.given().contentType("application/json; charset=UTF-8") .body(" { 'name' : 'TestOne-3', 'anInt' : 3, 'aLong' : 30, 'aDate' : '2012-09-18T03:33:12+0200' } ") .expect().statusCode(201).when().post(resource).getHeader("Location")); String test4Id = getUuidFromLocation(RestAssured.given().contentType("application/json; charset=UTF-8") .body(" { 'name' : 'TestOne-4', 'anInt' : 4, 'aLong' : 40, 'aDate' : '2012-09-18T04:33:12+0200' } ") .expect().statusCode(201).when().post(resource).getHeader("Location")); String test5Id = getUuidFromLocation(RestAssured.given().contentType("application/json; charset=UTF-8") .body(" { 'name' : 'TestOne-5', 'anInt' : 5, 'aLong' : 50, 'aDate' : '2012-09-18T05:33:12+0200' } ") .expect().statusCode(201).when().post(resource).getHeader("Location")); String test6Id = getUuidFromLocation(RestAssured.given().contentType("application/json; charset=UTF-8") .body(" { 'name' : 'TestOne-6', 'anInt' : 6, 'aLong' : 60, 'aDate' : '2012-09-18T06:33:12+0200' } ") .expect().statusCode(201).when().post(resource).getHeader("Location")); String test7Id = getUuidFromLocation(RestAssured.given().contentType("application/json; charset=UTF-8") .body(" { 'name' : 'TestOne-7', 'anInt' : 7, 'aLong' : 70, 'aDate' : '2012-09-18T07:33:12+0200' } ") .expect().statusCode(201).when().post(resource).getHeader("Location")); Object result = RestAssured .given().contentType("application/csv; charset=UTF-8") .filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)) .filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)) .filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)) .filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)) .filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).expect().statusCode(200).when() .get("http://" + host + ":" + httpPort + csvUrl + resource + "?sort=name"); System.out.println("result: " + ((RestAssuredResponseImpl) result).prettyPrint()); String expected = "\"id\";\"type\";\"name\";\"anInt\";\"aLong\";\"aDate\"\r\n" + "\"" + test0Id + "\";\"TestOne\";\"TestOne-0\";\"0\";\"0\";\"Mon Sep 17 22:33:12 UTC 2012\"\r\n" + "\"" + test1Id + "\";\"TestOne\";\"TestOne-1\";\"1\";\"10\";\"Mon Sep 17 23:33:12 UTC 2012\"\r\n" + "\"" + test2Id + "\";\"TestOne\";\"TestOne-2\";\"2\";\"20\";\"Tue Sep 18 00:33:12 UTC 2012\"\r\n" + "\"" + test3Id + "\";\"TestOne\";\"TestOne-3\";\"3\";\"30\";\"Tue Sep 18 01:33:12 UTC 2012\"\r\n" + "\"" + test4Id + "\";\"TestOne\";\"TestOne-4\";\"4\";\"40\";\"Tue Sep 18 02:33:12 UTC 2012\"\r\n" + "\"" + test5Id + "\";\"TestOne\";\"TestOne-5\";\"5\";\"50\";\"Tue Sep 18 03:33:12 UTC 2012\"\r\n" + "\"" + test6Id + "\";\"TestOne\";\"TestOne-6\";\"6\";\"60\";\"Tue Sep 18 04:33:12 UTC 2012\"\r\n" + "\"" + test7Id + "\";\"TestOne\";\"TestOne-7\";\"7\";\"70\";\"Tue Sep 18 05:33:12 UTC 2012\"\r\n"; String resultString = ((RestAssuredResponseImpl) result).asString(); System.out.println(StringEscapeUtils.escapeJava(expected)); System.out.println(StringEscapeUtils.escapeJava(resultString)); assertEquals(expected, resultString); result = RestAssured .given().contentType("application/csv; charset=UTF-8") .filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)) .filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)) .filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)) .filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)) .filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).expect().statusCode(200).when() .get("http://" + host + ":" + httpPort + csvUrl + resource + "?sort=name&pageSize=2&page=1"); System.out.println("result: " + ((RestAssuredResponseImpl) result).prettyPrint()); expected = "\"id\";\"type\";\"name\";\"anInt\";\"aLong\";\"aDate\"\r\n" + "\"" + test0Id + "\";\"TestOne\";\"TestOne-0\";\"0\";\"0\";\"Mon Sep 17 22:33:12 UTC 2012\"\r\n" + "\"" + test1Id + "\";\"TestOne\";\"TestOne-1\";\"1\";\"10\";\"Mon Sep 17 23:33:12 UTC 2012\"\r\n"; resultString = ((RestAssuredResponseImpl) result).asString(); System.out.println(StringEscapeUtils.escapeJava(expected)); System.out.println(StringEscapeUtils.escapeJava(resultString)); assertEquals(expected, resultString); result = RestAssured .given().contentType("application/csv; charset=UTF-8") .filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)) .filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)) .filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)) .filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)) .filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).expect().statusCode(200).when() .get("http://" + host + ":" + httpPort + csvUrl + resource + "?sort=name&pageSize=2&page=2"); System.out.println("result: " + ((RestAssuredResponseImpl) result).prettyPrint()); expected = "\"id\";\"type\";\"name\";\"anInt\";\"aLong\";\"aDate\"\r\n" + "\"" + test2Id + "\";\"TestOne\";\"TestOne-2\";\"2\";\"20\";\"Tue Sep 18 00:33:12 UTC 2012\"\r\n" + "\"" + test3Id + "\";\"TestOne\";\"TestOne-3\";\"3\";\"30\";\"Tue Sep 18 01:33:12 UTC 2012\"\r\n"; resultString = ((RestAssuredResponseImpl) result).asString(); System.out.println(StringEscapeUtils.escapeJava(expected)); System.out.println(StringEscapeUtils.escapeJava(resultString)); assertEquals(expected, resultString); }From source file:org.structr.schema.action.ActionEntry.java
public String getSource(final String objVariable, final boolean includeParameters) { final StringBuilder buf = new StringBuilder(); switch (type) { case Create://from w w w . j a v a 2 s. c o m case Save: case Delete: // prepend boolean evaluation for passive actions buf.append("Boolean.TRUE.equals("); break; case Custom: break; } buf.append(Actions.class.getSimpleName()); buf.append(".execute(securityContext, ").append(objVariable).append(", \"${"); buf.append(StringEscapeUtils.escapeJava(call)); buf.append("}\""); if (includeParameters) { buf.append(", parameters"); } buf.append(")"); switch (type) { case Create: case Save: case Delete: // append closing brace for passive actions buf.append(")"); break; case Custom: break; } return buf.toString(); }From source file:org.structr.schema.parser.CypherPropertyParser.java
@Override public void parseFormatString(final Schema entity, String expression) throws FrameworkException { auxType = ", \"" + StringEscapeUtils.escapeJava(expression) + "\""; }From source file:org.structr.schema.parser.PropertySourceGenerator.java
protected void getPropertySource(final StringBuilder buf) { buf.append("\tpublic static final Property<").append(getValueType()).append("> ") .append(SchemaHelper.cleanPropertyName(source.getPropertyName())).append("Property"); buf.append(" = new ").append(getPropertyType()).append("(\"").append(source.getPropertyName()).append("\""); if (StringUtils.isNotBlank(source.getDbName())) { buf.append(", \"").append(source.getDbName()).append("\""); }//w w w . j av a 2s . co m buf.append(getPropertyParameters()); buf.append(localValidator); buf.append(")"); if (StringUtils.isNotBlank(source.getContentType())) { buf.append(".contentType(\"").append(source.getContentType()).append("\")"); } if (StringUtils.isNotBlank(source.getDefaultValue())) { buf.append(".defaultValue(").append(getDefaultValue()).append(")"); } if (StringUtils.isNotBlank(source.getFormat())) { buf.append(".format(\"").append(StringEscapeUtils.escapeJava(source.getFormat())).append("\")"); } if (StringUtils.isNotBlank(source.getReadFunction())) { buf.append(".readFunction(\"").append(StringEscapeUtils.escapeJava(source.getReadFunction())) .append("\")"); } if (StringUtils.isNotBlank(source.getWriteFunction())) { buf.append(".writeFunction(\"").append(StringEscapeUtils.escapeJava(source.getWriteFunction())) .append("\")"); } if (source.isUnique()) { buf.append(".unique()"); } if (source.isNotNull()) { buf.append(".notNull()"); } if (source.isIndexed()) { if (StringUtils.isNotBlank(source.getDefaultValue())) { buf.append(".indexedWhenEmpty()"); } else { buf.append(".indexed()"); } } buf.append(".dynamic()"); buf.append(";\n"); }From source file:org.tinymediamanager.core.Utils.java
/** * replaces a string with placeholder ({}) with the string from the replacement array the strings in the replacement array have to be in the same * order as the placeholder in the source string * /*from w w w . j av a2 s . c o m*/ * @param source * string * @param replacements * array * @return replaced string */ public static String replacePlaceholders(String source, String[] replacements) { String result = source; int index = 0; Pattern pattern = Pattern.compile("\\{\\}"); while (true) { Matcher matcher = pattern.matcher(result); if (matcher.find()) { try { // int index = Integer.parseInt(matcher.group(1)); if (replacements.length > index) { result = result.replaceFirst(pattern.pattern(), StringEscapeUtils.escapeJava(replacements[index])); } else { result = result.replaceFirst(pattern.pattern(), ""); } } catch (Exception e) { result = result.replaceFirst(pattern.pattern(), ""); } index++; } else { break; } } return StrgUtils.removeDuplicateWhitespace(result); }From source file:org.ut.biolab.medsavant.server.db.util.DBUtils.java
public static synchronized void copyQueryResultToNewTable(final String sid, Query sq, final String dstTable, MedSavantServerJob parent) throws IOException, SQLException, SessionExpiredException { File f = getTemporaryDatabaseFile(); try {/*from ww w. j a v a 2 s . com*/ String intoString = "INTO OUTFILE \"" + f.getAbsolutePath().replaceAll("\\\\", "/") + "\" " + "FIELDS TERMINATED BY '" + StringEscapeUtils.escapeJava(VariantManagerUtils.FIELD_DELIMITER) + "' " + "ENCLOSED BY '" + VariantManagerUtils.ENCLOSED_BY + "' " + "ESCAPED BY '" + StringEscapeUtils.escapeJava(VariantManagerUtils.ESCAPE_CHAR) + "' " + " LINES TERMINATED BY '\\r\\n' "; String queryString; if (sq instanceof UnionQuery) { queryString = sq.toString() + " " + intoString; } else { //In MySQL, the 'INTO OUTFILE' is placed after the last select in a union, but //everything in the union will actually be outputted to the file. queryString = sq.toString().replace("FROM", intoString + "FROM"); } LOG.info(queryString); ConnectionController.executeQuery(sid, queryString); loadTable(sid, f, dstTable); LOG.info("Query result copied!"); } finally { if (f != null && f.exists()) { f.delete(); } } }From source file:org.ut.biolab.medsavant.server.db.util.DBUtils.java
public static synchronized void copyQueryResultToNewTable(final String sid, String query, final String dstTable, MedSavantServerJob parent) throws IOException, SQLException, SessionExpiredException { File f = getTemporaryDatabaseFile(); try {//from w w w . j a va2s .c om String intoString = "INTO OUTFILE \"" + f.getAbsolutePath().replaceAll("\\\\", "/") + "\" " + "FIELDS TERMINATED BY '" + StringEscapeUtils.escapeJava(VariantManagerUtils.FIELD_DELIMITER) + "' " + "ENCLOSED BY '" + VariantManagerUtils.ENCLOSED_BY + "' " + "ESCAPED BY '" + StringEscapeUtils.escapeJava(VariantManagerUtils.ESCAPE_CHAR) + "' " + " LINES TERMINATED BY '\\r\\n' "; String queryString = query + " " + intoString; LOG.info(queryString); ConnectionController.executeQuery(sid, queryString); loadTable(sid, f, dstTable); LOG.info("Query result copied!"); } finally { if (f != null && f.exists()) { f.delete(); } } }From source file:org.ut.biolab.medsavant.server.db.util.DBUtils.java
public static void dumpTable(String sessID, String tableName, File dst) throws SQLException, SessionExpiredException { String intoString = "INTO OUTFILE \"" + dst.getAbsolutePath().replaceAll("\\\\", "/") + "\" " + "FIELDS TERMINATED BY '" + StringEscapeUtils.escapeJava(VariantManagerUtils.FIELD_DELIMITER) + "' " + "ENCLOSED BY '" + VariantManagerUtils.ENCLOSED_BY + "' " + "ESCAPED BY '" + StringEscapeUtils.escapeJava(VariantManagerUtils.ESCAPE_CHAR) + "' " + " LINES TERMINATED BY '\\r\\n' "; String query = "SELECT * FROM " + tableName + " " + intoString; ConnectionController.executeQuery(sessID, query); }