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:
parent
72d6e797ca
commit
a65806a126
|
@ -126,6 +126,42 @@ public class CSVFormat implements Cloneable, Serializable {
|
|||
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').
|
||||
*
|
||||
|
@ -140,8 +176,13 @@ public class CSVFormat implements Cloneable, Serializable {
|
|||
*
|
||||
* @param delimiter the 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) {
|
||||
if (isLineBreak(delimiter)) {
|
||||
throw new IllegalArgumentException("The delimiter cannot be a line break");
|
||||
}
|
||||
|
||||
CSVFormat format = clone();
|
||||
format.delimiter = delimiter;
|
||||
return format;
|
||||
|
@ -161,8 +202,13 @@ public class CSVFormat implements Cloneable, Serializable {
|
|||
*
|
||||
* @param encapsulator the 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) {
|
||||
if (isLineBreak(encapsulator)) {
|
||||
throw new IllegalArgumentException("The encapsulator cannot be a line break");
|
||||
}
|
||||
|
||||
CSVFormat format = clone();
|
||||
format.encapsulator = encapsulator;
|
||||
return format;
|
||||
|
@ -186,8 +232,13 @@ public class CSVFormat implements Cloneable, Serializable {
|
|||
*
|
||||
* @param commentStart 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) {
|
||||
if (isLineBreak(commentStart)) {
|
||||
throw new IllegalArgumentException("The comment start character cannot be a line break");
|
||||
}
|
||||
|
||||
CSVFormat format = clone();
|
||||
format.commentStart = commentStart;
|
||||
return format;
|
||||
|
@ -216,8 +267,13 @@ public class CSVFormat implements Cloneable, Serializable {
|
|||
*
|
||||
* @param escape the 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) {
|
||||
if (isLineBreak(escape)) {
|
||||
throw new IllegalArgumentException("The escape character cannot be a line break");
|
||||
}
|
||||
|
||||
CSVFormat format = clone();
|
||||
format.escape = escape;
|
||||
return format;
|
||||
|
|
|
@ -76,6 +76,7 @@ public class CSVParser implements Iterable<String[]> {
|
|||
* CSV parser using the default {@link CSVFormat}.
|
||||
*
|
||||
* @param input a Reader containing "csv-formatted" input
|
||||
* @throws IllegalArgumentException thrown if the parameters of the format are inconsistent
|
||||
*/
|
||||
public CSVParser(Reader input) {
|
||||
this(input, CSVFormat.DEFAULT);
|
||||
|
@ -86,8 +87,11 @@ public class CSVParser implements Iterable<String[]> {
|
|||
*
|
||||
* @param input a Reader containing "csv-formatted" input
|
||||
* @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) {
|
||||
format.validate();
|
||||
|
||||
if (format.isUnicodeEscapesInterpreted()) {
|
||||
input = new UnicodeUnescapeReader(input);
|
||||
}
|
||||
|
@ -100,6 +104,7 @@ public class CSVParser implements Iterable<String[]> {
|
|||
*
|
||||
* @param input a String containing "csv-formatted" input
|
||||
* @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) {
|
||||
this(new StringReader(input), format);
|
||||
|
|
|
@ -39,11 +39,14 @@ public class CSVPrinter {
|
|||
* is supported. Hybrid formats (encapsulation and escaping with a different character) are not supported.
|
||||
*
|
||||
* @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) {
|
||||
this.out = out;
|
||||
this.format = format == null ? CSVFormat.DEFAULT : format;
|
||||
|
||||
this.format.validate();
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
|
|
|
@ -70,4 +70,78 @@ public class CSVFormatTest extends TestCase {
|
|||
assertEquals("a,b,c", format.format("a", "b", "c"));
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue