001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.model.dataformat;
018
019import javax.xml.bind.annotation.XmlAccessType;
020import javax.xml.bind.annotation.XmlAccessorType;
021import javax.xml.bind.annotation.XmlAttribute;
022import javax.xml.bind.annotation.XmlRootElement;
023
024import org.apache.camel.CamelContext;
025import org.apache.camel.model.DataFormatDefinition;
026import org.apache.camel.spi.DataFormat;
027import org.apache.camel.spi.Metadata;
028import org.apache.camel.spi.RouteContext;
029import org.apache.camel.util.CamelContextHelper;
030import org.apache.camel.util.ObjectHelper;
031
032/**
033 * The Flatpack data format is used for working with flat payloads (such as CSV, delimited, or fixed length formats).
034 */
035@Metadata(firstVersion = "2.1.0", label = "dataformat,transformation,csv", title = "Flatpack")
036@XmlRootElement(name = "flatpack")
037@XmlAccessorType(XmlAccessType.FIELD)
038public class FlatpackDataFormat extends DataFormatDefinition {
039    @XmlAttribute
040    private String definition;
041    @XmlAttribute
042    private Boolean fixed;
043    @XmlAttribute @Metadata(defaultValue = "true")
044    private Boolean ignoreFirstRecord;
045    @XmlAttribute
046    private String textQualifier;
047    @XmlAttribute @Metadata(defaultValue = ",")
048    private String delimiter;
049    @XmlAttribute
050    private Boolean allowShortLines;
051    @XmlAttribute
052    private Boolean ignoreExtraColumns;
053    @XmlAttribute @Metadata(label = "advanced")
054    private String parserFactoryRef;
055
056    public FlatpackDataFormat() {
057        super("flatpack");
058    }
059
060    public String getDefinition() {
061        return definition;
062    }
063
064    /**
065     * The flatpack pzmap configuration file. Can be omitted in simpler situations, but its preferred to use the pzmap.
066     */
067    public void setDefinition(String definition) {
068        this.definition = definition;
069    }
070
071    public Boolean getFixed() {
072        return fixed;
073    }
074
075    /**
076     * Delimited or fixed.
077     * Is by default false = delimited
078     */
079    public void setFixed(Boolean fixed) {
080        this.fixed = fixed;
081    }
082
083    public Boolean getIgnoreFirstRecord() {
084        return ignoreFirstRecord;
085    }
086
087    /**
088     * Whether the first line is ignored for delimited files (for the column headers).
089     * <p/>
090     * Is by default true.
091     */
092    public void setIgnoreFirstRecord(Boolean ignoreFirstRecord) {
093        this.ignoreFirstRecord = ignoreFirstRecord;
094    }
095
096    public String getTextQualifier() {
097        return textQualifier;
098    }
099
100    /**
101     * If the text is qualified with a character.
102     * <p/>
103     * Uses quote character by default.
104     */
105    public void setTextQualifier(String textQualifier) {
106        this.textQualifier = textQualifier;
107    }
108
109    public String getDelimiter() {
110        return delimiter;
111    }
112
113    /**
114     * The delimiter char (could be ; , or similar)
115     */
116    public void setDelimiter(String delimiter) {
117        this.delimiter = delimiter;
118    }
119
120    public Boolean getAllowShortLines() {
121        return allowShortLines;
122    }
123
124    /**
125     * Allows for lines to be shorter than expected and ignores the extra characters
126     */
127    public void setAllowShortLines(Boolean allowShortLines) {
128        this.allowShortLines = allowShortLines;
129    }
130
131    public Boolean getIgnoreExtraColumns() {
132        return ignoreExtraColumns;
133    }
134
135    /**
136     * Allows for lines to be longer than expected and ignores the extra characters.
137     */
138    public void setIgnoreExtraColumns(Boolean ignoreExtraColumns) {
139        this.ignoreExtraColumns = ignoreExtraColumns;
140    }
141
142    public String getParserFactoryRef() {
143        return parserFactoryRef;
144    }
145
146    /**
147     * References to a custom parser factory to lookup in the registry
148     */
149    public void setParserFactoryRef(String parserFactoryRef) {
150        this.parserFactoryRef = parserFactoryRef;
151    }
152
153    @Override
154    protected DataFormat createDataFormat(RouteContext routeContext) {
155        DataFormat flatpack = super.createDataFormat(routeContext);
156
157        if (ObjectHelper.isNotEmpty(parserFactoryRef)) {
158            Object parserFactory = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), parserFactoryRef);
159            setProperty(routeContext.getCamelContext(), flatpack, "parserFactory", parserFactory);
160        }
161
162        return flatpack;
163    }
164
165    @Override
166    protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) {
167        if (ObjectHelper.isNotEmpty(definition)) {
168            setProperty(camelContext, dataFormat, "definition", definition);
169        }
170        if (fixed != null) {
171            setProperty(camelContext, dataFormat, "fixed", fixed);
172        }
173        if (ignoreFirstRecord != null) {
174            setProperty(camelContext, dataFormat, "ignoreFirstRecord", ignoreFirstRecord);
175        }
176        if (ObjectHelper.isNotEmpty(textQualifier)) {
177            if (textQualifier.length() > 1) {
178                throw new IllegalArgumentException("Text qualifier must be one character long!");
179            }
180            setProperty(camelContext, dataFormat, "textQualifier", textQualifier.charAt(0));
181        }
182        if (ObjectHelper.isNotEmpty(delimiter)) {
183            if (delimiter.length() > 1) {
184                throw new IllegalArgumentException("Delimiter must be one character long!");
185            }
186            setProperty(camelContext, dataFormat, "delimiter", delimiter.charAt(0));
187        }
188        if (allowShortLines != null) {
189            setProperty(camelContext, dataFormat, "allowShortLines", allowShortLines);
190        }
191        if (ignoreExtraColumns != null) {
192            setProperty(camelContext, dataFormat, "ignoreExtraColumns", ignoreExtraColumns);
193        }
194    }
195}