Uploaded image for project: 'Commons CSV'
  1. Commons CSV
  2. CSV-33

The CSVPrinter ecapsing inconsistant with CSVParser

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • None
    • 1.0
    • Printer
    • None

    Description

      The CSVPrinter ecapses new line and return character to "\n" and "\r" if these occur within the encapsulators (this is within the CSVPrinter.escapeAndQuote(String) method). However, the CSVParser do not convert these back to new line and return characters in the same fashion. So if you use the CSVPrinter to create a delimited file containing new line or return characters within an entry and then read this file using the CSVParser the text read in by the CSVParser will not match the text written by the CSVPrinter (the difference being that every new line and return character will be replaced by "\n" and "\r" respectively).

      A possible fix for this would be to add two extra 'else if' statements to CSVParser.encapsulatedTokenLexer(Token, int) starting at line 49, as detailed below (the ehampsised text indicated the changes):

      else if (c == '
      ' && in.lookAhead() == '
      ')

      { // doubled escape char, it does not escape itself, only encapsulator // -> add both escape chars to stream tkn.content.append((char) c); c = in.read(); tkn.content.append((char) c); }

      else if (c == '
      ' && in.lookAhead() == 'n')

      {
      _ // escaped java new line character, append a new line character_
      tkn.content.append('\n');
      c = in.read();
      }
      else if (c == '
      ' && in.lookAhead() == 'r')

      {
      // escaped java return character, append a return character
      tkn.content.append('\r');
      c = in.read();
      }
      else if (strategy.getUnicodeEscapeInterpretation() && c == '
      '
      && in.lookAhead() == 'u')

      { // interpret unicode escaped chars (like \u0070 -> p) tkn.content.append((char) unicodeEscapeLexer(c)); }

      Attachments

        1. CSVPrintTest.java
          3 kB
          Colin Goodheart-Smithe
        2. CSVParser.java
          23 kB
          Colin Goodheart-Smithe

        Activity

          People

            Unassigned Unassigned
            colings Colin Goodheart-Smithe
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: