diff --git a/src/main/java/org/apache/commons/csv/CSVRecord.java b/src/main/java/org/apache/commons/csv/CSVRecord.java index 5567f5ec..a2360c50 100644 --- a/src/main/java/org/apache/commons/csv/CSVRecord.java +++ b/src/main/java/org/apache/commons/csv/CSVRecord.java @@ -161,6 +161,16 @@ public final class CSVRecord implements Serializable, Iterable { return mapping == null || mapping.size() == values.length; } + /** + * Checks whether this record is a comment, false otherwise. + * + * @return true if this record is a comment, false otherwise + * @since 1.3 + */ + public boolean isComment() { + return comment != null; + } + /** * Checks whether a given column is mapped, i.e. its name has been defined to the parser. * diff --git a/src/test/java/org/apache/commons/csv/JiraCsv167Test.java b/src/test/java/org/apache/commons/csv/JiraCsv167Test.java new file mode 100644 index 00000000..d8973d46 --- /dev/null +++ b/src/test/java/org/apache/commons/csv/JiraCsv167Test.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.csv; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.Charset; + +import org.junit.Assert; +import org.junit.Ignore; +import org.junit.Test; + +public class JiraCsv167Test { + + @Test + @Ignore("Fails") + public void parse() throws IOException { + File csvData = new File("src/test/resources/csv-167/sample1.csv"); + CSVFormat format = CSVFormat.DEFAULT; + // + format = format.withAllowMissingColumnNames(false); + format = format.withCommentMarker('#'); + format = format.withDelimiter(','); + format = format.withEscape('\\'); + format = format.withHeader("author", "title", "publishDate"); + format = format.withHeaderComments("headerComment"); + format = format.withNullString("NULL"); + format = format.withIgnoreEmptyLines(true); + format = format.withIgnoreSurroundingSpaces(true); + format = format.withQuote('"'); + format = format.withQuoteMode(QuoteMode.ALL); + format = format.withRecordSeparator('\n'); + format = format.withSkipHeaderRecord(false); + // + CSVParser parser = CSVParser.parse(csvData, Charset.defaultCharset(), format); + int comments = 0; + int records = 0; + for (CSVRecord csvRecord : parser) { + if (csvRecord.isComment()) { + comments++; + } else { + records++; + // System.out.println("[" + csvRecord.toString() + "]"); + } + } + // Comment lines are concatenated, in this example 4 lines become 2 comments. + Assert.assertEquals(2, comments); + Assert.assertEquals(3, records); + } +} diff --git a/src/test/resources/csv-167/sample1.csv b/src/test/resources/csv-167/sample1.csv new file mode 100644 index 00000000..ee3b3ed9 --- /dev/null +++ b/src/test/resources/csv-167/sample1.csv @@ -0,0 +1,8 @@ +# Comment before header +author,title,publishDate +Dan Simmons,Hyperion,"1989" +# Comment Line 1 +# Comment Line 2 +# Comment Line 3 +Douglas Adams,The Hitchhiker's \"Guide\" to the Galaxy,1979 +Douglas John,The Hitchhiker's \"Guide\" to the Mars,1979