From a4f737108b806d987ac4d2be686c35196d42bc6e Mon Sep 17 00:00:00 2001
From: "Gary D. Gregory"
+ * CSVFormat format = aformat.withHeaderComments("Generated by Apache Commons CSV 1.1.", new Date());
+ *
+ * @param header
+ * the header, {@code null} if disabled, empty if parsed automatically, user specified otherwise.
+ *
+ * @return A new CSVFormat that is equal to this but with the specified header
+ * @see #withSkipHeaderRecord(boolean)
+ * @since 1.1
+ */
+ public CSVFormat withHeaderComments(final Object... headerComments) {
+ return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
+ ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
+ skipHeaderRecord, allowMissingColumnNames);
}
/**
@@ -872,8 +919,8 @@ public final class CSVFormat implements Serializable {
*/
public CSVFormat withAllowMissingColumnNames(final boolean allowMissingColumnNames) {
return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
- ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, header, skipHeaderRecord,
- allowMissingColumnNames);
+ ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, null, header,
+ skipHeaderRecord, allowMissingColumnNames);
}
/**
@@ -897,8 +944,8 @@ public final class CSVFormat implements Serializable {
*/
public CSVFormat withIgnoreEmptyLines(final boolean ignoreEmptyLines) {
return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
- ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, header, skipHeaderRecord,
- allowMissingColumnNames);
+ ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, null, header,
+ skipHeaderRecord, allowMissingColumnNames);
}
/**
@@ -922,8 +969,8 @@ public final class CSVFormat implements Serializable {
*/
public CSVFormat withIgnoreSurroundingSpaces(final boolean ignoreSurroundingSpaces) {
return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
- ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, header, skipHeaderRecord,
- allowMissingColumnNames);
+ ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, null, header,
+ skipHeaderRecord, allowMissingColumnNames);
}
/**
@@ -943,8 +990,8 @@ public final class CSVFormat implements Serializable {
*/
public CSVFormat withNullString(final String nullString) {
return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
- ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, header, skipHeaderRecord,
- allowMissingColumnNames);
+ ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, null, header,
+ skipHeaderRecord, allowMissingColumnNames);
}
/**
@@ -974,8 +1021,8 @@ public final class CSVFormat implements Serializable {
throw new IllegalArgumentException("The quoteChar cannot be a line break");
}
return new CSVFormat(delimiter, quoteChar, quoteMode, commentMarker, escapeCharacter,
- ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, header, skipHeaderRecord,
- allowMissingColumnNames);
+ ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, null, header,
+ skipHeaderRecord, allowMissingColumnNames);
}
/**
@@ -988,8 +1035,8 @@ public final class CSVFormat implements Serializable {
*/
public CSVFormat withQuoteMode(final QuoteMode quoteModePolicy) {
return new CSVFormat(delimiter, quoteCharacter, quoteModePolicy, commentMarker, escapeCharacter,
- ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, header, skipHeaderRecord,
- allowMissingColumnNames);
+ ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, null, header,
+ skipHeaderRecord, allowMissingColumnNames);
}
/**
@@ -1022,8 +1069,8 @@ public final class CSVFormat implements Serializable {
*/
public CSVFormat withRecordSeparator(final String recordSeparator) {
return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
- ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, header, skipHeaderRecord,
- allowMissingColumnNames);
+ ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, null, header,
+ skipHeaderRecord, allowMissingColumnNames);
}
/**
@@ -1052,7 +1099,7 @@ public final class CSVFormat implements Serializable {
*/
public CSVFormat withSkipHeaderRecord(final boolean skipHeaderRecord) {
return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
- ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, header, skipHeaderRecord,
- allowMissingColumnNames);
+ ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, null, header,
+ skipHeaderRecord, allowMissingColumnNames);
}
}
diff --git a/src/main/java/org/apache/commons/csv/CSVPrinter.java b/src/main/java/org/apache/commons/csv/CSVPrinter.java
index c32d0885..81e3d57b 100644
--- a/src/main/java/org/apache/commons/csv/CSVPrinter.java
+++ b/src/main/java/org/apache/commons/csv/CSVPrinter.java
@@ -50,13 +50,13 @@ public final class CSVPrinter implements Flushable, Closeable {
*
If the given collection only contains simple objects, this method will print a single record like + *
+ * If the given collection only contains simple objects, this method will print a single record like * {@link #printRecord(Iterable)}. If the given collections contains nested collections/arrays those nested elements - * will each be printed as records using {@link #printRecord(Object...)}.
+ * will each be printed as records using {@link #printRecord(Object...)}. + * * - *Given the following data structure:
+ *+ * Given the following data structure: + *
+ * *
*
* List<String[]> data = ...
@@ -395,7 +407,10 @@ public final class CSVPrinter implements Flushable, Closeable {
*
*
*
- * Calling this method will print:
+ *+ * Calling this method will print: + *
+ * *
*
* A, B, C
@@ -424,11 +439,16 @@ public final class CSVPrinter implements Flushable, Closeable {
/**
* Prints all the objects in the given array handling nested collections/arrays as records.
*
- * If the given array only contains simple objects, this method will print a single record like
+ *
+ * If the given array only contains simple objects, this method will print a single record like
* {@link #printRecord(Object...)}. If the given collections contains nested collections/arrays those nested
- * elements will each be printed as records using {@link #printRecord(Object...)}.
+ * elements will each be printed as records using {@link #printRecord(Object...)}.
+ *
*
- * Given the following data structure:
+ *
+ * Given the following data structure:
+ *
+ *
*
*
* String[][] data = new String[3][]
@@ -438,7 +458,10 @@ public final class CSVPrinter implements Flushable, Closeable {
*
*
*
- * Calling this method will print:
+ *
+ * Calling this method will print:
+ *
+ *
*
*
* A, B, C
@@ -467,11 +490,12 @@ public final class CSVPrinter implements Flushable, Closeable {
/**
* Prints all the objects in the given JDBC result set.
*
- * @param resultSet result set
- * the values to print.
+ * @param resultSet
+ * result set the values to print.
* @throws IOException
* If an I/O error occurs
- * @throws SQLException if a database access error occurs
+ * @throws SQLException
+ * if a database access error occurs
*/
public void printRecords(final ResultSet resultSet) throws SQLException, IOException {
final int columnCount = resultSet.getMetaData().getColumnCount();
diff --git a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
index d78c479c..f4665f64 100644
--- a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
+++ b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
@@ -29,6 +29,7 @@ import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
+import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
@@ -496,6 +497,37 @@ public class CSVPrinterTest {
printer.close();
}
+ @Test
+ public void testHeaderCommentExcel() throws IOException {
+ final StringWriter sw = new StringWriter();
+ Date now = new Date();
+ CSVFormat format = CSVFormat.EXCEL;
+ final CSVPrinter csvPrinter = printWithHeaderComments(sw, now, format);
+ assertEquals("# Generated by Apache Commons CSV 1.1\r\n# " + now + "\r\nCol1,Col2\r\nA,B\r\nC,D\r\n", sw.toString());
+ csvPrinter.close();
+ }
+
+ @Test
+ public void testHeaderCommentTdf() throws IOException {
+ final StringWriter sw = new StringWriter();
+ Date now = new Date();
+ CSVFormat format = CSVFormat.TDF;
+ final CSVPrinter csvPrinter = printWithHeaderComments(sw, now, format);
+ assertEquals("# Generated by Apache Commons CSV 1.1\r\n# " + now + "\r\nCol1\tCol2\r\nA\tB\r\nC\tD\r\n", sw.toString());
+ csvPrinter.close();
+ }
+
+ private CSVPrinter printWithHeaderComments(final StringWriter sw, Date now, CSVFormat format)
+ throws IOException {
+ format = format.withCommentMarker('#').withHeader("Col1", "Col2");
+ format = format.withHeaderComments("Generated by Apache Commons CSV 1.1", now);
+ final CSVPrinter csvPrinter = format.print(sw);
+ csvPrinter.printRecord("A", "B");
+ csvPrinter.printRecord("C", "D");
+ csvPrinter.close();
+ return csvPrinter;
+ }
+
@Test
public void testEOLPlain() throws IOException {
final StringWriter sw = new StringWriter();