From df08acad745aef61a44c7f392abbee4ef9e201f8 Mon Sep 17 00:00:00 2001 From: "Gary D. Gregory" Date: Tue, 26 Mar 2013 16:19:48 +0000 Subject: [PATCH] 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 --- .../org/apache/commons/csv/CSVFormat.java | 23 +++++++++++++++++-- .../apache/commons/csv/CSVFileParserTest.java | 18 +++++++-------- .../org/apache/commons/csv/CSVParserTest.java | 8 +++---- 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/apache/commons/csv/CSVFormat.java b/src/main/java/org/apache/commons/csv/CSVFormat.java index 99ab34be..82c11d28 100644 --- a/src/main/java/org/apache/commons/csv/CSVFormat.java +++ b/src/main/java/org/apache/commons/csv/CSVFormat.java @@ -32,14 +32,19 @@ import java.io.StringWriter; import java.util.Arrays; /** - * The format specification of a CSV file. + * Specifies the format of a CSV file and parses input. *

* This class is immutable. *

- *

* You can extend a format through a builder. For example, to extend the Excel format with columns header, you write: *

*
CSVFormat.EXCEL.toBuilder().withHeader("Col1", "Col2", "Col3").build();
+ *

+ * You can parse through a format. For example, to parse an Excel file with columns header, you write: + *

+ *
Reader in = ...;
+ *CSVFormat.EXCEL.toBuilder().withHeader("Col1", "Col2", "Col3").parse(in);
+ *

* * @version $Id$ */ @@ -346,6 +351,7 @@ public class CSVFormat implements Serializable { * * @param in * the input stream + * @return a stream of CSVRecord * @throws IOException */ public Iterable parse(final Reader in) throws IOException { @@ -585,6 +591,19 @@ public class CSVFormat implements Serializable { ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, header); } + /** + * Parses the specified content. Short-hand for: + *

format.build().parse(in);
+ * + * @param in + * the input stream + * @return a CSVRecord stream + * @throws IOException + */ + public Iterable parse(final Reader in) throws IOException { + return this.build().parse(in); + } + /** * Verifies the consistency of the parameters and throws an IllegalStateException if necessary. * diff --git a/src/test/java/org/apache/commons/csv/CSVFileParserTest.java b/src/test/java/org/apache/commons/csv/CSVFileParserTest.java index 75958d8f..6565ceb7 100644 --- a/src/test/java/org/apache/commons/csv/CSVFileParserTest.java +++ b/src/test/java/org/apache/commons/csv/CSVFileParserTest.java @@ -91,17 +91,17 @@ public class CSVFileParserTest { // first line starts with csv data file name final BufferedReader csvFile = new BufferedReader(new FileReader(new File(BASE, split[0]))); final CSVFormatBuilder builder = CSVFormat.newBuilder(',').withQuoteChar('"'); - CSVFormat fmt = builder.build(); + CSVFormat format = builder.build(); boolean checkComments = false; for(int i=1; i < split.length; i++) { final String option = split[i]; final String[] option_parts = option.split("=",2); if ("IgnoreEmpty".equalsIgnoreCase(option_parts[0])){ - fmt = builder.withIgnoreEmptyLines(Boolean.parseBoolean(option_parts[1])).build(); + format = builder.withIgnoreEmptyLines(Boolean.parseBoolean(option_parts[1])).build(); } else if ("IgnoreSpaces".equalsIgnoreCase(option_parts[0])) { - fmt = builder.withIgnoreSurroundingSpaces(Boolean.parseBoolean(option_parts[1])).build(); + format = builder.withIgnoreSurroundingSpaces(Boolean.parseBoolean(option_parts[1])).build(); } else if ("CommentStart".equalsIgnoreCase(option_parts[0])) { - fmt = builder.withCommentStart(option_parts[1].charAt(0)).build(); + format = builder.withCommentStart(option_parts[1].charAt(0)).build(); } else if ("CheckComments".equalsIgnoreCase(option_parts[0])) { checkComments = true; } else { @@ -109,18 +109,18 @@ public class CSVFileParserTest { } } line = readTestData(); // get string version of format - assertEquals(testName+" Expected format ", line, fmt.toString()); + assertEquals(testName+" Expected format ", line, format.toString()); // Now parse the file and compare against the expected results - for(final CSVRecord rec : fmt.parse(csvFile)) { - String parsed = rec.toString(); + for(final CSVRecord record : format.parse(csvFile)) { + String parsed = record.toString(); if (checkComments) { - final String comment = rec.getComment().replace("\n", "\\n"); + final String comment = record.getComment().replace("\n", "\\n"); if (comment != null) { parsed += "#" + comment; } } - final int count = rec.size(); + final int count = record.size(); assertEquals(testName, readTestData(), count+":"+parsed); } } diff --git a/src/test/java/org/apache/commons/csv/CSVParserTest.java b/src/test/java/org/apache/commons/csv/CSVParserTest.java index 11d70bea..9f60a62a 100644 --- a/src/test/java/org/apache/commons/csv/CSVParserTest.java +++ b/src/test/java/org/apache/commons/csv/CSVParserTest.java @@ -481,7 +481,7 @@ public class CSVParserTest { public void testHeader() throws Exception { final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z"); - final Iterator records = CSVFormat.newBuilder().withHeader().build().parse(in).iterator(); + final Iterator records = CSVFormat.newBuilder().withHeader().parse(in).iterator(); for (int i = 0; i < 2; i++) { assertTrue(records.hasNext()); @@ -498,7 +498,7 @@ public class CSVParserTest { public void testHeaderComment() throws Exception { final Reader in = new StringReader("# comment\na,b,c\n1,2,3\nx,y,z"); - final Iterator records = CSVFormat.newBuilder().withCommentStart('#').withHeader().build().parse(in).iterator(); + final Iterator records = CSVFormat.newBuilder().withCommentStart('#').withHeader().parse(in).iterator(); for (int i = 0; i < 2; i++) { assertTrue(records.hasNext()); @@ -515,7 +515,7 @@ public class CSVParserTest { public void testProvidedHeader() throws Exception { final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z"); - final Iterator records = CSVFormat.newBuilder().withHeader("A", "B", "C").build().parse(in).iterator(); + final Iterator records = CSVFormat.newBuilder().withHeader("A", "B", "C").parse(in).iterator(); for (int i = 0; i < 3; i++) { assertTrue(records.hasNext()); @@ -536,7 +536,7 @@ public class CSVParserTest { public void testMappedButNotSetAsOutlook2007ContactExport() throws Exception { final Reader in = new StringReader("a,b,c\n1,2\nx,y,z"); - final Iterator records = CSVFormat.newBuilder().withHeader("A", "B", "C").build().parse(in).iterator(); + final Iterator records = CSVFormat.newBuilder().withHeader("A", "B", "C").parse(in).iterator(); // header record assertTrue(records.hasNext());