Skip to content

Commit df08aca

Browse files
committed
Add org.apache.commons.csv.CSVFormat.CSVFormatBuilder.parse(Reader).
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1461202 13f79535-47bb-0310-9956-ffa450edef68
1 parent e33bede commit df08aca

3 files changed

Lines changed: 34 additions & 15 deletions

File tree

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,19 @@
3232
import java.util.Arrays;
3333

3434
/**
35-
* The format specification of a CSV file.
35+
* Specifies the format of a CSV file and parses input.
3636
* <p>
3737
* This class is immutable.
3838
* </p>
39-
* <p>
4039
* You can extend a format through a builder. For example, to extend the Excel format with columns header, you write:
4140
* </p>
4241
* <pre>CSVFormat.EXCEL.toBuilder().withHeader(&quot;Col1&quot;, &quot;Col2&quot;, &quot;Col3&quot;).build();</pre>
42+
* <p>
43+
* You can parse through a format. For example, to parse an Excel file with columns header, you write:
44+
* </p>
45+
* <pre>Reader in = ...;
46+
*CSVFormat.EXCEL.toBuilder().withHeader(&quot;Col1&quot;, &quot;Col2&quot;, &quot;Col3&quot;).parse(in);</pre>
47+
* <p>
4348
*
4449
* @version $Id$
4550
*/
@@ -346,6 +351,7 @@ String[] getHeader() {
346351
*
347352
* @param in
348353
* the input stream
354+
* @return a stream of CSVRecord
349355
* @throws IOException
350356
*/
351357
public Iterable<CSVRecord> parse(final Reader in) throws IOException {
@@ -585,6 +591,19 @@ public CSVFormat build() {
585591
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, header);
586592
}
587593

594+
/**
595+
* Parses the specified content. Short-hand for:
596+
* <pre>format.build().parse(in);</pre>
597+
*
598+
* @param in
599+
* the input stream
600+
* @return a CSVRecord stream
601+
* @throws IOException
602+
*/
603+
public Iterable<CSVRecord> parse(final Reader in) throws IOException {
604+
return this.build().parse(in);
605+
}
606+
588607
/**
589608
* Verifies the consistency of the parameters and throws an IllegalStateException if necessary.
590609
*

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,36 +91,36 @@ public void testCSVFile() throws Exception {
9191
// first line starts with csv data file name
9292
final BufferedReader csvFile = new BufferedReader(new FileReader(new File(BASE, split[0])));
9393
final CSVFormatBuilder builder = CSVFormat.newBuilder(',').withQuoteChar('"');
94-
CSVFormat fmt = builder.build();
94+
CSVFormat format = builder.build();
9595
boolean checkComments = false;
9696
for(int i=1; i < split.length; i++) {
9797
final String option = split[i];
9898
final String[] option_parts = option.split("=",2);
9999
if ("IgnoreEmpty".equalsIgnoreCase(option_parts[0])){
100-
fmt = builder.withIgnoreEmptyLines(Boolean.parseBoolean(option_parts[1])).build();
100+
format = builder.withIgnoreEmptyLines(Boolean.parseBoolean(option_parts[1])).build();
101101
} else if ("IgnoreSpaces".equalsIgnoreCase(option_parts[0])) {
102-
fmt = builder.withIgnoreSurroundingSpaces(Boolean.parseBoolean(option_parts[1])).build();
102+
format = builder.withIgnoreSurroundingSpaces(Boolean.parseBoolean(option_parts[1])).build();
103103
} else if ("CommentStart".equalsIgnoreCase(option_parts[0])) {
104-
fmt = builder.withCommentStart(option_parts[1].charAt(0)).build();
104+
format = builder.withCommentStart(option_parts[1].charAt(0)).build();
105105
} else if ("CheckComments".equalsIgnoreCase(option_parts[0])) {
106106
checkComments = true;
107107
} else {
108108
fail(testName+" unexpected option: "+option);
109109
}
110110
}
111111
line = readTestData(); // get string version of format
112-
assertEquals(testName+" Expected format ", line, fmt.toString());
112+
assertEquals(testName+" Expected format ", line, format.toString());
113113

114114
// Now parse the file and compare against the expected results
115-
for(final CSVRecord rec : fmt.parse(csvFile)) {
116-
String parsed = rec.toString();
115+
for(final CSVRecord record : format.parse(csvFile)) {
116+
String parsed = record.toString();
117117
if (checkComments) {
118-
final String comment = rec.getComment().replace("\n", "\\n");
118+
final String comment = record.getComment().replace("\n", "\\n");
119119
if (comment != null) {
120120
parsed += "#" + comment;
121121
}
122122
}
123-
final int count = rec.size();
123+
final int count = record.size();
124124
assertEquals(testName, readTestData(), count+":"+parsed);
125125
}
126126
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ public void testIterator() throws Exception {
481481
public void testHeader() throws Exception {
482482
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
483483

484-
final Iterator<CSVRecord> records = CSVFormat.newBuilder().withHeader().build().parse(in).iterator();
484+
final Iterator<CSVRecord> records = CSVFormat.newBuilder().withHeader().parse(in).iterator();
485485

486486
for (int i = 0; i < 2; i++) {
487487
assertTrue(records.hasNext());
@@ -498,7 +498,7 @@ public void testHeader() throws Exception {
498498
public void testHeaderComment() throws Exception {
499499
final Reader in = new StringReader("# comment\na,b,c\n1,2,3\nx,y,z");
500500

501-
final Iterator<CSVRecord> records = CSVFormat.newBuilder().withCommentStart('#').withHeader().build().parse(in).iterator();
501+
final Iterator<CSVRecord> records = CSVFormat.newBuilder().withCommentStart('#').withHeader().parse(in).iterator();
502502

503503
for (int i = 0; i < 2; i++) {
504504
assertTrue(records.hasNext());
@@ -515,7 +515,7 @@ public void testHeaderComment() throws Exception {
515515
public void testProvidedHeader() throws Exception {
516516
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
517517

518-
final Iterator<CSVRecord> records = CSVFormat.newBuilder().withHeader("A", "B", "C").build().parse(in).iterator();
518+
final Iterator<CSVRecord> records = CSVFormat.newBuilder().withHeader("A", "B", "C").parse(in).iterator();
519519

520520
for (int i = 0; i < 3; i++) {
521521
assertTrue(records.hasNext());
@@ -536,7 +536,7 @@ public void testProvidedHeader() throws Exception {
536536
public void testMappedButNotSetAsOutlook2007ContactExport() throws Exception {
537537
final Reader in = new StringReader("a,b,c\n1,2\nx,y,z");
538538

539-
final Iterator<CSVRecord> records = CSVFormat.newBuilder().withHeader("A", "B", "C").build().parse(in).iterator();
539+
final Iterator<CSVRecord> records = CSVFormat.newBuilder().withHeader("A", "B", "C").parse(in).iterator();
540540

541541
// header record
542542
assertTrue(records.hasNext());

0 commit comments

Comments
 (0)