/** * Converts connection properties to a String to be passed to the mappers. * * @param properties JDBC connection parameters * @return String to be passed to configuration */ protected static String propertiesToString(Properties properties) { List<String> propertiesList = new ArrayList<String>(properties.size()); for (Entry<Object, Object> property : properties.entrySet()) { String key = StringEscapeUtils.escapeCsv(property.getKey().toString()); if (key.equals(property.getKey().toString()) && key.contains("=")) { key = "\"" + key + "\""; } String val = StringEscapeUtils.escapeCsv(property.getValue().toString()); if (val.equals(property.getValue().toString()) && val.contains("=")) { val = "\"" + val + "\""; } propertiesList.add(StringEscapeUtils.escapeCsv(key + "=" + val)); } return StringUtils.join(propertiesList, ','); }
/** 5、字符串的Escape */ @Test public void test5() { System.out.println(StringEscapeUtils.escapeCsv("测试测试哦")); // "测试测试哦" System.out.println(StringEscapeUtils.escapeCsv("测试,测试哦")); // "\"测试,测试哦\"" System.out.println(StringEscapeUtils.escapeCsv("测试\n测试哦")); // "\"测试\n测试哦\"" System.out.println(StringEscapeUtils.escapeHtml("测试测试哦")); // "<p>测试测试哦</p>" System.out.println( StringEscapeUtils.escapeJava( "\"rensaninng\",欢迎您!")); // "\"rensaninng\"\uFF0C\u6B22\u8FCE\u60A8\uFF01" // System.out.println(StringEscapeUtils.escapeEcmaScript("测试'测试哦"));//"\u6D4B\u8BD5\'\u6D4B\u8BD5\u54E6" System.out.println( StringEscapeUtils.escapeXml( "<tt>\"bread\" & \"butter\"</tt>")); // "<tt>"bread" & "butter"</tt>" }
private String getCoPIsPerYearCSVContent(Map<String, Set<Collaborator>> yearToCoPI) { StringBuilder csvFileContent = new StringBuilder(); csvFileContent.append("Year, Count, Co-investigator(s)\n"); for (Map.Entry<String, Set<Collaborator>> currentEntry : yearToCoPI.entrySet()) { csvFileContent.append(StringEscapeUtils.escapeCsv(currentEntry.getKey())); csvFileContent.append(","); csvFileContent.append(currentEntry.getValue().size()); csvFileContent.append(","); csvFileContent.append( StringEscapeUtils.escapeCsv(getCoPINamesAsString(currentEntry.getValue()))); csvFileContent.append("\n"); } return csvFileContent.toString(); }
protected String escapeQuotes(final String input) { final String[] splitedInput = StringUtils.splitPreserveAllTokens(input, SEMICOLON_CHAR); final List<String> tmp = new ArrayList<String>(); for (final String string : splitedInput) { if (doesNotContainNewLine(string)) { tmp.add(StringEscapeUtils.escapeCsv(string)); } else { tmp.add(string); } } return StringUtils.join(tmp, SEMICOLON_CHAR); }
/** * Returns a <code>String</code> value for a CSV column enclosed in double quotes, if required. * * <p>If the value contains a comma, newline or double quote, then the String value is returned * enclosed in double quotes. * * <p>Any double quote characters in the value are escaped with another double quote. * * <p>If the value does not contain a comma, newline or double quote, then the String value is * returned unchanged. see <a * href="http://en.wikipedia.org/wiki/Comma-separated_values">Wikipedia</a> and <a * href="http://tools.ietf.org/html/rfc4180">RFC 4180</a>. * * @param str the input CSV column String, may be null * @return the input String, enclosed in double quotes if the value contains a comma, newline or * double quote, <code>null</code> if null string input * @since 2.4 */ public static String escapeCsv(String str) { if (StringUtils.containsNone(str, CSV_SEARCH_CHARS)) { return str; } try { StringWriter writer = new StringWriter(); escapeCsv(writer, str); return writer.toString(); } catch (IOException ioe) { // this should never ever happen while writing to a StringWriter throw new UnhandledException(ioe); } }
private String getCoPIsListCSVContent(CollaborationData coPIData) { StringBuilder csvFileContent = new StringBuilder(); csvFileContent.append("Co-investigator, Count\n"); for (Collaborator currNode : coPIData.getCollaborators()) { /* * We have already printed the Ego Node info. * */ if (currNode != coPIData.getEgoCollaborator()) { csvFileContent.append(StringEscapeUtils.escapeCsv(currNode.getCollaboratorName())); csvFileContent.append(","); csvFileContent.append(currNode.getNumOfActivities()); csvFileContent.append("\n"); } } return csvFileContent.toString(); }
public static String escapeCsv(String string) { return string == null ? null : StringEscapeUtils.escapeCsv(string); }