From 4b2e26bf83a7a503371189bbfd5c4a62ddfe8ad6 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Wed, 31 Dec 2014 22:26:02 -0800 Subject: [PATCH] Implemnt #56 --- release-notes/CREDITS | 7 +++ release-notes/VERSION | 2 + .../jackson/dataformat/csv/CsvParser.java | 2 + .../dataformat/csv/impl/CsvDecoder.java | 57 +++++++++++-------- 4 files changed, 43 insertions(+), 25 deletions(-) diff --git a/release-notes/CREDITS b/release-notes/CREDITS index ca6b1d0..764559c 100644 --- a/release-notes/CREDITS +++ b/release-notes/CREDITS @@ -28,3 +28,10 @@ Wei Li (wli600@github) * Contributed fix for 54: Encounter ArrayIndexOutOfBoundsException in the corner case delimiter or end-of-line happened to be the leading character of a segment buffer (2.4.4) + +Luke Nezda (nezda@github) + +* Suggested #56: Support comments (either via `CsvSchema`, or using std + `JsonParser.Feature.ALLOW_YAML_COMMENTS. + (requested by nezda@github) + (2.5.0) \ No newline at end of file diff --git a/release-notes/VERSION b/release-notes/VERSION index e0f7c72..33a0072 100644 --- a/release-notes/VERSION +++ b/release-notes/VERSION @@ -9,6 +9,8 @@ Project: jackson-dataformat-csv #50: Support `JsonGenerator.Feature.IGNORE_KNOWN` for CSV, to ignoring extra columns #53: Add a way to specify "null value" (String) for `CsvGenerator` to use when writing `null`s (part of `CsvSchema`; method `withNullValue()`) +#56: Support comments (either via `CsvSchema`, or using std `JsonParser.Feature.ALLOW_YAML_COMMENTS. + (requested by nezda@github) #57: Support simple array types #61: Add a feature to always quote non-numeric values: `CsvGenerator.Feature.ALWAYS_QUOTE_STRINGS` diff --git a/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvParser.java b/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvParser.java index 755b8dd..f70cb29 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvParser.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvParser.java @@ -512,6 +512,8 @@ protected JsonToken _handleStartDoc() throws IOException if (_schema.skipsFirstDataRow()) { _reader.skipLine(); } + // also, if comments enabled, may need to skip leading ones + _reader.skipLeadingComments(); /* Only one real complication, actually; empy documents (zero bytes). * Those have no entries. Should be easy enough to detect like so: diff --git a/src/main/java/com/fasterxml/jackson/dataformat/csv/impl/CsvDecoder.java b/src/main/java/com/fasterxml/jackson/dataformat/csv/impl/CsvDecoder.java index b83f8f6..6253bba 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/csv/impl/CsvDecoder.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/csv/impl/CsvDecoder.java @@ -519,7 +519,7 @@ public boolean startNewLine() throws IOException } if (_allowComments && _inputBuffer[_inputPtr] == '#') { - int i = _skipCommentLine(); + int i = _skipCommentLines(); // end-of-input? if (i < 0) { return false; @@ -530,6 +530,37 @@ public boolean startNewLine() throws IOException return true; } + public void skipLeadingComments() throws IOException + { + if (_allowComments) { + if ((_inputPtr < _inputEnd) || loadMore()) { + if (_inputBuffer[_inputPtr] == '#') { + _skipCommentLines(); + --_inputPtr; + } + } + } + } + + protected int _skipCommentLines() throws IOException + { + while ((_inputPtr < _inputEnd) || loadMore()) { + char ch = _inputBuffer[_inputPtr++]; + if (ch >= ' ' || (ch != '\r' && ch != '\n')) { + continue; + } + _pendingLF = ch; + _handleLF(); + + // Ok, skipped the end of the line. Check next one... + int i = _nextChar(); + if (i != INT_HASH) { + return i; + } + } + return -1; // end of input + } + public boolean skipLine() throws IOException { if (_pendingLF != 0) { @@ -674,30 +705,6 @@ public JsonToken nextNumberOrString() throws IOException } return JsonToken.VALUE_STRING; } - - protected int _skipCommentLine() throws IOException - { - while ((_inputPtr < _inputEnd) || loadMore()) { - char ch = _inputBuffer[_inputPtr++]; - if (ch >= ' ' || (ch != '\r' && ch != '\n')) { - continue; - } - _pendingLF = ch; - _handleLF(); - - // Ok, skipped the end of the line. And parse next one... - int i; - if (_trimSpaces) { - i = _skipLeadingSpace(); - } else { - i = _nextChar(); - } - if (i != INT_HASH) { - return i; - } - } - return -1; // end of input - } /* /**********************************************************************