Validation of the format parameters (suggested by Bob Smith in SANDBOX-291)

git-svn-id: https://svn.apache.org/repos/asf/commons/sandbox/csv/trunk@1298234 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Emmanuel Bourg 2012-03-08 00:00:42 +00:00
parent 72d6e797ca
commit a65806a126
4 changed files with 139 additions and 1 deletions

View File

@ -126,6 +126,42 @@ public class CSVFormat implements Cloneable, Serializable {
this.emptyLinesIgnored = emptyLinesIgnored; this.emptyLinesIgnored = emptyLinesIgnored;
} }
/**
* Returns true if the given character is a line break character.
*
* @param c the character to check
*
* @return true if <code>c</code> is a line break character
*/
private static boolean isLineBreak(char c) {
return c == '\n' || c == '\r';
}
/**
* Verifies the consistency of the parameters and throws an IllegalArgumentException if necessary.
*/
void validate() throws IllegalArgumentException {
if (delimiter == encapsulator) {
throw new IllegalArgumentException("The encapsulator character and the delimiter cannot be the same (\"" + encapsulator + "\")");
}
if (delimiter == escape) {
throw new IllegalArgumentException("The escape character and the delimiter cannot be the same (\"" + escape + "\")");
}
if (delimiter == commentStart) {
throw new IllegalArgumentException("The comment start character and the delimiter cannot be the same (\"" + commentStart + "\")");
}
if (encapsulator != DISABLED && encapsulator == commentStart) {
throw new IllegalArgumentException("The comment start character and the encapsulator cannot be the same (\"" + commentStart + "\")");
}
if (escape != DISABLED && escape == commentStart) {
throw new IllegalArgumentException("The comment start and the escape character cannot be the same (\"" + commentStart + "\")");
}
}
/** /**
* Returns the character delimiting the values (typically ';', ',' or '\t'). * Returns the character delimiting the values (typically ';', ',' or '\t').
* *
@ -140,8 +176,13 @@ public class CSVFormat implements Cloneable, Serializable {
* *
* @param delimiter the delimiter character * @param delimiter the delimiter character
* @return A copy of this format using the specified delimiter character * @return A copy of this format using the specified delimiter character
* @throws IllegalArgumentException thrown if the specified character is a line break
*/ */
public CSVFormat withDelimiter(char delimiter) { public CSVFormat withDelimiter(char delimiter) {
if (isLineBreak(delimiter)) {
throw new IllegalArgumentException("The delimiter cannot be a line break");
}
CSVFormat format = clone(); CSVFormat format = clone();
format.delimiter = delimiter; format.delimiter = delimiter;
return format; return format;
@ -161,8 +202,13 @@ public class CSVFormat implements Cloneable, Serializable {
* *
* @param encapsulator the encapsulator character * @param encapsulator the encapsulator character
* @return A copy of this format using the specified encapsulator character * @return A copy of this format using the specified encapsulator character
* @throws IllegalArgumentException thrown if the specified character is a line break
*/ */
public CSVFormat withEncapsulator(char encapsulator) { public CSVFormat withEncapsulator(char encapsulator) {
if (isLineBreak(encapsulator)) {
throw new IllegalArgumentException("The encapsulator cannot be a line break");
}
CSVFormat format = clone(); CSVFormat format = clone();
format.encapsulator = encapsulator; format.encapsulator = encapsulator;
return format; return format;
@ -186,8 +232,13 @@ public class CSVFormat implements Cloneable, Serializable {
* *
* @param commentStart the comment start marker * @param commentStart the comment start marker
* @return A copy of this format using the specified character as the comment start marker * @return A copy of this format using the specified character as the comment start marker
* @throws IllegalArgumentException thrown if the specified character is a line break
*/ */
public CSVFormat withCommentStart(char commentStart) { public CSVFormat withCommentStart(char commentStart) {
if (isLineBreak(commentStart)) {
throw new IllegalArgumentException("The comment start character cannot be a line break");
}
CSVFormat format = clone(); CSVFormat format = clone();
format.commentStart = commentStart; format.commentStart = commentStart;
return format; return format;
@ -216,8 +267,13 @@ public class CSVFormat implements Cloneable, Serializable {
* *
* @param escape the escape character * @param escape the escape character
* @return A copy of this format using the specified escape character * @return A copy of this format using the specified escape character
* @throws IllegalArgumentException thrown if the specified character is a line break
*/ */
public CSVFormat withEscape(char escape) { public CSVFormat withEscape(char escape) {
if (isLineBreak(escape)) {
throw new IllegalArgumentException("The escape character cannot be a line break");
}
CSVFormat format = clone(); CSVFormat format = clone();
format.escape = escape; format.escape = escape;
return format; return format;

View File

@ -76,6 +76,7 @@ public class CSVParser implements Iterable<String[]> {
* CSV parser using the default {@link CSVFormat}. * CSV parser using the default {@link CSVFormat}.
* *
* @param input a Reader containing "csv-formatted" input * @param input a Reader containing "csv-formatted" input
* @throws IllegalArgumentException thrown if the parameters of the format are inconsistent
*/ */
public CSVParser(Reader input) { public CSVParser(Reader input) {
this(input, CSVFormat.DEFAULT); this(input, CSVFormat.DEFAULT);
@ -86,8 +87,11 @@ public class CSVParser implements Iterable<String[]> {
* *
* @param input a Reader containing "csv-formatted" input * @param input a Reader containing "csv-formatted" input
* @param format the CSVFormat used for CSV parsing * @param format the CSVFormat used for CSV parsing
* @throws IllegalArgumentException thrown if the parameters of the format are inconsistent
*/ */
public CSVParser(Reader input, CSVFormat format) { public CSVParser(Reader input, CSVFormat format) {
format.validate();
if (format.isUnicodeEscapesInterpreted()) { if (format.isUnicodeEscapesInterpreted()) {
input = new UnicodeUnescapeReader(input); input = new UnicodeUnescapeReader(input);
} }
@ -100,6 +104,7 @@ public class CSVParser implements Iterable<String[]> {
* *
* @param input a String containing "csv-formatted" input * @param input a String containing "csv-formatted" input
* @param format the CSVFormat used for CSV parsing * @param format the CSVFormat used for CSV parsing
* @throws IllegalArgumentException thrown if the parameters of the format are inconsistent
*/ */
public CSVParser(String input, CSVFormat format) { public CSVParser(String input, CSVFormat format) {
this(new StringReader(input), format); this(new StringReader(input), format);

View File

@ -39,11 +39,14 @@ public class CSVPrinter {
* is supported. Hybrid formats (encapsulation and escaping with a different character) are not supported. * is supported. Hybrid formats (encapsulation and escaping with a different character) are not supported.
* *
* @param out stream to which to print. * @param out stream to which to print.
* @param format describes the CSV variation. * @param format the CSV format. If null the default format is used ({@link CSVFormat#DEFAULT})
* @throws IllegalArgumentException thrown if the parameters of the format are inconsistent
*/ */
public CSVPrinter(Appendable out, CSVFormat format) { public CSVPrinter(Appendable out, CSVFormat format) {
this.out = out; this.out = out;
this.format = format == null ? CSVFormat.DEFAULT : format; this.format = format == null ? CSVFormat.DEFAULT : format;
this.format.validate();
} }
// ====================================================== // ======================================================

View File

@ -70,4 +70,78 @@ public class CSVFormatTest extends TestCase {
assertEquals("a,b,c", format.format("a", "b", "c")); assertEquals("a,b,c", format.format("a", "b", "c"));
assertEquals("\"x,y\",z", format.format("x,y", "z")); assertEquals("\"x,y\",z", format.format("x,y", "z"));
} }
public void testValidation() {
CSVFormat format = CSVFormat.DEFAULT;
try {
format.withDelimiter('\n');
fail();
} catch (IllegalArgumentException e) {
// expected
}
try {
format.withEscape('\r');
fail();
} catch (IllegalArgumentException e) {
// expected
}
try {
format.withEncapsulator('\n');
fail();
} catch (IllegalArgumentException e) {
// expected
}
try {
format.withCommentStart('\r');
fail();
} catch (IllegalArgumentException e) {
// expected
}
try {
format.withDelimiter('!').withEscape('!').validate();
fail();
} catch (IllegalArgumentException e) {
// expected
}
try {
format.withDelimiter('!').withCommentStart('!').validate();
fail();
} catch (IllegalArgumentException e) {
// expected
}
try {
format.withEncapsulator('!').withCommentStart('!').validate();
fail();
} catch (IllegalArgumentException e) {
// expected
}
format.withEncapsulator(CSVFormat.DISABLED).withCommentStart(CSVFormat.DISABLED).validate();
try {
format.withEscape('!').withCommentStart('!').validate();
fail();
} catch (IllegalArgumentException e) {
// expected
}
format.withEscape(CSVFormat.DISABLED).withCommentStart(CSVFormat.DISABLED).validate();
try {
format.withEncapsulator('!').withDelimiter('!').validate();
fail();
} catch (IllegalArgumentException e) {
// expected
}
}
} }