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;
023import javax.xml.bind.annotation.XmlTransient;
024
025import org.apache.camel.CamelContext;
026import org.apache.camel.model.DataFormatDefinition;
027import org.apache.camel.spi.DataFormat;
028import org.apache.camel.spi.Metadata;
029import org.apache.camel.spi.RouteContext;
030import org.apache.camel.util.ObjectHelper;
031
032/**
033 * The Bindy data format is used for working with flat payloads (such as CSV, delimited, fixed length formats, or FIX messages).
034 *
035 * @version 
036 */
037@Metadata(firstVersion = "2.0.0", label = "dataformat,transformation,csv", title = "Bindy")
038@XmlRootElement(name = "bindy")
039@XmlAccessorType(XmlAccessType.FIELD)
040public class BindyDataFormat extends DataFormatDefinition {
041    @XmlAttribute(required = true)
042    private BindyType type;
043    @XmlAttribute
044    private String classType;
045    @XmlAttribute
046    private String locale;
047    @XmlAttribute @Metadata(defaultValue = "true")
048    private Boolean unwrapSingleInstance;
049    @XmlTransient
050    private Class<?> clazz;
051
052    public BindyDataFormat() {
053        super("bindy");
054    }
055
056    public BindyType getType() {
057        return type;
058    }
059
060    /**
061     * Whether to use csv, fixed or key value pairs mode.
062     */
063    public void setType(BindyType type) {
064        this.type = type;
065    }
066
067    public String getClassType() {
068        return classType;
069    }
070
071    /**
072     * Name of model class to use.
073     */
074    public void setClassType(String classType) {
075        this.classType = classType;
076    }
077
078    /**
079     * Type of model class to use.
080     */
081    public void setClassType(Class<?> classType) {
082        this.clazz = classType;
083    }
084
085    public String getLocale() {
086        return locale;
087    }
088
089    /**
090     * To configure a default locale to use, such as <tt>us</tt> for united states.
091     * <p/>
092     * To use the JVM platform default locale then use the name <tt>default</tt>
093     */
094    public void setLocale(String locale) {
095        this.locale = locale;
096    }
097
098    public Boolean getUnwrapSingleInstance() {
099        return unwrapSingleInstance;
100    }
101
102    /**
103     * When unmarshalling should a single instance be unwrapped and returned instead of wrapped in a <tt>java.util.List</tt>.
104     */
105    public void setUnwrapSingleInstance(Boolean unwrapSingleInstance) {
106        this.unwrapSingleInstance = unwrapSingleInstance;
107    }
108
109    protected DataFormat createDataFormat(RouteContext routeContext) {
110        if (classType == null && clazz == null) {
111            throw new IllegalArgumentException("Either packages or classType must be specified");
112        }
113
114        if (type == BindyType.Csv) {
115            setDataFormatName("bindy-csv");
116        } else if (type == BindyType.Fixed) {
117            setDataFormatName("bindy-fixed");
118        } else {
119            setDataFormatName("bindy-kvp");
120        }
121
122        if (clazz == null && classType != null) {
123            try {
124                clazz = routeContext.getCamelContext().getClassResolver().resolveMandatoryClass(classType);
125            } catch (ClassNotFoundException e) {
126                throw ObjectHelper.wrapRuntimeCamelException(e);
127            }
128        }
129        return super.createDataFormat(routeContext);
130    }
131
132    @Override
133    protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) {
134        setProperty(camelContext, dataFormat, "locale", locale);
135        setProperty(camelContext, dataFormat, "classType", clazz);
136        if (unwrapSingleInstance != null) {
137            setProperty(camelContext, dataFormat, "unwrapSingleInstance", unwrapSingleInstance);
138        }
139    }
140
141}