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