<action issue="CSV-120" type="add" dev="ggregory" due-to="Sergei Lebedev">CSVFormat#withHeader doesn't work with CSVPrinter</action>

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1601517 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary D. Gregory 2014-06-09 22:21:22 +00:00
parent 65f110ec14
commit 1282503fb9
4 changed files with 30 additions and 9 deletions

View File

@ -40,6 +40,7 @@
<body> <body>
<release version="1.0" date="TBD" description="First release"> <release version="1.0" date="TBD" description="First release">
<action issue="CSV-120" type="add" dev="ggregory" due-to="Sergei Lebedev">CSVFormat#withHeader doesn't work with CSVPrinter</action>
<action issue="CSV-119" type="add" dev="ggregory" due-to="Sergei Lebedev">CSVFormat is missing a print(...) method</action> <action issue="CSV-119" type="add" dev="ggregory" due-to="Sergei Lebedev">CSVFormat is missing a print(...) method</action>
<action issue="CSV-118" type="fix" dev="ggregory" due-to="Enrique Lara">CSVRecord.toMap() throws NPE on formats with no <action issue="CSV-118" type="fix" dev="ggregory" due-to="Enrique Lara">CSVRecord.toMap() throws NPE on formats with no
headers.</action> headers.</action>

View File

@ -605,8 +605,10 @@ public final class CSVFormat implements Serializable {
* @param out * @param out
* the output * the output
* @return a printer to an output * @return a printer to an output
* @throws IOException
* thrown if the optional header cannot be printed.
*/ */
public CSVPrinter print(final Appendable out) { public CSVPrinter print(final Appendable out) throws IOException {
return new CSVPrinter(out, this); return new CSVPrinter(out, this);
} }

View File

@ -45,24 +45,31 @@ public final class CSVPrinter implements Flushable, Closeable {
/** /**
* Creates a printer that will print values to the given stream following the CSVFormat. * Creates a printer that will print values to the given stream following the CSVFormat.
* <p> * <p>
* Currently, only a pure encapsulation format or a pure escaping format is supported. Hybrid formats * Currently, only a pure encapsulation format or a pure escaping format is supported. Hybrid formats (encapsulation
* (encapsulation and escaping with a different character) are not supported. * and escaping with a different character) are not supported.
* </p> * </p>
* *
* @param out * @param out
* stream to which to print. Must not be null. * stream to which to print. Must not be null.
* @param format * @param format
* the CSV format. Must not be null. * the CSV format. Must not be null.
* @throws IOException
* thrown if the optional header cannot be printed.
* @throws IllegalArgumentException * @throws IllegalArgumentException
* thrown if the parameters of the format are inconsistent or if either out or format are null. * thrown if the parameters of the format are inconsistent or if either out or format are null.
*/ */
public CSVPrinter(final Appendable out, final CSVFormat format) { public CSVPrinter(final Appendable out, final CSVFormat format) throws IOException {
Assertions.notNull(out, "out"); Assertions.notNull(out, "out");
Assertions.notNull(format, "format"); Assertions.notNull(format, "format");
this.out = out; this.out = out;
this.format = format; this.format = format;
this.format.validate(); this.format.validate();
// TODO: Is it a good idea to do this here instead of on the first call to a print method?
// It seems a pain to have to track whether the header has already been printed or not.
if (format.getHeader() != null) {
this.printRecord((Object[]) format.getHeader());
}
} }
// ====================================================== // ======================================================

View File

@ -485,6 +485,17 @@ public class CSVPrinterTest {
printer.close(); printer.close();
} }
@Test
public void testHeader() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuoteChar(null)
.withHeader("C1", "C2", "C3"));
printer.printRecord("a", "b", "c");
printer.printRecord("x", "y", "z");
assertEquals("C1,C2,C3\r\na,b,c\r\nx,y,z\r\n", sw.toString());
printer.close();
}
@Test @Test
public void testEOLPlain() throws IOException { public void testEOLPlain() throws IOException {
final StringWriter sw = new StringWriter(); final StringWriter sw = new StringWriter();