Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id A093BF6D3 for ; Tue, 26 Mar 2013 16:20:13 +0000 (UTC) Received: (qmail 50310 invoked by uid 500); 26 Mar 2013 16:20:13 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 50212 invoked by uid 500); 26 Mar 2013 16:20:13 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 50202 invoked by uid 99); 26 Mar 2013 16:20:13 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 26 Mar 2013 16:20:13 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 26 Mar 2013 16:20:10 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id E2E9D23888CD; Tue, 26 Mar 2013 16:19:48 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1461202 - in /commons/proper/csv/trunk/src: main/java/org/apache/commons/csv/CSVFormat.java test/java/org/apache/commons/csv/CSVFileParserTest.java test/java/org/apache/commons/csv/CSVParserTest.java Date: Tue, 26 Mar 2013 16:19:48 -0000 To: commits@commons.apache.org From: ggregory@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20130326161948.E2E9D23888CD@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: ggregory Date: Tue Mar 26 16:19:48 2013 New Revision: 1461202 URL: http://svn.apache.org/r1461202 Log: Add org.apache.commons.csv.CSVFormat.CSVFormatBuilder.parse(Reader). Modified: commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFileParserTest.java commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java Modified: commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java?rev=1461202&r1=1461201&r2=1461202&view=diff ============================================================================== --- commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java (original) +++ commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java Tue Mar 26 16:19:48 2013 @@ -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 Serial * * @param in * the input stream + * @return a stream of CSVRecord * @throws IOException */ public Iterable parse(final Reader in) throws IOException { @@ -586,6 +592,19 @@ public class CSVFormat implements Serial } /** + * 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. * * @throws IllegalStateException Modified: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFileParserTest.java URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFileParserTest.java?rev=1461202&r1=1461201&r2=1461202&view=diff ============================================================================== --- commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFileParserTest.java (original) +++ commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFileParserTest.java Tue Mar 26 16:19:48 2013 @@ -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); } } Modified: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java?rev=1461202&r1=1461201&r2=1461202&view=diff ============================================================================== --- commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java (original) +++ commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java Tue Mar 26 16:19:48 2013 @@ -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());