Skip to content

Commit c22ff41

Browse files
committed
Revert "Add support for trailing text after the closing quote, for Excel compatibility."
This reverts commit ed0ca22.
1 parent d81528f commit c22ff41

3 files changed

Lines changed: 11 additions & 64 deletions

File tree

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

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,6 @@ public static Builder create(final CSVFormat csvFormat) {
208208

209209
private boolean allowMissingColumnNames;
210210

211-
private boolean allowTrailingText;
212-
213211
private boolean autoFlush;
214212

215213
private Character commentMarker;
@@ -266,7 +264,6 @@ private Builder(final CSVFormat csvFormat) {
266264
this.autoFlush = csvFormat.autoFlush;
267265
this.quotedNullString = csvFormat.quotedNullString;
268266
this.duplicateHeaderMode = csvFormat.duplicateHeaderMode;
269-
this.allowTrailingText = csvFormat.allowTrailingText;
270267
}
271268

272269
/**
@@ -304,20 +301,6 @@ public Builder setAllowMissingColumnNames(final boolean allowMissingColumnNames)
304301
return this;
305302
}
306303

307-
/**
308-
* Sets whether to allow trailing text in a quoted field, after the closing quote.
309-
*
310-
* @param allowTrailingText the trailing text behavior, {@code true} to append that text to the field contents, {@code false} to throw
311-
* an {@link IOException}.
312-
*
313-
* @return This instance.
314-
* @since 1.10.0
315-
*/
316-
public Builder setAllowTrailingText(final boolean allowTrailingText) {
317-
this.allowTrailingText = allowTrailingText;
318-
return this;
319-
}
320-
321304
/**
322305
* Sets whether to flush on close.
323306
*
@@ -827,7 +810,7 @@ public CSVFormat getFormat() {
827810
* @see Predefined#Default
828811
*/
829812
public static final CSVFormat DEFAULT = new CSVFormat(COMMA, DOUBLE_QUOTE_CHAR, null, null, null, false, true, CRLF, null, null, null, false, false, false,
830-
false, false, false, DuplicateHeaderMode.ALLOW_ALL, false);
813+
false, false, false, DuplicateHeaderMode.ALLOW_ALL);
831814

832815
/**
833816
* Excel file format (using a comma as the value delimiter). Note that the actual value delimiter used by Excel is locale dependent, it might be necessary
@@ -851,7 +834,6 @@ public CSVFormat getFormat() {
851834
* <li>{@code setIgnoreEmptyLines(false)}</li>
852835
* <li>{@code setAllowMissingColumnNames(true)}</li>
853836
* <li>{@code setDuplicateHeaderMode(DuplicateHeaderMode.ALLOW_ALL)}</li>
854-
* <li>{@code setAllowTrailingText(true)}</li>
855837
* </ul>
856838
* <p>
857839
* Note: This is currently like {@link #RFC4180} plus {@link Builder#setAllowMissingColumnNames(boolean) Builder#setAllowMissingColumnNames(true)} and
@@ -864,7 +846,6 @@ public CSVFormat getFormat() {
864846
public static final CSVFormat EXCEL = DEFAULT.builder()
865847
.setIgnoreEmptyLines(false)
866848
.setAllowMissingColumnNames(true)
867-
.setAllowTrailingText(true)
868849
.build();
869850
// @formatter:on
870851

@@ -1287,7 +1268,7 @@ private static boolean isTrimChar(final CharSequence charSequence, final int pos
12871268
*/
12881269
public static CSVFormat newFormat(final char delimiter) {
12891270
return new CSVFormat(String.valueOf(delimiter), null, null, null, null, false, false, null, null, null, null, false, false, false, false, false, false,
1290-
DuplicateHeaderMode.ALLOW_ALL, false);
1271+
DuplicateHeaderMode.ALLOW_ALL);
12911272
}
12921273

