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 clazz '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     * @see {@code EndpointTransformerDefinition}, {@code ProcessorTransformer}
106     * 
107     * @param uri endpoint URI
108     */
109    public TransformerBuilder withUri(String uri) {
110        resetType();
111        this.uri = uri;
112        return this;
113    }
114
115    /**
116     * Set the {@code DataFormatDefinition} to be used for the {@code DataFormat} {@code Transformer}.
117     * @see {@code DataFormatTransformerDefinition}, {@code DataFormatTransformer}
118     * 
119     * @param dataFormatDefinition {@code DataFormatDefinition}
120     */
121    public TransformerBuilder withDataFormat(DataFormatDefinition dataFormatDefinition) {
122        resetType();
123        this.dataFormat = dataFormatDefinition;
124        return this;
125    }
126
127    /**
128     * Set the Java {@code Class} represents a custom {@code Transformer} implementation class.
129     * @see {@code CustomTransformerDefinition}
130     * 
131     * @param clazz {@code Class} object represents custom transformer implementation
132     */
133    public TransformerBuilder withJava(Class<? extends Transformer> clazz) {
134        resetType();
135        this.clazz = clazz;
136        return this;
137    }
138
139    /**
140     * Set the Java Bean name to be used for custom {@code Transformer}.
141     * @see {@code CustomTransformerDefinition}
142     * 
143     * @param ref bean name for the custom {@code Transformer}
144     */
145    public TransformerBuilder withBean(String ref) {
146        resetType();
147        this.beanRef = ref;
148        return this;
149    }
150
151    private void resetType() {
152        this.uri = null;
153        this.dataFormat = null;
154        this.clazz = null;
155        this.beanRef = null;
156    }
157
158    /**
159     * Configure a Transformer according to the configurations built on this builder
160     * and register it into given {@code CamelContext}.
161     * 
162     * @param camelContext {@code CamelContext}
163     */
164    public void configure(CamelContext camelContext) {
165        TransformerDefinition transformer;
166        if (uri != null) {
167            EndpointTransformerDefinition etd = new EndpointTransformerDefinition();
168            etd.setUri(uri);
169            transformer = etd;
170        } else if (dataFormat != null) {
171            DataFormatTransformerDefinition dtd = new DataFormatTransformerDefinition();
172            dtd.setDataFormatType(dataFormat);
173            transformer = dtd;
174        } else if (clazz != null) {
175            CustomTransformerDefinition ctd = new CustomTransformerDefinition();
176            ctd.setClassName(clazz.getName());
177            transformer = ctd;
178        } else if (beanRef != null) {
179            CustomTransformerDefinition ctd = new CustomTransformerDefinition();
180            ctd.setRef(beanRef);
181            transformer = ctd;
182        } else {
183            throw new IllegalArgumentException("No Transformer type was specified");
184        }
185        
186        if (scheme != null) {
187            transformer.setScheme(scheme);
188        } else {
189            transformer.setFromType(from);
190            transformer.setToType(to);
191        }
192        
193        camelContext.getTransformers().add(transformer);
194    }
195}