Skip to content

Commit 79eb6e0

Browse files
committed
Add APIs to print arrays of records and iterables of records.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1398019 13f79535-47bb-0310-9956-ffa450edef68
1 parent 7fd16f3 commit 79eb6e0

3 files changed

Lines changed: 109 additions & 15 deletions

File tree

src/main/java/org/apache/commons/csv/CSVFormat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ public Iterable<CSVRecord> parse(final Reader in) throws IOException {
531531
public String format(final Object... values) {
532532
final StringWriter out = new StringWriter();
533533
try {
534-
new CSVPrinter(out, this).println(values);
534+
new CSVPrinter(out, this).printRecord(values);
535535
return out.toString().trim();
536536
} catch (final IOException e) {
537537
// should not happen because a StringWriter does not do IO.

src/main/java/org/apache/commons/csv/CSVPrinter.java

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
import java.io.Flushable;
2727
import java.io.IOException;
28+
import java.util.Arrays;
29+
import java.util.Collection;
2830

2931
/**
3032
* Prints values in a CSV format.
@@ -81,21 +83,35 @@ public void flush() throws IOException {
8183
}
8284

8385
/**
84-
* Prints a single line of comma separated values. The values will be quoted if needed. Quotes and newLine
86+
* Prints a single line of delimiter separated values. The values will be quoted if needed. Quotes and newLine
8587
* characters will be escaped.
8688
*
8789
* @param values
8890
* values to output.
8991
*/
90-
public void println(final Object... values) throws IOException {
92+
public void printRecord(final Object... values) throws IOException {
9193
for (final Object value : values) {
9294
print(value);
9395
}
9496
println();
9597
}
9698

9799
/**
98-
* Prints a comment on a new line among the comma separated values. Comments will always begin on a new line and
100+
* Prints a single line of delimiter separated values. The values will be quoted if needed. Quotes and newLine
101+
* characters will be escaped.
102+
*
103+
* @param values
104+
* values to output.
105+
*/
106+
public void printRecord(final Iterable<?> values) throws IOException {
107+
for (final Object value : values) {
108+
print(value);
109+
}
110+
println();
111+
}
112+
113+
/**
114+
* Prints a comment on a new line among the delimiter separated values. Comments will always begin on a new line and
99115
* occupy a least one full line. The character specified to start comments and a space will be inserted at the
100116
* beginning of each new line in the comment.
101117
* <p/>
@@ -282,6 +298,8 @@ void printAndQuote(final CharSequence value, final int offset, final int len) th
282298
*
283299
* @param object
284300
* value to output.
301+
* @throws IOException
302+
* If an I/O error occurs
285303
*/
286304
public void print(Object object, final boolean checkForEscape) throws IOException {
287305
// null values are considered empty
@@ -300,8 +318,50 @@ public void print(Object object, final boolean checkForEscape) throws IOExceptio
300318
*
301319
* @param value
302320
* value to be output.
321+
* @throws IOException
322+
* If an I/O error occurs
303323
*/
304324
public void print(final Object value) throws IOException {
305325
print(value, true);
306326
}
327+
328+
/**
329+
* Prints all the objects in the given array.
330+
*
331+
* @param values
332+
* the values to print.
333+
* @throws IOException
334+
* If an I/O error occurs
335+
*/
336+
public void printRecords(Object[] values) throws IOException {
337+
for (Object value : values) {
338+
if (value instanceof Object[]) {
339+
this.printRecord((Object[]) value);
340+
} else if (value instanceof Iterable) {
341+
this.printRecord((Iterable<?>) value);
342+
} else {
343+
this.printRecord(value);
344+
}
345+
}
346+
}
347+
348+
/**
349+
* Prints all the objects in the given collection.
350+
*
351+
* @param values
352+
* the values to print.
353+
* @throws IOException
354+
* If an I/O error occurs
355+
*/
356+
public void printRecords(Iterable<?> values) throws IOException {
357+
for (Object value : values) {
358+
if (value instanceof Object[]) {
359+
this.printRecord((Object[]) value);
360+
} else if (value instanceof Iterable) {
361+
this.printRecord((Iterable<?>) value);
362+
} else {
363+
this.printRecord(value);
364+
}
365+
}
366+
}
307367
}

src/test/java/org/apache/commons/csv/CSVPrinterTest.java

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.io.IOException;
2323
import java.io.StringWriter;
24+
import java.util.Arrays;
2425
import java.util.List;
2526
import java.util.Random;
2627

@@ -34,79 +35,112 @@ public class CSVPrinterTest {
3435
public void testPrinter1() throws IOException {
3536
final StringWriter sw = new StringWriter();
3637
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
37-
printer.println("a", "b");
38+
printer.printRecord("a", "b");
3839
assertEquals("a,b" + lineSeparator, sw.toString());
3940
}
4041

4142
@Test
4243
public void testPrinter2() throws IOException {
4344
final StringWriter sw = new StringWriter();
4445
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
45-
printer.println("a,b", "b");
46+
printer.printRecord("a,b", "b");
4647
assertEquals("\"a,b\",b" + lineSeparator, sw.toString());
4748
}
4849

4950
@Test
5051
public void testPrinter3() throws IOException {
5152
final StringWriter sw = new StringWriter();
5253
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
53-
printer.println("a, b", "b ");
54+
printer.printRecord("a, b", "b ");
5455
assertEquals("\"a, b\",\"b \"" + lineSeparator, sw.toString());
5556
}
5657

5758
@Test
5859
public void testPrinter4() throws IOException {
5960
final StringWriter sw = new StringWriter();
6061
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
61-
printer.println("a", "b\"c");
62+
printer.printRecord("a", "b\"c");
6263
assertEquals("a,\"b\"\"c\"" + lineSeparator, sw.toString());
6364
}
6465

6566
@Test
6667
public void testPrinter5() throws IOException {
6768
final StringWriter sw = new StringWriter();
6869
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
69-
printer.println("a", "b\nc");
70+
printer.printRecord("a", "b\nc");
7071
assertEquals("a,\"b\nc\"" + lineSeparator, sw.toString());
7172
}
7273

7374
@Test
7475
public void testPrinter6() throws IOException {
7576
final StringWriter sw = new StringWriter();
7677
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
77-
printer.println("a", "b\r\nc");
78+
printer.printRecord("a", "b\r\nc");
7879
assertEquals("a,\"b\r\nc\"" + lineSeparator, sw.toString());
7980
}
8081

8182
@Test
8283
public void testPrinter7() throws IOException {
8384
final StringWriter sw = new StringWriter();
8485
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
85-
printer.println("a", "b\\c");
86+
printer.printRecord("a", "b\\c");
8687
assertEquals("a,b\\c" + lineSeparator, sw.toString());
8788
}
8889

90+
@Test
91+
public void testExcelPrintAllArrayOfArrays() throws IOException {
92+
final StringWriter sw = new StringWriter();
93+
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
94+
printer.printRecords(new String[][] { { "r1c1", "r1c2" }, { "r2c1", "r2c2" } });
95+
assertEquals("r1c1,r1c2" + lineSeparator + "r2c1,r2c2" + lineSeparator, sw.toString());
96+
}
97+
98+
@Test
99+
public void testExcelPrintAllArrayOfLists() throws IOException {
100+
final StringWriter sw = new StringWriter();
101+
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
102+
printer.printRecords(new List[] { Arrays.asList(new String[] { "r1c1", "r1c2" }), Arrays.asList(new String[] { "r2c1", "r2c2" }) });
103+
assertEquals("r1c1,r1c2" + lineSeparator + "r2c1,r2c2" + lineSeparator, sw.toString());
104+
}
105+
106+
@Test
107+
public void testExcelPrintAllIterableOfLists() throws IOException {
108+
final StringWriter sw = new StringWriter();
109+
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
110+
printer.printRecords(Arrays.asList(new List[] { Arrays.asList(new String[] { "r1c1", "r1c2" }),
111+
Arrays.asList(new String[] { "r2c1", "r2c2" }) }));
112+
assertEquals("r1c1,r1c2" + lineSeparator + "r2c1,r2c2" + lineSeparator, sw.toString());
113+
}
114+
115+
@Test
116+
public void testExcelPrintAllIterableOfArrays() throws IOException {
117+
final StringWriter sw = new StringWriter();
118+
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
119+
printer.printRecords(Arrays.asList(new String[][] { { "r1c1", "r1c2" }, { "r2c1", "r2c2" } }));
120+
assertEquals("r1c1,r1c2" + lineSeparator + "r2c1,r2c2" + lineSeparator, sw.toString());
121+
}
122+
89123
@Test
90124
public void testExcelPrinter1() throws IOException {
91125
final StringWriter sw = new StringWriter();
92126
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
93-
printer.println("a", "b");
127+
printer.printRecord("a", "b");
94128
assertEquals("a,b" + lineSeparator, sw.toString());
95129
}
96130

97131
@Test
98132
public void testExcelPrinter2() throws IOException {
99133
final StringWriter sw = new StringWriter();
100134
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
101-
printer.println("a,b", "b");
135+
printer.printRecord("a,b", "b");
102136
assertEquals("\"a,b\",b" + lineSeparator, sw.toString());
103137
}
104138

105139
@Test
106140
public void testPrintNullValues() throws IOException {
107141
final StringWriter sw = new StringWriter();
108142
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
109-
printer.println("a", null, "b");
143+
printer.printRecord("a", null, "b");
110144
assertEquals("a,,b" + lineSeparator, sw.toString());
111145
}
112146

@@ -171,7 +205,7 @@ public void doOneRandom(final CSVFormat format) throws Exception {
171205

172206
for (int i = 0; i < nLines; i++) {
173207
// for (int j=0; j<lines[i].length; j++) System.out.println("### VALUE=:" + printable(lines[i][j]));
174-
printer.println(lines[i]);
208+
printer.printRecord(lines[i]);
175209
}
176210

177211
printer.flush();

0 commit comments

Comments
 (0)