Skip to content

Commit

Permalink
Revert "Add support for trailing text after the closing quote, for Ex…
Browse files Browse the repository at this point in the history
…cel compatibility."

This reverts commit ed0ca22.
  • Loading branch information
garydgregory committed Jan 21, 2023
1 parent d81528f commit c22ff41
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 64 deletions.
48 changes: 7 additions & 41 deletions src/main/java/org/apache/commons/csv/CSVFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,6 @@ public static Builder create(final CSVFormat csvFormat) {

private boolean allowMissingColumnNames;

private boolean allowTrailingText;

private boolean autoFlush;

private Character commentMarker;
Expand Down Expand Up @@ -266,7 +264,6 @@ private Builder(final CSVFormat csvFormat) {
this.autoFlush = csvFormat.autoFlush;
this.quotedNullString = csvFormat.quotedNullString;
this.duplicateHeaderMode = csvFormat.duplicateHeaderMode;
this.allowTrailingText = csvFormat.allowTrailingText;
}

/**
Expand Down Expand Up @@ -304,20 +301,6 @@ public Builder setAllowMissingColumnNames(final boolean allowMissingColumnNames)
return this;
}

/**
* Sets whether to allow trailing text in a quoted field, after the closing quote.
*
* @param allowTrailingText the trailing text behavior, {@code true} to append that text to the field contents, {@code false} to throw
* an {@link IOException}.
*
* @return This instance.
* @since 1.10.0
*/
public Builder setAllowTrailingText(final boolean allowTrailingText) {
this.allowTrailingText = allowTrailingText;
return this;
}

/**
* Sets whether to flush on close.
*
Expand Down Expand Up @@ -827,7 +810,7 @@ public CSVFormat getFormat() {
* @see Predefined#Default
*/
public static final CSVFormat DEFAULT = new CSVFormat(COMMA, DOUBLE_QUOTE_CHAR, null, null, null, false, true, CRLF, null, null, null, false, false, false,
false, false, false, DuplicateHeaderMode.ALLOW_ALL, false);
false, false, false, DuplicateHeaderMode.ALLOW_ALL);

/**
* 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
Expand All @@ -851,7 +834,6 @@ public CSVFormat getFormat() {
* <li>{@code setIgnoreEmptyLines(false)}</li>
* <li>{@code setAllowMissingColumnNames(true)}</li>
* <li>{@code setDuplicateHeaderMode(DuplicateHeaderMode.ALLOW_ALL)}</li>
* <li>{@code setAllowTrailingText(true)}</li>
* </ul>
* <p>
* Note: This is currently like {@link #RFC4180} plus {@link Builder#setAllowMissingColumnNames(boolean) Builder#setAllowMissingColumnNames(true)} and
Expand All @@ -864,7 +846,6 @@ public CSVFormat getFormat() {
public static final CSVFormat EXCEL = DEFAULT.builder()
.setIgnoreEmptyLines(false)
.setAllowMissingColumnNames(true)
.setAllowTrailingText(true)
.build();
// @formatter:on

Expand Down Expand Up @@ -1287,7 +1268,7 @@ private static boolean isTrimChar(final CharSequence charSequence, final int pos
*/
public static CSVFormat newFormat(final char delimiter) {
return new CSVFormat(String.valueOf(delimiter), null, null, null, null, false, false, null, null, null, null, false, false, false, false, false, false,
DuplicateHeaderMode.ALLOW_ALL, false);
DuplicateHeaderMode.ALLOW_ALL);
}

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

private final boolean allowMissingColumnNames;

private final boolean allowTrailingText;

private final boolean autoFlush;

private final Character commentMarker; // null if commenting is disabled
Expand Down Expand Up @@ -1387,7 +1366,6 @@ private CSVFormat(final Builder builder) {
this.autoFlush = builder.autoFlush;
this.quotedNullString = builder.quotedNullString;
this.duplicateHeaderMode = builder.duplicateHeaderMode;
this.allowTrailingText = builder.allowTrailingText;
validate();
}

