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