You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by hans-christian <ha...@regionh.dk> on 2015/04/08 13:36:37 UTC

Problem to write quotes correct on CSV-fields when marshalling with CsvDataFormat

Hi all,

I'm using CsvDataFormat to unmarshal a csv file, modify some of the fields
and then marshal it back to csv again. I only wants to modify a few fields
and the let the other one be untouched. 

Everything works fine but my problem is that I don't get the quotes on my
output files as I want. I would like to have quotes on the output fields
when there is quotes on the input field, or be able to force all fields to
have quotes on the output fields.

I have only been able to have get quotes on the output field when there is
an extra blankspaces in the input field, example: (tab as delimiter and
ignore surrounding blankspaces set to false)
Input record: "0"   "1 "   "2"    4
Current Output record: 0    "1 "    2    4
Wanted Output record: "0"    "1 "    "2"    4

I would like that the quote on the output records follows the input record
Any suggestions how to do this?

Snippet of my camel route:
final CsvDataFormat csv = new CsvDataFormat();
csv.setLazyLoad(Boolean.FALSE);
csv.setDelimiter("\u0009");
csv.setRecordSeparator("\r\n");
csv.setIgnoreSurroundingSpaces(Boolean.FALSE);
...
 .unmarshal(csv)
 .beanRef("csvParser")
 .marshal(csv)
 
The csvParser returns a List<Map&lt;String, String>>

I'm using Camel 2.15.0

Regards
Hans-Christian



--
View this message in context: http://camel.465427.n5.nabble.com/Problem-to-write-quotes-correct-on-CSV-fields-when-marshalling-with-CsvDataFormat-tp5765477.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Problem to write quotes correct on CSV-fields when marshalling with CsvDataFormat

Posted by hans-christian <ha...@regionh.dk>.
Hi!

I tested with the suggested change and now I have qoutes at all fields,
which solves my issue.
Thanks!


yogu13 wrote
> Thank you for the test case... I am assuming you are using camel-csv
> component here
> 
> In that case couple of things i noticed would require a change:-
> 
> 1. Use org.apache.camel.dataformat.csv.CsvDataFormat (This has its own
> DataFormat defined as well)
> 2. The Dataformat would be set as below
>                         final CsvDataFormat csv = new CsvDataFormat(); 
> 	                csv.setLazyLoad(Boolean.FALSE); 
> 	                csv.setDelimiter(Character.valueOf(';'));	                 
> 	                csv.setRecordSeparator("\r\n"); 
> 	                csv.setIgnoreSurroundingSpaces(Boolean.FALSE); 
> 	                csv.setQuoteDisabled(Boolean.FALSE);
> 	                csv.setQuoteMode(QuoteMode.ALL);
> 3. Include a dependency for 
>            
> <dependency>
>               
> <groupId>
> org.apache.commons
> </groupId>
>               
> <artifactId>
> commons-csv
> </artifactId>
>               
> <version>
> 1.0
> </version>
>           
> </dependency>
> 4. The ouput then generated would be 
>         String output = "\"ABC\";\"DEF \";\"GHI\"\r\n";
> 
> 
> Hope this helps!
> 
> Regards,
> -Yogesh





--
View this message in context: http://camel.465427.n5.nabble.com/Problem-to-write-quotes-correct-on-CSV-fields-when-marshalling-with-CsvDataFormat-tp5765477p5766055.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Problem to write quotes correct on CSV-fields when marshalling with CsvDataFormat

Posted by yogu13 <yo...@gmail.com>.
Hello !

Thank you for the test case... I am assuming you are using camel-csv
component here

In that case couple of things i noticed would require a change:-

1. Use org.apache.camel.dataformat.csv.CsvDataFormat (This has its own
DataFormat defined as well)
2. The Dataformat would be set as below
                        final CsvDataFormat csv = new CsvDataFormat(); 
	                csv.setLazyLoad(Boolean.FALSE); 
	                csv.setDelimiter(Character.valueOf(';'));	                 
	                csv.setRecordSeparator("\r\n"); 
	                csv.setIgnoreSurroundingSpaces(Boolean.FALSE); 
	                csv.setQuoteDisabled(Boolean.FALSE);
	                csv.setQuoteMode(QuoteMode.ALL);
3. Include a dependency for 
           <dependency>
              <groupId>org.apache.commons</groupId>
              <artifactId>commons-csv</artifactId>
              <version>1.0</version>
          </dependency>
4. The ouput then generated would be 
        String output = "\"ABC\";\"DEF \";\"GHI\"\r\n";


Hope this helps!

Regards,
-Yogesh




--
View this message in context: http://camel.465427.n5.nabble.com/Problem-to-write-quotes-correct-on-CSV-fields-when-marshalling-with-CsvDataFormat-tp5765477p5765668.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Problem to write quotes correct on CSV-fields when marshalling with CsvDataFormat

Posted by hans-christian <ha...@regionh.dk>.
Hi,


yogu13 wrote
> would you have any test case which can be used to reproduce it?

I wrote a simple unit test for the given scenario. The expected outcome is
"ABC" as the first csv field in the output file. I'm using Camel 2.15.0 and
Java 8.

import java.nio.file.Files;
import java.nio.file.Paths;

import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.dataformat.CsvDataFormat;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;

public class CsvTest extends CamelTestSupport {

    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {

                // Create and configure the csv component
                final CsvDataFormat csv = new CsvDataFormat();
                csv.setLazyLoad(Boolean.FALSE);
                csv.setDelimiter(";");
                csv.setRecordSeparator("\r\n");
                csv.setIgnoreSurroundingSpaces(Boolean.FALSE);

                from("file://target/inbox")
                  .unmarshal(csv)
                  .marshal(csv)
                .to("file://target/outbox");
            }
        };
    }

    @Test
    public void testCsvFieldWithQuotes() throws Exception {

        // A csv row: "one";"two ";three
        final String inputRow = "\"ABC\";\"DEF \";GHI\r\n";

        // Simulate file drop
        template
            .sendBodyAndHeader("file://target/inbox", inputRow,
Exchange.FILE_NAME, "csvFile.txt");
        Thread.sleep(2000);

        // Assert that the content of the file is the expected csv row
        // Expect ABC to be surrounded by quotes
        String outputRow= new
String(Files.readAllBytes(Paths.get("target/outbox/csvFile.txt")));
        assertEquals(inputRow, outputRow);

    }
}

I would like to "force" to have quotations on the ABC field

Regards 
Hans-Christian



--
View this message in context: http://camel.465427.n5.nabble.com/Problem-to-write-quotes-correct-on-CSV-fields-when-marshalling-with-CsvDataFormat-tp5765477p5765663.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Problem to write quotes correct on CSV-fields when marshalling with CsvDataFormat

Posted by yogu13 <yo...@gmail.com>.
Hello,

would you have any test case which can be used to reproduce it?

Regards,
-Yogesh



--
View this message in context: http://camel.465427.n5.nabble.com/Problem-to-write-quotes-correct-on-CSV-fields-when-marshalling-with-CsvDataFormat-tp5765477p5765522.html
Sent from the Camel - Users mailing list archive at Nabble.com.