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 @XmlTransient 048 private Class<?> clazz; 049 050 public BindyDataFormat() { 051 super("bindy"); 052 } 053 054 public BindyType getType() { 055 return type; 056 } 057 058 /** 059 * Whether to use csv, fixed or key value pairs mode. 060 */ 061 public void setType(BindyType type) { 062 this.type = type; 063 } 064 065 public String getClassType() { 066 return classType; 067 } 068 069 /** 070 * Name of model class to use. 071 */ 072 public void setClassType(String classType) { 073 this.classType = classType; 074 } 075 076 /** 077 * Type of model class to use. 078 */ 079 public void setClassType(Class<?> classType) { 080 this.clazz = classType; 081 } 082 083 public String getLocale() { 084 return locale; 085 } 086 087 /** 088 * To configure a default locale to use, such as <tt>us</tt> for united states. 089 * <p/> 090 * To use the JVM platform default locale then use the name <tt>default</tt> 091 */ 092 public void setLocale(String locale) { 093 this.locale = locale; 094 } 095 096 protected DataFormat createDataFormat(RouteContext routeContext) { 097 if (classType == null && clazz == null) { 098 throw new IllegalArgumentException("Either packages or classType must be specified"); 099 } 100 101 if (type == BindyType.Csv) { 102 setDataFormatName("bindy-csv"); 103 } else if (type == BindyType.Fixed) { 104 setDataFormatName("bindy-fixed"); 105 } else { 106 setDataFormatName("bindy-kvp"); 107 } 108 109 if (clazz == null && classType != null) { 110 try { 111 clazz = routeContext.getCamelContext().getClassResolver().resolveMandatoryClass(classType); 112 } catch (ClassNotFoundException e) { 113 throw ObjectHelper.wrapRuntimeCamelException(e); 114 } 115 } 116 return super.createDataFormat(routeContext); 117 } 118 119 @Override 120 protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) { 121 setProperty(camelContext, dataFormat, "locale", locale); 122 setProperty(camelContext, dataFormat, "classType", clazz); 123 } 124 125}