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/05 18:27:28 UTC
svn commit: r1297135 - in /commons/sandbox/csv/trunk/src:
main/java/org/apache/commons/csv/CSVParser.java
main/java/org/apache/commons/csv/CSVUtils.java
test/java/org/apache/commons/csv/CSVParserTest.java
test/java/org/apache/commons/csv/CSVUtilsTest.java
Author: ebourg
Date: Mon Mar 5 17:27:28 2012
New Revision: 1297135
URL: http://svn.apache.org/viewvc?rev=1297135&view=rev
Log:
Added a constructor with a String to CSVParser and removed CSVUtils
Removed:
commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVUtils.java
commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVUtilsTest.java
Modified:
commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVParser.java
commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java
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=1297135&r1=1297134&r2=1297135&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 Mon Mar 5 17:27:28 2012
@@ -19,6 +19,7 @@ package org.apache.commons.csv;
import java.io.IOException;
import java.io.Reader;
+import java.io.StringReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@@ -140,6 +141,16 @@ public class CSVParser implements Iterab
this.format = format;
}
+ /**
+ * Customized CSV parser using the given {@link CSVFormat}
+ *
+ * @param input a String containing "csv-formatted" input
+ * @param format the CSVFormat used for CSV parsing
+ */
+ public CSVParser(String input, CSVFormat format) {
+ this(new StringReader(input), format);
+ }
+
// ======================================================
// the parser
// ======================================================
Modified: commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java
URL: http://svn.apache.org/viewvc/commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java?rev=1297135&r1=1297134&r2=1297135&view=diff
==============================================================================
--- commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java (original)
+++ commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java Mon Mar 5 17:27:28 2012
@@ -244,7 +244,7 @@ public class CSVParserTest extends TestC
{""},
{"\"hello\"", " \"world\"", "abc\ndef", ""}
};
- CSVParser parser = new CSVParser(new StringReader(code), CSVFormat.EXCEL);
+ CSVParser parser = new CSVParser(code, CSVFormat.EXCEL);
String[][] tmp = parser.getRecords();
assertEquals(res.length, tmp.length);
assertTrue(tmp.length > 0);
@@ -262,7 +262,7 @@ public class CSVParserTest extends TestC
{""},
{"world", ""}
};
- CSVParser parser = new CSVParser(new StringReader(code), CSVFormat.EXCEL);
+ CSVParser parser = new CSVParser(code, CSVFormat.EXCEL);
String[][] tmp = parser.getRecords();
assertEquals(res.length, tmp.length);
assertTrue(tmp.length > 0);
@@ -289,7 +289,7 @@ public class CSVParserTest extends TestC
};
for (String code : codes) {
- CSVParser parser = new CSVParser(new StringReader(code), CSVFormat.EXCEL);
+ CSVParser parser = new CSVParser(code, CSVFormat.EXCEL);
String[][] tmp = parser.getRecords();
assertEquals(res.length, tmp.length);
assertTrue(tmp.length > 0);
@@ -314,9 +314,7 @@ public class CSVParserTest extends TestC
{"hello", ""}, // CSV format ignores empty lines
{"world", ""}
};
- String code;
- for (int codeIndex = 0; codeIndex < codes.length; codeIndex++) {
- code = codes[codeIndex];
+ for (String code : codes) {
CSVParser parser = new CSVParser(new StringReader(code));
String[][] tmp = parser.getRecords();
assertEquals(res.length, tmp.length);
@@ -339,10 +337,8 @@ public class CSVParserTest extends TestC
{""}, // Excel format does not ignore empty lines
{""}
};
- String code;
- for (int codeIndex = 0; codeIndex < codes.length; codeIndex++) {
- code = codes[codeIndex];
- CSVParser parser = new CSVParser(new StringReader(code), CSVFormat.EXCEL);
+ for (String code : codes) {
+ CSVParser parser = new CSVParser(code, CSVFormat.EXCEL);
String[][] tmp = parser.getRecords();
assertEquals(res.length, tmp.length);
assertTrue(tmp.length > 0);
@@ -362,9 +358,7 @@ public class CSVParserTest extends TestC
String[][] res = {
{"hello", ""} // CSV format ignores empty lines
};
- String code;
- for (int codeIndex = 0; codeIndex < codes.length; codeIndex++) {
- code = codes[codeIndex];
+ for (String code : codes) {
CSVParser parser = new CSVParser(new StringReader(code));
String[][] tmp = parser.getRecords();
assertEquals(res.length, tmp.length);
@@ -440,7 +434,7 @@ public class CSVParserTest extends TestC
CSVFormat format = new CSVFormat(',', '\'', CSVFormat.DISABLED, '/', false, false, true, true);
- CSVParser parser = new CSVParser(new StringReader(code), format);
+ CSVParser parser = new CSVParser(code, format);
String[][] tmp = parser.getRecords();
assertTrue(tmp.length > 0);
for (int i = 0; i < res.length; i++) {
@@ -468,7 +462,7 @@ public class CSVParserTest extends TestC
CSVFormat format = new CSVFormat(',', CSVFormat.DISABLED, CSVFormat.DISABLED, '/', false, false, true, true);
- CSVParser parser = new CSVParser(new StringReader(code), format);
+ CSVParser parser = new CSVParser(code, format);
String[][] tmp = parser.getRecords();
assertTrue(tmp.length > 0);
@@ -495,7 +489,7 @@ public class CSVParserTest extends TestC
CSVFormat format = CSVFormat.DEFAULT;
assertEquals(CSVFormat.DISABLED, format.getCommentStart());
- CSVParser parser = new CSVParser(new StringReader(code), format);
+ CSVParser parser = new CSVParser(code, format);
String[][] tmp = parser.getRecords();
assertTrue(tmp.length > 0);
@@ -510,7 +504,7 @@ public class CSVParserTest extends TestC
};
format = new CSVFormat(',', '"', '#');
- parser = new CSVParser(new StringReader(code), format);
+ parser = new CSVParser(code, format);
tmp = parser.getRecords();
if (!CSVPrinterTest.equals(res_comments, tmp)) {
@@ -521,7 +515,7 @@ public class CSVParserTest extends TestC
public void testUnicodeEscape() throws IOException {
String code = "abc,\\u0070\\u0075\\u0062\\u006C\\u0069\\u0063";
- CSVParser parser = new CSVParser(new StringReader(code), CSVFormat.DEFAULT.withUnicodeEscapesInterpreted(true));
+ CSVParser parser = new CSVParser(code, CSVFormat.DEFAULT.withUnicodeEscapesInterpreted(true));
String[] data = parser.iterator().next();
assertEquals(2, data.length);
assertEquals("abc", data[0]);