Skip to content

Commit 65883aa

Browse files
committed
[CSV-145] CSVFormat.with* methods clear the header comments
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1641845 13f79535-47bb-0310-9956-ffa450edef68
1 parent 081b070 commit 65883aa

3 files changed

Lines changed: 33 additions & 25 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
<title>Release Notes</title>
3939
</properties>
4040
<body>
41+
<release version="1.1.1" date="20??-MM-DD" description="Feature and bug fix release">
42+
<action issue="CSV-145" type="fix" dev="ggregory" due-to="Frank Ulbricht">CSVFormat.with* methods clear the header comments</action>
43+
</release>
4144
<release version="1.1" date="2014-11-16" description="Feature and bug fix release">
4245
<action issue="CSV-140" type="fix" dev="ggregory" due-to="Damjan Jovanovic">QuoteMode.NON_NUMERIC doesn't work with CSVPrinter.printRecords(ResultSet)</action>
4346
<action issue="CSV-130" type="fix" dev="ggregory" due-to="Sergei Lebedev">CSVFormat#withHeader doesn't work well with #printComment, add withHeaderComments(String...)</action>

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

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -804,8 +804,8 @@ public CSVFormat withCommentMarker(final Character commentMarker) {
804804
throw new IllegalArgumentException("The comment start marker character cannot be a line break");
805805
}
806806
return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
807-
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, null, header, skipHeaderRecord,
808-
allowMissingColumnNames);
807+
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
808+
skipHeaderRecord, allowMissingColumnNames);
809809
}
810810

811811
/**
@@ -822,8 +822,8 @@ public CSVFormat withDelimiter(final char delimiter) {
822822
throw new IllegalArgumentException("The delimiter cannot be a line break");
823823
}
824824
return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
825-
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, null, header, skipHeaderRecord,
826-
allowMissingColumnNames);
825+
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
826+
skipHeaderRecord, allowMissingColumnNames);
827827
}
828828

829829
/**
@@ -853,7 +853,8 @@ public CSVFormat withEscape(final Character escape) {
853853
throw new IllegalArgumentException("The escape character cannot be a line break");
854854
}
855855
return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escape, ignoreSurroundingSpaces,
856-
ignoreEmptyLines, recordSeparator, nullString, null, header, skipHeaderRecord, allowMissingColumnNames);
856+
ignoreEmptyLines, recordSeparator, nullString, headerComments, header, skipHeaderRecord,
857+
allowMissingColumnNames);
857858
}
858859

859860
/**
@@ -880,8 +881,8 @@ public CSVFormat withEscape(final Character escape) {
880881
*/
881882
public CSVFormat withHeader(final String... header) {
882883
return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
883-
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, null, header, skipHeaderRecord,
884-
allowMissingColumnNames);
884+
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
885+
skipHeaderRecord, allowMissingColumnNames);
885886
}
886887

887888
/**
@@ -948,8 +949,8 @@ public CSVFormat withHeader(final ResultSetMetaData metaData) throws SQLExceptio
948949
}
949950
}
950951
return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
951-
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, null, labels, skipHeaderRecord,
952-
allowMissingColumnNames);
952+
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, labels,
953+
skipHeaderRecord, allowMissingColumnNames);
953954
}
954955

955956
/**
@@ -994,8 +995,8 @@ public CSVFormat withAllowMissingColumnNames() {
994995
*/
995996
public CSVFormat withAllowMissingColumnNames(final boolean allowMissingColumnNames) {
996997
return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
997-
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, null, header, skipHeaderRecord,
998-
allowMissingColumnNames);
998+
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
999+
skipHeaderRecord, allowMissingColumnNames);
9991000
}
10001001

10011002
/**
@@ -1019,8 +1020,8 @@ public CSVFormat withIgnoreEmptyLines() {
10191020
*/
10201021
public CSVFormat withIgnoreEmptyLines(final boolean ignoreEmptyLines) {
10211022
return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
1022-
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, null, header, skipHeaderRecord,
1023-
allowMissingColumnNames);
1023+
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
1024+
skipHeaderRecord, allowMissingColumnNames);
10241025
}
10251026