Expand Down Expand Up @@ -1418,7 +1396,7 @@ private CSVFormat(final String delimiter, final Character quoteChar, final Quote
final boolean ignoreSurroundingSpaces, final boolean ignoreEmptyLines, final String recordSeparator, final String nullString,
final Object[] headerComments, final String[] header, final boolean skipHeaderRecord, final boolean allowMissingColumnNames,
final boolean ignoreHeaderCase, final boolean trim, final boolean trailingDelimiter, final boolean autoFlush,
final DuplicateHeaderMode duplicateHeaderMode, final boolean allowTrailingText) {
final DuplicateHeaderMode duplicateHeaderMode) {
this.delimiter = delimiter;
this.quoteCharacter = quoteChar;
this.quoteMode = quoteMode;
Expand All @@ -1438,7 +1416,6 @@ private CSVFormat(final String delimiter, final Character quoteChar, final Quote
this.autoFlush = autoFlush;
this.quotedNullString = quoteCharacter + nullString + quoteCharacter;
this.duplicateHeaderMode = duplicateHeaderMode;
this.allowTrailingText = allowTrailingText;
validate();
}

Expand Down Expand Up @@ -1492,8 +1469,7 @@ public boolean equals(final Object obj) {
ignoreHeaderCase == other.ignoreHeaderCase && ignoreSurroundingSpaces == other.ignoreSurroundingSpaces &&
Objects.equals(nullString, other.nullString) && Objects.equals(quoteCharacter, other.quoteCharacter) && quoteMode == other.quoteMode &&
Objects.equals(quotedNullString, other.quotedNullString) && Objects.equals(recordSeparator, other.recordSeparator) &&
skipHeaderRecord == other.skipHeaderRecord && trailingDelimiter == other.trailingDelimiter && trim == other.trim &&
allowTrailingText == other.allowTrailingText;
skipHeaderRecord == other.skipHeaderRecord && trailingDelimiter == other.trailingDelimiter && trim == other.trim;
}

/**
Expand Down Expand Up @@ -1536,16 +1512,6 @@ public boolean getAllowMissingColumnNames() {
return allowMissingColumnNames;
}

/**
* Gets whether quoted fields allow trailing text after the closing quote.
*
* @return {@code true} if allowed, {@code false} to throw an {@link IOException}.
* @since 1.10.0
*/
public boolean getAllowTrailingText() {
return allowTrailingText;
}

/**
* Gets whether to flush on close.
*
Expand Down Expand Up @@ -1726,9 +1692,9 @@ public int hashCode() {
int result = 1;
result = prime * result + Arrays.hashCode(headers);
result = prime * result + Arrays.hashCode(headerComments);
return prime * result + Objects.hash(duplicateHeaderMode, allowMissingColumnNames, allowTrailingText, autoFlush, commentMarker, delimiter,
escapeCharacter, ignoreEmptyLines, ignoreHeaderCase, ignoreSurroundingSpaces, nullString, quoteCharacter, quoteMode, quotedNullString,
recordSeparator, skipHeaderRecord, trailingDelimiter, trim);
return prime * result + Objects.hash(duplicateHeaderMode, allowMissingColumnNames, autoFlush, commentMarker, delimiter, escapeCharacter,
ignoreEmptyLines, ignoreHeaderCase, ignoreSurroundingSpaces, nullString, quoteCharacter, quoteMode, quotedNullString, recordSeparator,
skipHeaderRecord, trailingDelimiter, trim);
}

/**
Expand Down
14 changes: 4 additions & 10 deletions src/main/java/org/apache/commons/csv/Lexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ final class Lexer implements Closeable {

private final boolean ignoreSurroundingSpaces;
private final boolean ignoreEmptyLines;
private final boolean allowTrailingText;

/** The input stream */
private final ExtendedBufferedReader reader;
Expand All @@ -73,7 +72,6 @@ final class Lexer implements Closeable {
this.commentStart = mapNullToDisabled(format.getCommentMarker());
this.ignoreSurroundingSpaces = format.getIgnoreSurroundingSpaces();
this.ignoreEmptyLines = format.getIgnoreEmptyLines();
this.allowTrailingText = format.getAllowTrailingText();
this.delimiterBuf = new char[delimiter.length - 1];
this.escapeDelimiterBuf = new char[2 * delimiter.length - 1];
}
Expand Down Expand Up @@ -366,14 +364,10 @@ private Token parseEncapsulatedToken(final Token token) throws IOException {
token.type = EORECORD;
return token;
}
if (allowTrailingText) {
token.content.append((char) c);
} else {
if (!Character.isWhitespace((char)c)) {
// error invalid char between token and next delimiter
throw new IOException("(line " + getCurrentLineNumber() +
") invalid char between encapsulated token and delimiter");
}
if (!Character.isWhitespace((char)c)) {
// error invalid char between token and next delimiter
throw new IOException("(line " + getCurrentLineNumber() +
") invalid char between encapsulated token and delimiter");
}
}
}
Expand Down
13 changes: 0 additions & 13 deletions src/test/java/org/apache/commons/csv/LexerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -431,17 +431,4 @@ public void testTrimTrailingSpacesZeroLength() throws Exception {
lexer.trimTrailingSpaces(buffer);
assertThat(lexer.nextToken(new Token()), matches(EOF, ""));
}

@Test
public void testTrailingTextAfterQuote() throws Exception {
final String code = "\"a\" b,\"a\" \" b,\"a\" b \"\"";
try (final Lexer parser = createLexer(code, CSVFormat.Builder.create().setAllowTrailingText(true).build())) {
assertThat(parser.nextToken(new Token()), matches(TOKEN, "a b"));
assertThat(parser.nextToken(new Token()), matches(TOKEN, "a \" b"));
assertThat(parser.nextToken(new Token()), matches(EOF, "a b \"\""));
}
try (final Lexer parser = createLexer(code, CSVFormat.Builder.create().setAllowTrailingText(false).build())) {
assertThrows(IOException.class, () -> lexer.nextToken(new Token()));
}
}
}

0 comments on commit c22ff41

Please sign in to comment.