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
This commit is contained in:
Sebastian Bazley 2012-10-15 22:50:00 +00:00
parent 91058bb95f
commit cbce4da24b
2 changed files with 30 additions and 4 deletions

View File

@ -126,11 +126,22 @@ public class CSVFormat implements Serializable {
.withEscape(ESCAPE) .withEscape(ESCAPE)
.withLineSeparator(LF); .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. * Creates a customized CSV format.
* *
* @param delimiter * @param delimiter
* the char used for value separation * the char used for value separation, must not be a line break character
* @param quoteChar * @param quoteChar
* the char used as value encapsulation marker * the char used as value encapsulation marker
* @param quotePolicy * @param quotePolicy
@ -147,10 +158,14 @@ public class CSVFormat implements Serializable {
* the line separator to use for output * the line separator to use for output
* @param header * @param header
* the 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 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, boolean ignoreSurroundingSpaces, final boolean ignoreEmptyLines, final String lineSeparator,
final String[] header) { final String[] header) {
if (isLineBreak(delimiter)) {
throw new IllegalArgumentException("The delimiter cannot be a line break");
}
this.delimiter = delimiter; this.delimiter = delimiter;
this.quoteChar = quoteChar; this.quoteChar = quoteChar;
this.quotePolicy = quotePolicy; this.quotePolicy = quotePolicy;
@ -238,9 +253,6 @@ public class CSVFormat implements Serializable {
* thrown if the specified character is a line break * thrown if the specified character is a line break
*/ */
public CSVFormat withDelimiter(final char delimiter) { 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, return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,
ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header); ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header);
} }

View File

@ -89,6 +89,20 @@ public class CSVFormatTest {
@Test @Test
public void testValidation() { 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; final CSVFormat format = CSVFormat.DEFAULT;
try { try {