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 java.util.zip.Deflater; 020 021 import org.w3c.dom.Node; 022 023 import org.apache.camel.model.ProcessorDefinition; 024 import org.apache.camel.model.dataformat.ArtixDSContentType; 025 import org.apache.camel.model.dataformat.ArtixDSDataFormat; 026 import org.apache.camel.model.dataformat.CsvDataFormat; 027 import org.apache.camel.model.dataformat.DataFormatDefinition; 028 import org.apache.camel.model.dataformat.HL7DataFormat; 029 import org.apache.camel.model.dataformat.JaxbDataFormat; 030 import org.apache.camel.model.dataformat.JsonDataFormat; 031 import org.apache.camel.model.dataformat.RssDataFormat; 032 import org.apache.camel.model.dataformat.SerializationDataFormat; 033 import org.apache.camel.model.dataformat.StringDataFormat; 034 import org.apache.camel.model.dataformat.TidyMarkupDataFormat; 035 import org.apache.camel.model.dataformat.XMLBeansDataFormat; 036 import org.apache.camel.model.dataformat.XMLSecurityDataFormat; 037 import org.apache.camel.model.dataformat.XStreamDataFormat; 038 import org.apache.camel.model.dataformat.ZipDataFormat; 039 040 /** 041 * An expression for constructing the different possible {@link org.apache.camel.spi.DataFormat} 042 * options. 043 * 044 * @version $Revision: 751655 $ 045 */ 046 public class DataFormatClause<T extends ProcessorDefinition> { 047 private final T processorType; 048 private final Operation operation; 049 050 /** 051 * {@link org.apache.camel.spi.DataFormat} operations. 052 */ 053 public enum Operation { 054 Marshal, Unmarshal 055 } 056 057 public DataFormatClause(T processorType, Operation operation) { 058 this.processorType = processorType; 059 this.operation = operation; 060 } 061 062 /** 063 * Uses the 064 * <a href="http://camel.apache.org/artix-data-services.html">Artix Data Services</a> 065 * data format for dealing with lots of different message formats such as SWIFT etc. 066 */ 067 public T artixDS() { 068 return dataFormat(new ArtixDSDataFormat()); 069 } 070 071 /** 072 * Uses the 073 * <a href="http://camel.apache.org/artix-data-services.html">Artix Data Services</a> 074 * data format with the specified type of ComplexDataObject 075 * for marshalling and unmarshalling messages using the dataObject's default Source and Sink. 076 */ 077 public T artixDS(Class<?> dataObjectType) { 078 return dataFormat(new ArtixDSDataFormat(dataObjectType)); 079 } 080 081 082 /** 083 * Uses the 084 * <a href="http://camel.apache.org/artix-data-services.html">Artix Data Services</a> 085 * data format with the specified type of ComplexDataObject 086 * for marshalling and unmarshalling messages using the dataObject's default Source and Sink. 087 */ 088 public T artixDS(Class<?> elementType, ArtixDSContentType contentType) { 089 return dataFormat(new ArtixDSDataFormat(elementType, contentType)); 090 } 091 092 /** 093 * Uses the 094 * <a href="http://camel.apache.org/artix-data-services.html">Artix Data Services</a> 095 * data format with the specified content type 096 * for marshalling and unmarshalling messages 097 */ 098 public T artixDS(ArtixDSContentType contentType) { 099 return dataFormat(new ArtixDSDataFormat(contentType)); 100 } 101 102 /** 103 * Uses the CSV data format 104 */ 105 public T csv() { 106 return dataFormat(new CsvDataFormat()); 107 } 108 109 /** 110 * Uses the HL7 data format 111 */ 112 public T hl7() { 113 return dataFormat(new HL7DataFormat()); 114 } 115 116 /** 117 * Uses the JAXB data format 118 */ 119 public T jaxb() { 120 return dataFormat(new JaxbDataFormat()); 121 } 122 123 /** 124 * Uses the JAXB data format turning pretty printing on or off 125 */ 126 public T jaxb(boolean prettyPrint) { 127 return dataFormat(new JaxbDataFormat(prettyPrint)); 128 } 129 130 /** 131 * Uses the RSS data format 132 */ 133 public T rss() { 134 return dataFormat(new RssDataFormat()); 135 } 136 137 /** 138 * Uses the Java Serialization data format 139 */ 140 public T serialization() { 141 return dataFormat(new SerializationDataFormat()); 142 } 143 144 /** 145 * Uses the String data format 146 */ 147 public T string() { 148 return string(null); 149 } 150 151 /** 152 * Uses the String data format supporting encoding using given charset 153 */ 154 public T string(String charset) { 155 StringDataFormat sdf = new StringDataFormat(); 156 sdf.setCharset(charset); 157 return dataFormat(sdf); 158 } 159 160 /** 161 * Uses the JAXB data format 162 */ 163 public T xmlBeans() { 164 return dataFormat(new XMLBeansDataFormat()); 165 } 166 167 /** 168 * Return WellFormed HTML (an XML Document) either 169 * {@link java.lang.String} or {@link org.w3c.dom.Node} 170 */ 171 public T tidyMarkup(Class<?> dataObjectType) { 172 return dataFormat(new TidyMarkupDataFormat(dataObjectType)); 173 } 174 175 /** 176 * Return TidyMarkup in the default format 177 * as {@link org.w3c.dom.Node} 178 */ 179 public T tidyMarkup() { 180 return dataFormat(new TidyMarkupDataFormat(Node.class)); 181 } 182 183 184 /** 185 * Uses the XStream data format 186 */ 187 public T xstream() { 188 return dataFormat(new XStreamDataFormat()); 189 } 190 191 /** 192 * Uses the JSON data format 193 */ 194 public T json() { 195 return dataFormat(new JsonDataFormat()); 196 } 197 198 /** 199 * Uses the XML Security data format 200 */ 201 public T secureXML() { 202 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(); 203 return dataFormat(xsdf); 204 } 205 206 /** 207 * Uses the XML Security data format 208 */ 209 public T secureXML(String secureTag, boolean secureTagContents) { 210 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents); 211 return dataFormat(xsdf); 212 } 213 214 /** 215 * Uses the XML Security data format 216 */ 217 public T secureXML(String secureTag, boolean secureTagContents, String passPhrase) { 218 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents, passPhrase); 219 return dataFormat(xsdf); 220 } 221 222 /** 223 * Uses the XML Security data format 224 */ 225 public T secureXML(String secureTag, boolean secureTagContents, String passPhrase, String xmlCipherAlgorithm) { 226 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents, passPhrase, xmlCipherAlgorithm); 227 return dataFormat(xsdf); 228 } 229 230 /** 231 * Uses the ZIP deflater data format 232 */ 233 public T zip() { 234 ZipDataFormat zdf = new ZipDataFormat(Deflater.DEFAULT_COMPRESSION); 235 return dataFormat(zdf); 236 } 237 238 /** 239 * Uses the ZIP deflater data format 240 */ 241 public T zip(int compressionLevel) { 242 ZipDataFormat zdf = new ZipDataFormat(compressionLevel); 243 return dataFormat(zdf); 244 } 245 246 @SuppressWarnings("unchecked") 247 private T dataFormat(DataFormatDefinition dataFormatType) { 248 switch (operation) { 249 case Unmarshal: 250 return (T)processorType.unmarshal(dataFormatType); 251 case Marshal: 252 return (T)processorType.marshal(dataFormatType); 253 default: 254 throw new IllegalArgumentException("Unknown DataFormat operation: " + operation); 255 } 256 } 257 }