Skip to content

Commit f51f43d

Browse files
committed
Implement Quote.NON_NUMERIC and Quote.MINIMAL. NONE throws a 'not implemented yet' exception.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1398137 13f79535-47bb-0310-9956-ffa450edef68
1 parent b9b3b58 commit f51f43d

2 files changed

Lines changed: 36 additions & 8 deletions

File tree

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

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@ public void printComment(final String comment) throws IOException {
150150
println();
151151
}
152152

153-
private void print(final CharSequence value, final int offset, final int len) throws IOException {
153+
private void print(Object object, final CharSequence value, final int offset, final int len) throws IOException {
154154
if (format.isQuoting()) {
155-
printAndQuote(value, offset, len);
155+
printAndQuote(object, value, offset, len);
156156
} else if (format.isEscaping()) {
157157
printAndEscape(value, offset, len);
158158
} else {
@@ -207,7 +207,7 @@ void printAndEscape(final CharSequence value, final int offset, final int len) t
207207
}
208208
}
209209

210-
void printAndQuote(final CharSequence value, final int offset, final int len) throws IOException {
210+
void printAndQuote(Object object, final CharSequence value, final int offset, final int len) throws IOException {
211211
final boolean first = newLine; // is this the first value on this line?
212212
boolean quote = false;
213213
int start = offset;
@@ -219,9 +219,20 @@ 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 (format.getQuotePolicy() == Quote.ALL) {
222+
Quote quotePolicy = format.getQuotePolicy();
223+
if (quotePolicy == null) {
224+
quotePolicy = Quote.MINIMAL;
225+
}
226+
switch (quotePolicy) {
227+
case ALL:
223228
quote = true;
224-
} else {
229+
break;
230+
case NON_NUMERIC:
231+
quote = !(object instanceof Number);
232+
break;
233+
case NONE:
234+
throw new IllegalArgumentException("Not implemented yet");
235+
case MINIMAL:
225236
if (len <= 0) {
226237
// always quote an empty token that is the first
227238
// on the line, as it may be the only thing on the
@@ -270,8 +281,15 @@ void printAndQuote(final CharSequence value, final int offset, final int len) th
270281
out.append(value, start, end);
271282
return;
272283
}
284+
break;
273285
}
274286

287+
if (!quote) {
288+
// no encapsulation needed - write out the original value
289+
out.append(value, start, end);
290+
return;
291+
}
292+
275293
// we hit something that needed encapsulation
276294
out.append(quoteChar);
277295

@@ -313,7 +331,7 @@ public void print(Object object, final boolean checkForEscape) throws IOExceptio
313331
printDelimiter();
314332
out.append(value);
315333
} else {
316-
print(value, 0, value.length());
334+
print(object, value, 0, value.length());
317335
}
318336
}
319337

@@ -325,8 +343,10 @@ public void print(Object object, final boolean checkForEscape) throws IOExceptio
325343
* @throws IOException
326344
* If an I/O error occurs
327345
*/
328-
public void print(final Object value) throws IOException {
329-
print(value, true);
346+
public void print(final Object object) throws IOException {
347+
// null values are considered empty
348+
final String value = object == null ? EMPTY : object.toString();
349+
print(object, value, 0, value.length());
330350
}
331351

332352
/**

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,14 @@ public void testQuoteAll() throws IOException {
293293
assertEquals("\"a\",\"b\nc\",\"d\"" + lineSeparator, sw.toString());
294294
}
295295

296+
@Test
297+
public void testQuoteNonNumeric() throws IOException {
298+
final StringWriter sw = new StringWriter();
299+
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuotePolicy(Quote.NON_NUMERIC));
300+
printer.printRecord("a", "b\nc", Integer.valueOf(1));
301+
assertEquals("\"a\",\"b\nc\",1" + lineSeparator, sw.toString());
302+
}
303+
296304
@Test
297305
public void testRandom() throws Exception {
298306
final int iter = 10000;

0 commit comments

Comments
 (0)