[
https://issues.apache.org/jira/browse/CSV-304?focusedWorklogId=807050&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-807050
]
ASF GitHub Bot logged work on CSV-304:
--------------------------------------
Author: ASF GitHub Bot
Created on: 08/Sep/22 14:34
Start Date: 08/Sep/22 14:34
Worklog Time Spent: 10m
Work Description: garydgregory commented on code in PR #257:
URL: https://github.com/apache/commons-csv/pull/257#discussion_r964979116
##########
src/main/java/org/apache/commons/csv/CSVParser.java:
##########
@@ -480,10 +483,12 @@ private Headers createHeaders() throws IOException {
final CSVRecord nextRecord = this.nextRecord();
if (nextRecord != null) {
headerRecord = nextRecord.values();
+ headerComment = nextRecord.getComment();
}
} else {
if (this.format.getSkipHeaderRecord()) {
- this.nextRecord();
+ final CSVRecord csvRecord = this.nextRecord();
Review Comment:
NPE waiting to happen?
##########
src/main/java/org/apache/commons/csv/CSVParser.java:
##########
@@ -352,6 +352,9 @@ public static CSVParser parse(final URL url, final Charset
charset, final CSVFor
return new CSVParser(new InputStreamReader(url.openStream(), charset),
format);
}
+ private String headerComment;
Review Comment:
Add a blank line after the close brace.
##########
src/test/java/org/apache/commons/csv/CSVParserTest.java:
##########
@@ -1375,4 +1375,147 @@ private void validateRecordPosition(final String
lineSeparator) throws IOExcepti
parser.close();
}
+ // CSV with no header comments
+ static private final String CSV_INPUT_NO_COMMENT = "A,B"+CRLF+"1,2"+CRLF;
+ // CSV with a header comment
+ static private final String CSV_INPUT_HEADER_COMMENT = "# header comment"
+ CRLF + "A,B" + CRLF + "1,2" + CRLF;
+ // CSV with a single line header and trailer comment
+ static private final String CSV_INPUT_HEADER_TRAILER_COMMENT = "# header
comment" + CRLF + "A,B" + CRLF + "1,2" + CRLF + "# comment";
+ // CSV with a multi-line header and trailer comment
+ static private final String CSV_INPUT_MULTILINE_HEADER_TRAILER_COMMENT =
"# multi-line" + CRLF + "# header comment" + CRLF + "A,B" + CRLF + "1,2" + CRLF
+ "# multi-line" + CRLF + "# comment";
+ // Format with auto-detected header
+ static private final CSVFormat FORMAT_AUTO_HEADER =
CSVFormat.Builder.create(CSVFormat.DEFAULT).setCommentMarker('#').setHeader().build();
+ // Format with explicit header
+ static private final CSVFormat FORMAT_EXPLICIT_HEADER =
CSVFormat.Builder.create(CSVFormat.DEFAULT)
+ .setSkipHeaderRecord(true)
+ .setCommentMarker('#')
+ .setHeader("A", "B")
+ .build();
+ // Format with explicit header that does not skip the header line
+ CSVFormat FORMAT_EXPLICIT_HEADER_NOSKIP =
CSVFormat.Builder.create(CSVFormat.DEFAULT)
+ .setCommentMarker('#')
+ .setHeader("A", "B")
+ .build();
+ @Test
+ public void testGetHeaderComment_NoComment1() throws IOException {
+
+ try (CSVParser parser = CSVParser.parse(CSV_INPUT_NO_COMMENT,
FORMAT_AUTO_HEADER)) {
+ parser.getRecords();
+ // Expect no header comment
+ assertFalse(parser.hasHeaderComment());
+ assertNull(parser.getHeaderComment());
+ }
+ }
+ @Test
+ public void testGetHeaderComment_HeaderComment1() throws IOException {
+ try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_COMMENT,
FORMAT_AUTO_HEADER)) {
+ parser.getRecords();
+ // Expect a header comment
+ assertTrue(parser.hasHeaderComment());
+ assertEquals("header comment", parser.getHeaderComment());
+ }
+ }
+ @Test
+ public void testGetHeaderComment_HeaderTrailerComment() throws IOException
{
+ try (CSVParser parser =
CSVParser.parse(CSV_INPUT_MULTILINE_HEADER_TRAILER_COMMENT,
FORMAT_AUTO_HEADER)) {
+ parser.getRecords();
+ // Expect a header comment
+ assertTrue(parser.hasHeaderComment());
+ assertEquals("multi-line"+LF+"header comment",
parser.getHeaderComment());
+ }
+ }
+ @Test
+ public void testGetHeaderComment_NoComment2() throws IOException {
+ try (CSVParser parser = CSVParser.parse(CSV_INPUT_NO_COMMENT,
FORMAT_EXPLICIT_HEADER)) {
+ parser.getRecords();
+ // Expect no header comment
+ assertFalse(parser.hasHeaderComment());
+ assertNull(parser.getHeaderComment());
+ }
+ }
+ @Test
+ public void testGetHeaderComment_HeaderComment2() throws IOException {
+ try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_COMMENT,
FORMAT_EXPLICIT_HEADER)) {
+ parser.getRecords();
+ // Expect a header comment
+ assertTrue(parser.hasHeaderComment());
+ assertEquals("header comment", parser.getHeaderComment());
+ }
+ }
+ @Test
+ public void testGetHeaderComment_NoComment3() throws IOException {
+ try (CSVParser parser = CSVParser.parse(CSV_INPUT_NO_COMMENT,
FORMAT_EXPLICIT_HEADER_NOSKIP)) {
+ parser.getRecords();
+ // Expect no header comment
+ assertFalse(parser.hasHeaderComment());
+ assertNull(parser.getHeaderComment());
+ }
+ }
+ @Test
+ public void testGetHeaderComment_HeaderComment3() throws IOException {
+ try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_COMMENT,
FORMAT_EXPLICIT_HEADER_NOSKIP)) {
+ parser.getRecords();
+ // Expect no header comment - the text "comment" is attached to
the first record
+ assertFalse(parser.hasHeaderComment());
+ assertNull(parser.getHeaderComment());
+ }
+ }
+
+ @Test
+ public void testGetTrailerComment_HeaderComment1() throws IOException {
+ try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_COMMENT,
FORMAT_AUTO_HEADER)) {
+ parser.getRecords();
+ assertFalse(parser.hasTrailerComment());
+ assertNull(parser.getTrailerComment());
+ }
+ }
+ @Test
+ public void testGetTrailerComment_HeaderTrailerComment1() throws
IOException {
+ try (CSVParser parser =
CSVParser.parse(CSV_INPUT_HEADER_TRAILER_COMMENT, FORMAT_AUTO_HEADER)) {
+ parser.getRecords();
+ assertTrue(parser.hasTrailerComment());
+ assertEquals("comment", parser.getTrailerComment());
+ }
+ }
+ @Test
+ public void testGetTrailerComment_MultilineComment() throws IOException {
+ try (CSVParser parser =
CSVParser.parse(CSV_INPUT_MULTILINE_HEADER_TRAILER_COMMENT,
FORMAT_AUTO_HEADER)) {
+ parser.getRecords();
+ assertTrue(parser.hasTrailerComment());
+ assertEquals("multi-line"+LF+"comment",
parser.getTrailerComment());
+ }
+ }
+ @Test
+ public void testGetTrailerComment_HeaderComment2() throws IOException {
+ try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_COMMENT,
FORMAT_EXPLICIT_HEADER)) {
+ parser.getRecords();
+ assertFalse(parser.hasTrailerComment());
+ assertNull(parser.getTrailerComment());
+ }
+ }
+ @Test
+ public void testGetTrailerComment_HeaderTrailerComment2() throws
IOException {
+ try (CSVParser parser =
CSVParser.parse(CSV_INPUT_HEADER_TRAILER_COMMENT, FORMAT_EXPLICIT_HEADER)) {
+ parser.getRecords();
+ assertTrue(parser.hasTrailerComment());
+ assertEquals("comment", parser.getTrailerComment());
+ }
+ }
+ @Test
+ public void testGetTrailerComment_HeaderComment3() throws IOException {
+ try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_COMMENT,
FORMAT_EXPLICIT_HEADER_NOSKIP)) {
+ parser.getRecords();
+ assertFalse(parser.hasTrailerComment());
+ assertNull(parser.getTrailerComment());
+ }
+ }
+ @Test
Review Comment:
Please separate methods with a blank line.
##########
src/main/java/org/apache/commons/csv/CSVParser.java:
##########
@@ -713,6 +764,10 @@ CSVRecord nextRecord() throws IOException {
case EOF:
if (this.reusableToken.isReady) {
this.addRecordValue(true);
+ } else {
Review Comment:
Extra block level is not needed, IOW use "else if" instead of "else { if ...
}"
Issue Time Tracking
-------------------
Worklog Id: (was: 807050)
Time Spent: 50m (was: 40m)
> Provide access methods for header and trailer comments
> ------------------------------------------------------
>
> Key: CSV-304
> URL: https://issues.apache.org/jira/browse/CSV-304
> Project: Commons CSV
> Issue Type: Improvement
> Components: Parser
> Affects Versions: 1.9.0
> Reporter: Peter Hull
> Priority: Minor
> Fix For: Discussion
>
> Attachments: header-comment.patch
>
> Time Spent: 50m
> Remaining Estimate: 0h
>
> The Apache Commons CSV library can deal with comments by attaching the
> comment to the following record.
> It does not seem possible to access comments that are before the header line
> ("header comments"), or after the last record ("trailer comments")
> I feel this would be a useful enhancement to the current comment-handling
> features of version 1.9.
> I have not been able to find mention of this in other JIRA tickets.
> I have attached a patch implementing this, for discussion purposes. (n.b. I
> had trouble with git showing spurious ^M at the end of changed lines in
> CSVParserTest.java which I could not get rid of)
> This adds methods String CSVParser::getHeaderComment(), String
> CSVParser::getTrailerComment(), bool CSVParser::hasHeaderComment() and bool
> CSVParser::hasTrailerComment() . The last two could be omitted assuming a
> missing comment would return a null String.
>
> See also
> [https://stackoverflow.com/questions/72619095/get-leading-comments-from-csv-with-apache-commons-csv]
> and [https://lists.apache.org/thread/8tczpqlnjgbcyovyhb484tt11vqv91jt]
--
This message was sent by Atlassian Jira
(v8.20.10#820010)