You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2013/03/26 17:19:48 UTC

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

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.
  * <p>
  * This class is immutable.
  * </p>
- * <p>
  * You can extend a format through a builder. For example, to extend the Excel format with columns header, you write:
  * </p>
  * <pre>CSVFormat.EXCEL.toBuilder().withHeader(&quot;Col1&quot;, &quot;Col2&quot;, &quot;Col3&quot;).build();</pre>
+ * <p>
+ * You can parse through a format. For example, to parse an Excel file with columns header, you write:
+ * </p>
+ * <pre>Reader in = ...;
+ *CSVFormat.EXCEL.toBuilder().withHeader(&quot;Col1&quot;, &quot;Col2&quot;, &quot;Col3&quot;).parse(in);</pre>
+ * <p>
  * 
  * @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<CSVRecord> parse(final Reader in) throws IOException {
@@ -586,6 +592,19 @@ public class CSVFormat implements Serial
         }
 
         /**
+         * Parses the specified content. Short-hand for:
+         * <pre>format.build().parse(in);</pre>
+         *
+         * @param in
+         *            the input stream
+         * @return a CSVRecord stream
+         * @throws IOException
+         */
+        public Iterable<CSVRecord> 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<CSVRecord> records = CSVFormat.newBuilder().withHeader().build().parse(in).iterator();
+        final Iterator<CSVRecord> 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<CSVRecord> records = CSVFormat.newBuilder().withCommentStart('#').withHeader().build().parse(in).iterator();
+        final Iterator<CSVRecord> 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<CSVRecord> records = CSVFormat.newBuilder().withHeader("A", "B", "C").build().parse(in).iterator();
+        final Iterator<CSVRecord> 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<CSVRecord> records = CSVFormat.newBuilder().withHeader("A", "B", "C").build().parse(in).iterator();
+        final Iterator<CSVRecord> records = CSVFormat.newBuilder().withHeader("A", "B", "C").parse(in).iterator();
 
         // header record
         assertTrue(records.hasNext());