diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 67681987..7b8f0f75 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -39,7 +39,8 @@ - CSVFormat.EXCEL should ignore empty header names + CSVFormat.EXCEL should ignore empty header names + Add CSVFormat#with 0-arg methods matching boolean arg methods No longer works with Java 6 diff --git a/src/main/java/org/apache/commons/csv/CSVFormat.java b/src/main/java/org/apache/commons/csv/CSVFormat.java index 0338732f..e87be847 100644 --- a/src/main/java/org/apache/commons/csv/CSVFormat.java +++ b/src/main/java/org/apache/commons/csv/CSVFormat.java @@ -216,7 +216,7 @@ public final class CSVFormat implements Serializable { * Note: this is currently like {@link #RFC4180} plus {@link #withAllowMissingColumnNames(boolean) withAllowMissingColumnNames(true)}. *

*/ - public static final CSVFormat EXCEL = DEFAULT.withIgnoreEmptyLines(false).withAllowMissingColumnNames(true); + public static final CSVFormat EXCEL = DEFAULT.withIgnoreEmptyLines(false).withAllowMissingColumnNames(); /** * Tab-delimited format. @@ -234,7 +234,7 @@ public final class CSVFormat implements Serializable { public static final CSVFormat TDF = DEFAULT .withDelimiter(TAB) - .withIgnoreSurroundingSpaces(true); + .withIgnoreSurroundingSpaces(); /** * Default MySQL format used by the {@code SELECT INTO OUTFILE} and {@code LOAD DATA INFILE} operations. @@ -851,6 +851,17 @@ public final class CSVFormat implements Serializable { allowMissingColumnNames); } + /** + * Sets the missing column names behavior of the format to {@code true} + * + * @return A new CSVFormat that is equal to this but with the specified missing column names behavior. + * @see #withAllowMissingColumnNames(boolean) + * @since 1.1 + */ + public CSVFormat withAllowMissingColumnNames() { + return this.withAllowMissingColumnNames(true); + } + /** * Sets the missing column names behavior of the format. * @@ -865,6 +876,17 @@ public final class CSVFormat implements Serializable { allowMissingColumnNames); } + /** + * Sets the empty line skipping behavior of the format to {@code true}. + * + * @return A new CSVFormat that is equal to this but with the specified empty line skipping behavior. + * @since {@link #withIgnoreEmptyLines(boolean)} + * @since 1.1 + */ + public CSVFormat withIgnoreEmptyLines() { + return this.withIgnoreEmptyLines(true); + } + /** * Sets the empty line skipping behavior of the format. * @@ -879,6 +901,17 @@ public final class CSVFormat implements Serializable { allowMissingColumnNames); } + /** + * Sets the trimming behavior of the format to {@code true}. + * + * @return A new CSVFormat that is equal to this but with the specified trimming behavior. + * @see #withIgnoreSurroundingSpaces(boolean) + * @since 1.1 + */ + public CSVFormat withIgnoreSurroundingSpaces() { + return this.withIgnoreSurroundingSpaces(true); + } + /** * Sets the trimming behavior of the format. * @@ -993,6 +1026,21 @@ public final class CSVFormat implements Serializable { allowMissingColumnNames); } + /** + * Sets skipping the header record to {@code true}. + * + * @param skipHeaderRecord + * whether to skip the header record. + * + * @return A new CSVFormat that is equal to this but with the the specified skipHeaderRecord setting. + * @see #withSkipHeaderRecord(boolean) + * @see #withHeader(String...) + * @since 1.1 + */ + public CSVFormat withSkipHeaderRecord() { + return this.withSkipHeaderRecord(true); + } + /** * Sets whether to skip the header record. * diff --git a/src/test/java/org/apache/commons/csv/CSVFormatTest.java b/src/test/java/org/apache/commons/csv/CSVFormatTest.java index b836bbf6..6719571b 100644 --- a/src/test/java/org/apache/commons/csv/CSVFormatTest.java +++ b/src/test/java/org/apache/commons/csv/CSVFormatTest.java @@ -123,8 +123,8 @@ public class CSVFormatTest { .withCommentMarker('#') .withEscape('+') .withHeader("One", "Two", "Three") - .withIgnoreEmptyLines(true) - .withIgnoreSurroundingSpaces(true) + .withIgnoreEmptyLines() + .withIgnoreSurroundingSpaces() .withQuote('"') .withQuoteMode(QuoteMode.ALL); final CSVFormat left = right @@ -138,8 +138,8 @@ public class CSVFormatTest { final CSVFormat right = CSVFormat.newFormat('\'') .withCommentMarker('#') .withEscape('+') - .withIgnoreEmptyLines(true) - .withIgnoreSurroundingSpaces(true) + .withIgnoreEmptyLines() + .withIgnoreSurroundingSpaces() .withQuote('"') .withQuoteMode(QuoteMode.ALL); final CSVFormat left = right @@ -153,7 +153,7 @@ public class CSVFormatTest { final CSVFormat right = CSVFormat.newFormat('\'') .withCommentMarker('#') .withEscape('+') - .withIgnoreSurroundingSpaces(true) + .withIgnoreSurroundingSpaces() .withQuote('"') .withQuoteMode(QuoteMode.ALL); final CSVFormat left = right @@ -187,8 +187,8 @@ public class CSVFormatTest { .withRecordSeparator(CR) .withCommentMarker('#') .withEscape('+') - .withIgnoreEmptyLines(true) - .withIgnoreSurroundingSpaces(true) + .withIgnoreEmptyLines() + .withIgnoreSurroundingSpaces() .withQuote('"') .withQuoteMode(QuoteMode.ALL); final CSVFormat left = right @@ -203,8 +203,8 @@ public class CSVFormatTest { .withRecordSeparator(CR) .withCommentMarker('#') .withEscape('+') - .withIgnoreEmptyLines(true) - .withIgnoreSurroundingSpaces(true) + .withIgnoreEmptyLines() + .withIgnoreSurroundingSpaces() .withQuote('"') .withQuoteMode(QuoteMode.ALL) .withNullString("null"); @@ -220,12 +220,12 @@ public class CSVFormatTest { .withRecordSeparator(CR) .withCommentMarker('#') .withEscape('+') - .withIgnoreEmptyLines(true) - .withIgnoreSurroundingSpaces(true) + .withIgnoreEmptyLines() + .withIgnoreSurroundingSpaces() .withQuote('"') .withQuoteMode(QuoteMode.ALL) .withNullString("null") - .withSkipHeaderRecord(true); + .withSkipHeaderRecord(); final CSVFormat left = right .withSkipHeaderRecord(false); @@ -267,7 +267,7 @@ public class CSVFormatTest { @Test public void testNullRecordSeparatorCsv106() { - final CSVFormat format = CSVFormat.newFormat(';').withSkipHeaderRecord(true).withHeader("H1", "H2"); + final CSVFormat format = CSVFormat.newFormat(';').withSkipHeaderRecord().withHeader("H1", "H2"); final String formatStr = format.format("A", "B"); assertNotNull(formatStr); assertFalse(formatStr.endsWith("null")); @@ -377,13 +377,13 @@ public class CSVFormatTest { @Test public void testWithIgnoreEmptyLines() throws Exception { assertFalse(CSVFormat.DEFAULT.withIgnoreEmptyLines(false).getIgnoreEmptyLines()); - assertTrue(CSVFormat.DEFAULT.withIgnoreEmptyLines(true).getIgnoreEmptyLines()); + assertTrue(CSVFormat.DEFAULT.withIgnoreEmptyLines().getIgnoreEmptyLines()); } @Test public void testWithIgnoreSurround() throws Exception { assertFalse(CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(false).getIgnoreSurroundingSpaces()); - assertTrue(CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true).getIgnoreSurroundingSpaces()); + assertTrue(CSVFormat.DEFAULT.withIgnoreSurroundingSpaces().getIgnoreSurroundingSpaces()); } @Test diff --git a/src/test/java/org/apache/commons/csv/CSVParserTest.java b/src/test/java/org/apache/commons/csv/CSVParserTest.java index 059a7395..5c07d27c 100644 --- a/src/test/java/org/apache/commons/csv/CSVParserTest.java +++ b/src/test/java/org/apache/commons/csv/CSVParserTest.java @@ -100,7 +100,7 @@ public class CSVParserTest { { " 8 ", " \"quoted \"\" /\" / string\" " }, { "9", " \n " }, }; final CSVFormat format = CSVFormat.newFormat(',').withQuote('\'').withRecordSeparator(CRLF).withEscape('/') - .withIgnoreEmptyLines(true); + .withIgnoreEmptyLines(); final CSVParser parser = CSVParser.parse(code, format); final List records = parser.getRecords(); @@ -127,7 +127,7 @@ public class CSVParserTest { }; final CSVFormat format = CSVFormat.newFormat(',').withRecordSeparator(CRLF).withEscape('/') - .withIgnoreEmptyLines(true); + .withIgnoreEmptyLines(); final CSVParser parser = CSVParser.parse(code, format); final List records = parser.getRecords(); @@ -299,6 +299,23 @@ public class CSVParserTest { } } +// @Test +// public void testStartWithEmptyLinesThenHeaders() throws Exception { +// final String[] codes = { "\r\n\r\n\r\nhello,\r\n\r\n\r\n", "hello,\n\n\n", "hello,\"\"\r\n\r\n\r\n", "hello,\"\"\n\n\n" }; +// final String[][] res = { { "hello", "" }, { "" }, // Excel format does not ignore empty lines +// { "" } }; +// for (final String code : codes) { +// final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL); +// final List records = parser.getRecords(); +// assertEquals(res.length, records.size()); +// assertTrue(records.size() > 0); +// for (int i = 0; i < res.length; i++) { +// assertArrayEquals(res[i], records.get(i).values()); +// } +// parser.close(); +// } +// } + @Test public void testEndOfFileBehaviorCSV() throws Exception { final String[] codes = { "hello,\r\n\r\nworld,\r\n", "hello,\r\n\r\nworld,", "hello,\r\n\r\nworld,\"\"\r\n", @@ -433,7 +450,7 @@ public class CSVParserTest { @Test public void testGetLine() throws IOException { - final CSVParser parser = CSVParser.parse(CSV_INPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true)); + final CSVParser parser = CSVParser.parse(CSV_INPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces()); for (final String[] re : RESULT) { assertArrayEquals(re, parser.nextRecord().values()); } @@ -507,7 +524,7 @@ public class CSVParserTest { @Test public void testGetRecords() throws IOException { - final CSVParser parser = CSVParser.parse(CSV_INPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true)); + final CSVParser parser = CSVParser.parse(CSV_INPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces()); final List records = parser.getRecords(); assertEquals(RESULT.length, records.size()); assertTrue(records.size() > 0); @@ -584,13 +601,13 @@ public class CSVParserTest { @Test public void testHeadersMissing() throws Exception { final Reader in = new StringReader("a,,c,,d\n1,2,3,4\nx,y,z,zz"); - CSVFormat.DEFAULT.withHeader().withAllowMissingColumnNames(true).parse(in).iterator(); + CSVFormat.DEFAULT.withHeader().withAllowMissingColumnNames().parse(in).iterator(); } @Test public void testHeaderMissingWithNull() throws Exception { final Reader in = new StringReader("a,,c,,d\n1,2,3,4\nx,y,z,zz"); - CSVFormat.DEFAULT.withHeader().withNullString("").withAllowMissingColumnNames(true).parse(in).iterator(); + CSVFormat.DEFAULT.withHeader().withNullString("").withAllowMissingColumnNames().parse(in).iterator(); } @Test @@ -668,7 +685,7 @@ public class CSVParserTest { @Test public void testMappedButNotSetAsOutlook2007ContactExport() throws Exception { final Reader in = new StringReader("a,b,c\n1,2\nx,y,z"); - final Iterator records = CSVFormat.DEFAULT.withHeader("A", "B", "C").withSkipHeaderRecord(true) + final Iterator records = CSVFormat.DEFAULT.withHeader("A", "B", "C").withSkipHeaderRecord() .parse(in).iterator(); CSVRecord record; @@ -841,7 +858,7 @@ public class CSVParserTest { @Test public void testSkipSetHeader() throws Exception { final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z"); - final Iterator records = CSVFormat.DEFAULT.withHeader("a", "b", "c").withSkipHeaderRecord(true) + final Iterator records = CSVFormat.DEFAULT.withHeader("a", "b", "c").withSkipHeaderRecord() .parse(in).iterator(); final CSVRecord record = records.next(); assertEquals("1", record.get("a")); diff --git a/src/test/java/org/apache/commons/csv/LexerTest.java b/src/test/java/org/apache/commons/csv/LexerTest.java index 9f61d642..617b2404 100644 --- a/src/test/java/org/apache/commons/csv/LexerTest.java +++ b/src/test/java/org/apache/commons/csv/LexerTest.java @@ -59,7 +59,7 @@ public class LexerTest { @Test public void testSurroundingSpacesAreDeleted() throws IOException { final String code = "noSpaces, leadingSpaces,trailingSpaces , surroundingSpaces , ,,"; - final Lexer parser = getLexer(code, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true)); + final Lexer parser = getLexer(code, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces()); assertThat(parser.nextToken(new Token()), matches(TOKEN, "noSpaces")); assertThat(parser.nextToken(new Token()), matches(TOKEN, "leadingSpaces")); assertThat(parser.nextToken(new Token()), matches(TOKEN, "trailingSpaces")); @@ -72,7 +72,7 @@ public class LexerTest { @Test public void testSurroundingTabsAreDeleted() throws IOException { final String code = "noTabs,\tleadingTab,trailingTab\t,\tsurroundingTabs\t,\t\t,,"; - final Lexer parser = getLexer(code, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true)); + final Lexer parser = getLexer(code, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces()); assertThat(parser.nextToken(new Token()), matches(TOKEN, "noTabs")); assertThat(parser.nextToken(new Token()), matches(TOKEN, "leadingTab")); assertThat(parser.nextToken(new Token()), matches(TOKEN, "trailingTab")); @@ -98,7 +98,7 @@ public class LexerTest { "\n"+ "\n"+ "\n"; - final CSVFormat format = CSVFormat.DEFAULT.withIgnoreEmptyLines(true); + final CSVFormat format = CSVFormat.DEFAULT.withIgnoreEmptyLines(); final Lexer parser = getLexer(code, format); assertThat(parser.nextToken(new Token()), matches(TOKEN, "first")); @@ -241,7 +241,7 @@ public class LexerTest { * a, " foo " ,b */ final String code = "a,\"foo\",b\na, \" foo\",b\na,\"foo \" ,b\na, \" foo \" ,b"; - final Lexer parser = getLexer(code, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true)); + final Lexer parser = getLexer(code, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces()); assertThat(parser.nextToken(new Token()), matches(TOKEN, "a")); assertThat(parser.nextToken(new Token()), matches(TOKEN, "foo")); assertThat(parser.nextToken(new Token()), matches(EORECORD, "b"));