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