From cbce4da24bbf4e52a940a4b24bb0dec096a1d16e Mon Sep 17 00:00:00 2001 From: Sebastian Bazley Date: Mon, 15 Oct 2012 22:50:00 +0000 Subject: [PATCH] Add ctor to create simplest possible CSV parser Does not make sense to allow delim = EOL in ctor but disable it in withDelimiter() git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1398556 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/csv/CSVFormat.java | 20 +++++++++++++++---- .../org/apache/commons/csv/CSVFormatTest.java | 14 +++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/apache/commons/csv/CSVFormat.java b/src/main/java/org/apache/commons/csv/CSVFormat.java index fa7dcc0b..a1a178a0 100644 --- a/src/main/java/org/apache/commons/csv/CSVFormat.java +++ b/src/main/java/org/apache/commons/csv/CSVFormat.java @@ -126,11 +126,22 @@ public class CSVFormat implements Serializable { .withEscape(ESCAPE) .withLineSeparator(LF); + /** + * Creates a basic CSV format. + * + * @param delimiter + * the char used for value separation, must not be a line break character + * @throws IllegalArgumentException if the delimiter is a line break character + */ + public CSVFormat(char delimiter){ + this(delimiter, null, null, null, null, false, false, null, null); + } + /** * Creates a customized CSV format. * * @param delimiter - * the char used for value separation + * the char used for value separation, must not be a line break character * @param quoteChar * the char used as value encapsulation marker * @param quotePolicy @@ -147,10 +158,14 @@ public class CSVFormat implements Serializable { * the line separator to use for output * @param header * the header + * @throws IllegalArgumentException if the delimiter is a line break character */ public CSVFormat(final char delimiter, final Character quoteChar, final Quote quotePolicy, final Character commentStart, final Character escape, final boolean ignoreSurroundingSpaces, final boolean ignoreEmptyLines, final String lineSeparator, final String[] header) { + if (isLineBreak(delimiter)) { + throw new IllegalArgumentException("The delimiter cannot be a line break"); + } this.delimiter = delimiter; this.quoteChar = quoteChar; this.quotePolicy = quotePolicy; @@ -238,9 +253,6 @@ public class CSVFormat implements Serializable { * thrown if the specified character is a line break */ public CSVFormat withDelimiter(final char delimiter) { - if (isLineBreak(delimiter)) { - throw new IllegalArgumentException("The delimiter cannot be a line break"); - } return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape, ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header); } diff --git a/src/test/java/org/apache/commons/csv/CSVFormatTest.java b/src/test/java/org/apache/commons/csv/CSVFormatTest.java index c4da1413..3c37e1d0 100644 --- a/src/test/java/org/apache/commons/csv/CSVFormatTest.java +++ b/src/test/java/org/apache/commons/csv/CSVFormatTest.java @@ -89,6 +89,20 @@ public class CSVFormatTest { @Test public void testValidation() { + try { + new CSVFormat('\n'); + fail(); + } catch (final IllegalArgumentException e) { + // expected + } + + try { + new CSVFormat('\r'); + fail(); + } catch (final IllegalArgumentException e) { + // expected + } + final CSVFormat format = CSVFormat.DEFAULT; try {