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.transformer; 018 019import javax.xml.bind.annotation.XmlAccessType; 020import javax.xml.bind.annotation.XmlAccessorType; 021import javax.xml.bind.annotation.XmlAttribute; 022import javax.xml.bind.annotation.XmlType; 023import org.apache.camel.CamelContext; 024import org.apache.camel.model.InputTypeDefinition; 025import org.apache.camel.model.OutputTypeDefinition; 026import org.apache.camel.spi.DataType; 027import org.apache.camel.spi.Metadata; 028import org.apache.camel.spi.Transformer; 029 030/** 031 * <p>Represents a {@link Transformer} which declaratively transforms message content 032 * according to the input type declared by {@link InputTypeDefinition} and/or output type 033 * declared by {@link OutputTypeDefinition}.</p> 034 * <p>If you specify from='java:com.example.ABC' and to='xml:XYZ', the transformer 035 * will be picked up when current message type is 'java:com.example.ABC' and expected 036 * message type is 'xml:XYZ'. 037 * If you specify from='java' to='xml', then it will be picked up for all of java 038 * to xml transformation. 039 * Also it's possible to specify scheme='xml' so that the transformer will be picked up 040 * for all of java to xml and xml to java transformation.</p> 041 * 042 * {@see Transformer} 043 * {@see InputTypeDefinition} 044 * {@see OutputTypeDefinition} 045 */ 046@Metadata(label = "transformation") 047@XmlType(name = "transformer") 048@XmlAccessorType(XmlAccessType.FIELD) 049public abstract class TransformerDefinition { 050 051 @XmlAttribute 052 private String scheme; 053 @XmlAttribute 054 private String fromType; 055 @XmlAttribute 056 private String toType; 057 058 public Transformer createTransformer(CamelContext context) throws Exception { 059 return doCreateTransformer(context); 060 }; 061 062 protected abstract Transformer doCreateTransformer(CamelContext context) throws Exception; 063 064 public String getScheme() { 065 return scheme; 066 } 067 068 /** 069 * Set a scheme name supported by the transformer. 070 * If you specify 'csv', the transformer will be picked up for all of 'csv' from/to 071 * Java transformation. Note that the scheme matching is performed only when 072 * no exactly matched transformer exists. 073 * 074 * @param scheme scheme name 075 */ 076 public void setScheme(String scheme) { 077 this.scheme = scheme; 078 } 079 080 public String getFromType() { 081 return fromType; 082 } 083 084 /** 085 * Set the 'from' data type name. 086 * If you specify 'xml:XYZ', the transformer will be picked up if source type is 087 * 'xml:XYZ'. If you specify just 'xml', the transformer matches with all of 088 * 'xml' source type like 'xml:ABC' or 'xml:DEF'. 089 * 090 * @param from 'from' data type name 091 */ 092 public void setFromType(String from) { 093 this.fromType = from; 094 } 095 096 /** 097 * Set the 'from' data type using Java class. 098 * 099 * @param clazz 'from' Java class 100 */ 101 public void setFromType(Class<?> clazz) { 102 this.fromType = new DataType(clazz).toString(); 103 } 104 105 public String getToType() { 106 return toType; 107 } 108 109 /** 110 * Set the 'to' data type name. 111 * If you specify 'json:XYZ', the transformer will be picked up if destination type is 112 * 'json:XYZ'. If you specify just 'json', the transformer matches with all of 113 * 'json' destination type like 'json:ABC' or 'json:DEF'. 114 * 115 * @param to 'to' data type name 116 */ 117 public void setToType(String to) { 118 this.toType = to; 119 } 120 121 /** 122 * Set the 'to' data type using Java class. 123 * 124 * @param clazz 'to' Java class 125 */ 126 public void setToType(Class<?> clazz) { 127 this.toType = new DataType(clazz).toString(); 128 } 129 130} 131