Skip to content

Commit 9778244

Browse files
committed
[CSV-203]
withNullString value is printed without quotes when QuoteMode.ALL is specified; add QuoteMode.ALL_NON_NULL. PR #17.
1 parent a775784 commit 9778244

4 files changed

Lines changed: 129 additions & 2 deletions

File tree

src/changes/changes.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
<title>Release Notes</title>
3939
</properties>
4040
<body>
41-
<release version="1.5" date="2016-MM-DD" description="Bug fix release">
41+
<release version="1.5" date="2017-MM-DD" description="Bug fix release">
42+
<action issue="CSV-203" type="fix" dev="ggregory" due-to="Richard Wheeldon, Kai Paroth">withNullString value is printed without quotes when QuoteMode.ALL is specified; add QuoteMode.ALL_NON_NULL. PR #17.</action>
4243
<action issue="CSV-194" type="fix" dev="ggregory" due-to="Marc Prud'hommeaux">Fix outdated comments about FileReader in CSVParser #13</action>
4344
<action issue="CSV-193" type="fix" dev="ggregory" due-to="Matthias Wiehl">Fix incorrect method name 'withFirstRowAsHeader' in user guide.</action>
4445
<action issue="CSV-171" type="fix" dev="ggregory" due-to="Gary Gregory, Michael Graessle, Adrian Bridgett">Negative numeric values in the first column are always quoted in minimal mode.</action>

