[#4746] Allow for specifying empty string as formatCSV()'s nullString / emptyString

This commit is contained in:
lukaseder 2015-12-17 16:21:57 +01:00
parent 687884ec31
commit 0e23b09087
2 changed files with 92 additions and 16 deletions

View File

@ -49,13 +49,15 @@ public final class CSVFormat {
final String delimiter;
final String nullString;
final String emptyString;
final String newline;
final boolean header;
public CSVFormat() {
this(
",",
"",
"\"\"",
"\"\"",
"\n",
true
);
@ -64,11 +66,13 @@ public final class CSVFormat {
private CSVFormat(
String delimiter,
String nullString,
String emptyString,
String newline,
boolean header
) {
this.delimiter = delimiter;
this.nullString = nullString;
this.emptyString = emptyString;
this.newline = newline;
this.header = header;
}
@ -92,11 +96,31 @@ public final class CSVFormat {
return new CSVFormat(
newDelimiter,
nullString,
emptyString,
newline,
header
);
}
/**
* The delimiter to be used between CSV cells, defaulting to
* <code>","</code>.
* <p>
* <table border="1">
* <tr>
* <td>Using <code>","</code></td>
* <td><code>a,b,c</code></td>
* </tr>
* <tr>
* <td>Using <code>";"</code></td>
* <td><code>a;b;c</code></td>
* </tr>
* </table>
*/
public CSVFormat delimiter(char newDelimiter) {
return delimiter("" + newDelimiter);
}
/**
* The delimiter to be used between CSV cells, defaulting to
* <code>","</code>.
@ -139,6 +163,7 @@ public final class CSVFormat {
return new CSVFormat(
delimiter,
newNullString,
emptyString,
newline,
header
);
@ -167,6 +192,58 @@ public final class CSVFormat {
return nullString;
}
/**
* The string to be used for <code>null</code> values, defaulting to the
* empty string.
* <p>
* <table border="1">
* <tr>
* <td>Using <code>""</code></td>
* <td><code>a,,c</code></td>
* </tr>
* <tr>
* <td>Using <code>"\"\""</code></td>
* <td><code>a,"",c</code></td>
* </tr>
* <tr>
* <td>Using <code>"{null}"</code></td>
* <td><code>a,{null},c</code></td>
* </tr>
* </table>
*/
public CSVFormat emptyString(String newEmptyString) {
return new CSVFormat(
delimiter,
nullString,
newEmptyString,
newline,
header
);
}
/**
* The string to be used for <code>null</code> values, defaulting to the
* empty string.
* <p>
* <table border="1">
* <tr>
* <td>Using <code>""</code></td>
* <td><code>a,,c</code></td>
* </tr>
* <tr>
* <td>Using <code>"\"\""</code></td>
* <td><code>a,"",c</code></td>
* </tr>
* <tr>
* <td>Using <code>"{null}"</code></td>
* <td><code>a,{null},c</code></td>
* </tr>
* </table>
*/
public String emptyString() {
return emptyString;
}
/**
* The string to be used to separate rows, defaulting to <code>\n</code>.
*/
@ -174,6 +251,7 @@ public final class CSVFormat {
return new CSVFormat(
delimiter,
nullString,
emptyString,
newNewline,
header
);
@ -194,6 +272,7 @@ public final class CSVFormat {
return new CSVFormat(
delimiter,
nullString,
emptyString,
newline,
newHeader
);

View File

@ -668,7 +668,7 @@ class ResultImpl<R extends Record> implements Result<R>, AttachableInternal {
@Override
public final void formatCSV(Writer writer, boolean header) {
formatCSV(writer, header, ',', "");
formatCSV(writer, header, ',', "\"\"");
}
@Override
@ -700,7 +700,7 @@ class ResultImpl<R extends Record> implements Result<R>, AttachableInternal {
@Override
public final void formatCSV(Writer writer, boolean header, char delimiter) {
formatCSV(writer, header, delimiter, "");
formatCSV(writer, header, delimiter, "\"\"");
}
@Override
@ -744,7 +744,7 @@ class ResultImpl<R extends Record> implements Result<R>, AttachableInternal {
@Override
public final void formatCSV(Writer writer, boolean header, char delimiter, String nullString) {
formatCSV(writer, new CSVFormat().header(header).delimiter("" + delimiter).nullString(nullString));
formatCSV(writer, new CSVFormat().header(header).delimiter(delimiter).nullString(nullString));
}
@Override
@ -754,7 +754,7 @@ class ResultImpl<R extends Record> implements Result<R>, AttachableInternal {
String sep1 = "";
for (Field<?> field : fields.fields) {
writer.append(sep1);
writer.append(formatCSV0(field.getName(), ""));
writer.append(formatCSV0(field.getName(), format));
sep1 = format.delimiter();
}
@ -767,7 +767,7 @@ class ResultImpl<R extends Record> implements Result<R>, AttachableInternal {
for (int index = 0; index < fields.fields.length; index++) {
writer.append(sep2);
writer.append(formatCSV0(record.getValue(index), format.nullString()));
writer.append(formatCSV0(record.getValue(index), format));
sep2 = format.delimiter();
}
@ -780,17 +780,14 @@ class ResultImpl<R extends Record> implements Result<R>, AttachableInternal {
}
}
private final String formatCSV0(Object value, String nullString) {
private final String formatCSV0(Object value, CSVFormat format) {
// Escape null and empty strings
if (value == null || "".equals(value)) {
if (StringUtils.isEmpty(nullString)) {
return "\"\"";
}
else {
return nullString;
}
}
// [#4746] Escape null and empty strings
if (value == null)
return format.nullString();
if ("".equals(value.toString()))
return format.emptyString();
String result = format0(value, false, false);