Skip to content

Commit f6c0433

Browse files
committed
Added a constructor with a String to CSVParser and removed CSVUtils
git-svn-id: https://svn.apache.org/repos/asf/commons/sandbox/csv/trunk@1297135 13f79535-47bb-0310-9956-ffa450edef68
1 parent 1a35541 commit f6c0433

4 files changed

Lines changed: 23 additions & 253 deletions

File tree

src/main/java/org/apache/commons/csv/CSVParser.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.io.IOException;
2121
import java.io.Reader;
22+
import java.io.StringReader;
2223
import java.util.ArrayList;
2324
import java.util.Iterator;
2425
import java.util.List;
@@ -140,6 +141,16 @@ public CSVParser(Reader input, CSVFormat format) {
140141
this.format = format;
141142
}
142143

144+
/**
145+
* Customized CSV parser using the given {@link CSVFormat}
146+
*
147+
* @param input a String containing "csv-formatted" input
148+
* @param format the CSVFormat used for CSV parsing
149+
*/
150+
public CSVParser(String input, CSVFormat format) {
151+
this(new StringReader(input), format);
152+
}
153+
143154
// ======================================================
144155
// the parser
145156
// ======================================================

src/main/java/org/apache/commons/csv/CSVUtils.java

Lines changed: 0 additions & 85 deletions
This file was deleted.

src/test/java/org/apache/commons/csv/CSVParserTest.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ public void testExcelFormat1() throws IOException {
244244
{""},
245245
{"\"hello\"", " \"world\"", "abc\ndef", ""}
246246
};
247-
CSVParser parser = new CSVParser(new StringReader(code), CSVFormat.EXCEL);
247+
CSVParser parser = new CSVParser(code, CSVFormat.EXCEL);
248248
String[][] tmp = parser.getRecords();
249249
assertEquals(res.length, tmp.length);
250250
assertTrue(tmp.length > 0);
@@ -262,7 +262,7 @@ public void testExcelFormat2() throws Exception {
262262
{""},
263263
{"world", ""}
264264
};
265-
CSVParser parser = new CSVParser(new StringReader(code), CSVFormat.EXCEL);
265+
CSVParser parser = new CSVParser(code, CSVFormat.EXCEL);
266266
String[][] tmp = parser.getRecords();
267267
assertEquals(res.length, tmp.length);
268268
assertTrue(tmp.length > 0);
@@ -289,7 +289,7 @@ public void testEndOfFileBehaviourExcel() throws Exception {
289289
};
290290

291291
for (String code : codes) {
292-
CSVParser parser = new CSVParser(new StringReader(code), CSVFormat.EXCEL);
292+
CSVParser parser = new CSVParser(code, CSVFormat.EXCEL);
293293
String[][] tmp = parser.getRecords();
294294
assertEquals(res.length, tmp.length);
295295
assertTrue(tmp.length > 0);
@@ -314,9 +314,7 @@ public void testEndOfFileBehaviorCSV() throws Exception {
314314
{"hello", ""}, // CSV format ignores empty lines
315315
{"world", ""}
316316
};
317-
String code;
318-
for (int codeIndex = 0; codeIndex < codes.length; codeIndex++) {
319-
code = codes[codeIndex];
317+
for (String code : codes) {
320318
CSVParser parser = new CSVParser(new StringReader(code));
321319
String[][] tmp = parser.getRecords();
322320
assertEquals(res.length, tmp.length);
@@ -339,10 +337,8 @@ public void testEmptyLineBehaviourExcel() throws Exception {
339337
{""}, // Excel format does not ignore empty lines
340338
{""}
341339
};
342-
String code;
343-
for (int codeIndex = 0; codeIndex < codes.length; codeIndex++) {
344-
code = codes[codeIndex];
345-
CSVParser parser = new CSVParser(new StringReader(code), CSVFormat.EXCEL);
340+
for (String code : codes) {
341+
CSVParser parser = new CSVParser(code, CSVFormat.EXCEL);
346342
String[][] tmp = parser.getRecords();
347343
assertEquals(res.length, tmp.length);
348344
assertTrue(tmp.length > 0);
@@ -362,9 +358,7 @@ public void testEmptyLineBehaviourCSV() throws Exception {
362358
String[][] res = {
363359
{"hello", ""} // CSV format ignores empty lines
364360
};
365-
String code;
366-
for (int codeIndex = 0; codeIndex < codes.length; codeIndex++) {
367-
code = codes[codeIndex];
361+
for (String code : codes) {
368362
CSVParser parser = new CSVParser(new StringReader(code));
369363
String[][] tmp = parser.getRecords();
370364
assertEquals(res.length, tmp.length);
@@ -440,7 +434,7 @@ public void testBackslashEscaping() throws IOException {
440434

441435
CSVFormat format = new CSVFormat(',', '\'', CSVFormat.DISABLED, '/', false, false, true, true);
442436

443-
CSVParser parser = new CSVParser(new StringReader(code), format);
437+
CSVParser parser = new CSVParser(code, format);
444438
String[][] tmp = parser.getRecords();
445439
assertTrue(tmp.length > 0);
446440
for (int i = 0; i < res.length; i++) {
@@ -468,7 +462,7 @@ public void testBackslashEscaping2() throws IOException {
468462

469463
CSVFormat format = new CSVFormat(',', CSVFormat.DISABLED, CSVFormat.DISABLED, '/', false, false, true, true);
470464

471-
CSVParser parser = new CSVParser(new StringReader(code), format);
465+
CSVParser parser = new CSVParser(code, format);
472466
String[][] tmp = parser.getRecords();
473467
assertTrue(tmp.length > 0);
474468

@@ -495,7 +489,7 @@ public void testDefaultFormat() throws IOException {
495489
CSVFormat format = CSVFormat.DEFAULT;
496490
assertEquals(CSVFormat.DISABLED, format.getCommentStart());
497491

498-
CSVParser parser = new CSVParser(new StringReader(code), format);
492+
CSVParser parser = new CSVParser(code, format);
499493
String[][] tmp = parser.getRecords();
500494
assertTrue(tmp.length > 0);
501495

@@ -510,7 +504,7 @@ public void testDefaultFormat() throws IOException {
510504
};
511505

512506
format = new CSVFormat(',', '"', '#');
513-
parser = new CSVParser(new StringReader(code), format);
507+
parser = new CSVParser(code, format);
514508
tmp = parser.getRecords();
515509

516510
if (!CSVPrinterTest.equals(res_comments, tmp)) {
@@ -521,7 +515,7 @@ public void testDefaultFormat() throws IOException {
521515

522516
public void testUnicodeEscape() throws IOException {
523517
String code = "abc,\\u0070\\u0075\\u0062\\u006C\\u0069\\u0063";
524-
CSVParser parser = new CSVParser(new StringReader(code), CSVFormat.DEFAULT.withUnicodeEscapesInterpreted(true));
518+
CSVParser parser = new CSVParser(code, CSVFormat.DEFAULT.withUnicodeEscapesInterpreted(true));
525519
String[] data = parser.iterator().next();
526520
assertEquals(2, data.length);
527521
assertEquals("abc", data[0]);

src/test/java/org/apache/commons/csv/CSVUtilsTest.java

Lines changed: 0 additions & 150 deletions
This file was deleted.

0 commit comments

Comments
 (0)