diff --git a/src/main/java/org/apache/commons/csv/CSVFormat.java b/src/main/java/org/apache/commons/csv/CSVFormat.java index a2f8de61..a08b6c8a 100644 --- a/src/main/java/org/apache/commons/csv/CSVFormat.java +++ b/src/main/java/org/apache/commons/csv/CSVFormat.java @@ -531,7 +531,7 @@ public class CSVFormat implements Serializable { 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. diff --git a/src/main/java/org/apache/commons/csv/CSVPrinter.java b/src/main/java/org/apache/commons/csv/CSVPrinter.java index b1590df4..80b064c6 100644 --- a/src/main/java/org/apache/commons/csv/CSVPrinter.java +++ b/src/main/java/org/apache/commons/csv/CSVPrinter.java @@ -25,6 +25,8 @@ import static org.apache.commons.csv.Constants.SP; 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. *

@@ -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); + } + } + } } diff --git a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java index d6865d99..41e43871 100644 --- a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java +++ b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java @@ -21,6 +21,7 @@ import static org.junit.Assert.assertEquals; 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