10261027
/**
@@ -1044,8 +1045,8 @@ public CSVFormat withIgnoreSurroundingSpaces() {
10441045
*/
10451046
public CSVFormat withIgnoreSurroundingSpaces(final boolean ignoreSurroundingSpaces) {
10461047
return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
1047-
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, null, header, skipHeaderRecord,
1048-
allowMissingColumnNames);
1048+
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
1049+
skipHeaderRecord, allowMissingColumnNames);
10491050
}
10501051

10511052
/**
@@ -1065,8 +1066,8 @@ public CSVFormat withIgnoreSurroundingSpaces(final boolean ignoreSurroundingSpac
10651066
*/
10661067
public CSVFormat withNullString(final String nullString) {
10671068
return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
1068-
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, null, header, skipHeaderRecord,
1069-
allowMissingColumnNames);
1069+
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
1070+
skipHeaderRecord, allowMissingColumnNames);
10701071
}
10711072

10721073
/**
@@ -1096,7 +1097,8 @@ public CSVFormat withQuote(final Character quoteChar) {
10961097
throw new IllegalArgumentException("The quoteChar cannot be a line break");
10971098
}
10981099
return new CSVFormat(delimiter, quoteChar, quoteMode, commentMarker, escapeCharacter, ignoreSurroundingSpaces,
1099-
ignoreEmptyLines, recordSeparator, nullString, null, header, skipHeaderRecord, allowMissingColumnNames);
1100+
ignoreEmptyLines, recordSeparator, nullString, headerComments, header, skipHeaderRecord,
1101+
allowMissingColumnNames);
11001102
}
11011103

11021104
/**
@@ -1109,8 +1111,8 @@ public CSVFormat withQuote(final Character quoteChar) {
11091111
*/
11101112
public CSVFormat withQuoteMode(final QuoteMode quoteModePolicy) {
11111113
return new CSVFormat(delimiter, quoteCharacter, quoteModePolicy, commentMarker, escapeCharacter,
1112-
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, null, header, skipHeaderRecord,
1113-
allowMissingColumnNames);
1114+
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
1115+
skipHeaderRecord, allowMissingColumnNames);
11141116
}
11151117

11161118
/**
@@ -1147,8 +1149,8 @@ public CSVFormat withRecordSeparator(final char recordSeparator) {
11471149
*/
11481150
public CSVFormat withRecordSeparator(final String recordSeparator) {
11491151
return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
1150-
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, null, header, skipHeaderRecord,
1151-
allowMissingColumnNames);
1152+
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
1153+
skipHeaderRecord, allowMissingColumnNames);
11521154
}
11531155

11541156
/**
@@ -1174,7 +1176,7 @@ public CSVFormat withSkipHeaderRecord() {
11741176
*/
11751177
public CSVFormat withSkipHeaderRecord(final boolean skipHeaderRecord) {
11761178
return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
1177-
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, null, header, skipHeaderRecord,
1178-
allowMissingColumnNames);
1179+
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
1180+
skipHeaderRecord, allowMissingColumnNames);
11791181
}
11801182
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,8 +579,11 @@ public void testHeaderCommentTdf() throws IOException {
579579

580580
private CSVPrinter printWithHeaderComments(final StringWriter sw, final Date now, final CSVFormat baseFormat)
581581
throws IOException {
582-
CSVFormat format = baseFormat.withCommentMarker('#').withHeader("Col1", "Col2");
582+
CSVFormat format = baseFormat;
583+
// Use withHeaderComments first to test CSV-145
583584
format = format.withHeaderComments("Generated by Apache Commons CSV 1.1", now);
585+
format = format.withCommentMarker('#');
586+
format = format.withHeader("Col1", "Col2");
584587
final CSVPrinter csvPrinter = format.print(sw);
585588
csvPrinter.printRecord("A", "B");
586589
csvPrinter.printRecord("C", "D");

0 commit comments

Comments
 (0)