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 java.util.Locale;
020import java.util.Objects;
021
022import javax.xml.bind.annotation.XmlAccessType;
023import javax.xml.bind.annotation.XmlAccessorType;
024import javax.xml.bind.annotation.XmlAttribute;
025import javax.xml.bind.annotation.XmlRootElement;
026import javax.xml.bind.annotation.XmlTransient;
027
028import org.apache.camel.model.DataFormatDefinition;
029import org.apache.camel.spi.Metadata;
030
031/**
032 * The Bindy data format is used for working with flat payloads (such as CSV,
033 * delimited, fixed length formats, or FIX messages).
034 */
035@Metadata(firstVersion = "2.0.0", label = "dataformat,transformation,csv", title = "Bindy")
036@XmlRootElement(name = "bindy")
037@XmlAccessorType(XmlAccessType.FIELD)
038public class BindyDataFormat extends DataFormatDefinition {
039    @XmlAttribute(required = true)
040    @Metadata(required = true, javaType = "org.apache.camel.model.dataformat.BindyType", enums = "Csv,Fixed,KeyValue")
041    private String type;
042    @XmlAttribute
043    private String classType;
044    @XmlAttribute
045    private String locale;
046    @XmlAttribute
047    @Metadata(javaType = "java.lang.Boolean", defaultValue = "true")
048    private String unwrapSingleInstance;
049    @XmlAttribute
050    @Metadata(javaType = "java.lang.Boolean", defaultValue = "false")
051    private String allowEmptyStream;
052    @XmlTransient
053    private Class<?> clazz;
054
055    public BindyDataFormat() {
056        super("bindy");
057    }
058
059    public String getType() {
060        return type;
061    }
062
063    /**
064     * Whether to use Csv, Fixed, or KeyValue.
065     */
066    public void setType(String type) {
067        this.type = type;
068    }
069
070    public String getClassTypeAsString() {
071        return classType;
072    }
073
074    @Override
075    public String getDataFormatName() {
076        if ("Csv".equals(type)) {
077            return "bindy-csv";
078        } else if ("Fixed".equals(type)) {
079            return "bindy-fixed";
080        } else {
081            return "bindy-kvp";
082        }
083    }
084
085    /**
086     * Name of model class to use.
087     */
088    public void setClassTypeAsString(String classType) {
089        this.classType = classType;
090    }
091
092    /**
093     * Name of model class to use.
094     */
095    public void setClassType(String classType) {
096        setClassTypeAsString(classType);
097    }
098
099    /**
100     * Name of model class to use.
101     */
102    public void setClassType(Class<?> classType) {
103        this.clazz = classType;
104    }
105
106    public Class<?> getClassType() {
107        return clazz;
108    }
109
110    public String getLocale() {
111        return locale;
112    }
113
114    /**
115     * To configure a default locale to use, such as <tt>us</tt> for united
116     * states.
117     * <p/>
118     * To use the JVM platform default locale then use the name <tt>default</tt>
119     */
120    public void setLocale(String locale) {
121        this.locale = locale;
122    }
123
124    public String getUnwrapSingleInstance() {
125        return unwrapSingleInstance;
126    }
127
128    /**
129     * When unmarshalling should a single instance be unwrapped and returned
130     * instead of wrapped in a <tt>java.util.List</tt>.
131     */
132    public void setUnwrapSingleInstance(String unwrapSingleInstance) {
133        this.unwrapSingleInstance = unwrapSingleInstance;
134    }
135
136    public String getAllowEmptyStream() {
137        return allowEmptyStream;
138    }
139    
140    /**
141   * Whether to allow empty streams in the unmarshal process. If true, no
142   * exception will be thrown when a body without records is provided.
143   */
144    public void setAllowEmptyStream(String allowEmptyStream) {
145        this.allowEmptyStream = allowEmptyStream;
146    }
147
148    //
149    // Fluent builder api
150    //
151
152    public BindyDataFormat csv() {
153        return type(BindyType.Csv);
154    }
155
156    public BindyDataFormat fixed() {
157        return type(BindyType.Fixed);
158    }
159
160    public BindyDataFormat keyValue() {
161        return type(BindyType.KeyValue);
162    }
163
164    public BindyDataFormat type(BindyType type) {
165        return type(type.name());
166    }
167
168    public BindyDataFormat type(String type) {
169        this.type = type;
170        return this;
171    }
172
173    public BindyDataFormat classType(Class<?> classType) {
174        this.clazz = classType;
175        return this;
176    }
177
178    public BindyDataFormat classType(String classType) {
179        this.classType = classType;
180        return this;
181    }
182
183    public BindyDataFormat locale(Locale locale) {
184        return locale(locale.getCountry().isEmpty()
185                ? locale.getLanguage() : locale.getLanguage() + "-" + locale.getCountry());
186    }
187
188    public BindyDataFormat locale(String locale) {
189        this.locale = locale;
190        return this;
191    }
192
193    public BindyDataFormat unwrapSingleInstance(boolean unwrapSingleInstance) {
194        return unwrapSingleInstance(Boolean.toString(unwrapSingleInstance));
195    }
196
197    public BindyDataFormat unwrapSingleInstance(String unwrapSingleInstance) {
198        this.unwrapSingleInstance = unwrapSingleInstance;
199        return this;
200    }
201
202    public BindyDataFormat allowEmptyStream(boolean allowEmptyStream) {
203        return allowEmptyStream(Boolean.toString(allowEmptyStream));
204    }
205
206    public BindyDataFormat allowEmptyStream(String allowEmptyStream) {
207        this.allowEmptyStream = allowEmptyStream;
208        return this;
209    }
210
211
212}