This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push: new 4a538a1d6a9 CAMEL-19662: camel-bindy - Add option to @CvsRecord to turn on|off trimming line. (#10857) 4a538a1d6a9 is described below commit 4a538a1d6a94ea40f778bd533586da4890faf7f5 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Thu Jul 27 15:05:00 2023 +0200 CAMEL-19662: camel-bindy - Add option to @CvsRecord to turn on|off trimming line. (#10857) --- .../src/main/docs/bindy-dataformat.adoc | 2 + .../camel/dataformat/bindy/BindyCsvFactory.java | 7 ++ .../dataformat/bindy/annotation/CsvRecord.java | 5 + .../dataformat/bindy/csv/BindyCsvDataFormat.java | 17 ++-- .../csv/BindySimpleCsvUnmarshallTrimLineTest.java | 105 +++++++++++++++++++++ .../apache/camel/dataformat/bindy/csv/Cartoon.java | 46 +++++++++ ...indySimpleCsvUnmarshallTrimLineTest-context.xml | 34 +++++++ 7 files changed, 210 insertions(+), 6 deletions(-) diff --git a/components/camel-bindy/src/main/docs/bindy-dataformat.adoc b/components/camel-bindy/src/main/docs/bindy-dataformat.adoc index 91c07379118..5d0b701b273 100644 --- a/components/camel-bindy/src/main/docs/bindy-dataformat.adoc +++ b/components/camel-bindy/src/main/docs/bindy-dataformat.adoc @@ -140,6 +140,8 @@ skipped. | skipFirstLine | boolean | | false a| The skipFirstLine parameter will allow to skip or not the first line of a CSV file. This line often contains columns definition +| trimLine | boolean | | true | Whether to trim each line (stand and end) before parsing the line into data fields. + |=== // annotation options: END diff --git a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java index b167046ea7e..a24adf46cda 100644 --- a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java +++ b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java @@ -76,6 +76,7 @@ public class BindyCsvFactory extends BindyAbstractFactory implements BindyFactor private boolean quotingOnlyWhenNeeded; private boolean endWithLineBreak; private boolean removeQuotes; + private boolean trimLine; public BindyCsvFactory(Class<?> type) throws Exception { super(type); @@ -689,6 +690,9 @@ public class BindyCsvFactory extends BindyAbstractFactory implements BindyFactor removeQuotes = record.removeQuotes(); LOG.debug("Remove quotes: {}", removeQuotes); + + trimLine = record.trimLine(); + LOG.debug("Trim line: {}", trimLine); } if (section != null) { @@ -795,4 +799,7 @@ public class BindyCsvFactory extends BindyAbstractFactory implements BindyFactor return endWithLineBreak; } + public boolean isTrimLine() { + return trimLine; + } } diff --git a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/CsvRecord.java b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/CsvRecord.java index af4cb41abab..44120f31997 100644 --- a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/CsvRecord.java +++ b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/CsvRecord.java @@ -119,4 +119,9 @@ public @interface CsvRecord { */ boolean removeQuotes() default true; + /** + * Whether to trim each line (stand and end) before parsing the line into data fields. + */ + boolean trimLine() default true; + } diff --git a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/csv/BindyCsvDataFormat.java b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/csv/BindyCsvDataFormat.java index ec6d7777eaf..9fc96ec9ac3 100644 --- a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/csv/BindyCsvDataFormat.java +++ b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/csv/BindyCsvDataFormat.java @@ -206,15 +206,20 @@ public class BindyCsvDataFormat extends BindyAbstractDataFormat { String separator, Boolean removeQuotes, String quote, AtomicInteger count) { return line -> { try { - // Trim the line coming in to remove any trailing whitespace String trimmedLine; - // if separator is a tab, don't trim any leading whitespaces (could be empty values separated by tabs) - if (separator.equals("\t")) { - // trim only trailing whitespaces (remove new lines etc but keep tab character) - trimmedLine = line.replaceAll("[ \\n\\x0B\\f\\r]+$", ""); + // Trim the line coming in to remove any trailing whitespace + if (factory.isTrimLine()) { + // if separator is a tab, don't trim any leading whitespaces (could be empty values separated by tabs) + if (separator.equals("\t")) { + // trim only trailing whitespaces (remove new lines etc but keep tab character) + trimmedLine = line.replaceAll("[ \\n\\x0B\\f\\r]+$", ""); + } else { + trimmedLine = line.trim(); + } } else { - trimmedLine = line.trim(); + // no trim + trimmedLine = line; } // Increment counter diff --git a/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvUnmarshallTrimLineTest.java b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvUnmarshallTrimLineTest.java new file mode 100644 index 00000000000..c04ed6bfa98 --- /dev/null +++ b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvUnmarshallTrimLineTest.java @@ -0,0 +1,105 @@ +/* + * 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.camel.dataformat.bindy.csv; + +import org.apache.camel.EndpointInject; +import org.apache.camel.Exchange; +import org.apache.camel.Produce; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.dataformat.bindy.format.FormatException; +import org.apache.camel.dataformat.bindy.model.simple.oneclass.Order; +import org.apache.camel.test.spring.junit5.CamelSpringTest; +import org.junit.jupiter.api.Test; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ContextConfiguration; + +import java.util.List; + +import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +@ContextConfiguration +@CamelSpringTest +public class BindySimpleCsvUnmarshallTrimLineTest { + + private static final String URI_MOCK_RESULT = "mock:result"; + private static final String URI_MOCK_ERROR = "mock:error"; + private static final String URI_DIRECT_START = "direct:start"; + + @Produce(URI_DIRECT_START) + private ProducerTemplate template; + + @EndpointInject(URI_MOCK_RESULT) + private MockEndpoint result; + + @EndpointInject(URI_MOCK_ERROR) + private MockEndpoint error; + + private String expected; + + @Test + @DirtiesContext + public void testTrimLineFalse() throws Exception { + expected = "01,Donald Duck "; + + template.sendBody(expected); + + result.expectedMessageCount(1); + result.assertIsSatisfied(); + + Cartoon c = result.getExchanges().get(0).getMessage().getBody(Cartoon.class); + assertNotNull(c); + assertEquals(1, c.getNo()); + assertEquals("Donald Duck ", c.getName()); + } + + @Test + @DirtiesContext + public void testTrimLineFalseTwo() throws Exception { + expected = "01,Donald Duck \r\n02, Bugs Bunny "; + + template.sendBody(expected); + + result.expectedMessageCount(1); + result.assertIsSatisfied(); + + List<Cartoon> l = result.getExchanges().get(0).getMessage().getBody(List.class); + assertEquals(2, l.size()); + Cartoon c = l.get(0); + assertEquals(1, c.getNo()); + assertEquals("Donald Duck ", c.getName()); + + c = l.get(1); + assertEquals(2, c.getNo()); + assertEquals(" Bugs Bunny ", c.getName()); + } + + public static class ContextConfig extends RouteBuilder { + BindyCsvDataFormat camelDataFormat + = new BindyCsvDataFormat(Cartoon.class); + + @Override + public void configure() { + from(URI_DIRECT_START).unmarshal(camelDataFormat).to(URI_MOCK_RESULT); + } + + } +} diff --git a/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/Cartoon.java b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/Cartoon.java new file mode 100644 index 00000000000..c526c8c2dbd --- /dev/null +++ b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/Cartoon.java @@ -0,0 +1,46 @@ +/* + * 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.camel.dataformat.bindy.csv; + +import org.apache.camel.dataformat.bindy.annotation.CsvRecord; +import org.apache.camel.dataformat.bindy.annotation.DataField; + +@CsvRecord(separator = ",", trimLine = false) +public class Cartoon { + + @DataField(pos = 1) + private int no; + + @DataField(pos = 2) + private String name; + + public int getNo() { + return no; + } + + public void setNo(int no) { + this.no = no; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/components/camel-bindy/src/test/resources/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvUnmarshallTrimLineTest-context.xml b/components/camel-bindy/src/test/resources/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvUnmarshallTrimLineTest-context.xml new file mode 100644 index 00000000000..28a7f4f19bd --- /dev/null +++ b/components/camel-bindy/src/test/resources/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvUnmarshallTrimLineTest-context.xml @@ -0,0 +1,34 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + 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. + +--> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans + http://www.springframework.org/schema/beans/spring-beans.xsd + http://camel.apache.org/schema/spring + http://camel.apache.org/schema/spring/camel-spring.xsd"> + + <camelContext xmlns="http://camel.apache.org/schema/spring"> + <routeBuilder ref="myBuilder" /> + </camelContext> + + <bean id="myBuilder" class="org.apache.camel.dataformat.bindy.csv.BindySimpleCsvUnmarshallTrimLineTest$ContextConfig"/> + +</beans> \ No newline at end of file