Skip to content
This repository was archived by the owner on Oct 20, 2022. It is now read-only.

Commit 15f4ee5

Browse files
Bug fix - csv rendering, add escaping for formatted values with comma
1 parent 6e7df6e commit 15f4ee5

File tree

2 files changed

+53
-11
lines changed

2 files changed

+53
-11
lines changed

src/main/java/com/google/visualization/datasource/render/CsvRenderer.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,12 @@ public static CharSequence renderDataTable(DataTable dataTable, ULocale locale,
8989
if (cell.isNull()) {
9090
sb.append("null");
9191
} else {
92-
switch (cell.getType()) {
93-
case TEXT:
94-
sb.append(escapeString(formattedValue));
95-
break;
96-
case NUMBER:
97-
case BOOLEAN:
98-
case TIMEOFDAY:
99-
case DATE:
100-
case DATETIME:
101-
sb.append(formattedValue);
102-
break;
92+
ValueType type = cell.getType();
93+
// Escape the string with quotes if its a text value or if it contains a comma.
94+
if (formattedValue.indexOf(',') > -1 || type.equals(ValueType.TEXT)) {
95+
sb.append(escapeString(formattedValue));
96+
} else {
97+
sb.append(formattedValue);
10398
}
10499
}
105100
sb.append(separator);

src/test/java/com/google/visualization/datasource/render/CsvRendererTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.visualization.datasource.datatable.DataTable;
2424
import com.google.visualization.datasource.datatable.TableCell;
2525
import com.google.visualization.datasource.datatable.TableRow;
26+
import com.google.visualization.datasource.datatable.value.BooleanValue;
2627
import com.google.visualization.datasource.datatable.value.DateTimeValue;
2728
import com.google.visualization.datasource.datatable.value.DateValue;
2829
import com.google.visualization.datasource.datatable.value.NumberValue;
@@ -262,4 +263,50 @@ public void testRenderError() {
262263
"\"Error: Operation not supported. Cannot \"\"do\"\" that, too late!\"",
263264
CsvRenderer.renderCsvError(responseStatus));
264265
}
266+
267+
public void testRenderDataTableWithCommas() throws DataSourceException {
268+
testData = new DataTable();
269+
ColumnDescription c0 = new ColumnDescription("A", ValueType.TEXT, "col0");
270+
ColumnDescription c1 = new ColumnDescription("B", ValueType.NUMBER, "col1");
271+
ColumnDescription c2 = new ColumnDescription("C", ValueType.BOOLEAN, "col2");
272+
ColumnDescription c3 = new ColumnDescription("D", ValueType.DATE, "col3");
273+
ColumnDescription c4 = new ColumnDescription("E", ValueType.DATETIME, "col4");
274+
ColumnDescription c5 = new ColumnDescription("F", ValueType.TIMEOFDAY, "col5");
275+
276+
testData.addColumn(c0);
277+
testData.addColumn(c1);
278+
testData.addColumn(c2);
279+
testData.addColumn(c3);
280+
testData.addColumn(c4);
281+
testData.addColumn(c5);
282+
283+
rows = Lists.newArrayList();
284+
285+
TableRow row = new TableRow();
286+
row.addCell(new TableCell(new TextValue("aaa"), "aaa"));
287+
row.addCell(new TableCell(new NumberValue(222), "222"));
288+
row.addCell(new TableCell(BooleanValue.TRUE, "true"));
289+
row.addCell(new TableCell(new DateValue(2009, 1, 1), "2009-02-01"));
290+
row.addCell(new TableCell(new DateTimeValue(2009, 1, 1, 12, 14, 1, 0), "2009-02-01 12:14:01"));
291+
row.addCell(new TableCell(new TimeOfDayValue(12, 14, 1), "12:14:01"));
292+
rows.add(row);
293+
294+
row = new TableRow();
295+
row.addCell(new TableCell(new TextValue("aaa"), "a,aa"));
296+
row.addCell(new TableCell(new NumberValue(222), "2,22"));
297+
row.addCell(new TableCell(BooleanValue.TRUE, "true,"));
298+
row.addCell(new TableCell(new DateValue(2009, 1, 1), "2009-02-01"));
299+
row.addCell(new TableCell(new DateTimeValue(2009, 1, 1, 12, 14, 1, 0), "2009-02-01 12,14,01"));
300+
row.addCell(new TableCell(new TimeOfDayValue(12, 14, 1), "12:14:01"));
301+
rows.add(row);
302+
303+
testData.addRows(rows);
304+
305+
String expected = "\"col0\",\"col1\",\"col2\",\"col3\",\"col4\",\"col5\"\n";
306+
expected += "\"aaa\",222,true,2009-02-01,2009-02-01 12:14:01,12:14:01\n";
307+
expected += "\"a,aa\",\"2,22\",\"true,\",2009-02-01,\"2009-02-01 12,14,01\",12:14:01\n";
308+
assertEquals(expected, CsvRenderer.renderDataTable(testData, null, null));
309+
310+
}
265311
}
312+

0 commit comments

Comments
 (0)