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 * Flatpack data format
034 */
035@Metadata(label = "dataformat,transformation,csv", title = "Flatpack")
036@XmlRootElement(name = "flatpack")
037@XmlAccessorType(XmlAccessType.FIELD)
038public class FlatpackDataFormat extends DataFormatDefinition {
039    @XmlAttribute
040    private String parserFactoryRef;
041    @XmlAttribute
042    private String definition;
043    @XmlAttribute
044    private Boolean fixed;
045    @XmlAttribute @Metadata(defaultValue = "true")
046    private Boolean ignoreFirstRecord;
047    @XmlAttribute @Metadata(defaultValue = "\"")
048    private String textQualifier;
049    @XmlAttribute @Metadata(defaultValue = ",")
050    private String delimiter;
051    @XmlAttribute
052    private Boolean allowShortLines;
053    @XmlAttribute
054    private Boolean ignoreExtraColumns;
055
056    public FlatpackDataFormat() {
057        super("flatpack");
058    }
059
060    public String getParserFactoryRef() {
061        return parserFactoryRef;
062    }
063
064    /**
065     * References to a custom parser factory to lookup in the registry
066     */
067    public void setParserFactoryRef(String parserFactoryRef) {
068        this.parserFactoryRef = parserFactoryRef;
069    }
070
071    public String getDefinition() {
072        return definition;
073    }
074
075    /**
076     * The flatpack pzmap configuration file. Can be omitted in simpler situations, but its preferred to use the pzmap.
077     */
078    public void setDefinition(String definition) {
079        this.definition = definition;
080    }
081
082    public Boolean getFixed() {
083        return fixed;
084    }
085
086    /**
087     * Delimited or fixed.
088     * Is by default false = delimited
089     */
090    public void setFixed(Boolean fixed) {
091        this.fixed = fixed;
092    }
093
094    public Boolean getIgnoreFirstRecord() {
095        return ignoreFirstRecord;
096    }
097
098    /**
099     * Whether the first line is ignored for delimited files (for the column headers).
100     * <p/>
101     * Is by default true.
102     */
103    public void setIgnoreFirstRecord(Boolean ignoreFirstRecord) {
104        this.ignoreFirstRecord = ignoreFirstRecord;
105    }
106
107    public String getTextQualifier() {
108        return textQualifier;
109    }
110
111    /**
112     * If the text is qualified with a char such as &quot;
113     */
114    public void setTextQualifier(String textQualifier) {
115        this.textQualifier = textQualifier;
116    }
117
118    public String getDelimiter() {
119        return delimiter;
120    }
121
122    /**
123     * The delimiter char (could be ; , or similar)
124     */
125    public void setDelimiter(String delimiter) {
126        this.delimiter = delimiter;
127    }
128
129    public Boolean getAllowShortLines() {
130        return allowShortLines;
131    }
132
133    /**
134     * Allows for lines to be shorter than expected and ignores the extra characters
135     */
136    public void setAllowShortLines(Boolean allowShortLines) {
137        this.allowShortLines = allowShortLines;
138    }
139
140    public Boolean getIgnoreExtraColumns() {
141        return ignoreExtraColumns;
142    }
143
144    /**
145     * Allows for lines to be longer than expected and ignores the extra characters.
146     */
147    public void setIgnoreExtraColumns(Boolean ignoreExtraColumns) {
148        this.ignoreExtraColumns = ignoreExtraColumns;
149    }
150
151    @Override
152    protected DataFormat createDataFormat(RouteContext routeContext) {
153        DataFormat flatpack = super.createDataFormat(routeContext);
154
155        if (ObjectHelper.isNotEmpty(parserFactoryRef)) {
156            Object parserFactory = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), parserFactoryRef);
157            setProperty(routeContext.getCamelContext(), flatpack, "parserFactory", parserFactory);
158        }
159
160        return flatpack;
161    }
162
163    @Override
164    protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) {
165        if (ObjectHelper.isNotEmpty(definition)) {
166            setProperty(camelContext, dataFormat, "definition", definition);
167        }
168        if (fixed != null) {
169            setProperty(camelContext, dataFormat, "fixed", fixed);
170        }
171        if (ignoreFirstRecord != null) {
172            setProperty(camelContext, dataFormat, "ignoreFirstRecord", ignoreFirstRecord);
173        }
174        if (ObjectHelper.isNotEmpty(textQualifier)) {
175            if (textQualifier.length() > 1) {
176                throw new IllegalArgumentException("Text qualifier must be one character long!");
177            }
178            setProperty(camelContext, dataFormat, "textQualifier", textQualifier.charAt(0));
179        }
180        if (ObjectHelper.isNotEmpty(delimiter)) {
181            if (delimiter.length() > 1) {
182                throw new IllegalArgumentException("Delimiter must be one character long!");
183            }
184            setProperty(camelContext, dataFormat, "delimiter", delimiter.charAt(0));
185        }
186        if (allowShortLines != null) {
187            setProperty(camelContext, dataFormat, "allowShortLines", allowShortLines);
188        }
189        if (ignoreExtraColumns != null) {
190            setProperty(camelContext, dataFormat, "ignoreExtraColumns", ignoreExtraColumns);
191        }
192    }
193}