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 2012/10/14 07:54:07 UTC

svn commit: r1398019 - in /commons/proper/csv/trunk/src: main/java/org/apache/commons/csv/CSVFormat.java main/java/org/apache/commons/csv/CSVPrinter.java test/java/org/apache/commons/csv/CSVPrinterTest.java

Author: ggregory
Date: Sun Oct 14 05:54:06 2012
New Revision: 1398019

URL: http://svn.apache.org/viewvc?rev=1398019&view=rev
Log:
Add APIs to print arrays of records and iterables of records.

Modified:
    commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java
    commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java
    commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.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=1398019&r1=1398018&r2=1398019&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 Sun Oct 14 05:54:06 2012
@@ -531,7 +531,7 @@ public class CSVFormat implements Serial
     public String format(final Object... values) {
         final StringWriter out = new StringWriter();
         try {
-            new CSVPrinter(out, this).println(values);
+            new CSVPrinter(out, this).printRecord(values);
             return out.toString().trim();
         } catch (final IOException e) {
             // should not happen because a StringWriter does not do IO.

Modified: commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java?rev=1398019&r1=1398018&r2=1398019&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java (original)
+++ commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java Sun Oct 14 05:54:06 2012
@@ -25,6 +25,8 @@ import static org.apache.commons.csv.Con
 
 import java.io.Flushable;
 import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collection;
 
 /**
  * Prints values in a CSV format.
@@ -81,13 +83,13 @@ public class CSVPrinter {
     }
 
     /**
-     * Prints a single line of comma separated values. The values will be quoted if needed. Quotes and newLine
+     * Prints a single line of delimiter separated values. The values will be quoted if needed. Quotes and newLine
      * characters will be escaped.
      *
      * @param values
      *            values to output.
      */
-    public void println(final Object... values) throws IOException {
+    public void printRecord(final Object... values) throws IOException {
         for (final Object value : values) {
             print(value);
         }
@@ -95,7 +97,21 @@ public class CSVPrinter {
     }
 
     /**
-     * Prints a comment on a new line among the comma separated values. Comments will always begin on a new line and
+     * Prints a single line of delimiter separated values. The values will be quoted if needed. Quotes and newLine
+     * characters will be escaped.
+     *
+     * @param values
+     *            values to output.
+     */
+    public void printRecord(final Iterable<?> values) throws IOException {
+        for (final Object value : values) {
+            print(value);
+        }
+        println();
+    }
+
+    /**
+     * Prints a comment on a new line among the delimiter separated values. Comments will always begin on a new line and
      * occupy a least one full line. The character specified to start comments and a space will be inserted at the
      * beginning of each new line in the comment.
      * <p/>
@@ -282,6 +298,8 @@ public class CSVPrinter {
      *
      * @param object
      *            value to output.
+     * @throws  IOException
+     *          If an I/O error occurs
      */
     public void print(Object object, final boolean checkForEscape) throws IOException {
         // null values are considered empty
@@ -300,8 +318,50 @@ public class CSVPrinter {
      *
      * @param value
      *            value to be output.
+     * @throws  IOException
+     *          If an I/O error occurs
      */
     public void print(final Object value) throws IOException {
         print(value, true);
     }
+
+    /**
+     * Prints all the objects in the given array.
+     * 
+     * @param values
+     *            the values to print.
+     * @throws  IOException
+     *          If an I/O error occurs
+     */
+    public void printRecords(Object[] values) throws IOException {
+        for (Object value : values) {
+            if (value instanceof Object[]) {
+                this.printRecord((Object[]) value);
+            } else if (value instanceof Iterable) {
+                this.printRecord((Iterable<?>) value);
+            } else {
+                this.printRecord(value);
+            }
+        }
+    }
+
+    /**
+     * Prints all the objects in the given collection.
+     * 
+     * @param values
+     *            the values to print.
+     * @throws  IOException
+     *          If an I/O error occurs
+     */
+    public void printRecords(Iterable<?> values) throws IOException {
+        for (Object value : values) {
+            if (value instanceof Object[]) {
+                this.printRecord((Object[]) value);
+            } else if (value instanceof Iterable) {
+                this.printRecord((Iterable<?>) value);
+            } else {
+                this.printRecord(value);
+            }
+        }
+    }
 }

Modified: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java?rev=1398019&r1=1398018&r2=1398019&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java (original)
+++ commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java Sun Oct 14 05:54:06 2012
@@ -21,6 +21,7 @@ import static org.junit.Assert.assertEqu
 
 import java.io.IOException;
 import java.io.StringWriter;
+import java.util.Arrays;
 import java.util.List;
 import java.util.Random;
 
@@ -34,7 +35,7 @@ public class CSVPrinterTest {
     public void testPrinter1() throws IOException {
         final StringWriter sw = new StringWriter();
         final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
-        printer.println("a", "b");
+        printer.printRecord("a", "b");
         assertEquals("a,b" + lineSeparator, sw.toString());
     }
 
@@ -42,7 +43,7 @@ public class CSVPrinterTest {
     public void testPrinter2() throws IOException {
         final StringWriter sw = new StringWriter();
         final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
-        printer.println("a,b", "b");
+        printer.printRecord("a,b", "b");
         assertEquals("\"a,b\",b" + lineSeparator, sw.toString());
     }
 
@@ -50,7 +51,7 @@ public class CSVPrinterTest {
     public void testPrinter3() throws IOException {
         final StringWriter sw = new StringWriter();
         final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
-        printer.println("a, b", "b ");
+        printer.printRecord("a, b", "b ");
         assertEquals("\"a, b\",\"b \"" + lineSeparator, sw.toString());
     }
 
@@ -58,7 +59,7 @@ public class CSVPrinterTest {
     public void testPrinter4() throws IOException {
         final StringWriter sw = new StringWriter();
         final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
-        printer.println("a", "b\"c");
+        printer.printRecord("a", "b\"c");
         assertEquals("a,\"b\"\"c\"" + lineSeparator, sw.toString());
     }
 
@@ -66,7 +67,7 @@ public class CSVPrinterTest {
     public void testPrinter5() throws IOException {
         final StringWriter sw = new StringWriter();
         final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
-        printer.println("a", "b\nc");
+        printer.printRecord("a", "b\nc");
         assertEquals("a,\"b\nc\"" + lineSeparator, sw.toString());
     }
 
@@ -74,7 +75,7 @@ public class CSVPrinterTest {
     public void testPrinter6() throws IOException {
         final StringWriter sw = new StringWriter();
         final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
-        printer.println("a", "b\r\nc");
+        printer.printRecord("a", "b\r\nc");
         assertEquals("a,\"b\r\nc\"" + lineSeparator, sw.toString());
     }
 
@@ -82,15 +83,48 @@ public class CSVPrinterTest {
     public void testPrinter7() throws IOException {
         final StringWriter sw = new StringWriter();
         final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
-        printer.println("a", "b\\c");
+        printer.printRecord("a", "b\\c");
         assertEquals("a,b\\c" + lineSeparator, sw.toString());
     }
 
     @Test
+    public void testExcelPrintAllArrayOfArrays() throws IOException {
+        final StringWriter sw = new StringWriter();
+        final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
+        printer.printRecords(new String[][] { { "r1c1", "r1c2" }, { "r2c1", "r2c2" } });
+        assertEquals("r1c1,r1c2" + lineSeparator + "r2c1,r2c2" + lineSeparator, sw.toString());
+    }
+
+    @Test
+    public void testExcelPrintAllArrayOfLists() throws IOException {
+        final StringWriter sw = new StringWriter();
+        final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
+        printer.printRecords(new List[] { Arrays.asList(new String[] { "r1c1", "r1c2" }), Arrays.asList(new String[] { "r2c1", "r2c2" }) });
+        assertEquals("r1c1,r1c2" + lineSeparator + "r2c1,r2c2" + lineSeparator, sw.toString());
+    }
+
+    @Test
+    public void testExcelPrintAllIterableOfLists() throws IOException {
+        final StringWriter sw = new StringWriter();
+        final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
+        printer.printRecords(Arrays.asList(new List[] { Arrays.asList(new String[] { "r1c1", "r1c2" }),
+                Arrays.asList(new String[] { "r2c1", "r2c2" }) }));
+        assertEquals("r1c1,r1c2" + lineSeparator + "r2c1,r2c2" + lineSeparator, sw.toString());
+    }
+
+    @Test
+    public void testExcelPrintAllIterableOfArrays() throws IOException {
+        final StringWriter sw = new StringWriter();
+        final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
+        printer.printRecords(Arrays.asList(new String[][] { { "r1c1", "r1c2" }, { "r2c1", "r2c2" } }));
+        assertEquals("r1c1,r1c2" + lineSeparator + "r2c1,r2c2" + lineSeparator, sw.toString());
+    }
+
+    @Test
     public void testExcelPrinter1() throws IOException {
         final StringWriter sw = new StringWriter();
         final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
-        printer.println("a", "b");
+        printer.printRecord("a", "b");
         assertEquals("a,b" + lineSeparator, sw.toString());
     }
 
@@ -98,7 +132,7 @@ public class CSVPrinterTest {
     public void testExcelPrinter2() throws IOException {
         final StringWriter sw = new StringWriter();
         final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
-        printer.println("a,b", "b");
+        printer.printRecord("a,b", "b");
         assertEquals("\"a,b\",b" + lineSeparator, sw.toString());
     }
 
@@ -106,7 +140,7 @@ public class CSVPrinterTest {
     public void testPrintNullValues() throws IOException {
         final StringWriter sw = new StringWriter();
         final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
-        printer.println("a", null, "b");
+        printer.printRecord("a", null, "b");
         assertEquals("a,,b" + lineSeparator, sw.toString());
     }
 
@@ -171,7 +205,7 @@ public class CSVPrinterTest {
 
         for (int i = 0; i < nLines; i++) {
             // for (int j=0; j<lines[i].length; j++) System.out.println("### VALUE=:" + printable(lines[i][j]));
-            printer.println(lines[i]);
+            printer.printRecord(lines[i]);
         }
 
         printer.flush();