You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2014/11/19 07:48:08 UTC
[3/5] camel git commit: CAMEL-8060 Add data formats based on
uniVocity-parsers
CAMEL-8060 Add data formats based on uniVocity-parsers
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/d308620f
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/d308620f
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/d308620f
Branch: refs/heads/master
Commit: d308620f47bd8a0cd13ca44594da3d8faa5cb96e
Parents: 1e974d0
Author: Antoine DESSAIGNE <an...@gmail.com>
Authored: Tue Nov 18 13:38:13 2014 +0100
Committer: Willem Jiang <wi...@gmail.com>
Committed: Wed Nov 19 09:58:24 2014 +0800
----------------------------------------------------------------------
.../apache/camel/model/MarshalDefinition.java | 6 +
.../apache/camel/model/UnmarshalDefinition.java | 6 +
.../model/dataformat/DataFormatsDefinition.java | 3 +
.../dataformat/UniVocityAbstractDataFormat.java | 259 ++++++++
.../dataformat/UniVocityCsvDataFormat.java | 96 +++
.../UniVocityFixedWidthDataFormat.java | 94 +++
.../camel/model/dataformat/UniVocityHeader.java | 52 ++
.../dataformat/UniVocityTsvDataFormat.java | 57 ++
.../apache/camel/model/dataformat/jaxb.index | 4 +
components/camel-univocity-parsers/pom.xml | 82 +++
.../univocity/AbstractUniVocityDataFormat.java | 612 +++++++++++++++++++
.../univocity/HeaderRowProcessor.java | 69 +++
.../camel/dataformat/univocity/Marshaller.java | 101 +++
.../univocity/UniVocityCsvDataFormat.java | 209 +++++++
.../UniVocityFixedWidthDataFormat.java | 218 +++++++
.../univocity/UniVocityTsvDataFormat.java | 101 +++
.../dataformat/univocity/Unmarshaller.java | 198 ++++++
.../src/main/resources/META-INF/LICENSE.txt | 203 ++++++
.../src/main/resources/META-INF/NOTICE.txt | 11 +
.../org/apache/camel/dataformat/univocity-csv | 18 +
.../org/apache/camel/dataformat/univocity-fixed | 18 +
.../org/apache/camel/dataformat/univocity-tsv | 18 +
...UniVocityCsvDataFormatMarshalSpringTest.java | 125 ++++
.../UniVocityCsvDataFormatMarshalTest.java | 153 +++++
.../univocity/UniVocityCsvDataFormatTest.java | 202 ++++++
...iVocityCsvDataFormatUnmarshalSpringTest.java | 152 +++++
.../UniVocityCsvDataFormatUnmarshalTest.java | 177 ++++++
...tyFixedWidthDataFormatMarshalSpringTest.java | 125 ++++
...niVocityFixedWidthDataFormatMarshalTest.java | 155 +++++
.../UniVocityFixedWidthDataFormatTest.java | 260 ++++++++
...FixedWidthDataFormatUnmarshalSpringTest.java | 152 +++++
...VocityFixedWidthDataFormatUnmarshalTest.java | 196 ++++++
.../univocity/UniVocityTestHelper.java | 63 ++
...UniVocityTsvDataFormatMarshalSpringTest.java | 125 ++++
.../UniVocityTsvDataFormatMarshalTest.java | 150 +++++
.../univocity/UniVocityTsvDataFormatTest.java | 172 ++++++
...iVocityTsvDataFormatUnmarshalSpringTest.java | 152 +++++
.../UniVocityTsvDataFormatUnmarshalTest.java | 191 ++++++
.../UniVocityCsvDataFormatMarshalSpringTest.xml | 58 ++
...niVocityCsvDataFormatUnmarshalSpringTest.xml | 74 +++
...ityFixedWidthDataFormatMarshalSpringTest.xml | 64 ++
...yFixedWidthDataFormatUnmarshalSpringTest.xml | 88 +++
.../UniVocityTsvDataFormatMarshalSpringTest.xml | 57 ++
...niVocityTsvDataFormatUnmarshalSpringTest.xml | 74 +++
components/pom.xml | 1 +
parent/pom.xml | 1 +
.../features/src/main/resources/features.xml | 5 +
47 files changed, 5407 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/d308620f/camel-core/src/main/java/org/apache/camel/model/MarshalDefinition.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/MarshalDefinition.java b/camel-core/src/main/java/org/apache/camel/model/MarshalDefinition.java
index a4b18a9..e6bc9b3 100644
--- a/camel-core/src/main/java/org/apache/camel/model/MarshalDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/MarshalDefinition.java
@@ -46,6 +46,9 @@ import org.apache.camel.model.dataformat.SoapJaxbDataFormat;
import org.apache.camel.model.dataformat.StringDataFormat;
import org.apache.camel.model.dataformat.SyslogDataFormat;
import org.apache.camel.model.dataformat.TidyMarkupDataFormat;
+import org.apache.camel.model.dataformat.UniVocityCsvDataFormat;
+import org.apache.camel.model.dataformat.UniVocityFixedWidthDataFormat;
+import org.apache.camel.model.dataformat.UniVocityTsvDataFormat;
import org.apache.camel.model.dataformat.XMLBeansDataFormat;
import org.apache.camel.model.dataformat.XMLSecurityDataFormat;
import org.apache.camel.model.dataformat.XStreamDataFormat;
@@ -95,6 +98,9 @@ public class MarshalDefinition extends NoOutputDefinition<MarshalDefinition> {
@XmlElement(required = false, name = "string", type = StringDataFormat.class),
@XmlElement(required = false, name = "syslog", type = SyslogDataFormat.class),
@XmlElement(required = false, name = "tidyMarkup", type = TidyMarkupDataFormat.class),
+ @XmlElement(required = false, name = "univocity-csv", type = UniVocityCsvDataFormat.class),
+ @XmlElement(required = false, name = "univocity-fixed", type = UniVocityFixedWidthDataFormat.class),
+ @XmlElement(required = false, name = "univocity-tsv", type = UniVocityTsvDataFormat.class),
@XmlElement(required = false, name = "xmlBeans", type = XMLBeansDataFormat.class),
@XmlElement(required = false, name = "xmljson", type = XmlJsonDataFormat.class),
@XmlElement(required = false, name = "xmlrpc", type = XmlRpcDataFormat.class),
http://git-wip-us.apache.org/repos/asf/camel/blob/d308620f/camel-core/src/main/java/org/apache/camel/model/UnmarshalDefinition.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/UnmarshalDefinition.java b/camel-core/src/main/java/org/apache/camel/model/UnmarshalDefinition.java
index 7367fcd..0e694ba 100644
--- a/camel-core/src/main/java/org/apache/camel/model/UnmarshalDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/UnmarshalDefinition.java
@@ -46,6 +46,9 @@ import org.apache.camel.model.dataformat.SoapJaxbDataFormat;
import org.apache.camel.model.dataformat.StringDataFormat;
import org.apache.camel.model.dataformat.SyslogDataFormat;
import org.apache.camel.model.dataformat.TidyMarkupDataFormat;
+import org.apache.camel.model.dataformat.UniVocityCsvDataFormat;
+import org.apache.camel.model.dataformat.UniVocityFixedWidthDataFormat;
+import org.apache.camel.model.dataformat.UniVocityTsvDataFormat;
import org.apache.camel.model.dataformat.XMLBeansDataFormat;
import org.apache.camel.model.dataformat.XMLSecurityDataFormat;
import org.apache.camel.model.dataformat.XStreamDataFormat;
@@ -95,6 +98,9 @@ public class UnmarshalDefinition extends NoOutputDefinition<UnmarshalDefinition>
@XmlElement(required = false, name = "string", type = StringDataFormat.class),
@XmlElement(required = false, name = "syslog", type = SyslogDataFormat.class),
@XmlElement(required = false, name = "tidyMarkup", type = TidyMarkupDataFormat.class),
+ @XmlElement(required = false, name = "univocity-csv", type = UniVocityCsvDataFormat.class),
+ @XmlElement(required = false, name = "univocity-fixed", type = UniVocityFixedWidthDataFormat.class),
+ @XmlElement(required = false, name = "univocity-tsv", type = UniVocityTsvDataFormat.class),
@XmlElement(required = false, name = "xmlBeans", type = XMLBeansDataFormat.class),
@XmlElement(required = false, name = "xmljson", type = XmlJsonDataFormat.class),
@XmlElement(required = false, name = "xmlrpc", type = XmlRpcDataFormat.class),
http://git-wip-us.apache.org/repos/asf/camel/blob/d308620f/camel-core/src/main/java/org/apache/camel/model/dataformat/DataFormatsDefinition.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/dataformat/DataFormatsDefinition.java b/camel-core/src/main/java/org/apache/camel/model/dataformat/DataFormatsDefinition.java
index 3b09d76..1cd207d 100644
--- a/camel-core/src/main/java/org/apache/camel/model/dataformat/DataFormatsDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/dataformat/DataFormatsDefinition.java
@@ -60,6 +60,9 @@ public class DataFormatsDefinition {
@XmlElement(required = false, name = "string", type = StringDataFormat.class),
@XmlElement(required = false, name = "syslog", type = SyslogDataFormat.class),
@XmlElement(required = false, name = "tidyMarkup", type = TidyMarkupDataFormat.class),
+ @XmlElement(required = false, name = "univocity-csv", type = UniVocityCsvDataFormat.class),
+ @XmlElement(required = false, name = "univocity-fixed", type = UniVocityFixedWidthDataFormat.class),
+ @XmlElement(required = false, name = "univocity-tsv", type = UniVocityTsvDataFormat.class),
@XmlElement(required = false, name = "xmlBeans", type = XMLBeansDataFormat.class),
@XmlElement(required = false, name = "xmljson", type = XmlJsonDataFormat.class),
@XmlElement(required = false, name = "xmlrpc", type = XmlRpcDataFormat.class),
http://git-wip-us.apache.org/repos/asf/camel/blob/d308620f/camel-core/src/main/java/org/apache/camel/model/dataformat/UniVocityAbstractDataFormat.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/dataformat/UniVocityAbstractDataFormat.java b/camel-core/src/main/java/org/apache/camel/model/dataformat/UniVocityAbstractDataFormat.java
new file mode 100644
index 0000000..58c881c
--- /dev/null
+++ b/camel-core/src/main/java/org/apache/camel/model/dataformat/UniVocityAbstractDataFormat.java
@@ -0,0 +1,259 @@
+/**
+ * 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.model.dataformat;
+
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlAttribute;
+import javax.xml.bind.annotation.XmlElementRef;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.model.DataFormatDefinition;
+import org.apache.camel.spi.DataFormat;
+
+import static javax.xml.bind.annotation.XmlAccessType.FIELD;
+
+/**
+ * Represents the common parts of all uniVocity {@link org.apache.camel.spi.DataFormat} parsers.
+ */
+@XmlAccessorType(FIELD)
+public abstract class UniVocityAbstractDataFormat extends DataFormatDefinition {
+ @SuppressWarnings("unused")
+ protected UniVocityAbstractDataFormat() {
+ // This constructor is needed by jaxb for schema generation
+ }
+
+ protected UniVocityAbstractDataFormat(String dataFormatName) {
+ super(dataFormatName);
+ }
+
+ @XmlAttribute
+ protected String nullValue;
+ @XmlAttribute
+ protected Boolean skipEmptyLines;
+ @XmlAttribute
+ protected Boolean ignoreTrailingWhitespaces;
+ @XmlAttribute
+ protected Boolean ignoreLeadingWhitespaces;
+ @XmlAttribute
+ protected Boolean headersDisabled;
+ @XmlElementRef
+ protected List<UniVocityHeader> headers;
+ @XmlAttribute
+ protected Boolean headerExtractionEnabled;
+ @XmlAttribute
+ protected Integer numberOfRecordsToRead;
+ @XmlAttribute
+ protected String emptyValue;
+ @XmlAttribute
+ protected String lineSeparator;
+ @XmlAttribute
+ protected String normalizedLineSeparator;
+ @XmlAttribute
+ protected String comment;
+ @XmlAttribute
+ protected Boolean lazyLoad;
+ @XmlAttribute
+ protected Boolean asMap;
+
+ public String getNullValue() {
+ return nullValue;
+ }
+
+ public void setNullValue(String nullValue) {
+ this.nullValue = nullValue;
+ }
+
+ public Boolean getSkipEmptyLines() {
+ return skipEmptyLines;
+ }
+
+ public void setSkipEmptyLines(Boolean skipEmptyLines) {
+ this.skipEmptyLines = skipEmptyLines;
+ }
+
+ public Boolean getIgnoreTrailingWhitespaces() {
+ return ignoreTrailingWhitespaces;
+ }
+
+ public void setIgnoreTrailingWhitespaces(Boolean ignoreTrailingWhitespaces) {
+ this.ignoreTrailingWhitespaces = ignoreTrailingWhitespaces;
+ }
+
+ public Boolean getIgnoreLeadingWhitespaces() {
+ return ignoreLeadingWhitespaces;
+ }
+
+ public void setIgnoreLeadingWhitespaces(Boolean ignoreLeadingWhitespaces) {
+ this.ignoreLeadingWhitespaces = ignoreLeadingWhitespaces;
+ }
+
+ public Boolean getHeadersDisabled() {
+ return headersDisabled;
+ }
+
+ public void setHeadersDisabled(Boolean headersDisabled) {
+ this.headersDisabled = headersDisabled;
+ }
+
+ public List<UniVocityHeader> getHeaders() {
+ return headers;
+ }
+
+ public void setHeaders(List<UniVocityHeader> headers) {
+ this.headers = headers;
+ }
+
+ public Boolean getHeaderExtractionEnabled() {
+ return headerExtractionEnabled;
+ }
+
+ public void setHeaderExtractionEnabled(Boolean headerExtractionEnabled) {
+ this.headerExtractionEnabled = headerExtractionEnabled;
+ }
+
+ public Integer getNumberOfRecordsToRead() {
+ return numberOfRecordsToRead;
+ }
+
+ public void setNumberOfRecordsToRead(Integer numberOfRecordsToRead) {
+ this.numberOfRecordsToRead = numberOfRecordsToRead;
+ }
+
+ public String getEmptyValue() {
+ return emptyValue;
+ }
+
+ public void setEmptyValue(String emptyValue) {
+ this.emptyValue = emptyValue;
+ }
+
+ public String getLineSeparator() {
+ return lineSeparator;
+ }
+
+ public void setLineSeparator(String lineSeparator) {
+ this.lineSeparator = lineSeparator;
+ }
+
+ public String getNormalizedLineSeparator() {
+ return normalizedLineSeparator;
+ }
+
+ public void setNormalizedLineSeparator(String normalizedLineSeparator) {
+ this.normalizedLineSeparator = normalizedLineSeparator;
+ }
+
+ public String getComment() {
+ return comment;
+ }
+
+ public void setComment(String comment) {
+ this.comment = comment;
+ }
+
+ public Boolean getLazyLoad() {
+ return lazyLoad;
+ }
+
+ public void setLazyLoad(Boolean lazyLoad) {
+ this.lazyLoad = lazyLoad;
+ }
+
+ public Boolean getAsMap() {
+ return asMap;
+ }
+
+ public void setAsMap(Boolean asMap) {
+ this.asMap = asMap;
+ }
+
+ @Override
+ protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) {
+ super.configureDataFormat(dataFormat, camelContext);
+
+ if (nullValue != null) {
+ setProperty(camelContext, dataFormat, "nullValue", nullValue);
+ }
+ if (skipEmptyLines != null) {
+ setProperty(camelContext, dataFormat, "skipEmptyLines", skipEmptyLines);
+ }
+ if (ignoreTrailingWhitespaces != null) {
+ setProperty(camelContext, dataFormat, "ignoreTrailingWhitespaces", ignoreTrailingWhitespaces);
+ }
+ if (ignoreLeadingWhitespaces != null) {
+ setProperty(camelContext, dataFormat, "ignoreLeadingWhitespaces", ignoreLeadingWhitespaces);
+ }
+ if (headersDisabled != null) {
+ setProperty(camelContext, dataFormat, "headersDisabled", headersDisabled);
+ }
+ String[] validHeaderNames = getValidHeaderNames();
+ if (validHeaderNames != null) {
+ setProperty(camelContext, dataFormat, "headers", validHeaderNames);
+ }
+ if (headerExtractionEnabled != null) {
+ setProperty(camelContext, dataFormat, "headerExtractionEnabled", headerExtractionEnabled);
+ }
+ if (numberOfRecordsToRead != null) {
+ setProperty(camelContext, dataFormat, "numberOfRecordsToRead", numberOfRecordsToRead);
+ }
+ if (emptyValue != null) {
+ setProperty(camelContext, dataFormat, "emptyValue", emptyValue);
+ }
+ if (lineSeparator != null) {
+ setProperty(camelContext, dataFormat, "lineSeparator", lineSeparator);
+ }
+ if (normalizedLineSeparator != null) {
+ setProperty(camelContext, dataFormat, "normalizedLineSeparator", singleCharOf("normalizedLineSeparator", normalizedLineSeparator));
+ }
+ if (comment != null) {
+ setProperty(camelContext, dataFormat, "comment", singleCharOf("comment", comment));
+ }
+ if (lazyLoad != null) {
+ setProperty(camelContext, dataFormat, "lazyLoad", lazyLoad);
+ }
+ if (asMap != null) {
+ setProperty(camelContext, dataFormat, "asMap", asMap);
+ }
+ }
+
+ protected static Character singleCharOf(String attributeName, String string) {
+ if (string.length() != 1) {
+ throw new IllegalArgumentException("Only one character must be defined for " + attributeName);
+ }
+ return string.charAt(0);
+ }
+
+ /**
+ * Gets only the headers with non-null and non-empty names. It returns {@code null} if there's no such headers.
+ *
+ * @return The headers with non-null and non-empty names
+ */
+ private String[] getValidHeaderNames() {
+ if (headers == null) {
+ return null;
+ }
+ List<String> names = new ArrayList<>(headers.size());
+ for (UniVocityHeader header : headers) {
+ if (header.getName() != null && !header.getName().isEmpty()) {
+ names.add(header.getName());
+ }
+ }
+ return names.isEmpty() ? null : names.toArray(new String[names.size()]);
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/d308620f/camel-core/src/main/java/org/apache/camel/model/dataformat/UniVocityCsvDataFormat.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/dataformat/UniVocityCsvDataFormat.java b/camel-core/src/main/java/org/apache/camel/model/dataformat/UniVocityCsvDataFormat.java
new file mode 100644
index 0000000..7a1be7f
--- /dev/null
+++ b/camel-core/src/main/java/org/apache/camel/model/dataformat/UniVocityCsvDataFormat.java
@@ -0,0 +1,96 @@
+/**
+ * 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.model.dataformat;
+
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlAttribute;
+import javax.xml.bind.annotation.XmlRootElement;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.spi.DataFormat;
+
+import static javax.xml.bind.annotation.XmlAccessType.FIELD;
+
+/**
+ * Represents the uniVocity CSV {@link org.apache.camel.spi.DataFormat}
+ */
+@XmlRootElement(name = "univocity-csv")
+@XmlAccessorType(FIELD)
+public class UniVocityCsvDataFormat extends UniVocityAbstractDataFormat {
+ @XmlAttribute
+ protected Boolean quoteAllFields;
+ @XmlAttribute
+ protected String quote;
+ @XmlAttribute
+ protected String quoteEscape;
+ @XmlAttribute
+ protected String delimiter;
+
+ public UniVocityCsvDataFormat() {
+ super("univocity-csv");
+ }
+
+ public Boolean getQuoteAllFields() {
+ return quoteAllFields;
+ }
+
+ public void setQuoteAllFields(Boolean quoteAllFields) {
+ this.quoteAllFields = quoteAllFields;
+ }
+
+ public String getQuote() {
+ return quote;
+ }
+
+ public void setQuote(String quote) {
+ this.quote = quote;
+ }
+
+ public String getQuoteEscape() {
+ return quoteEscape;
+ }
+
+ public void setQuoteEscape(String quoteEscape) {
+ this.quoteEscape = quoteEscape;
+ }
+
+ public String getDelimiter() {
+ return delimiter;
+ }
+
+ public void setDelimiter(String delimiter) {
+ this.delimiter = delimiter;
+ }
+
+ @Override
+ protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) {
+ super.configureDataFormat(dataFormat, camelContext);
+
+ if (quoteAllFields != null) {
+ setProperty(camelContext, dataFormat, "quoteAllFields", quoteAllFields);
+ }
+ if (quote != null) {
+ setProperty(camelContext, dataFormat, "quote", singleCharOf("quote", quote));
+ }
+ if (quoteEscape != null) {
+ setProperty(camelContext, dataFormat, "quoteEscape", singleCharOf("quoteEscape", quoteEscape));
+ }
+ if (delimiter != null) {
+ setProperty(camelContext, dataFormat, "delimiter", singleCharOf("delimiter", delimiter));
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/d308620f/camel-core/src/main/java/org/apache/camel/model/dataformat/UniVocityFixedWidthDataFormat.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/dataformat/UniVocityFixedWidthDataFormat.java b/camel-core/src/main/java/org/apache/camel/model/dataformat/UniVocityFixedWidthDataFormat.java
new file mode 100644
index 0000000..93a2a11
--- /dev/null
+++ b/camel-core/src/main/java/org/apache/camel/model/dataformat/UniVocityFixedWidthDataFormat.java
@@ -0,0 +1,94 @@
+/**
+ * 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.model.dataformat;
+
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlAttribute;
+import javax.xml.bind.annotation.XmlRootElement;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.spi.DataFormat;
+
+import static javax.xml.bind.annotation.XmlAccessType.FIELD;
+
+/**
+ * Represents the uniVocity fixed-width {@link org.apache.camel.spi.DataFormat}
+ */
+@XmlRootElement(name = "univocity-fixed")
+@XmlAccessorType(FIELD)
+public class UniVocityFixedWidthDataFormat extends UniVocityAbstractDataFormat {
+ @XmlAttribute
+ protected Boolean skipTrailingCharsUntilNewline;
+ @XmlAttribute
+ protected Boolean recordEndsOnNewline;
+ @XmlAttribute
+ protected String padding;
+
+ public UniVocityFixedWidthDataFormat() {
+ super("univocity-fixed");
+ }
+
+ public Boolean getSkipTrailingCharsUntilNewline() {
+ return skipTrailingCharsUntilNewline;
+ }
+
+ public void setSkipTrailingCharsUntilNewline(Boolean skipTrailingCharsUntilNewline) {
+ this.skipTrailingCharsUntilNewline = skipTrailingCharsUntilNewline;
+ }
+
+ public Boolean getRecordEndsOnNewline() {
+ return recordEndsOnNewline;
+ }
+
+ public void setRecordEndsOnNewline(Boolean recordEndsOnNewline) {
+ this.recordEndsOnNewline = recordEndsOnNewline;
+ }
+
+ public String getPadding() {
+ return padding;
+ }
+
+ public void setPadding(String padding) {
+ this.padding = padding;
+ }
+
+ @Override
+ protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) {
+ super.configureDataFormat(dataFormat, camelContext);
+
+ if (headers != null) {
+ int[] lengths = new int[headers.size()];
+ for (int i = 0; i < lengths.length; i++) {
+ Integer length = headers.get(i).getLength();
+ if (length == null) {
+ throw new IllegalArgumentException("The length of all headers must be defined.");
+ }
+ lengths[i] = length;
+ }
+ setProperty(camelContext, dataFormat, "fieldLengths", lengths);
+ }
+ if (skipTrailingCharsUntilNewline != null) {
+ setProperty(camelContext, dataFormat, "skipTrailingCharsUntilNewline", skipTrailingCharsUntilNewline);
+ }
+ if (recordEndsOnNewline != null) {
+ setProperty(camelContext, dataFormat, "recordEndsOnNewline", recordEndsOnNewline);
+ }
+ if (padding != null) {
+ setProperty(camelContext, dataFormat, "padding", singleCharOf("padding", padding));
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/d308620f/camel-core/src/main/java/org/apache/camel/model/dataformat/UniVocityHeader.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/dataformat/UniVocityHeader.java b/camel-core/src/main/java/org/apache/camel/model/dataformat/UniVocityHeader.java
new file mode 100644
index 0000000..9a4135a
--- /dev/null
+++ b/camel-core/src/main/java/org/apache/camel/model/dataformat/UniVocityHeader.java
@@ -0,0 +1,52 @@
+/**
+ * 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.model.dataformat;
+
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlAttribute;
+import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlValue;
+
+import static javax.xml.bind.annotation.XmlAccessType.FIELD;
+
+/**
+ * Represents a header for the uniVocity data formats.
+ */
+@XmlRootElement(name = "univocity-header")
+@XmlAccessorType(FIELD)
+public class UniVocityHeader {
+ @XmlValue
+ private String name;
+ @XmlAttribute
+ private Integer length;
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public Integer getLength() {
+ return length;
+ }
+
+ public void setLength(Integer length) {
+ this.length = length;
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/d308620f/camel-core/src/main/java/org/apache/camel/model/dataformat/UniVocityTsvDataFormat.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/dataformat/UniVocityTsvDataFormat.java b/camel-core/src/main/java/org/apache/camel/model/dataformat/UniVocityTsvDataFormat.java
new file mode 100644
index 0000000..069a00c
--- /dev/null
+++ b/camel-core/src/main/java/org/apache/camel/model/dataformat/UniVocityTsvDataFormat.java
@@ -0,0 +1,57 @@
+/**
+ * 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.model.dataformat;
+
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlAttribute;
+import javax.xml.bind.annotation.XmlRootElement;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.spi.DataFormat;
+
+import static javax.xml.bind.annotation.XmlAccessType.FIELD;
+
+/**
+ * Represents the uniVocity TSV {@link org.apache.camel.spi.DataFormat}
+ */
+@XmlRootElement(name = "univocity-tsv")
+@XmlAccessorType(FIELD)
+public class UniVocityTsvDataFormat extends UniVocityAbstractDataFormat {
+ @XmlAttribute
+ protected String escapeChar;
+
+ public UniVocityTsvDataFormat() {
+ super("univocity-tsv");
+ }
+
+ public String getEscapeChar() {
+ return escapeChar;
+ }
+
+ public void setEscapeChar(String escapeChar) {
+ this.escapeChar = escapeChar;
+ }
+
+ @Override
+ protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) {
+ super.configureDataFormat(dataFormat, camelContext);
+
+ if (escapeChar != null) {
+ setProperty(camelContext, dataFormat, "escapeChar", singleCharOf("escapeChar", escapeChar));
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/d308620f/camel-core/src/main/resources/org/apache/camel/model/dataformat/jaxb.index
----------------------------------------------------------------------
diff --git a/camel-core/src/main/resources/org/apache/camel/model/dataformat/jaxb.index b/camel-core/src/main/resources/org/apache/camel/model/dataformat/jaxb.index
index d1412d7..279ed22 100644
--- a/camel-core/src/main/resources/org/apache/camel/model/dataformat/jaxb.index
+++ b/camel-core/src/main/resources/org/apache/camel/model/dataformat/jaxb.index
@@ -38,6 +38,10 @@ SoapJaxbDataFormat
StringDataFormat
SyslogDataFormat
TidyMarkupDataFormat
+UniVocityCsvDataFormat
+UniVocityFixedWidthDataFormat
+UniVocityHeader
+UniVocityTsvDataFormat
XMLBeansDataFormat
XMLSecurityDataFormat
XStreamDataFormat
http://git-wip-us.apache.org/repos/asf/camel/blob/d308620f/components/camel-univocity-parsers/pom.xml
----------------------------------------------------------------------
diff --git a/components/camel-univocity-parsers/pom.xml b/components/camel-univocity-parsers/pom.xml
new file mode 100644
index 0000000..6cbe258
--- /dev/null
+++ b/components/camel-univocity-parsers/pom.xml
@@ -0,0 +1,82 @@
+<?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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>components</artifactId>
+ <version>2.15-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>camel-univocity-parsers</artifactId>
+ <packaging>bundle</packaging>
+ <name>Camel :: UniVocity Parsers</name>
+ <description>Camel UniVocity parsers data format support</description>
+
+ <properties>
+ <camel.osgi.export.pkg>org.apache.camel.dataformat.univocity.*</camel.osgi.export.pkg>
+ <camel.osgi.export.service>
+ org.apache.camel.spi.DataFormatResolver;dataformat=univocity-csv,
+ org.apache.camel.spi.DataFormatResolver;dataformat=univocity-fixed,
+ org.apache.camel.spi.DataFormatResolver;dataformat=univocity-tsv
+ </camel.osgi.export.service>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-core</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>com.univocity</groupId>
+ <artifactId>univocity-parsers</artifactId>
+ <version>${univocity-parsers-version}</version>
+ <type>jar</type>
+ </dependency>
+
+ <!-- testing -->
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-core-xml</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-spring</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-test-spring</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-log4j12</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+</project>
http://git-wip-us.apache.org/repos/asf/camel/blob/d308620f/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/AbstractUniVocityDataFormat.java
----------------------------------------------------------------------
diff --git a/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/AbstractUniVocityDataFormat.java b/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/AbstractUniVocityDataFormat.java
new file mode 100644
index 0000000..2a3923a
--- /dev/null
+++ b/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/AbstractUniVocityDataFormat.java
@@ -0,0 +1,612 @@
+/**
+ * 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.univocity;
+
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Reader;
+import java.io.Writer;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.spi.DataFormat;
+
+import com.univocity.parsers.common.AbstractParser;
+import com.univocity.parsers.common.AbstractWriter;
+import com.univocity.parsers.common.CommonParserSettings;
+import com.univocity.parsers.common.CommonSettings;
+import com.univocity.parsers.common.CommonWriterSettings;
+import com.univocity.parsers.common.Format;
+
+import static org.apache.camel.util.IOHelper.getCharsetName;
+
+/**
+ * This abstract class contains all the common parts for all the uniVocity parsers.
+ * <p/>
+ * All setters methods <b>must</b> call the {@link #reset()} method in order to clear the cache for marshalling and
+ * unmarshalling.
+ *
+ * @param <F> uniVocity format class
+ * @param <CWS> uniVocity writer settings class
+ * @param <W> uniVocity writer class
+ * @param <CPS> uniVocity parser settings class
+ * @param <P> uniVocity parser class
+ * @param <DF> the data format class (for providing a fluent API)
+ */
+public abstract class AbstractUniVocityDataFormat<F extends Format, CWS extends CommonWriterSettings<F>, W extends AbstractWriter<CWS>, CPS extends CommonParserSettings<F>, P extends AbstractParser<CPS>, DF extends AbstractUniVocityDataFormat<F, CWS, W, CPS, P, DF>> implements DataFormat {
+ protected String nullValue;
+ protected Boolean skipEmptyLines;
+ protected Boolean ignoreTrailingWhitespaces;
+ protected Boolean ignoreLeadingWhitespaces;
+ protected boolean headersDisabled;
+ protected String[] headers;
+ protected Boolean headerExtractionEnabled;
+ protected Integer numberOfRecordsToRead;
+ protected String emptyValue;
+ protected String lineSeparator;
+ protected Character normalizedLineSeparator;
+ protected Character comment;
+ protected boolean lazyLoad;
+ protected boolean asMap;
+
+ private volatile CWS writerSettings;
+ private final Object writerSettingsToken = new Object();
+ private volatile Marshaller<W> marshaller;
+
+ // We're using a ThreadLocal for the parser settings because in order to retrieve the headers we need to change the
+ // settings each time we're parsing...
+ private volatile ThreadLocal<CPS> parserSettings;
+ private final Object parserSettingsToken = new Object();
+ private volatile Unmarshaller<P> unmarshaller;
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void marshal(Exchange exchange, Object body, OutputStream stream) throws Exception {
+ if (writerSettings == null) {
+ synchronized (writerSettingsToken) {
+ if (writerSettings == null) {
+ writerSettings = createAndConfigureWriterSettings();
+ marshaller = new Marshaller<>(headers, headers == null);
+ }
+ }
+ }
+
+ try (Writer writer = new OutputStreamWriter(stream, getCharsetName(exchange))) {
+ marshaller.marshal(exchange, body, createWriter(writer, writerSettings));
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Object unmarshal(Exchange exchange, InputStream stream) throws Exception {
+ if (parserSettings == null) {
+ synchronized (parserSettingsToken) {
+ if (parserSettings == null) {
+ parserSettings = new ThreadLocal<CPS>() {
+ @Override
+ protected CPS initialValue() {
+ return createAndConfigureParserSettings();
+ }
+ };
+ unmarshaller = new Unmarshaller<>(lazyLoad, asMap);
+ }
+ }
+ }
+
+ try (Reader reader = new InputStreamReader(stream, getCharsetName(exchange))) {
+ HeaderRowProcessor headerRowProcessor = new HeaderRowProcessor();
+ CPS settings = parserSettings.get();
+ settings.setRowProcessor(headerRowProcessor);
+ return unmarshaller.unmarshal(reader, createParser(settings), headerRowProcessor);
+ }
+ }
+
+ /**
+ * Gets the String representation of a null value.
+ * If {@code null} then the default settings value is used.
+ *
+ * @return the String representation of a null value
+ * @see com.univocity.parsers.common.CommonSettings#getNullValue()
+ */
+ public String getNullValue() {
+ return nullValue;
+ }
+
+ /**
+ * Sets the String representation of a null value.
+ * If {@code null} then the default settings value is used.
+ *
+ * @param nullValue the String representation of a null value
+ * @return current data format instance, fluent API
+ * @see com.univocity.parsers.common.CommonSettings#setNullValue(String)
+ */
+ public DF setNullValue(String nullValue) {
+ this.nullValue = nullValue;
+ reset();
+ return self();
+ }
+
+ /**
+ * Gets whether or not empty lines should be ignored.
+ * If {@code null} then the default settings value is used.
+ *
+ * @return whether or not empty lines should be ignored
+ * @see com.univocity.parsers.common.CommonSettings#getSkipEmptyLines()
+ */
+ public Boolean getSkipEmptyLines() {
+ return skipEmptyLines;
+ }
+
+ /**
+ * Sets whether or not empty lines should be ignored.
+ * If {@code null} then the default settings value is used.
+ *
+ * @param skipEmptyLines whether or not empty lines should be ignored
+ * @return current data format instance, fluent API
+ * @see com.univocity.parsers.common.CommonSettings#setSkipEmptyLines(boolean)
+ */
+ public DF setSkipEmptyLines(Boolean skipEmptyLines) {
+ this.skipEmptyLines = skipEmptyLines;
+ reset();
+ return self();
+ }
+
+ /**
+ * Gets whether or not trailing whitespaces should be ignored.
+ * If {@code null} then the default settings value is used.
+ *
+ * @return whether or not trailing whitespaces should be ignored
+ * @see com.univocity.parsers.common.CommonSettings#getIgnoreTrailingWhitespaces()
+ */
+ public Boolean getIgnoreTrailingWhitespaces() {
+ return ignoreTrailingWhitespaces;
+ }
+
+ /**
+ * Sets whether or not trailing whitespaces should be ignored.
+ * If {@code null} then the default settings value is used.
+ *
+ * @param ignoreTrailingWhitespaces whether or not trailing whitespaces should be ignored
+ * @return current data format instance, fluent API
+ * @see com.univocity.parsers.common.CommonSettings#setIgnoreTrailingWhitespaces(boolean)
+ */
+ public DF setIgnoreTrailingWhitespaces(Boolean ignoreTrailingWhitespaces) {
+ this.ignoreTrailingWhitespaces = ignoreTrailingWhitespaces;
+ reset();
+ return self();
+ }
+
+ /**
+ * Gets whether or not leading whitespaces should be ignored.
+ * If {@code null} then the default settings value is used.
+ *
+ * @return whether or not leading whitespaces should be ignored
+ * @see com.univocity.parsers.common.CommonSettings#getIgnoreLeadingWhitespaces()
+ */
+ public Boolean getIgnoreLeadingWhitespaces() {
+ return ignoreLeadingWhitespaces;
+ }
+
+ /**
+ * Sets whether or not leading whitespaces should be ignored.
+ * If {@code null} then the default settings value is used.
+ *
+ * @param ignoreLeadingWhitespaces whether or not leading whitespaces should be ignored
+ * @return current data format instance, fluent API
+ * @see com.univocity.parsers.common.CommonSettings#setIgnoreLeadingWhitespaces(boolean)
+ */
+ public DF setIgnoreLeadingWhitespaces(Boolean ignoreLeadingWhitespaces) {
+ this.ignoreLeadingWhitespaces = ignoreLeadingWhitespaces;
+ reset();
+ return self();
+ }
+
+ /**
+ * Gets whether or not headers are disabled.
+ * If {@code true} then it passes {@code null} to
+ * {@link com.univocity.parsers.common.CommonSettings#setHeaders(String...)} in order to disabled them.
+ *
+ * @return whether or not headers are disabled
+ * @see com.univocity.parsers.common.CommonSettings#getHeaders()
+ */
+ public boolean isHeadersDisabled() {
+ return headersDisabled;
+ }
+
+ /**
+ * Sets whether or not headers are disabled.
+ * If {@code true} then it passes {@code null} to
+ * {@link com.univocity.parsers.common.CommonSettings#setHeaders(String...)} in order to disabled them.
+ *
+ * @param headersDisabled whether or not headers are disabled
+ * @return current data format instance, fluent API
+ * @see com.univocity.parsers.common.CommonSettings#setHeaders(String...)
+ */
+ public DF setHeadersDisabled(boolean headersDisabled) {
+ this.headersDisabled = headersDisabled;
+ reset();
+ return self();
+ }
+
+ /**
+ * Gets the headers.
+ * If {@code null} then the default settings value is used.
+ *
+ * @return the headers
+ * @see com.univocity.parsers.common.CommonSettings#getHeaders()
+ */
+ public String[] getHeaders() {
+ return headers;
+ }
+
+ /**
+ * Sets the headers.
+ * If {@code null} then the default settings value is used.
+ *
+ * @param headers the headers
+ * @return current data format instance, fluent API
+ * @see com.univocity.parsers.common.CommonSettings#setHeaders(String...)
+ */
+ public DF setHeaders(String[] headers) {
+ this.headers = headers;
+ reset();
+ return self();
+ }
+
+ /**
+ * Gets whether or not the header extraction is enabled.
+ * If {@code null} then the default settings value is used.
+ *
+ * @return whether or not the header extraction is enabled
+ * @see com.univocity.parsers.common.CommonParserSettings#isHeaderExtractionEnabled()
+ */
+ public Boolean getHeaderExtractionEnabled() {
+ return headerExtractionEnabled;
+ }
+
+ /**
+ * Sets whether or not the header extraction is enabled.
+ * If {@code null} then the default settings value is used.
+ *
+ * @param headerExtractionEnabled whether or not the header extraction is enabled
+ * @return current data format instance, fluent API
+ * @see com.univocity.parsers.common.CommonParserSettings#setHeaderExtractionEnabled(boolean)
+ */
+ public DF setHeaderExtractionEnabled(Boolean headerExtractionEnabled) {
+ this.headerExtractionEnabled = headerExtractionEnabled;
+ reset();
+ return self();
+ }
+
+ /**
+ * Gets the number of records to read.
+ * If {@code null} then the default settings value is used.
+ *
+ * @return the number of records to read
+ * @see com.univocity.parsers.common.CommonParserSettings#getNumberOfRecordsToRead()
+ */
+ public Integer getNumberOfRecordsToRead() {
+ return numberOfRecordsToRead;
+ }
+
+ /**
+ * Sets the number of records to read.
+ * If {@code null} then the default settings value is used.
+ *
+ * @param numberOfRecordsToRead the number of records to read
+ * @return current data format instance, fluent API
+ * @see com.univocity.parsers.common.CommonParserSettings#setNumberOfRecordsToRead(int)
+ */
+ public DF setNumberOfRecordsToRead(Integer numberOfRecordsToRead) {
+ this.numberOfRecordsToRead = numberOfRecordsToRead;
+ reset();
+ return self();
+ }
+
+ /**
+ * Gets the String representation of an empty value.
+ * If {@code null} then the default settings value is used.
+ *
+ * @return the String representation of an empty value
+ * @see com.univocity.parsers.common.CommonWriterSettings#getEmptyValue()
+ */
+ public String getEmptyValue() {
+ return emptyValue;
+ }
+
+ /**
+ * Sets the String representation of an empty value.
+ * If {@code null} then the default settings value is used.
+ *
+ * @param emptyValue the String representation of an empty value
+ * @return current data format instance, fluent API
+ * @see com.univocity.parsers.common.CommonWriterSettings#setEmptyValue(String)
+ */
+ public DF setEmptyValue(String emptyValue) {
+ this.emptyValue = emptyValue;
+ reset();
+ return self();
+ }
+
+ /**
+ * Gets the line separator.
+ * If {@code null} then the default format value is used.
+ *
+ * @return the line separator
+ * @see com.univocity.parsers.common.Format#getLineSeparatorString()
+ */
+ public String getLineSeparator() {
+ return lineSeparator;
+ }
+
+ /**
+ * Sets the line separator.
+ * If {@code null} then the default format value is used.
+ *
+ * @param lineSeparator the line separator
+ * @return current data format instance, fluent API
+ * @see Format#setLineSeparator(String)
+ */
+ public DF setLineSeparator(String lineSeparator) {
+ this.lineSeparator = lineSeparator;
+ reset();
+ return self();
+ }
+
+ /**
+ * Gets the normalized line separator.
+ * If {@code null} then the default format value is used.
+ *
+ * @return the normalized line separator
+ * @see com.univocity.parsers.common.Format#getNormalizedNewline()
+ */
+ public Character getNormalizedLineSeparator() {
+ return normalizedLineSeparator;
+ }
+
+ /**
+ * Sets the normalized line separator.
+ * If {@code null} then the default format value is used.
+ *
+ * @param normalizedLineSeparator the normalized line separator
+ * @return current data format instance, fluent API
+ * @see Format#setNormalizedNewline(char)
+ */
+ public DF setNormalizedLineSeparator(Character normalizedLineSeparator) {
+ this.normalizedLineSeparator = normalizedLineSeparator;
+ reset();
+ return self();
+ }
+
+ /**
+ * Gets the comment symbol.
+ * If {@code null} then the default format value is used.
+ *
+ * @return the comment symbol
+ * @see com.univocity.parsers.common.Format#getComment()
+ */
+ public Character getComment() {
+ return comment;
+ }
+
+ /**
+ * Gets the comment symbol.
+ * If {@code null} then the default format value is used.
+ *
+ * @param comment the comment symbol
+ * @return current data format instance, fluent API
+ * @see com.univocity.parsers.common.Format#setComment(char)
+ */
+ public DF setComment(Character comment) {
+ this.comment = comment;
+ reset();
+ return self();
+ }
+
+ /**
+ * Gets whether or not the unmarshalling should read lines lazily.
+ *
+ * @return whether or not the unmarshalling should read lines lazily
+ */
+ public boolean isLazyLoad() {
+ return lazyLoad;
+ }
+
+ /**
+ * Sets whether or not the unmarshalling should read lines lazily.
+ *
+ * @param lazyLoad whether or not the unmarshalling should read lines lazily
+ * @return current data format instance, fluent API
+ */
+ public DF setLazyLoad(boolean lazyLoad) {
+ this.lazyLoad = lazyLoad;
+ reset();
+ return self();
+ }
+
+ /**
+ * Gets whether or not the unmarshalling should produces maps instead of lists.
+ *
+ * @return whether or not the unmarshalling should produces maps instead of lists
+ */
+ public boolean isAsMap() {
+ return asMap;
+ }
+
+ /**
+ * Sets whether or not the unmarshalling should produces maps instead of lists.
+ *
+ * @param asMap whether or not the unmarshalling should produces maps instead of lists
+ * @return current data format instance, fluent API
+ */
+ public DF setAsMap(boolean asMap) {
+ this.asMap = asMap;
+ reset();
+ return self();
+ }
+
+ /**
+ * Creates a new instance of the writer settings.
+ *
+ * @return New instance of the writer settings
+ */
+ protected abstract CWS createWriterSettings();
+
+ /**
+ * Configures the writer settings.
+ *
+ * @param settings Writer settings to configure
+ */
+ protected void configureWriterSettings(CWS settings) {
+ configureCommonSettings(settings);
+
+ if (emptyValue != null) {
+ settings.setEmptyValue(emptyValue);
+ }
+ }
+
+ /**
+ * Creates a new instance of the uniVocity writer.
+ *
+ * @param writer Output writer to use
+ * @param settings Writer settings to use
+ * @return New uinstance of the uniVocity writer
+ */
+ protected abstract W createWriter(Writer writer, CWS settings);
+
+ /**
+ * Creates a new instance of the parser settings.
+ *
+ * @return New instance of the parser settings
+ */
+ protected abstract CPS createParserSettings();
+
+ /**
+ * Configure the parser settings.
+ *
+ * @param settings Parser settings to configure
+ */
+ protected void configureParserSettings(CPS settings) {
+ configureCommonSettings(settings);
+
+ if (headerExtractionEnabled != null) {
+ settings.setHeaderExtractionEnabled(headerExtractionEnabled);
+ }
+ if (numberOfRecordsToRead != null) {
+ settings.setNumberOfRecordsToRead(numberOfRecordsToRead);
+ }
+ }
+
+ /**
+ * Creates a new instance of the uniVocity parser.
+ *
+ * @param settings Parser settings to use
+ * @return New instance of the uniVocity parser
+ */
+ protected abstract P createParser(CPS settings);
+
+ /**
+ * Configures the format.
+ *
+ * @param format format to configure
+ */
+ protected void configureFormat(F format) {
+ if (lineSeparator != null) {
+ format.setLineSeparator(lineSeparator);
+ }
+ if (normalizedLineSeparator != null) {
+ format.setNormalizedNewline(normalizedLineSeparator);
+ }
+ if (comment != null) {
+ format.setComment(comment);
+ }
+ }
+
+ /**
+ * Resets the caches. It <b>must</b> be called when a setter is called.
+ */
+ protected void reset() {
+ writerSettings = null;
+ marshaller = null;
+ parserSettings = null;
+ unmarshaller = null;
+ }
+
+ /**
+ * Creates and configures the writer settings.
+ *
+ * @return new configured instance of the writer settings
+ */
+ final CWS createAndConfigureWriterSettings() {
+ CWS settings = createWriterSettings();
+ configureWriterSettings(settings);
+ configureFormat(settings.getFormat());
+ return settings;
+ }
+
+ /**
+ * Creates and configures the parser settings.
+ *
+ * @return new configured instance of the parser settings
+ */
+ final CPS createAndConfigureParserSettings() {
+ CPS settings = createParserSettings();
+ configureParserSettings(settings);
+ configureFormat(settings.getFormat());
+ return settings;
+ }
+
+ /**
+ * Configures the common settings shared by parser and writer.
+ *
+ * @param settings settings to configure
+ */
+ private void configureCommonSettings(CommonSettings<F> settings) {
+ if (nullValue != null) {
+ settings.setNullValue(nullValue);
+ }
+ if (skipEmptyLines != null) {
+ settings.setSkipEmptyLines(skipEmptyLines);
+ }
+ if (ignoreTrailingWhitespaces != null) {
+ settings.setIgnoreTrailingWhitespaces(ignoreTrailingWhitespaces);
+ }
+ if (ignoreLeadingWhitespaces != null) {
+ settings.setIgnoreLeadingWhitespaces(ignoreLeadingWhitespaces);
+ }
+ if (headersDisabled) {
+ settings.setHeaders((String[]) null);
+ } else if (headers != null) {
+ settings.setHeaders(headers);
+ }
+ }
+
+ /**
+ * Returns {@code this} as the proper data format type. It helps the fluent API with inheritance.
+ *
+ * @return {@code this} as the proper data format type
+ */
+ @SuppressWarnings("unchecked")
+ private DF self() {
+ return (DF) this;
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/d308620f/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/HeaderRowProcessor.java
----------------------------------------------------------------------
diff --git a/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/HeaderRowProcessor.java b/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/HeaderRowProcessor.java
new file mode 100644
index 0000000..6db57b6
--- /dev/null
+++ b/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/HeaderRowProcessor.java
@@ -0,0 +1,69 @@
+/**
+ * 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.univocity;
+
+import com.univocity.parsers.common.ParsingContext;
+import com.univocity.parsers.common.processor.RowProcessor;
+
+/**
+ * This class is used by the unmarshaller in order to retrieve the headers.
+ */
+final class HeaderRowProcessor implements RowProcessor {
+ private String[] headers;
+
+ /**
+ * Called when the processing starts, it clears the headers
+ *
+ * @param context Parsing context
+ */
+ @Override
+ public void processStarted(ParsingContext context) {
+ headers = null;
+ }
+
+ /**
+ * Called when a row is processed, it retrieve the headers if necessary.
+ *
+ * @param row Processed row
+ * @param context Parsing context
+ */
+ @Override
+ public void rowProcessed(String[] row, ParsingContext context) {
+ if (headers == null) {
+ headers = context.headers();
+ }
+ }
+
+ /**
+ * Called when the processing completes, it clears the headers.
+ *
+ * @param context Parsing context
+ */
+ @Override
+ public void processEnded(ParsingContext context) {
+ headers = null;
+ }
+
+ /**
+ * Gets the headers.
+ *
+ * @return the headers
+ */
+ public String[] getHeaders() {
+ return headers;
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/d308620f/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/Marshaller.java
----------------------------------------------------------------------
diff --git a/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/Marshaller.java b/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/Marshaller.java
new file mode 100644
index 0000000..890babf
--- /dev/null
+++ b/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/Marshaller.java
@@ -0,0 +1,101 @@
+/**
+ * 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.univocity;
+
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.NoTypeConversionAvailableException;
+
+import com.univocity.parsers.common.AbstractWriter;
+
+import static java.util.Arrays.asList;
+import static org.apache.camel.util.ExchangeHelper.convertToMandatoryType;
+import static org.apache.camel.util.ExchangeHelper.convertToType;
+
+/**
+ * This class marshalls the exchange body using an uniVocity writer. It can automatically generates headers and keep
+ * their order in memory.
+ *
+ * @param <W> Writer class
+ */
+final class Marshaller<W extends AbstractWriter<?>> {
+ private final LinkedHashSet<String> headers = new LinkedHashSet<>();
+ private final boolean adaptHeaders;
+
+ /**
+ * Creates a new instance.
+ *
+ * @param headers the base headers to use
+ * @param adaptHeaders whether or not we can add headers on the fly depending on the data
+ */
+ Marshaller(String[] headers, boolean adaptHeaders) {
+ if (headers != null) {
+ this.headers.addAll(asList(headers));
+ }
+ this.adaptHeaders = adaptHeaders;
+ }
+
+ /**
+ * Marshals the given body.
+ *
+ * @param exchange exchange to use (for type conversion)
+ * @param body body to marshal
+ * @param writer uniVocity writer to use
+ * @throws NoTypeConversionAvailableException when it's not possible to convert the body as list and maps.
+ */
+ public void marshal(Exchange exchange, Object body, W writer) throws NoTypeConversionAvailableException {
+ try {
+ List list = convertToType(exchange, List.class, body);
+ if (list != null) {
+ for (Object row : list) {
+ writeRow(exchange, row, writer);
+ }
+ } else {
+ writeRow(exchange, body, writer);
+ }
+ } finally {
+ writer.close();
+ }
+ }
+
+ /**
+ * Writes the given row.
+ *
+ * @param exchange exchange to use (for type conversion)
+ * @param row row to write
+ * @param writer uniVocity writer to use
+ * @throws NoTypeConversionAvailableException when it's not possible to convert the row as map.
+ */
+ private void writeRow(Exchange exchange, Object row, W writer) throws NoTypeConversionAvailableException {
+ Map map = convertToMandatoryType(exchange, Map.class, row);
+ if (adaptHeaders) {
+ for (Object key : map.keySet()) {
+ headers.add(convertToMandatoryType(exchange, String.class, key));
+ }
+ }
+
+ Object[] values = new Object[headers.size()];
+ int index = 0;
+ for (String header : headers) {
+ values[index++] = map.get(header);
+ }
+ writer.writeRow(values);
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/d308620f/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/UniVocityCsvDataFormat.java
----------------------------------------------------------------------
diff --git a/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/UniVocityCsvDataFormat.java b/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/UniVocityCsvDataFormat.java
new file mode 100644
index 0000000..1a204bf
--- /dev/null
+++ b/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/UniVocityCsvDataFormat.java
@@ -0,0 +1,209 @@
+/**
+ * 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.univocity;
+
+import java.io.Writer;
+
+import com.univocity.parsers.csv.CsvFormat;
+import com.univocity.parsers.csv.CsvParser;
+import com.univocity.parsers.csv.CsvParserSettings;
+import com.univocity.parsers.csv.CsvWriter;
+import com.univocity.parsers.csv.CsvWriterSettings;
+
+/**
+ * This class is the data format that uses the CSV uniVocity parser.
+ */
+public class UniVocityCsvDataFormat extends AbstractUniVocityDataFormat<CsvFormat, CsvWriterSettings, CsvWriter, CsvParserSettings, CsvParser, UniVocityCsvDataFormat> {
+ protected Boolean quoteAllFields;
+ protected Character quote;
+ protected Character quoteEscape;
+ protected Character delimiter;
+
+ /**
+ * Gets whether or not all fields must be quoted.
+ * If {@code null} then the default settings value is used.
+ *
+ * @return whether or not all fields must be quoted
+ * @see com.univocity.parsers.csv.CsvWriterSettings#getQuoteAllFields()
+ */
+ public Boolean getQuoteAllFields() {
+ return quoteAllFields;
+ }
+
+ /**
+ * Gets whether or not all fields must be quoted.
+ * If {@code null} then the default settings value is used.
+ *
+ * @param quoteAllFields whether or not all fields must be quoted
+ * @return current data format instance, fluent API
+ * @see com.univocity.parsers.csv.CsvWriterSettings#setQuoteAllFields(boolean)
+ */
+ public UniVocityCsvDataFormat setQuoteAllFields(Boolean quoteAllFields) {
+ this.quoteAllFields = quoteAllFields;
+ reset();
+ return this;
+ }
+
+ /**
+ * Gets the quote symbol.
+ * If {@code null} then the default format value is used.
+ *
+ * @return the quote symbol
+ * @see com.univocity.parsers.csv.CsvFormat#getQuote()
+ */
+ public Character getQuote() {
+ return quote;
+ }
+
+ /**
+ * Sets the quote symbol.
+ * If {@code null} then the default format value is used.
+ *
+ * @param quote the quote symbol
+ * @return current data format instance, fluent API
+ * @see com.univocity.parsers.csv.CsvFormat#setQuote(char)
+ */
+ public UniVocityCsvDataFormat setQuote(Character quote) {
+ this.quote = quote;
+ reset();
+ return this;
+ }
+
+ /**
+ * Gets the quote escape symbol.
+ * If {@code null} then the default format value is used.
+ *
+ * @return the quote escape symbol
+ * @see com.univocity.parsers.csv.CsvFormat#getQuoteEscape()
+ */
+ public Character getQuoteEscape() {
+ return quoteEscape;
+ }
+
+ /**
+ * Sets the quote escape symbol.
+ * If {@code null} then the default format value is used.
+ *
+ * @param quoteEscape the quote escape symbol
+ * @return current data format instance, fluent API
+ * @see com.univocity.parsers.csv.CsvFormat#setQuoteEscape(char)
+ */
+ public UniVocityCsvDataFormat setQuoteEscape(Character quoteEscape) {
+ this.quoteEscape = quoteEscape;
+ reset();
+ return this;
+ }
+
+ /**
+ * Gets the delimiter symbol.
+ * If {@code null} then the default format value is used.
+ *
+ * @return the delimiter symbol
+ * @see com.univocity.parsers.csv.CsvFormat#getDelimiter()
+ */
+ public Character getDelimiter() {
+ return delimiter;
+ }
+
+ /**
+ * Sets the delimiter symbol.
+ * If {@code null} then the default format value is used.
+ *
+ * @param delimiter the delimiter symbol
+ * @return current data format instance, fluent API
+ * @see com.univocity.parsers.csv.CsvFormat#setDelimiter(char)
+ */
+ public UniVocityCsvDataFormat setDelimiter(Character delimiter) {
+ this.delimiter = delimiter;
+ reset();
+ return this;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected CsvWriterSettings createWriterSettings() {
+ return new CsvWriterSettings();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected void configureWriterSettings(CsvWriterSettings settings) {
+ super.configureWriterSettings(settings);
+
+ if (quoteAllFields != null) {
+ settings.setQuoteAllFields(quoteAllFields);
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected CsvWriter createWriter(Writer writer, CsvWriterSettings settings) {
+ return new CsvWriter(writer, settings);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected CsvParserSettings createParserSettings() {
+ return new CsvParserSettings();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected void configureParserSettings(CsvParserSettings settings) {
+ super.configureParserSettings(settings);
+
+ if (emptyValue != null) {
+ settings.setEmptyValue(emptyValue);
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected CsvParser createParser(CsvParserSettings settings) {
+ return new CsvParser(settings);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected void configureFormat(CsvFormat format) {
+ super.configureFormat(format);
+
+ if (quote != null) {
+ format.setQuote(quote);
+ }
+ if (quoteEscape != null) {
+ format.setQuoteEscape(quoteEscape);
+ }
+ if (delimiter != null) {
+ format.setDelimiter(delimiter);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/d308620f/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/UniVocityFixedWidthDataFormat.java
----------------------------------------------------------------------
diff --git a/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/UniVocityFixedWidthDataFormat.java b/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/UniVocityFixedWidthDataFormat.java
new file mode 100644
index 0000000..44e9553
--- /dev/null
+++ b/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/UniVocityFixedWidthDataFormat.java
@@ -0,0 +1,218 @@
+/**
+ * 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.univocity;
+
+import java.io.Writer;
+import java.util.LinkedHashMap;
+
+import com.univocity.parsers.fixed.FixedWidthFieldLengths;
+import com.univocity.parsers.fixed.FixedWidthFormat;
+import com.univocity.parsers.fixed.FixedWidthParser;
+import com.univocity.parsers.fixed.FixedWidthParserSettings;
+import com.univocity.parsers.fixed.FixedWidthWriter;
+import com.univocity.parsers.fixed.FixedWidthWriterSettings;
+
+/**
+ * This class is the data format that uses the fixed-width uniVocity parser.
+ */
+public class UniVocityFixedWidthDataFormat extends AbstractUniVocityDataFormat<FixedWidthFormat, FixedWidthWriterSettings, FixedWidthWriter, FixedWidthParserSettings, FixedWidthParser, UniVocityFixedWidthDataFormat> {
+ protected int[] fieldLengths;
+ protected Boolean skipTrailingCharsUntilNewline;
+ protected Boolean recordEndsOnNewline;
+ protected Character padding;
+
+ /**
+ * Gets the field lengths.
+ * It's used to construct uniVocity {@link com.univocity.parsers.fixed.FixedWidthFieldLengths} instance.
+ *
+ * @return the field lengths
+ */
+ public int[] getFieldLengths() {
+ return fieldLengths;
+ }
+
+ /**
+ * Sets the field lengths
+ * It's used to construct uniVocity {@link com.univocity.parsers.fixed.FixedWidthFieldLengths} instance.
+ *
+ * @param fieldLengths the field length
+ * @return current data format instance, fluent API
+ */
+ public UniVocityFixedWidthDataFormat setFieldLengths(int[] fieldLengths) {
+ this.fieldLengths = fieldLengths;
+ reset();
+ return this;
+ }
+
+ /**
+ * Gets whether or not trailing characters until new line must be ignored.
+ *
+ * @return whether or not trailing characters until new line must be ignored
+ * @see com.univocity.parsers.fixed.FixedWidthParserSettings#getSkipTrailingCharsUntilNewline()
+ */
+ public Boolean getSkipTrailingCharsUntilNewline() {
+ return skipTrailingCharsUntilNewline;
+ }
+
+ /**
+ * Sets whether or not trailing characters until new line must be ignored.
+ *
+ * @param skipTrailingCharsUntilNewline whether or not trailing characters until new line must be ignored
+ * @return current data format instance, fluent API
+ * @see com.univocity.parsers.fixed.FixedWidthParserSettings#setSkipTrailingCharsUntilNewline(boolean)
+ */
+ public UniVocityFixedWidthDataFormat setSkipTrailingCharsUntilNewline(Boolean skipTrailingCharsUntilNewline) {
+ this.skipTrailingCharsUntilNewline = skipTrailingCharsUntilNewline;
+ reset();
+ return this;
+ }
+
+ /**
+ * Gets whether or not the record ends on new line.
+ *
+ * @return whether or not the record ends on new line
+ * @see com.univocity.parsers.fixed.FixedWidthParserSettings#getRecordEndsOnNewline()
+ */
+ public Boolean getRecordEndsOnNewline() {
+ return recordEndsOnNewline;
+ }
+
+ /**
+ * Sets whether or not the record ends on new line
+ *
+ * @param recordEndsOnNewline whether or not the record ends on new line
+ * @return current data format instance, fluent API
+ * @see com.univocity.parsers.fixed.FixedWidthParserSettings#setRecordEndsOnNewline(boolean)
+ */
+ public UniVocityFixedWidthDataFormat setRecordEndsOnNewline(Boolean recordEndsOnNewline) {
+ this.recordEndsOnNewline = recordEndsOnNewline;
+ reset();
+ return this;
+ }
+
+ /**
+ * Gets the padding symbol.
+ * If {@code null} then the default format value is used.
+ *
+ * @return the padding symbol
+ * @see com.univocity.parsers.fixed.FixedWidthFormat#getPadding()
+ */
+ public Character getPadding() {
+ return padding;
+ }
+
+ /**
+ * Sets the padding symbol.
+ * If {@code null} then the default format value is used.
+ *
+ * @param padding the padding symbol
+ * @return current data format instance, fluent API
+ * @see com.univocity.parsers.fixed.FixedWidthFormat#setPadding(char)
+ */
+ public UniVocityFixedWidthDataFormat setPadding(Character padding) {
+ this.padding = padding;
+ reset();
+ return this;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected FixedWidthWriterSettings createWriterSettings() {
+ return new FixedWidthWriterSettings(createFixedWidthFieldLengths());
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected FixedWidthWriter createWriter(Writer writer, FixedWidthWriterSettings settings) {
+ return new FixedWidthWriter(writer, settings);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected FixedWidthParserSettings createParserSettings() {
+ return new FixedWidthParserSettings(createFixedWidthFieldLengths());
+ }
+
+ @Override
+ protected void configureParserSettings(FixedWidthParserSettings settings) {
+ super.configureParserSettings(settings);
+
+ if (skipTrailingCharsUntilNewline != null) {
+ settings.setSkipTrailingCharsUntilNewline(skipTrailingCharsUntilNewline);
+ }
+ if (recordEndsOnNewline != null) {
+ settings.setRecordEndsOnNewline(true);
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected FixedWidthParser createParser(FixedWidthParserSettings settings) {
+ return new FixedWidthParser(settings);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected void configureFormat(FixedWidthFormat format) {
+ super.configureFormat(format);
+
+ if (padding != null) {
+ format.setPadding(padding);
+ }
+ }
+
+ /**
+ * Creates the {@link com.univocity.parsers.fixed.FixedWidthFieldLengths} instance based on the headers and field
+ * lengths.
+ *
+ * @return new {@code FixedWidthFieldLengths} based on the header and field lengthsl
+ */
+ private FixedWidthFieldLengths createFixedWidthFieldLengths() {
+ // Ensure that the field lengths have been defined.
+ if (fieldLengths == null) {
+ throw new IllegalArgumentException("The fieldLengths must have been defined in order to use the fixed-width format.");
+ }
+
+ // If there's no header then we only use their length
+ if (headers == null) {
+ return new FixedWidthFieldLengths(fieldLengths);
+ }
+
+ // Use both headers and field lengths (same size and no duplicate headers)
+ if (fieldLengths.length != headers.length) {
+ throw new IllegalArgumentException("The headers and fieldLengths must have the same number of element in order to use the fixed-width format.");
+ }
+ LinkedHashMap<String, Integer> fields = new LinkedHashMap<>();
+ for (int i = 0; i < headers.length; i++) {
+ fields.put(headers[i], fieldLengths[i]);
+ }
+ if (fields.size() != headers.length) {
+ throw new IllegalArgumentException("The headers cannot have duplicates in order to use the fixed-width format.");
+ }
+ return new FixedWidthFieldLengths(fields);
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/d308620f/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/UniVocityTsvDataFormat.java
----------------------------------------------------------------------
diff --git a/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/UniVocityTsvDataFormat.java b/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/UniVocityTsvDataFormat.java
new file mode 100644
index 0000000..5bb5fbd
--- /dev/null
+++ b/components/camel-univocity-parsers/src/main/java/org/apache/camel/dataformat/univocity/UniVocityTsvDataFormat.java
@@ -0,0 +1,101 @@
+/**
+ * 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.univocity;
+
+import java.io.Writer;
+
+import com.univocity.parsers.tsv.TsvFormat;
+import com.univocity.parsers.tsv.TsvParser;
+import com.univocity.parsers.tsv.TsvParserSettings;
+import com.univocity.parsers.tsv.TsvWriter;
+import com.univocity.parsers.tsv.TsvWriterSettings;
+
+/**
+ * This class is the data format that uses the TSV uniVocity parser.
+ */
+public class UniVocityTsvDataFormat extends AbstractUniVocityDataFormat<TsvFormat, TsvWriterSettings, TsvWriter, TsvParserSettings, TsvParser, UniVocityTsvDataFormat> {
+ protected Character escapeChar;
+
+ /**
+ * Gets the escape character symbol.
+ * If {@code null} then the default format value is used.
+ *
+ * @return the escape character symbol
+ * @see com.univocity.parsers.tsv.TsvFormat#getEscapeChar()
+ */
+ public Character getEscapeChar() {
+ return escapeChar;
+ }
+
+ /**
+ * Sets the escape character symbol.
+ * If {@code null} then the default settings value is used.
+ *
+ * @param escapeChar the escape character symbol
+ * @return current data format instance, fluent API
+ * @see com.univocity.parsers.tsv.TsvFormat#setEscapeChar(char)
+ */
+ public UniVocityTsvDataFormat setEscapeChar(Character escapeChar) {
+ this.escapeChar = escapeChar;
+ reset();
+ return this;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected TsvWriterSettings createWriterSettings() {
+ return new TsvWriterSettings();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected TsvWriter createWriter(Writer writer, TsvWriterSettings settings) {
+ return new TsvWriter(writer, settings);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected TsvParserSettings createParserSettings() {
+ return new TsvParserSettings();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected TsvParser createParser(TsvParserSettings settings) {
+ return new TsvParser(settings);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected void configureFormat(TsvFormat format) {
+ super.configureFormat(format);
+
+ if (escapeChar != null) {
+ format.setEscapeChar(escapeChar);
+ }
+ }
+}