[
https://issues.apache.org/jira/browse/CAMEL-12080?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16292250#comment-16292250
]
ASF GitHub Bot commented on CAMEL-12080:
----------------------------------------
davsclaus closed pull request #2149: CAMEL-12080: allow creating csvcontent
without last eol
URL: https://github.com/apache/camel/pull/2149
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/components/camel-bindy/src/main/docs/bindy-dataformat.adoc
b/components/camel-bindy/src/main/docs/bindy-dataformat.adoc
index aa34c701883..3153fab9447 100644
--- a/components/camel-bindy/src/main/docs/bindy-dataformat.adoc
+++ b/components/camel-bindy/src/main/docs/bindy-dataformat.adoc
@@ -108,8 +108,8 @@ to several children model classes.
|separator |string |mandatory - can be ',' or ';' or 'anything'. This value is
interpreted
as a regular expression. If you want to use a sign which has a special
-meaning in regular expressions, e.g. the '|' sign, than you have to mask
-it, like '|'
+meaning in regular expressions, e.g. the '\|' sign, than you have to mask
+it, like '\|'
|skipFirstLine |boolean |optional - default value = false - allow to skip the
first line of the
CSV file
@@ -136,6 +136,11 @@ declared one time.
|quoting |boolean |*Camel 2.11:*optional - default value = false - Indicate if
the values
must be quoted when marshaling when CSV is generated.
+
+|endWithLineBreak |boolean |*Camel 2.21:* optional - default value = true -
Indicate if the CSV generated file
+should end with a line break.
+
+|
|=======================================================================
*case 1 : separator = ','*
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 994bed07069..9906402ef6b 100755
---
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
@@ -74,6 +74,7 @@
private boolean autospanLine;
private boolean allowEmptyStream;
private boolean quotingEscaped;
+ private boolean endWithLineBreak;
public BindyCsvFactory(Class<?> type) throws Exception {
super(type);
@@ -624,6 +625,10 @@ private void initCsvRecordParameters() {
// Get quotingEscaped parameter
quotingEscaped = record.quotingEscaped();
LOG.debug("Escape quote character flag of the CSV: {}" +
quotingEscaped);
+
+ // Get endWithLineBreak parameter
+ endWithLineBreak = record.endWithLineBreak();
+ LOG.debug("End with line break: {}" + endWithLineBreak);
}
if (section != null) {
@@ -710,4 +715,8 @@ public int getMaxpos() {
public boolean isAllowEmptyStream() {
return allowEmptyStream;
}
+
+ public boolean isEndWithLineBreak() {
+ return endWithLineBreak;
+ }
}
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 63f1d119bc6..dd7bd186304 100755
---
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
@@ -95,5 +95,10 @@
* the unavaiable stream for CSV file.
*/
boolean allowEmptyStream() default false;
+
+ /**
+ * The endWithLineBreak parameter flags if the CSV file should end with a
line break or not (optional)
+ */
+ boolean endWithLineBreak() 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 ad24a998da7..00ea80900bd 100755
---
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
@@ -94,16 +94,18 @@ public void marshal(Exchange exchange, Object body,
OutputStream outputStream) t
models.add(row);
}
}
-
- for (Map<String, Object> model : models) {
-
- String result = factory.unbind(getCamelContext(), model);
+
+ Iterator<Map<String, Object>> modelsMap = models.iterator();
+ while (modelsMap.hasNext()) {
+ String result = factory.unbind(getCamelContext(),
modelsMap.next());
byte[] bytes =
exchange.getContext().getTypeConverter().convertTo(byte[].class, exchange,
result);
outputStream.write(bytes);
- // Add a carriage return
- outputStream.write(bytesCRLF);
+ if (factory.isEndWithLineBreak() || modelsMap.hasNext()) {
+ // Add a carriage return
+ outputStream.write(bytesCRLF);
+ }
}
}
diff --git
a/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyMarshalEndWithLineBreakTest.java
b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyMarshalEndWithLineBreakTest.java
new file mode 100644
index 00000000000..f8959e83639
--- /dev/null
+++
b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyMarshalEndWithLineBreakTest.java
@@ -0,0 +1,87 @@
+/**
+ * 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.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.dataformat.bindy.annotation.CsvRecord;
+import org.apache.camel.dataformat.bindy.model.csv.MyCsvRecord;
+import org.apache.camel.dataformat.bindy.model.csv.MyCsvRecord2;
+import org.apache.camel.dataformat.bindy.util.ConverterUtils;
+import org.apache.camel.model.dataformat.BindyType;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+/**
+ *
+ */
+public class BindyMarshalEndWithLineBreakTest extends CamelTestSupport {
+
+ @Test
+ public void testCsvWithEndingLineBreak() throws Exception {
+ final CsvRecord record =
MyCsvRecord.class.getAnnotation(CsvRecord.class);
+ final MyCsvRecord csvRecord = new MyCsvRecord();
+ csvRecord.setAddressLine1("221b Baker Street");
+ csvRecord.setCity("London");
+ csvRecord.setCountry("England");
+ csvRecord.setAttention("1");
+
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedMessageCount(1);
+
mock.message(0).body().convertToString().endsWith(ConverterUtils.getStringCarriageReturn(record.crlf()));
+
+ template.sendBody("direct:withlb", csvRecord);
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Test
+ public void testCsvWithoutEndingLineBreak() throws Exception {
+ final CsvRecord record =
MyCsvRecord2.class.getAnnotation(CsvRecord.class);
+ final MyCsvRecord2 csvRecord2 = new MyCsvRecord2();
+ csvRecord2.setAddressLine1("221b Baker Street");
+ csvRecord2.setCity("London");
+ csvRecord2.setCountry("England");
+ csvRecord2.setAttention("1");
+
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedMessageCount(1);
+ mock.message(0).body().convertToString().endsWith(record.separator());
+
+ template.sendBody("direct:withoutlb", csvRecord2);
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:withoutlb")
+ .marshal().bindy(BindyType.Csv, MyCsvRecord2.class)
+ .to("log:after.unmarshal")
+ .to("mock:result");
+
+ from("direct:withlb")
+ .marshal().bindy(BindyType.Csv, MyCsvRecord.class)
+ .to("log:after.marshal")
+ .to("mock:result");
+ }
+ };
+ }
+}
diff --git
a/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/csv/MyCsvRecord2.java
b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/csv/MyCsvRecord2.java
new file mode 100644
index 00000000000..8b09765fde1
--- /dev/null
+++
b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/csv/MyCsvRecord2.java
@@ -0,0 +1,130 @@
+/**
+ * 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.model.csv;
+
+import java.io.Serializable;
+
+import org.apache.camel.dataformat.bindy.annotation.CsvRecord;
+import org.apache.camel.dataformat.bindy.annotation.DataField;
+
+@CsvRecord(separator = ",", skipFirstLine = false, endWithLineBreak = false)
+public class MyCsvRecord2 implements Serializable {
+
+ private static final long serialVersionUID = 1L;
+
+ @DataField(pos = 1)
+ private String attention;
+ @DataField(pos = 2)
+ private String addressLine1;
+ @DataField(pos = 3)
+ private String addressLine2;
+ @DataField(pos = 4)
+ private String city;
+ @DataField(pos = 5)
+ private String state;
+ @DataField(pos = 6)
+ private String zip;
+ @DataField(pos = 7)
+ private String country;
+ @DataField(pos = 8)
+ private String dummy1;
+ @DataField(pos = 9)
+ private String dummy2;
+
+ public MyCsvRecord2() {
+ }
+
+ public String getAttention() {
+ return attention;
+ }
+
+ public void setAttention(String attention) {
+ this.attention = attention;
+ }
+
+ public String getAddressLine1() {
+ return addressLine1;
+ }
+
+ public void setAddressLine1(String addressLine1) {
+ this.addressLine1 = addressLine1;
+ }
+
+ public String getAddressLine2() {
+ return addressLine2;
+ }
+
+ public void setAddressLine2(String addressLine2) {
+ this.addressLine2 = addressLine2;
+ }
+
+ public String getCity() {
+ return city;
+ }
+
+ public void setCity(String city) {
+ this.city = city;
+ }
+
+ public String getState() {
+ return state;
+ }
+
+ public void setState(String state) {
+ this.state = state;
+ }
+
+ public String getZip() {
+ return zip;
+ }
+
+ public void setZip(String zip) {
+ this.zip = zip;
+ }
+
+ public String getCountry() {
+ return country;
+ }
+
+ public void setCountry(String country) {
+ this.country = country;
+ }
+
+ public String getDummy1() {
+ return dummy1;
+ }
+
+ public void setDummy1(String dummy1) {
+ this.dummy1 = dummy1;
+ }
+
+ public String getDummy2() {
+ return dummy2;
+ }
+
+ public void setDummy2(String dummy2) {
+ this.dummy2 = dummy2;
+ }
+
+ @Override
+ public String toString() {
+ return "Record [attention=" + attention + ", addressLine1=" +
addressLine1 + ", addressLine2="
+ + addressLine2 + ", city=" + city + ", state=" + state + ",
zip=" + zip + ", country="
+ + country + ", dummy1=" + dummy1 + ", dummy2=" + dummy2 + "]";
+ }
+
+}
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> Allow creating csv content without last eol
> -------------------------------------------
>
> Key: CAMEL-12080
> URL: https://issues.apache.org/jira/browse/CAMEL-12080
> Project: Camel
> Issue Type: Improvement
> Components: camel-bindy
> Reporter: Marcin Domanski
> Priority: Minor
> Fix For: Future
>
>
> Hello,
> rfc4180 for CSV states:
> bq. The last record in the file may or may not have an ending line break.
> For deserialization Bindy accepts both types of files, but for serialization
> Bindy to my knowledge always creates content with last row ending with
> newline. Unfortunately some legacy systems treat last newline as one more
> empty row. Would it be possible to introduce control over this? I have this
> problem in CSV and also in fixed format for one other place. For now we need
> to strip the last char in Camel processor.
--
This message was sent by Atlassian JIRA
(v6.4.14#64029)