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 */ 017 package org.apache.camel.builder; 018 019 import org.apache.camel.model.ProcessorType; 020 import org.apache.camel.model.dataformat.ArtixDSContentType; 021 import org.apache.camel.model.dataformat.ArtixDSDataFormat; 022 import org.apache.camel.model.dataformat.CsvDataFormat; 023 import org.apache.camel.model.dataformat.DataFormatType; 024 import org.apache.camel.model.dataformat.HL7DataFormat; 025 import org.apache.camel.model.dataformat.JaxbDataFormat; 026 import org.apache.camel.model.dataformat.JsonDataFormat; 027 import org.apache.camel.model.dataformat.SerializationDataFormat; 028 import org.apache.camel.model.dataformat.StringDataFormat; 029 import org.apache.camel.model.dataformat.XMLBeansDataFormat; 030 import org.apache.camel.model.dataformat.XStreamDataFormat; 031 import org.apache.camel.spi.DataFormat; 032 033 /** 034 * An expression for constructing the different possible {@link DataFormat} 035 * options. 036 * 037 * @version $Revision: 61208 $ 038 */ 039 public class DataFormatClause<T extends ProcessorType> { 040 private final T processorType; 041 private final Operation operation; 042 043 /** 044 * {@link DataFormat} operations. 045 */ 046 public enum Operation { 047 Marshal, Unmarshal 048 } 049 050 public DataFormatClause(T processorType, Operation operation) { 051 this.processorType = processorType; 052 this.operation = operation; 053 } 054 055 /** 056 * Uses the 057 * <a href="http://activemq.apache.org/camel/artix-data-services.html">Artix Data Services</a> 058 * data format for dealing with lots of different message formats such as SWIFT etc. 059 */ 060 public T artixDS() { 061 return dataFormat(new ArtixDSDataFormat()); 062 } 063 064 /** 065 * Uses the 066 * <a href="http://activemq.apache.org/camel/artix-data-services.html">Artix Data Services</a> 067 * data format with the specified type of ComplexDataObject 068 * for marshalling and unmarshalling messages using the dataObject's default Source and Sink. 069 */ 070 public T artixDS(Class<?> dataObjectType) { 071 return dataFormat(new ArtixDSDataFormat(dataObjectType)); 072 } 073 074 075 /** 076 * Uses the 077 * <a href="http://activemq.apache.org/camel/artix-data-services.html">Artix Data Services</a> 078 * data format with the specified type of ComplexDataObject 079 * for marshalling and unmarshalling messages using the dataObject's default Source and Sink. 080 */ 081 public T artixDS(Class<?> elementType, ArtixDSContentType contentType) { 082 return dataFormat(new ArtixDSDataFormat(elementType, contentType)); 083 } 084 085 /** 086 * Uses the 087 * <a href="http://activemq.apache.org/camel/artix-data-services.html">Artix Data Services</a> 088 * data format with the specified content type 089 * for marshalling and unmarshalling messages 090 */ 091 public T artixDS(ArtixDSContentType contentType) { 092 return dataFormat(new ArtixDSDataFormat(contentType)); 093 } 094 095 /** 096 * Uses the CSV data format 097 */ 098 public T csv() { 099 return dataFormat(new CsvDataFormat()); 100 } 101 102 /** 103 * Uses the HL7 data format 104 */ 105 public T hl7() { 106 return dataFormat(new HL7DataFormat()); 107 } 108 109 /** 110 * Uses the JAXB data format 111 */ 112 public T jaxb() { 113 return dataFormat(new JaxbDataFormat()); 114 } 115 116 /** 117 * Uses the JAXB data format turning pretty printing on or off 118 */ 119 public T jaxb(boolean prettyPrint) { 120 return dataFormat(new JaxbDataFormat(prettyPrint)); 121 } 122 123 /** 124 * Uses the Java Serialization data format 125 */ 126 public T serialization() { 127 return dataFormat(new SerializationDataFormat()); 128 } 129 130 /** 131 * Uses the String data format 132 */ 133 public T string() { 134 return string(null); 135 } 136 137 /** 138 * Uses the String data format supporting encoding using given charset 139 */ 140 public T string(String charset) { 141 StringDataFormat sdf = new StringDataFormat(); 142 sdf.setCharset(charset); 143 return dataFormat(sdf); 144 } 145 146 /** 147 * Uses the JAXB data format 148 */ 149 public T xmlBeans() { 150 return dataFormat(new XMLBeansDataFormat()); 151 } 152 153 /** 154 * Uses the XStream data format 155 */ 156 public T xstream() { 157 return dataFormat(new XStreamDataFormat()); 158 } 159 160 /** 161 * Uses the JSON data format 162 */ 163 public T json() { 164 return dataFormat(new JsonDataFormat()); 165 } 166 167 private T dataFormat(DataFormatType dataFormatType) { 168 switch (operation) { 169 case Unmarshal: 170 return (T)processorType.unmarshal(dataFormatType); 171 case Marshal: 172 return (T)processorType.marshal(dataFormatType); 173 default: 174 throw new IllegalArgumentException("Unknown DataFormat operation: " + operation); 175 } 176 } 177 178 }