12931274
static String[] toStringArray(final Object[] values) {
@@ -1331,8 +1312,6 @@ public static CSVFormat valueOf(final String format) {
13311312

13321313
private final boolean allowMissingColumnNames;
13331314

1334-
private final boolean allowTrailingText;
1335-
13361315
private final boolean autoFlush;
13371316

13381317
private final Character commentMarker; // null if commenting is disabled
@@ -1387,7 +1366,6 @@ private CSVFormat(final Builder builder) {
13871366
this.autoFlush = builder.autoFlush;
13881367
this.quotedNullString = builder.quotedNullString;
13891368
this.duplicateHeaderMode = builder.duplicateHeaderMode;
1390-
this.allowTrailingText = builder.allowTrailingText;
13911369
validate();
13921370
}
13931371

@@ -1418,7 +1396,7 @@ private CSVFormat(final String delimiter, final Character quoteChar, final Quote
14181396
final boolean ignoreSurroundingSpaces, final boolean ignoreEmptyLines, final String recordSeparator, final String nullString,
14191397
final Object[] headerComments, final String[] header, final boolean skipHeaderRecord, final boolean allowMissingColumnNames,
14201398
final boolean ignoreHeaderCase, final boolean trim, final boolean trailingDelimiter, final boolean autoFlush,
1421-
final DuplicateHeaderMode duplicateHeaderMode, final boolean allowTrailingText) {
1399+
final DuplicateHeaderMode duplicateHeaderMode) {
14221400
this.delimiter = delimiter;
14231401
this.quoteCharacter = quoteChar;
14241402
this.quoteMode = quoteMode;
@@ -1438,7 +1416,6 @@ private CSVFormat(final String delimiter, final Character quoteChar, final Quote
14381416
this.autoFlush = autoFlush;
14391417
this.quotedNullString = quoteCharacter + nullString + quoteCharacter;
14401418
this.duplicateHeaderMode = duplicateHeaderMode;
1441-
this.allowTrailingText = allowTrailingText;
14421419
validate();
14431420
}
14441421

@@ -1492,8 +1469,7 @@ public boolean equals(final Object obj) {
14921469
ignoreHeaderCase == other.ignoreHeaderCase && ignoreSurroundingSpaces == other.ignoreSurroundingSpaces &&
14931470
Objects.equals(nullString, other.nullString) && Objects.equals(quoteCharacter, other.quoteCharacter) && quoteMode == other.quoteMode &&
14941471
Objects.equals(quotedNullString, other.quotedNullString) && Objects.equals(recordSeparator, other.recordSeparator) &&
1495-
skipHeaderRecord == other.skipHeaderRecord && trailingDelimiter == other.trailingDelimiter && trim == other.trim &&
1496-
allowTrailingText == other.allowTrailingText;
1472+
skipHeaderRecord == other.skipHeaderRecord && trailingDelimiter == other.trailingDelimiter && trim == other.trim;
14971473
}
14981474

14991475
/**
@@ -1536,16 +1512,6 @@ public boolean getAllowMissingColumnNames() {
15361512
return allowMissingColumnNames;
15371513
}
15381514

1539-
/**
1540-
* Gets whether quoted fields allow trailing text after the closing quote.
1541-
*
1542-
* @return {@code true} if allowed, {@code false} to throw an {@link IOException}.
1543-
* @since 1.10.0
1544-
*/
1545-
public boolean getAllowTrailingText() {
1546-
return allowTrailingText;
1547-
}
1548-
15491515
/**
15501516
* Gets whether to flush on close.
15511517
*
@@ -1726,9 +1692,9 @@ public int hashCode() {
17261692
int result = 1;
17271693
result = prime * result + Arrays.hashCode(headers);
17281694
result = prime * result + Arrays.hashCode(headerComments);
1729-
return prime * result + Objects.hash(duplicateHeaderMode, allowMissingColumnNames, allowTrailingText, autoFlush, commentMarker, delimiter,
1730-
escapeCharacter, ignoreEmptyLines, ignoreHeaderCase, ignoreSurroundingSpaces, nullString, quoteCharacter, quoteMode, quotedNullString,
1731-
recordSeparator, skipHeaderRecord, trailingDelimiter, trim);
1695+
return prime * result + Objects.hash(duplicateHeaderMode, allowMissingColumnNames, autoFlush, commentMarker, delimiter, escapeCharacter,
1696+
ignoreEmptyLines, ignoreHeaderCase, ignoreSurroundingSpaces, nullString, quoteCharacter, quoteMode, quotedNullString, recordSeparator,
1697+
skipHeaderRecord, trailingDelimiter, trim);
17321698
}
17331699

17341700
/**

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ final class Lexer implements Closeable {
5757

5858
private final boolean ignoreSurroundingSpaces;
5959
private final boolean ignoreEmptyLines;
60-
private final boolean allowTrailingText;
6160

6261
/** The input stream */
6362
private final ExtendedBufferedReader reader;
@@ -73,7 +72,6 @@ final class Lexer implements Closeable {
7372
this.commentStart = mapNullToDisabled(format.getCommentMarker());
7473
this.ignoreSurroundingSpaces = format.getIgnoreSurroundingSpaces();
7574
this.ignoreEmptyLines = format.getIgnoreEmptyLines();
76-
this.allowTrailingText = format.getAllowTrailingText();
7775
this.delimiterBuf = new char[delimiter.length - 1];
7876
this.escapeDelimiterBuf = new char[2 * delimiter.length - 1];
7977
}
@@ -366,14 +364,10 @@ private Token parseEncapsulatedToken(final Token token) throws IOException {
366364
token.type = EORECORD;
367365
return token;
368366
}
369-
if (allowTrailingText) {
370-
token.content.append((char) c);
371-
} else {
372-
if (!Character.isWhitespace((char)c)) {
373-
// error invalid char between token and next delimiter
374-
throw new IOException("(line " + getCurrentLineNumber() +
375-
") invalid char between encapsulated token and delimiter");
376-
}
367+
if (!Character.isWhitespace((char)c)) {
368+
// error invalid char between token and next delimiter
369+
throw new IOException("(line " + getCurrentLineNumber() +
370+
") invalid char between encapsulated token and delimiter");
377371
}
378372
}
379373
}

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -431,17 +431,4 @@ public void testTrimTrailingSpacesZeroLength() throws Exception {
431431
lexer.trimTrailingSpaces(buffer);
432432
assertThat(lexer.nextToken(new Token()), matches(EOF, ""));
433433
}
434-
435-
@Test
436-
public void testTrailingTextAfterQuote() throws Exception {
437-
final String code = "\"a\" b,\"a\" \" b,\"a\" b \"\"";
438-
try (final Lexer parser = createLexer(code, CSVFormat.Builder.create().setAllowTrailingText(true).build())) {
439-
assertThat(parser.nextToken(new Token()), matches(TOKEN, "a b"));
440-
assertThat(parser.nextToken(new Token()), matches(TOKEN, "a \" b"));
441-
assertThat(parser.nextToken(new Token()), matches(EOF, "a b \"\""));
442-
}
443-
try (final Lexer parser = createLexer(code, CSVFormat.Builder.create().setAllowTrailingText(false).build())) {
444-
assertThrows(IOException.class, () -> lexer.nextToken(new Token()));
445-
}
446-
}
447434
}

0 commit comments

Comments
 (0)