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.builder; 018 019import org.apache.camel.CamelContext; 020import org.apache.camel.model.DataFormatDefinition; 021import org.apache.camel.model.transformer.CustomTransformerDefinition; 022import org.apache.camel.model.transformer.DataFormatTransformerDefinition; 023import org.apache.camel.model.transformer.EndpointTransformerDefinition; 024import org.apache.camel.model.transformer.TransformerDefinition; 025import org.apache.camel.spi.DataType; 026import org.apache.camel.spi.Transformer; 027 028/** 029 * A <a href="http://camel.apache.org/dsl.html">Java DSL</a> which is 030 * used to build a {@link org.apache.camel.spi.Transformer} and register into {@link org.apache.camel.CamelContext}. 031 * It requires 'scheme' or a pair of 'from' and 'to' to be specified by scheme(), from() and to() method. 032 * And then you can choose a type of transformer by withUri(), withDataFormat(), withJava() or withBean() method. 033 */ 034public class TransformerBuilder { 035 036 private String scheme; 037 private String from; 038 private String to; 039 private String uri; 040 private DataFormatDefinition dataFormat; 041 private Class<? extends Transformer> clazz; 042 private String beanRef; 043 044 /** 045 * Set the scheme name supported by the transformer. 046 * If you specify 'csv', the transformer will be picked up for all of 'csv' from/to 047 * Java transformation. Note that the scheme matching is performed only when 048 * no exactly matched transformer exists. 049 * 050 * @param scheme scheme name 051 */ 052 public TransformerBuilder scheme(String scheme) { 053 this.scheme = scheme; 054 return this; 055 } 056 057 /** 058 * Set the 'from' data type name. 059 * If you specify 'xml:XYZ', the transformer will be picked up if source type is 060 * 'xml:XYZ'. If you specify just 'xml', the transformer matches with all of 061 * 'xml' source type like 'xml:ABC' or 'xml:DEF'. 062 * 063 * @param from 'from' data type name 064 */ 065 public TransformerBuilder fromType(String from) { 066 this.from = from; 067 return this; 068 } 069 070 /** 071 * Set the 'from' data type using Java class. 072 * 073 * @param from 'from' Java class 074 */ 075 public TransformerBuilder fromType(Class<?> from) { 076 this.from = new DataType(from).toString(); 077 return this; 078 } 079 080 /** 081 * Set the 'to' data type name. 082 * If you specify 'json:XYZ', the transformer will be picked up if destination type is 083 * 'json:XYZ'. If you specify just 'json', the transformer matches with all of 084 * 'json' destination type like 'json:ABC' or 'json:DEF'. 085 * 086 * @param to 'to' data type 087 */ 088 public TransformerBuilder toType(String to) { 089 this.to = to; 090 return this; 091 } 092 093 /** 094 * Set the 'to' data type using Java class. 095 * 096 * @param to 'to' Java class 097 */ 098 public TransformerBuilder toType(Class<?> to) { 099 this.to = new DataType(to).toString(); 100 return this; 101 } 102 103 /** 104 * Set the URI to be used for the endpoint {@code Transformer}. 105 * 106 * @param uri endpoint URI 107 */ 108 public TransformerBuilder withUri(String uri) { 109 resetType(); 110 this.uri = uri; 111 return this; 112 } 113 114 /** 115 * Set the {@code DataFormatDefinition} to be used for the {@code DataFormat} {@code Transformer}. 116 */ 117 public TransformerBuilder withDataFormat(DataFormatDefinition dataFormatDefinition) { 118 resetType(); 119 this.dataFormat = dataFormatDefinition; 120 return this; 121 } 122 123 /** 124 * Set the Java {@code Class} represents a custom {@code Transformer} implementation class. 125 */ 126 public TransformerBuilder withJava(Class<? extends Transformer> clazz) { 127 resetType(); 128 this.clazz = clazz; 129 return this; 130 } 131 132 /** 133 * Set the Java Bean name to be used for custom {@code Transformer}. 134 */ 135 public TransformerBuilder withBean(String ref) { 136 resetType(); 137 this.beanRef = ref; 138 return this; 139 } 140 141 private void resetType() { 142 this.uri = null; 143 this.dataFormat = null; 144 this.clazz = null; 145 this.beanRef = null; 146 } 147 148 /** 149 * Configure a Transformer according to the configurations built on this builder 150 * and register it into given {@code CamelContext}. 151 * 152 * @param camelContext {@code CamelContext} 153 */ 154 public void configure(CamelContext camelContext) { 155 TransformerDefinition transformer; 156 if (uri != null) { 157 EndpointTransformerDefinition etd = new EndpointTransformerDefinition(); 158 etd.setUri(uri); 159 transformer = etd; 160 } else if (dataFormat != null) { 161 DataFormatTransformerDefinition dtd = new DataFormatTransformerDefinition(); 162 dtd.setDataFormatType(dataFormat); 163 transformer = dtd; 164 } else if (clazz != null) { 165 CustomTransformerDefinition ctd = new CustomTransformerDefinition(); 166 ctd.setClassName(clazz.getName()); 167 transformer = ctd; 168 } else if (beanRef != null) { 169 CustomTransformerDefinition ctd = new CustomTransformerDefinition(); 170 ctd.setRef(beanRef); 171 transformer = ctd; 172 } else { 173 throw new IllegalArgumentException("No Transformer type was specified"); 174 } 175 176 if (scheme != null) { 177 transformer.setScheme(scheme); 178 } else { 179 transformer.setFromType(from); 180 transformer.setToType(to); 181 } 182 183 camelContext.getTransformers().add(transformer); 184 } 185}