Skip to content

Commit 2f2d9aa

Browse files
committed
Implement Quote.ALL. Bullet-proof a unit test.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1398133 13f79535-47bb-0310-9956-ffa450edef68
1 parent cdef24d commit 2f2d9aa

2 files changed

Lines changed: 62 additions & 46 deletions

File tree

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

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -219,53 +219,57 @@ void printAndQuote(final CharSequence value, final int offset, final int len) th
219219
final char delimChar = format.getDelimiter();
220220
final char quoteChar = format.getQuoteChar();
221221

222-
if (len <= 0) {
223-
// always quote an empty token that is the first
224-
// on the line, as it may be the only thing on the
225-
// line. If it were not quoted in that case,
226-
// an empty line has no tokens.
227-
if (first) {
228-
quote = true;
229-
}
222+
if (format.getQuotePolicy() == Quote.ALL) {
223+
quote = true;
230224
} else {
231-
char c = value.charAt(pos);
232-
233-
// Hmmm, where did this rule come from?
234-
if (first && (c < '0' || (c > '9' && c < 'A') || (c > 'Z' && c < 'a') || (c > 'z'))) {
235-
quote = true;
236-
// } else if (c == ' ' || c == '\f' || c == '\t') {
237-
} else if (c <= COMMENT) {
238-
// Some other chars at the start of a value caused the parser to fail, so for now
239-
// encapsulate if we start in anything less than '#'. We are being conservative
240-
// by including the default comment char too.
241-
quote = true;
225+
if (len <= 0) {
226+
// always quote an empty token that is the first
227+
// on the line, as it may be the only thing on the
228+
// line. If it were not quoted in that case,
229+
// an empty line has no tokens.
230+
if (first) {
231+
quote = true;
232+
}
242233
} else {
243-
while (pos < end) {
244-
c = value.charAt(pos);
245-
if (c == LF || c == CR || c == quoteChar || c == delimChar) {
246-
quote = true;
247-
break;
234+
char c = value.charAt(pos);
235+
236+
// Hmmm, where did this rule come from?
237+
if (first && (c < '0' || (c > '9' && c < 'A') || (c > 'Z' && c < 'a') || (c > 'z'))) {
238+
quote = true;
239+
// } else if (c == ' ' || c == '\f' || c == '\t') {
240+
} else if (c <= COMMENT) {
241+
// Some other chars at the start of a value caused the parser to fail, so for now
242+
// encapsulate if we start in anything less than '#'. We are being conservative
243+
// by including the default comment char too.
244+
quote = true;
245+
} else {
246+
while (pos < end) {
247+
c = value.charAt(pos);
248+
if (c == LF || c == CR || c == quoteChar || c == delimChar) {
249+
quote = true;
250+
break;
251+
}
252+
pos++;
248253
}
249-
pos++;
250-
}
251254

252-
if (!quote) {
253-
pos = end - 1;
254-
c = value.charAt(pos);
255-
// if (c == ' ' || c == '\f' || c == '\t') {
256-
// Some other chars at the end caused the parser to fail, so for now
257-
// encapsulate if we end in anything less than ' '
258-
if (c <= SP) {
259-
quote = true;
255+
if (!quote) {
256+
pos = end - 1;
257+
c = value.charAt(pos);
258+
// if (c == ' ' || c == '\f' || c == '\t') {
259+
// Some other chars at the end caused the parser to fail, so for now
260+
// encapsulate if we end in anything less than ' '
261+
if (c <= SP) {
262+
quote = true;
263+
}
260264
}
261265
}
262266
}
263-
}
264267

265-
if (!quote) {
266-
// no encapsulation needed - write out the original value
267-
out.append(value, start, end);
268-
return;
268+
if (!quote) {
269+
// no encapsulation needed - write out the original value
270+
out.append(value, start, end);
271+
return;
272+
}
269273
}
270274

271275
// we hit something that needed encapsulation

src/test/java/org/apache/commons/csv/CSVPrinterTest.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ public void testPrinter5() throws IOException {
7676
assertEquals("a,\"b\nc\"" + lineSeparator, sw.toString());
7777
}
7878

79+
@Test
80+
public void testPrinterQuoteAll() throws IOException {
81+
final StringWriter sw = new StringWriter();
82+
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuotePolicy(Quote.ALL));
83+
printer.printRecord("a", "b\nc", "d");
84+
assertEquals("\"a\",\"b\nc\",\"d\"" + lineSeparator, sw.toString());
85+
}
86+
7987
@Test
8088
public void testPrinter6() throws IOException {
8189
final StringWriter sw = new StringWriter();
@@ -87,15 +95,19 @@ public void testPrinter6() throws IOException {
8795
@Test
8896
public void testJdbcPrinter() throws IOException, ClassNotFoundException, SQLException {
8997
final StringWriter sw = new StringWriter();
90-
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
9198
Class.forName("org.h2.Driver");
9299
final Connection connection = DriverManager.getConnection("jdbc:h2:mem:my_test;", "sa", "");
93-
final Statement stmt = connection.createStatement();
94-
stmt.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))");
95-
stmt.execute("insert into TEST values(1, 'r1')");
96-
stmt.execute("insert into TEST values(2, 'r2')");
97-
printer.printRecords(stmt.executeQuery("select ID, NAME from TEST"));
98-
assertEquals("1,r1" + lineSeparator + "2,r2" + lineSeparator, sw.toString());
100+
try {
101+
final Statement stmt = connection.createStatement();
102+
stmt.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))");
103+
stmt.execute("insert into TEST values(1, 'r1')");
104+
stmt.execute("insert into TEST values(2, 'r2')");
105+
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
106+
printer.printRecords(stmt.executeQuery("select ID, NAME from TEST"));
107+
assertEquals("1,r1" + lineSeparator + "2,r2" + lineSeparator, sw.toString());
108+
} finally {
109+
connection.close();
110+
}
99111
}
100112

101113
@Test

0 commit comments

Comments
 (0)