Author: ggregory
Date: Mon Jun 9 22:21:22 2014
New Revision: 1601517
URL: http://svn.apache.org/r1601517
Log:
<action issue="CSV-120" type="add" dev="ggregory" due-to="Sergei
Lebedev">CSVFormat#withHeader doesn't work with CSVPrinter</action>
Modified:
commons/proper/csv/trunk/src/changes/changes.xml
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/changes/changes.xml
URL:
http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/changes/changes.xml?rev=1601517&r1=1601516&r2=1601517&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/changes/changes.xml (original)
+++ commons/proper/csv/trunk/src/changes/changes.xml Mon Jun 9 22:21:22 2014
@@ -39,7 +39,8 @@
</properties>
<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-118" type="fix" dev="ggregory" due-to="Enrique
Lara">CSVRecord.toMap() throws NPE on formats with no
headers.</action>
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=1601517&r1=1601516&r2=1601517&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
Mon Jun 9 22:21:22 2014
@@ -605,8 +605,10 @@ public final class CSVFormat implements
* @param out
* the 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);
}
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=1601517&r1=1601516&r2=1601517&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
Mon Jun 9 22:21:22 2014
@@ -45,24 +45,31 @@ public final class CSVPrinter implements
/**
* Creates a printer that will print values to the given stream following
the CSVFormat.
* <p>
- * Currently, only a pure encapsulation format or a pure escaping format
is supported. Hybrid formats
- * (encapsulation and escaping with a different character) are not
supported.
+ * Currently, only a pure encapsulation format or a pure escaping format
is supported. Hybrid formats (encapsulation
+ * and escaping with a different character) are not supported.
* </p>
- *
+ *
* @param out
- * stream to which to print. Must not be null.
+ * stream to which to print. Must not be null.
* @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
- * 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(format, "format");
this.out = out;
this.format = format;
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());
+ }
}
// ======================================================
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=1601517&r1=1601516&r2=1601517&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
Mon Jun 9 22:21:22 2014
@@ -486,6 +486,17 @@ public class CSVPrinterTest {
}
@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
public void testEOLPlain() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw,
CSVFormat.DEFAULT.withQuoteChar(null));