Skip to content

Commit 0030e1a

Browse files
committed
Add quote policy to format. (Considering renaming "encapsulator" to "quoteChar" so we have quoteChar and quotePolicy. Encapsulator makes me want to ask "encapsulate what"? fieldEncapsulator would be better but so verbose, quoteChar feels more to the point to me. )
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1398008 13f79535-47bb-0310-9956-ffa450edef68
1 parent 33eb71e commit 0030e1a

2 files changed

Lines changed: 55 additions & 23 deletions

File tree

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

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,12 @@ public class CSVFormat implements Serializable {
4747
private final boolean ignoreEmptyLines;
4848
private final String lineSeparator; // for outputs
4949
private final String[] header;
50+
private final Quote quotePolicy;
5051

5152
/**
5253
* Starting format; used for creating other formats.
5354
*/
54-
static final CSVFormat PRISTINE = new CSVFormat(COMMA, null, null, null, false, false, null, null);
55+
static final CSVFormat PRISTINE = new CSVFormat(COMMA, null, null, null, null, false, false, null, null);
5556

5657
/**
5758
* Standard comma separated format, as for {@link #RFC4180} but allowing blank lines.
@@ -130,6 +131,8 @@ public class CSVFormat implements Serializable {
130131
* the char used for value separation
131132
* @param encapsulator
132133
* the char used as value encapsulation marker
134+
* @param quotePolicy
135+
* the quote policy
133136
* @param commentStart
134137
* the char used for comment identification
135138
* @param escape
@@ -143,10 +146,12 @@ public class CSVFormat implements Serializable {
143146
* @param header
144147
* the header
145148
*/
146-
public CSVFormat(final char delimiter, final Character encapsulator, final Character commentStart, final Character escape, final
147-
boolean ignoreSurroundingSpaces, final boolean ignoreEmptyLines, final String lineSeparator, final String[] header) {
149+
public CSVFormat(final char delimiter, final Character encapsulator, final Quote quotePolicy, final Character commentStart, final Character escape, final
150+
boolean ignoreSurroundingSpaces, final boolean ignoreEmptyLines, final String lineSeparator,
151+
final String[] header) {
148152
this.delimiter = delimiter;
149153
this.encapsulator = encapsulator;
154+
this.quotePolicy = quotePolicy;
150155
this.commentStart = commentStart;
151156
this.escape = escape;
152157
this.ignoreSurroundingSpaces = ignoreSurroundingSpaces;
@@ -231,8 +236,8 @@ public CSVFormat withDelimiter(final Character delimiter) {
231236
if (isLineBreak(delimiter)) {
232237
throw new IllegalArgumentException("The delimiter cannot be a line break");
233238
}
234-
return new CSVFormat(delimiter, encapsulator, commentStart, escape, ignoreSurroundingSpaces,
235-
ignoreEmptyLines, lineSeparator, header);
239+
return new CSVFormat(delimiter, encapsulator, quotePolicy, commentStart, escape,
240+
ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header);
236241
}
237242

238243
/**
@@ -270,8 +275,8 @@ public CSVFormat withEncapsulator(final Character encapsulator) {
270275
if (isLineBreak(encapsulator)) {
271276
throw new IllegalArgumentException("The encapsulator cannot be a line break");
272277
}
273-
return new CSVFormat(delimiter, encapsulator, commentStart, escape, ignoreSurroundingSpaces,
274-
ignoreEmptyLines, lineSeparator, header);
278+
return new CSVFormat(delimiter, encapsulator, quotePolicy, commentStart, escape,
279+
ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header);
275280
}
276281

277282
/**
@@ -322,8 +327,8 @@ public CSVFormat withCommentStart(final Character commentStart) {
322327
if (isLineBreak(commentStart)) {
323328
throw new IllegalArgumentException("The comment start character cannot be a line break");
324329
}
325-
return new CSVFormat(delimiter, encapsulator, commentStart, escape, ignoreSurroundingSpaces,
326-
ignoreEmptyLines, lineSeparator, header);
330+
return new CSVFormat(delimiter, encapsulator, quotePolicy, commentStart, escape,
331+
ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header);
327332
}
328333

329334
/**
@@ -372,8 +377,8 @@ public CSVFormat withEscape(final Character escape) {
372377
if (isLineBreak(escape)) {
373378
throw new IllegalArgumentException("The escape character cannot be a line break");
374379
}
375-
return new CSVFormat(delimiter, encapsulator, commentStart, escape, ignoreSurroundingSpaces,
376-
ignoreEmptyLines, lineSeparator, header);
380+
return new CSVFormat(delimiter, encapsulator, quotePolicy, commentStart, escape,
381+
ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header);
377382
}
378383

379384
/**
@@ -404,8 +409,8 @@ public boolean getIgnoreSurroundingSpaces() {
404409
* @return A copy of this format with the specified trimming behavior.
405410
*/
406411
public CSVFormat withIgnoreSurroundingSpaces(final boolean ignoreSurroundingSpaces) {
407-
return new CSVFormat(delimiter, encapsulator, commentStart, escape, ignoreSurroundingSpaces,
408-
ignoreEmptyLines, lineSeparator, header);
412+
return new CSVFormat(delimiter, encapsulator, quotePolicy, commentStart, escape,
413+
ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header);
409414
}
410415

411416
/**
@@ -427,8 +432,8 @@ public boolean getIgnoreEmptyLines() {
427432
* @return A copy of this format with the specified empty line skipping behavior.
428433
*/
429434
public CSVFormat withIgnoreEmptyLines(final boolean ignoreEmptyLines) {
430-
return new CSVFormat(delimiter, encapsulator, commentStart, escape, ignoreSurroundingSpaces,
431-
ignoreEmptyLines, lineSeparator, header);
435+
return new CSVFormat(delimiter, encapsulator, quotePolicy, commentStart, escape,
436+
ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header);
432437
}
433438

434439
/**
@@ -449,8 +454,8 @@ public String getLineSeparator() {
449454
* @return A copy of this format using the specified output line separator
450455
*/
451456
public CSVFormat withLineSeparator(final char lineSeparator) {
452-
return new CSVFormat(delimiter, encapsulator, commentStart, escape, ignoreSurroundingSpaces,
453-
ignoreEmptyLines, String.valueOf(lineSeparator), header);
457+
return new CSVFormat(delimiter, encapsulator, quotePolicy, commentStart, escape,
458+
ignoreSurroundingSpaces, ignoreEmptyLines, String.valueOf(lineSeparator), header);
454459
}
455460

456461
/**
@@ -462,8 +467,21 @@ public CSVFormat withLineSeparator(final char lineSeparator) {
462467
* @return A copy of this format using the specified output line separator
463468
*/
464469
public CSVFormat withLineSeparator(final String lineSeparator) {
465-
return new CSVFormat(delimiter, encapsulator, commentStart, escape, ignoreSurroundingSpaces,
466-
ignoreEmptyLines, lineSeparator, header);
470+
return new CSVFormat(delimiter, encapsulator, quotePolicy, commentStart, escape,
471+
ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header);
472+
}
473+
474+
/**
475+
* Returns a copy of this format using the specified output quote policy.
476+
*
477+
* @param quotePolicy
478+
* the quote policy to be used for output.
479+
*
480+
* @return A copy of this format using the specified output line separator
481+
*/
482+
public CSVFormat withQuotePolicy(final Quote quotePolicy) {
483+
return new CSVFormat(delimiter, encapsulator, quotePolicy, commentStart, escape,
484+
ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header);
467485
}
468486

469487
String[] getHeader() {
@@ -490,8 +508,8 @@ String[] getHeader() {
490508
* @return A copy of this format using the specified header
491509
*/
492510
public CSVFormat withHeader(final String... header) {
493-
return new CSVFormat(delimiter, encapsulator, commentStart, escape, ignoreSurroundingSpaces,
494-
ignoreEmptyLines, lineSeparator, header);
511+
return new CSVFormat(delimiter, encapsulator, quotePolicy, commentStart, escape,
512+
ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header);
495513
}
496514

497515
/**
@@ -546,4 +564,13 @@ public String toString() {
546564
return sb.toString();
547565
}
548566

567+
/**
568+
* Returns the quote policy output fields.
569+
*
570+
* @return the quote policy
571+
*/
572+
public Quote getQuotePolicy() {
573+
return quotePolicy;
574+
}
575+
549576
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class CSVFormatTest {
3535

3636
@Test
3737
public void testImmutalibity() {
38-
final CSVFormat format = new CSVFormat('!', '!', '!', '!', true, true, CRLF, null);
38+
final CSVFormat format = new CSVFormat('!', '!', Quote.MINIMAL, '!', '!', true, true, CRLF, null);
3939

4040
format.withDelimiter('?');
4141
format.withEncapsulator('?');
@@ -44,6 +44,7 @@ public void testImmutalibity() {
4444
format.withEscape('?');
4545
format.withIgnoreSurroundingSpaces(false);
4646
format.withIgnoreEmptyLines(false);
47+
format.withQuotePolicy(Quote.ALL);
4748

4849
assertEquals('!', format.getDelimiter());
4950
assertEquals('!', format.getEncapsulator().charValue());
@@ -53,11 +54,13 @@ public void testImmutalibity() {
5354

5455
assertTrue(format.getIgnoreSurroundingSpaces());
5556
assertTrue(format.getIgnoreEmptyLines());
57+
58+
assertEquals(Quote.MINIMAL, format.getQuotePolicy());
5659
}
5760

5861
@Test
5962
public void testMutators() {
60-
final CSVFormat format = new CSVFormat('!', '!', '!', '!', true, true, CRLF, null);
63+
final CSVFormat format = new CSVFormat('!', '!', null, '!', '!', true, true, CRLF, null);
6164

6265
assertEquals('?', format.withDelimiter('?').getDelimiter());
6366
assertEquals('?', format.withEncapsulator('?').getEncapsulator().charValue());
@@ -67,6 +70,8 @@ public void testMutators() {
6770

6871
assertFalse(format.withIgnoreSurroundingSpaces(false).getIgnoreSurroundingSpaces());
6972
assertFalse(format.withIgnoreEmptyLines(false).getIgnoreEmptyLines());
73+
74+
assertEquals(Quote.ALL, format.withQuotePolicy(Quote.ALL).getQuotePolicy());
7075
}
7176

7277
@Test

0 commit comments

Comments
 (0)