use strategy encapsulator in printer rather than assuming double-quote

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/sandbox/csv/trunk@558885 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Matthew Jason Benson 2007-07-23 22:32:42 +00:00
parent 14182380d5
commit 1d7ebab211
1 changed files with 8 additions and 6 deletions

View File

@ -250,7 +250,7 @@ public class CSVPrinter {
* @param value needs to be escaped and quoted * @param value needs to be escaped and quoted
* @return the value, escaped and quoted * @return the value, escaped and quoted
*/ */
private static String escapeAndQuote(String value) { private String escapeAndQuote(String value) {
// the initial count is for the preceding and trailing quotes // the initial count is for the preceding and trailing quotes
int count = 2; int count = 2;
for (int i = 0; i < value.length(); i++) { for (int i = 0; i < value.length(); i++) {
@ -266,13 +266,15 @@ public class CSVPrinter {
} }
} }
StringBuffer sb = new StringBuffer(value.length() + count); StringBuffer sb = new StringBuffer(value.length() + count);
sb.append('"'); sb.append(strategy.getEncapsulator());
for (int i = 0; i < value.length(); i++) { for (int i = 0; i < value.length(); i++) {
char c = value.charAt(i); char c = value.charAt(i);
if (c == strategy.getEncapsulator()) {
sb.append('\\').append(c);
continue;
}
switch (c) { switch (c) {
case '\"' :
sb.append("\\\"");
break;
case '\n' : case '\n' :
sb.append("\\n"); sb.append("\\n");
break; break;
@ -286,7 +288,7 @@ public class CSVPrinter {
sb.append(c); sb.append(c);
} }
} }
sb.append('"'); sb.append(strategy.getEncapsulator());
return sb.toString(); return sb.toString();
} }