You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by eb...@apache.org on 2012/03/08 01:00:42 UTC
svn commit: r1298234 - in /commons/sandbox/csv/trunk/src:
main/java/org/apache/commons/csv/CSVFormat.java
main/java/org/apache/commons/csv/CSVParser.java
main/java/org/apache/commons/csv/CSVPrinter.java
test/java/org/apache/commons/csv/CSVFormatTest.java
Author: ebourg
Date: Thu Mar 8 00:00:42 2012
New Revision: 1298234
URL: http://svn.apache.org/viewvc?rev=1298234&view=rev
Log:
Validation of the format parameters (suggested by Bob Smith in SANDBOX-291)
Modified:
commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java
commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVParser.java
commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java
commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java
Modified: commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java
URL: http://svn.apache.org/viewvc/commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java?rev=1298234&r1=1298233&r2=1298234&view=diff
==============================================================================
--- commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java (original)
+++ commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java Thu Mar 8 00:00:42 2012
@@ -127,6 +127,42 @@ public class CSVFormat implements Clonea
}
/**
+ * 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').
*
* @return the delimiter character
@@ -140,8 +176,13 @@ public class CSVFormat implements Clonea
*
* @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 Clonea
*
* @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 Clonea
*
* @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 Clonea
*
* @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;
Modified: commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVParser.java
URL: http://svn.apache.org/viewvc/commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVParser.java?rev=1298234&r1=1298233&r2=1298234&view=diff
==============================================================================
--- commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVParser.java (original)
+++ commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVParser.java Thu Mar 8 00:00:42 2012
@@ -76,6 +76,7 @@ public class CSVParser implements Iterab
* 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 Iterab
*
* @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 Iterab
*
* @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);
Modified: commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java
URL: http://svn.apache.org/viewvc/commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java?rev=1298234&r1=1298233&r2=1298234&view=diff
==============================================================================
--- commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java (original)
+++ commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java Thu Mar 8 00:00:42 2012
@@ -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();
}
// ======================================================
Modified: commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java
URL: http://svn.apache.org/viewvc/commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java?rev=1298234&r1=1298233&r2=1298234&view=diff
==============================================================================
--- commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java (original)
+++ commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java Thu Mar 8 00:00:42 2012
@@ -70,4 +70,78 @@ public class CSVFormatTest extends TestC
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
+ }
+
+
+ }
}