src/main/java/org/apache/commons/csv/CSVFormat.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,16 @@ public void print(final Object value, final Appendable out, final boolean newRec
946946
// Only call CharSequence.toString() if you have to, helps GC-free use cases.
947947
CharSequence charSequence;
948948
if (value == null) {
949-
charSequence = nullString == null ? Constants.EMPTY : nullString;
949+
// https://issues.apache.org/jira/browse/CSV-203
950+
if (null == nullString) {
951+
charSequence = Constants.EMPTY;
952+
} else {
953+
if (QuoteMode.ALL == quoteMode) {
954+
charSequence = quoteCharacter + nullString + quoteCharacter;
955+
} else {
956+
charSequence = nullString;
957+
}
958+
}
950959
} else {
951960
charSequence = value instanceof CharSequence ? (CharSequence) value : value.toString();
952961
}
@@ -1031,6 +1040,7 @@ private void printAndQuote(final Object object, final CharSequence value, final
10311040
}
10321041
switch (quoteModePolicy) {
10331042
case ALL:
1043+
case ALL_NON_NULL:
10341044
quote = true;
10351045
break;
10361046
case NON_NUMERIC:

src/main/java/org/apache/commons/csv/QuoteMode.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public enum QuoteMode {
2828
*/
2929
ALL,
3030

31+
/**
32+
* Quotes all non-null fields.
33+
*/
34+
ALL_NON_NULL,
35+
3136
/**
3237
* Quotes fields which contain special characters such as a delimiter, quotes character or any of the characters in
3338
* line separator.
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package org.apache.commons.csv.issues;
2+
3+
import org.apache.commons.csv.CSVFormat;
4+
import org.apache.commons.csv.CSVPrinter;
5+
import org.apache.commons.csv.QuoteMode;
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
/**
10+
* JIRA: <a href="https://issues.apache.org/jira/browse/CSV-203">withNullString value is printed without quotes when QuoteMode.ALL is specified</a>
11+
*/
12+
public class JiraCsv203Test {
13+
14+
@Test
15+
public void testQuoteModeAll() throws Exception {
16+
CSVFormat format = CSVFormat.EXCEL
17+
.withNullString("N/A")
18+
.withIgnoreSurroundingSpaces(true)
19+
.withQuoteMode(QuoteMode.ALL);
20+
21+
StringBuffer buffer = new StringBuffer();
22+
CSVPrinter printer = new CSVPrinter(buffer, format);
23+
printer.printRecord(new Object[] { null, "Hello", null, "World" });
24+
25+
Assert.assertEquals("\"N/A\",\"Hello\",\"N/A\",\"World\"\r\n", buffer.toString());
26+
}
27+
28+
@Test
29+
public void testQuoteModeAllNonNull() throws Exception {
30+
CSVFormat format = CSVFormat.EXCEL
31+
.withNullString("N/A")
32+
.withIgnoreSurroundingSpaces(true)
33+
.withQuoteMode(QuoteMode.ALL_NON_NULL);
34+
35+
StringBuffer buffer = new StringBuffer();
36+
CSVPrinter printer = new CSVPrinter(buffer, format);
37+
printer.printRecord(new Object[] { null, "Hello", null, "World" });
38+
39+
Assert.assertEquals("N/A,\"Hello\",N/A,\"World\"\r\n", buffer.toString());
40+
}
41+
42+
@Test
43+
public void testWithoutQuoteMode() throws Exception {
44+
CSVFormat format = CSVFormat.EXCEL
45+
.withNullString("N/A")
46+
.withIgnoreSurroundingSpaces(true);
47+
48+
StringBuffer buffer = new StringBuffer();
49+
CSVPrinter printer = new CSVPrinter(buffer, format);
50+
printer.printRecord(new Object[] { null, "Hello", null, "World" });
51+
52+
Assert.assertEquals("N/A,Hello,N/A,World\r\n", buffer.toString());
53+
}
54+
55+
@Test
56+
public void testQuoteModeMinimal() throws Exception {
57+
CSVFormat format = CSVFormat.EXCEL
58+
.withNullString("N/A")
59+
.withIgnoreSurroundingSpaces(true)
60+
.withQuoteMode(QuoteMode.MINIMAL);
61+
62+
StringBuffer buffer = new StringBuffer();
63+
CSVPrinter printer = new CSVPrinter(buffer, format);
64+
printer.printRecord(new Object[] { null, "Hello", null, "World" });
65+
66+
Assert.assertEquals("N/A,Hello,N/A,World\r\n", buffer.toString());
67+
}
68+
69+
@Test
70+
public void testQuoteModeNonNumeric() throws Exception {
71+
CSVFormat format = CSVFormat.EXCEL
72+
.withNullString("N/A")
73+
.withIgnoreSurroundingSpaces(true)
74+
.withQuoteMode(QuoteMode.NON_NUMERIC);
75+
76+
StringBuffer buffer = new StringBuffer();
77+
CSVPrinter printer = new CSVPrinter(buffer, format);
78+
printer.printRecord(new Object[] { null, "Hello", null, "World" });
79+
80+
Assert.assertEquals("N/A,\"Hello\",N/A,\"World\"\r\n", buffer.toString());
81+
}
82+
83+
@Test
84+
public void testWithoutNullString() throws Exception {
85+
CSVFormat format = CSVFormat.EXCEL
86+
//.withNullString("N/A")
87+
.withIgnoreSurroundingSpaces(true)
88+
.withQuoteMode(QuoteMode.ALL);
89+
90+
StringBuffer buffer = new StringBuffer();
91+
CSVPrinter printer = new CSVPrinter(buffer, format);
92+
printer.printRecord(new Object[] { null, "Hello", null, "World" });
93+
94+
Assert.assertEquals(",\"Hello\",,\"World\"\r\n", buffer.toString());
95+
}
96+
97+
@Test
98+
public void testWithEmptyValues() throws Exception {
99+
CSVFormat format = CSVFormat.EXCEL
100+
.withNullString("N/A")
101+
.withIgnoreSurroundingSpaces(true)
102+
.withQuoteMode(QuoteMode.ALL);
103+
104+
StringBuffer buffer = new StringBuffer();
105+
CSVPrinter printer = new CSVPrinter(buffer, format);
106+
printer.printRecord(new Object[] { "", "Hello", "", "World" });
107+
//printer.printRecord(new Object[] { null, "Hello", null, "World" });
108+
109+
Assert.assertEquals("\"\",\"Hello\",\"\",\"World\"\r\n", buffer.toString());
110+
}
111+
}

0 commit comments

Comments
 (0)