diff --git a/src/main/java/org/apache/commons/csv/CSVFormat.java b/src/main/java/org/apache/commons/csv/CSVFormat.java index caa2005f..4ee41337 100644 --- a/src/main/java/org/apache/commons/csv/CSVFormat.java +++ b/src/main/java/org/apache/commons/csv/CSVFormat.java @@ -49,7 +49,7 @@ import java.util.Objects; import java.util.Set; /** - * Specifies the format of a CSV file and parses input. + * Specifies the format of a CSV file for parsing and writing. * *
* This class is immutable. *
+ *+ * Not all settings are used for both parsing and writing. + *
*/ public final class CSVFormat implements Serializable { @@ -1210,6 +1213,10 @@ public final class CSVFormat implements Serializable { return contains(source, CR) || contains(source, LF); } + static boolean isBlank(final String value) { + return value == null || value.trim().isEmpty(); + } + /** * Returns true if the given character is a line break character. * @@ -1232,10 +1239,12 @@ public final class CSVFormat implements Serializable { return c != null && isLineBreak(c.charValue()); } + /** Same test as in as {@link String#trim()}. */ private static boolean isTrimChar(final char ch) { return ch <= SP; } + /** Same test as in as {@link String#trim()}. */ private static boolean isTrimChar(final CharSequence charSequence, final int pos) { return isTrimChar(charSequence.charAt(pos)); } @@ -2250,8 +2259,16 @@ public final class CSVFormat implements Serializable { return sb.toString(); } + String trim(final String value) { + return getTrim() ? value.trim() : value; + } + /** - * Verifies the validity and consistency of the attributes, and throws an IllegalArgumentException if necessary. + * Verifies the validity and consistency of the attributes, and throws an {@link IllegalArgumentException} if necessary. + *+ * Because an instance can be used for both writing an parsing, not all conditions can be tested here. For example allowMissingColumnNames is only used for + * parsing, so it cannot be used here. + *
* * @throws IllegalArgumentException Throw when any attribute is invalid or inconsistent with other attributes. */ @@ -2289,11 +2306,11 @@ public final class CSVFormat implements Serializable { final Set* Calling this method is equivalent to calling: @@ -2463,7 +2480,7 @@ public final class CSVFormat implements Serializable { } /** - * Returns a new {@code CSVFormat} with the header of the format defined by the enum class. + * Builds a new {@code CSVFormat} with the header of the format defined by the enum class. * *
* Example: @@ -2493,7 +2510,7 @@ public final class CSVFormat implements Serializable { } /** - * Returns a new {@code CSVFormat} with the header of the format set from the result set metadata. The header can either be parsed automatically from the + * Builds a new {@code CSVFormat} with the header of the format set from the result set metadata. The header can either be parsed automatically from the * input file with: * *
@@ -2521,7 +2538,7 @@ public final class CSVFormat implements Serializable { } /** - * Returns a new {@code CSVFormat} with the header of the format set from the result set metadata. The header can either be parsed automatically from the + * Builds a new {@code CSVFormat} with the header of the format set from the result set metadata. The header can either be parsed automatically from the * input file with: * *@@ -2549,7 +2566,7 @@ public final class CSVFormat implements Serializable { } /** - * Returns a new {@code CSVFormat} with the header of the format set to the given values. The header can either be parsed automatically from the input file + * Builds a new {@code CSVFormat} with the header of the format set to the given values. The header can either be parsed automatically from the input file * with: * *@@ -2576,7 +2593,7 @@ public final class CSVFormat implements Serializable { } /** - * Returns a new {@code CSVFormat} with the header comments of the format set to the given values. The comments will be printed first, before the headers. + * Builds a new {@code CSVFormat} with the header comments of the format set to the given values. The comments will be printed first, before the headers. * This setting is ignored by the parser. * *@@ -2595,7 +2612,7 @@ public final class CSVFormat implements Serializable { } /** - * Returns a new {@code CSVFormat} with the empty line skipping behavior of the format set to {@code true}. + * Builds a new {@code CSVFormat} with the empty line skipping behavior of the format set to {@code true}. * * @return A new CSVFormat that is equal to this but with the specified empty line skipping behavior. * @see Builder#setIgnoreEmptyLines(boolean) @@ -2608,7 +2625,7 @@ public final class CSVFormat implements Serializable { } /** - * Returns a new {@code CSVFormat} with the empty line skipping behavior of the format set to the given value. + * Builds a new {@code CSVFormat} with the empty line skipping behavior of the format set to the given value. * * @param ignoreEmptyLines the empty line skipping behavior, {@code true} to ignore the empty lines between the records, {@code false} to translate empty * lines to empty records. @@ -2621,7 +2638,7 @@ public final class CSVFormat implements Serializable { } /** - * Returns a new {@code CSVFormat} with the header ignore case behavior set to {@code true}. + * Builds a new {@code CSVFormat} with the header ignore case behavior set to {@code true}. * * @return A new CSVFormat that will ignore case header name. * @see Builder#setIgnoreHeaderCase(boolean) @@ -2634,7 +2651,7 @@ public final class CSVFormat implements Serializable { } /** - * Returns a new {@code CSVFormat} with whether header names should be accessed ignoring case. + * Builds a new {@code CSVFormat} with whether header names should be accessed ignoring case. * * @param ignoreHeaderCase the case mapping behavior, {@code true} to access name/values, {@code false} to leave the mapping as is. * @return A new CSVFormat that will ignore case header name if specified as {@code true} @@ -2647,7 +2664,7 @@ public final class CSVFormat implements Serializable { } /** - * Returns a new {@code CSVFormat} with the parser trimming behavior of the format set to {@code true}. + * Builds a new {@code CSVFormat} with the parser trimming behavior of the format set to {@code true}. * * @return A new CSVFormat that is equal to this but with the specified parser trimming behavior. * @see Builder#setIgnoreSurroundingSpaces(boolean) @@ -2660,7 +2677,7 @@ public final class CSVFormat implements Serializable { } /** - * Returns a new {@code CSVFormat} with the parser trimming behavior of the format set to the given value. + * Builds a new {@code CSVFormat} with the parser trimming behavior of the format set to the given value. * * @param ignoreSurroundingSpaces the parser trimming behavior, {@code true} to remove the surrounding spaces, {@code false} to leave the spaces as is. * @return A new CSVFormat that is equal to this but with the specified trimming behavior. @@ -2672,7 +2689,7 @@ public final class CSVFormat implements Serializable { } /** - * Returns a new {@code CSVFormat} with conversions to and from null for strings on input and output. + * Builds a new {@code CSVFormat} with conversions to and from null for strings on input and output. *
* Note: This setting is only used during printing and does not affect parsing. Parsing currently only works for inputs with '\n', '\r' and @@ -2744,7 +2761,7 @@ public final class CSVFormat implements Serializable { } /** - * Returns a new {@code CSVFormat} with the record separator of the format set to the specified String. + * Builds a new {@code CSVFormat} with the record separator of the format set to the specified String. * *
* Note: This setting is only used during printing and does not affect parsing. Parsing currently only works for inputs with '\n', '\r' and @@ -2762,7 +2779,7 @@ public final class CSVFormat implements Serializable { } /** - * Returns a new {@code CSVFormat} with skipping the header record set to {@code true}. + * Builds a new {@code CSVFormat} with skipping the header record set to {@code true}. * * @return A new CSVFormat that is equal to this but with the specified skipHeaderRecord setting. * @see Builder#setSkipHeaderRecord(boolean) @@ -2776,7 +2793,7 @@ public final class CSVFormat implements Serializable { } /** - * Returns a new {@code CSVFormat} with whether to skip the header record. + * Builds a new {@code CSVFormat} with whether to skip the header record. * * @param skipHeaderRecord whether to skip the header record. * @return A new CSVFormat that is equal to this but with the specified skipHeaderRecord setting. @@ -2789,7 +2806,7 @@ public final class CSVFormat implements Serializable { } /** - * Returns a new {@code CSVFormat} with the record separator of the format set to the operating system's line separator string, typically CR+LF on Windows + * Builds a new {@code CSVFormat} with the record separator of the format set to the operating system's line separator string, typically CR+LF on Windows * and LF on Linux. * *
@@ -2807,7 +2824,7 @@ public final class CSVFormat implements Serializable {
}
/**
- * Returns a new {@code CSVFormat} to add a trailing delimiter.
+ * Builds a new {@code CSVFormat} to add a trailing delimiter.
*
* @return A new CSVFormat that is equal to this but with the trailing delimiter setting.
* @since 1.3
@@ -2819,7 +2836,7 @@ public final class CSVFormat implements Serializable {
}
/**
- * Returns a new {@code CSVFormat} with whether to add a trailing delimiter.
+ * Builds a new {@code CSVFormat} with whether to add a trailing delimiter.
*
* @param trailingDelimiter whether to add a trailing delimiter.
* @return A new CSVFormat that is equal to this but with the specified trailing delimiter setting.
@@ -2832,7 +2849,7 @@ public final class CSVFormat implements Serializable {
}
/**
- * Returns a new {@code CSVFormat} to trim leading and trailing blanks. See {@link #getTrim()} for details of where this is used.
+ * Builds a new {@code CSVFormat} to trim leading and trailing blanks. See {@link #getTrim()} for details of where this is used.
*
* @return A new CSVFormat that is equal to this but with the trim setting on.
* @since 1.3
@@ -2844,7 +2861,7 @@ public final class CSVFormat implements Serializable {
}
/**
- * Returns a new {@code CSVFormat} with whether to trim leading and trailing blanks. See {@link #getTrim()} for details of where this is used.
+ * Builds a new {@code CSVFormat} with whether to trim leading and trailing blanks. See {@link #getTrim()} for details of where this is used.
*
* @param trim whether to trim leading and trailing blanks.
* @return A new CSVFormat that is equal to this but with the specified trim setting.
diff --git a/src/main/java/org/apache/commons/csv/CSVParser.java b/src/main/java/org/apache/commons/csv/CSVParser.java
index 74fade45..451b7c9f 100644
--- a/src/main/java/org/apache/commons/csv/CSVParser.java
+++ b/src/main/java/org/apache/commons/csv/CSVParser.java
@@ -441,12 +441,11 @@ public final class CSVParser implements Iterable