You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2018/03/23 09:34:14 UTC
[camel] branch camel-2.21.x updated: CAMEL-12394: bindy csv now
supports quoting headers as well. Thanks to Clemens Blamauer for the patch.
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch camel-2.21.x
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-2.21.x by this push:
new d90de1a CAMEL-12394: bindy csv now supports quoting headers as well. Thanks to Clemens Blamauer for the patch.
d90de1a is described below
commit d90de1a505746d2beb55bdde4eb99b06936d0cc1
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Mar 23 10:28:45 2018 +0100
CAMEL-12394: bindy csv now supports quoting headers as well. Thanks to Clemens Blamauer for the patch.
---
.../src/main/docs/bindy-dataformat.adoc | 2 +-
.../camel/dataformat/bindy/BindyCsvFactory.java | 17 ++++-
.../dataformat/bindy/annotation/CsvRecord.java | 2 +-
.../bindy/csv/WickedHeaderWithCommaCsv.java | 71 +++++++++++++++++++
.../bindy/csv/WickedHeaderWithCommaCsvTest.java | 80 ++++++++++++++++++++++
5 files changed, 168 insertions(+), 4 deletions(-)
diff --git a/components/camel-bindy/src/main/docs/bindy-dataformat.adoc b/components/camel-bindy/src/main/docs/bindy-dataformat.adoc
index b8a64cc..3b5b267 100644
--- a/components/camel-bindy/src/main/docs/bindy-dataformat.adoc
+++ b/components/camel-bindy/src/main/docs/bindy-dataformat.adoc
@@ -136,7 +136,7 @@ fields when CSV is generated
fields when CSV is generated. This annotation is associated to the root class of the model and must be
declared one time.
-|quoting |boolean |*Camel 2.11:*optional - default value = false - Indicate if the values
+|quoting |boolean |*Camel 2.11:*optional - default value = false - Indicate if the values (and headers)
must be quoted when marshaling when CSV is generated.
|endWithLineBreak |boolean |*Camel 2.21:* optional - default value = true - Indicate if the CSV generated file
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 e2046c9..00e6af0 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
@@ -567,10 +567,23 @@ public class BindyCsvFactory extends BindyAbstractFactory implements BindyFactor
field.setAccessible(true);
// Get dataField
+ final String res;
if (!dataField.columnName().equals("")) {
- builderHeader.append(dataField.columnName());
+ res = dataField.columnName();
} else {
- builderHeader.append(field.getName());
+ res = field.getName();
+ }
+
+ if (quoting && quote != null) {
+ builderHeader.append(quote);
+ }
+ if (quoting && quote != null && (res.contains("\\" + quote) || res.contains(quote)) && quotingEscaped) {
+ builderHeader.append(res.replaceAll("\\" + quote, "\\\\" + quote));
+ } else {
+ builderHeader.append(res);
+ }
+ if (quoting && quote != null) {
+ builderHeader.append(quote);
}
if (it.hasNext()) {
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 6175e5c..8d7f322 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
@@ -82,7 +82,7 @@ public @interface CsvRecord {
String quote() default "\"";
/**
- * Indicate if the values must be quoted when marshaling (optional)
+ * Indicate if the values (and headers) must be quoted when marshaling (optional)
*/
boolean quoting() default false;
diff --git a/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/WickedHeaderWithCommaCsv.java b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/WickedHeaderWithCommaCsv.java
new file mode 100644
index 0000000..42aa5ed
--- /dev/null
+++ b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/WickedHeaderWithCommaCsv.java
@@ -0,0 +1,71 @@
+/**
+ * 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 = ",", skipFirstLine = true, quoting = true, generateHeaderColumns = true)
+public class WickedHeaderWithCommaCsv {
+
+ @DataField(columnName = "Foo (one, or more, foos)", pos = 1)
+ private String foo;
+
+ @DataField(columnName = "Bar (one, or more, bars)", pos = 2)
+ private String bar;
+
+ public String getFoo() {
+ return foo;
+ }
+
+ public void setFoo(String foo) {
+ this.foo = foo;
+ }
+
+ public String getBar() {
+ return bar;
+ }
+
+ public void setBar(String bar) {
+ this.bar = bar;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+
+ WickedHeaderWithCommaCsv wickedHeaderWithCommaCsv = (WickedHeaderWithCommaCsv) o;
+
+ if (foo != null ? !foo.equals(wickedHeaderWithCommaCsv.foo) : wickedHeaderWithCommaCsv.foo != null) {
+ return false;
+ }
+ return bar != null ? bar.equals(wickedHeaderWithCommaCsv.bar) : wickedHeaderWithCommaCsv.bar == null;
+ }
+
+ @Override
+ public int hashCode() {
+ int result = foo != null ? foo.hashCode() : 0;
+ result = 31 * result + (bar != null ? bar.hashCode() : 0);
+ return result;
+ }
+
+}
diff --git a/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/WickedHeaderWithCommaCsvTest.java b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/WickedHeaderWithCommaCsvTest.java
new file mode 100644
index 0000000..0d4f87f
--- /dev/null
+++ b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/WickedHeaderWithCommaCsvTest.java
@@ -0,0 +1,80 @@
+/**
+ * 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 java.util.List;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class WickedHeaderWithCommaCsvTest extends CamelTestSupport {
+
+ @Test
+ public void testHeadersWithComma() throws Exception {
+
+ final MockEndpoint unmarshalMock = getMockEndpoint("mock:receiveUnmarshal");
+ unmarshalMock.expectedMessageCount(1);
+
+ String csv = ("\"Foo (one, or more, foos)\",\"Bar (one, or more, bars)\"" + "\r\n")
+ + "\"1,000.00\",\"1,500.00\"" + "\r\n"
+ + "\"2,000.00\",\"2,700.00\"" + "\r\n";
+
+ template.sendBody("direct:startUnmarshal", csv);
+
+ assertMockEndpointsSatisfied();
+
+ final List<WickedHeaderWithCommaCsv> wickedHeaderWithCommaCsvs = unmarshalMock.getReceivedExchanges().get(0).getIn().getBody(List.class);
+
+ final WickedHeaderWithCommaCsv row1000 = wickedHeaderWithCommaCsvs.get(0);
+ assertEquals("1,000.00", row1000.getFoo());
+ assertEquals("1,500.00", row1000.getBar());
+
+ final WickedHeaderWithCommaCsv row2000 = (WickedHeaderWithCommaCsv) wickedHeaderWithCommaCsvs.get(1);
+ assertEquals("2,000.00", row2000.getFoo());
+ assertEquals("2,700.00", row2000.getBar());
+
+ final MockEndpoint marshalMock = getMockEndpoint("mock:receiveMarshal");
+ template.sendBody("direct:startMarshal", wickedHeaderWithCommaCsvs);
+
+ marshalMock.expectedMessageCount(1);
+ assertMockEndpointsSatisfied();
+
+ final String result = marshalMock.getReceivedExchanges().get(0).getIn().getBody(String.class);
+
+ assertEquals(csv.trim(), result.trim());
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:startUnmarshal")
+ .unmarshal(new BindyCsvDataFormat(WickedHeaderWithCommaCsv.class))
+ .to("mock:receiveUnmarshal");
+
+ from("direct:startMarshal")
+ .marshal(new BindyCsvDataFormat(WickedHeaderWithCommaCsv.class))
+ .to("mock:receiveMarshal");
+
+ }
+ };
+ }
+
+}
--
To stop receiving notification emails like this one, please contact
davsclaus@apache.org.