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.builder;
018
019import java.nio.charset.Charset;
020import java.util.Map;
021import java.util.zip.Deflater;
022
023import org.w3c.dom.Node;
024
025import org.apache.camel.model.DataFormatDefinition;
026import org.apache.camel.model.ProcessorDefinition;
027import org.apache.camel.model.dataformat.AvroDataFormat;
028import org.apache.camel.model.dataformat.Base64DataFormat;
029import org.apache.camel.model.dataformat.BeanioDataFormat;
030import org.apache.camel.model.dataformat.BindyDataFormat;
031import org.apache.camel.model.dataformat.BindyType;
032import org.apache.camel.model.dataformat.BoonDataFormat;
033import org.apache.camel.model.dataformat.CastorDataFormat;
034import org.apache.camel.model.dataformat.CsvDataFormat;
035import org.apache.camel.model.dataformat.CustomDataFormat;
036import org.apache.camel.model.dataformat.GzipDataFormat;
037import org.apache.camel.model.dataformat.HL7DataFormat;
038import org.apache.camel.model.dataformat.HessianDataFormat;
039import org.apache.camel.model.dataformat.IcalDataFormat;
040import org.apache.camel.model.dataformat.JacksonXMLDataFormat;
041import org.apache.camel.model.dataformat.JaxbDataFormat;
042import org.apache.camel.model.dataformat.JibxDataFormat;
043import org.apache.camel.model.dataformat.JsonDataFormat;
044import org.apache.camel.model.dataformat.JsonLibrary;
045import org.apache.camel.model.dataformat.LZFDataFormat;
046import org.apache.camel.model.dataformat.MimeMultipartDataFormat;
047import org.apache.camel.model.dataformat.PGPDataFormat;
048import org.apache.camel.model.dataformat.ProtobufDataFormat;
049import org.apache.camel.model.dataformat.RssDataFormat;
050import org.apache.camel.model.dataformat.SerializationDataFormat;
051import org.apache.camel.model.dataformat.SoapJaxbDataFormat;
052import org.apache.camel.model.dataformat.StringDataFormat;
053import org.apache.camel.model.dataformat.SyslogDataFormat;
054import org.apache.camel.model.dataformat.TidyMarkupDataFormat;
055import org.apache.camel.model.dataformat.XMLBeansDataFormat;
056import org.apache.camel.model.dataformat.XMLSecurityDataFormat;
057import org.apache.camel.model.dataformat.XStreamDataFormat;
058import org.apache.camel.model.dataformat.XmlJsonDataFormat;
059import org.apache.camel.model.dataformat.YAMLDataFormat;
060import org.apache.camel.model.dataformat.YAMLLibrary;
061import org.apache.camel.model.dataformat.ZipDataFormat;
062import org.apache.camel.model.dataformat.ZipFileDataFormat;
063import org.apache.camel.util.CollectionStringBuffer;
064import org.apache.camel.util.jsse.KeyStoreParameters;
065
066/**
067 * An expression for constructing the different possible {@link org.apache.camel.spi.DataFormat}
068 * options.
069 *
070 * @version 
071 */
072public class DataFormatClause<T extends ProcessorDefinition<?>> {
073    private final T processorType;
074    private final Operation operation;
075
076    /**
077     * {@link org.apache.camel.spi.DataFormat} operations.
078     */
079    public enum Operation {
080        Marshal, Unmarshal
081    }
082
083    public DataFormatClause(T processorType, Operation operation) {
084        this.processorType = processorType;
085        this.operation = operation;
086    }
087
088    /**
089     * Uses the Avro data format
090     */
091    public T avro() {
092        return dataFormat(new AvroDataFormat());
093    }
094
095    public T avro(Object schema) {
096        AvroDataFormat dataFormat = new AvroDataFormat();
097        dataFormat.setSchema(schema);
098        return dataFormat(dataFormat);
099    }
100
101    public T avro(String instanceClassName) {
102        return dataFormat(new AvroDataFormat(instanceClassName));
103    }
104
105    /**
106     * Uses the base64 data format
107     */
108    public T base64() {
109        Base64DataFormat dataFormat = new Base64DataFormat();
110        return dataFormat(dataFormat);
111    }
112
113    /**
114     * Uses the base64 data format
115     */
116    public T base64(int lineLength, String lineSeparator, boolean urlSafe) {
117        Base64DataFormat dataFormat = new Base64DataFormat();
118        dataFormat.setLineLength(lineLength);
119        dataFormat.setLineSeparator(lineSeparator);
120        dataFormat.setUrlSafe(urlSafe);
121        return dataFormat(dataFormat);
122    }
123
124    /**
125     * Uses the beanio data format
126     */
127    public T beanio(String mapping, String streamName) {
128        BeanioDataFormat dataFormat = new BeanioDataFormat();
129        dataFormat.setMapping(mapping);
130        dataFormat.setStreamName(streamName);
131        return dataFormat(dataFormat);
132    }
133
134    /**
135     * Uses the beanio data format
136     */
137    public T beanio(String mapping, String streamName, String encoding) {
138        BeanioDataFormat dataFormat = new BeanioDataFormat();
139        dataFormat.setMapping(mapping);
140        dataFormat.setStreamName(streamName);
141        dataFormat.setEncoding(encoding);
142        return dataFormat(dataFormat);
143    }
144
145    /**
146     * Uses the beanio data format
147     */
148    public T beanio(String mapping, String streamName, String encoding,
149                    boolean ignoreUnidentifiedRecords, boolean ignoreUnexpectedRecords, boolean ignoreInvalidRecords) {
150        BeanioDataFormat dataFormat = new BeanioDataFormat();
151        dataFormat.setMapping(mapping);
152        dataFormat.setStreamName(streamName);
153        dataFormat.setEncoding(encoding);
154        dataFormat.setIgnoreUnidentifiedRecords(ignoreUnidentifiedRecords);
155        dataFormat.setIgnoreUnexpectedRecords(ignoreUnexpectedRecords);
156        dataFormat.setIgnoreInvalidRecords(ignoreInvalidRecords);
157        return dataFormat(dataFormat);
158    }
159    
160    /**
161     * Uses the Bindy data format
162     *
163     * @param type      the type of bindy data format to use
164     * @param classType the POJO class type
165     */
166    public T bindy(BindyType type, Class<?> classType) {
167        BindyDataFormat bindy = new BindyDataFormat();
168        bindy.setType(type);
169        bindy.setClassType(classType);
170        return dataFormat(bindy);
171    }
172
173    /**
174     * Uses the Boon data format
175     *
176     * @param classType the POJO class type
177     */
178    public T boon(Class<?> classType) {
179        BoonDataFormat boon = new BoonDataFormat();
180        boon.setUnmarshalType(classType);
181        return dataFormat(boon);
182    }
183
184    /**
185     * Uses the CSV data format
186     */
187    public T csv() {
188        return dataFormat(new CsvDataFormat());
189    }
190
191    /**
192     * Uses the CSV data format for a huge file.
193     * Sequential access through an iterator.
194     */
195    public T csvLazyLoad() {
196        return dataFormat(new CsvDataFormat(true));
197    }
198
199    /**
200     * Uses the custom data format
201     */
202    public T custom(String ref) {
203        return dataFormat(new CustomDataFormat(ref));
204    }
205
206    /**
207     * Uses the Castor data format
208     */
209    public T castor() {
210        return dataFormat(new CastorDataFormat());
211    }
212
213    /**
214     * Uses the Castor data format
215     *
216     * @param mappingFile name of mapping file to locate in classpath
217     */
218    public T castor(String mappingFile) {
219        CastorDataFormat castor = new CastorDataFormat();
220        castor.setMappingFile(mappingFile);
221        return dataFormat(castor);
222    }
223
224    /**
225     * Uses the Castor data format
226     *
227     * @param mappingFile name of mapping file to locate in classpath
228     * @param validation  whether validation is enabled or not
229     */
230    public T castor(String mappingFile, boolean validation) {
231        CastorDataFormat castor = new CastorDataFormat();
232        castor.setMappingFile(mappingFile);
233        castor.setValidation(validation);
234        return dataFormat(castor);
235    }
236
237    /**
238     * Uses the GZIP deflater data format
239     */
240    public T gzip() {
241        GzipDataFormat gzdf = new GzipDataFormat();
242        return dataFormat(gzdf);
243    }
244
245    /**
246     * Uses the Hessian data format
247     */
248    public T hessian() {
249        return dataFormat(new HessianDataFormat());
250    }
251
252    /**
253     * Uses the HL7 data format
254     */
255    public T hl7() {
256        return dataFormat(new HL7DataFormat());
257    }
258
259    /**
260     * Uses the HL7 data format
261     */
262    public T hl7(boolean validate) {
263        HL7DataFormat hl7 = new HL7DataFormat();
264        hl7.setValidate(validate);
265        return dataFormat(hl7);
266    }
267    
268    /**
269     * Uses the HL7 data format
270     */
271    public T hl7(Object parser) {
272        HL7DataFormat hl7 = new HL7DataFormat();
273        hl7.setParser(parser);
274        return dataFormat(hl7);
275    }
276
277    /**
278     * Uses the iCal data format
279     */
280    public T ical(boolean validating) {
281        IcalDataFormat ical = new IcalDataFormat();
282        ical.setValidating(validating);
283        return dataFormat(ical);
284    }
285
286    /**
287     * Uses the LZF deflater data format
288     */
289    public T lzf() {
290        LZFDataFormat lzfdf = new LZFDataFormat();
291        return dataFormat(lzfdf);
292    }
293
294    /**
295     * Uses the MIME Multipart data format
296     */
297    public T mimeMultipart() {
298        MimeMultipartDataFormat mm = new MimeMultipartDataFormat();
299        return dataFormat(mm);
300    }
301
302    /**
303     * Uses the MIME Multipart data format
304     *
305     * @param multipartSubType Specifies the subtype of the MIME Multipart
306     */
307    public T mimeMultipart(String multipartSubType) {
308        MimeMultipartDataFormat mm = new MimeMultipartDataFormat();
309        mm.setMultipartSubType(multipartSubType);
310        return dataFormat(mm);
311    }
312
313    /**
314     * Uses the MIME Multipart data format
315     *
316     * @param multipartSubType           the subtype of the MIME Multipart
317     * @param multipartWithoutAttachment defines whether a message without attachment is also marshaled
318     *                                   into a MIME Multipart (with only one body part).
319     * @param headersInline              define the MIME Multipart headers as part of the message body
320     *                                   or as Camel headers
321     * @param binaryContent              have binary encoding for binary content (true) or use Base-64
322     *                                   encoding for binary content (false)
323     */
324    public T mimeMultipart(String multipartSubType, boolean multipartWithoutAttachment, boolean headersInline,
325                           boolean binaryContent) {
326        MimeMultipartDataFormat mm = new MimeMultipartDataFormat();
327        mm.setMultipartSubType(multipartSubType);
328        mm.setMultipartWithoutAttachment(multipartWithoutAttachment);
329        mm.setHeadersInline(headersInline);
330        mm.setBinaryContent(binaryContent);
331        return dataFormat(mm);
332    }
333
334    /**
335     * Uses the MIME Multipart data format
336     *
337     * @param multipartSubType           the subtype of the MIME Multipart
338     * @param multipartWithoutAttachment defines whether a message without attachment is also marshaled
339     *                                   into a MIME Multipart (with only one body part).
340     * @param headersInline              define the MIME Multipart headers as part of the message body
341     *                                   or as Camel headers
342     * @param includeHeadeers            if headersInline is set to true all camel headers matching this
343     *                                   regex are also stored as MIME headers on the Multipart
344     * @param binaryContent              have binary encoding for binary content (true) or use Base-64
345     *                                   encoding for binary content (false)
346     */
347    public T mimeMultipart(String multipartSubType, boolean multipartWithoutAttachment, boolean headersInline,
348                           String includeHeaders, boolean binaryContent) {
349        MimeMultipartDataFormat mm = new MimeMultipartDataFormat();
350        mm.setMultipartSubType(multipartSubType);
351        mm.setMultipartWithoutAttachment(multipartWithoutAttachment);
352        mm.setHeadersInline(headersInline);
353        mm.setIncludeHeaders(includeHeaders);
354        mm.setBinaryContent(binaryContent);
355        return dataFormat(mm);
356    }
357
358    /**
359     * Uses the MIME Multipart data format
360     *
361     * @param multipartWithoutAttachment defines whether a message without attachment is also marshaled
362     *                                   into a MIME Multipart (with only one body part).
363     * @param headersInline              define the MIME Multipart headers as part of the message body
364     *                                   or as Camel headers
365     * @param binaryContent              have binary encoding for binary content (true) or use Base-64
366     *                                   encoding for binary content (false)
367     */
368    public T mimeMultipart(boolean multipartWithoutAttachment, boolean headersInline,
369                           boolean binaryContent) {
370        MimeMultipartDataFormat mm = new MimeMultipartDataFormat();
371        mm.setMultipartWithoutAttachment(multipartWithoutAttachment);
372        mm.setHeadersInline(headersInline);
373        mm.setBinaryContent(binaryContent);
374        return dataFormat(mm);
375    }
376
377    /**
378     * Uses the PGP data format
379     */
380    public T pgp(String keyFileName, String keyUserid) {
381        PGPDataFormat pgp = new PGPDataFormat();
382        pgp.setKeyFileName(keyFileName);
383        pgp.setKeyUserid(keyUserid);
384        return dataFormat(pgp);
385    }
386
387    /**
388     * Uses the PGP data format
389     */
390    public T pgp(String keyFileName, String keyUserid, String password) {
391        PGPDataFormat pgp = new PGPDataFormat();
392        pgp.setKeyFileName(keyFileName);
393        pgp.setKeyUserid(keyUserid);
394        pgp.setPassword(password);
395        return dataFormat(pgp);
396    }
397
398    /**
399     * Uses the PGP data format
400     */
401    public T pgp(String keyFileName, String keyUserid, String password, boolean armored, boolean integrity) {
402        PGPDataFormat pgp = new PGPDataFormat();
403        pgp.setKeyFileName(keyFileName);
404        pgp.setKeyUserid(keyUserid);
405        pgp.setPassword(password);
406        pgp.setArmored(armored);
407        pgp.setIntegrity(integrity);
408        return dataFormat(pgp);
409    }
410    
411    /**
412     * Uses the Jackson XML data format
413     */
414    public T jacksonxml() {
415        return dataFormat(new JacksonXMLDataFormat());
416    }
417
418    /**
419     * Uses the Jackson XML data format
420     *
421     * @param unmarshalType
422     *            unmarshal type for xml jackson type
423     */
424    public T jacksonxml(Class<?> unmarshalType) {
425        JacksonXMLDataFormat jacksonXMLDataFormat = new JacksonXMLDataFormat();
426        jacksonXMLDataFormat.setUnmarshalType(unmarshalType);
427        return dataFormat(jacksonXMLDataFormat);
428    }
429
430    /**
431     * Uses the Jackson XML data format
432     *
433     * @param unmarshalType
434     *            unmarshal type for xml jackson type
435     * @param jsonView
436     *            the view type for xml jackson type
437     */
438    public T jacksonxml(Class<?> unmarshalType, Class<?> jsonView) {
439        JacksonXMLDataFormat jacksonXMLDataFormat = new JacksonXMLDataFormat();
440        jacksonXMLDataFormat.setUnmarshalType(unmarshalType);
441        jacksonXMLDataFormat.setJsonView(jsonView);
442        return dataFormat(jacksonXMLDataFormat);
443    }
444
445    /**
446     * Uses the Jackson XML data format using the Jackson library turning pretty
447     * printing on or off
448     * 
449     * @param prettyPrint
450     *            turn pretty printing on or off
451     */
452    public T jacksonxml(boolean prettyPrint) {
453        JacksonXMLDataFormat jacksonXMLDataFormat = new JacksonXMLDataFormat();
454        jacksonXMLDataFormat.setPrettyPrint(prettyPrint);
455        return dataFormat(jacksonXMLDataFormat);
456    }
457
458    /**
459     * Uses the Jackson XML data format
460     *
461     * @param unmarshalType
462     *            unmarshal type for xml jackson type
463     * @param prettyPrint
464     *            turn pretty printing on or off
465     */
466    public T jacksonxml(Class<?> unmarshalType, boolean prettyPrint) {
467        JacksonXMLDataFormat jacksonXMLDataFormat = new JacksonXMLDataFormat();
468        jacksonXMLDataFormat.setUnmarshalType(unmarshalType);
469        jacksonXMLDataFormat.setPrettyPrint(prettyPrint);
470        return dataFormat(jacksonXMLDataFormat);
471    }
472
473    /**
474     * Uses the Jackson XML data format
475     *
476     * @param unmarshalType
477     *            unmarshal type for xml jackson type
478     * @param jsonView
479     *            the view type for xml jackson type
480     * @param prettyPrint
481     *            turn pretty printing on or off
482     */
483    public T jacksonxml(Class<?> unmarshalType, Class<?> jsonView, boolean prettyPrint) {
484        JacksonXMLDataFormat jacksonXMLDataFormat = new JacksonXMLDataFormat();
485        jacksonXMLDataFormat.setUnmarshalType(unmarshalType);
486        jacksonXMLDataFormat.setJsonView(jsonView);
487        jacksonXMLDataFormat.setPrettyPrint(prettyPrint);
488        return dataFormat(jacksonXMLDataFormat);
489    }
490
491    /**
492     * Uses the Jackson XML data format
493     *
494     * @param unmarshalType
495     *            unmarshal type for xml jackson type
496     * @param jsonView
497     *            the view type for xml jackson type
498     * @param include
499     *            include such as <tt>ALWAYS</tt>, <tt>NON_NULL</tt>, etc.
500     */
501    public T jacksonxml(Class<?> unmarshalType, Class<?> jsonView, String include) {
502        JacksonXMLDataFormat jacksonXMLDataFormat = new JacksonXMLDataFormat();
503        jacksonXMLDataFormat.setUnmarshalType(unmarshalType);
504        jacksonXMLDataFormat.setJsonView(jsonView);
505        jacksonXMLDataFormat.setInclude(include);
506        return dataFormat(jacksonXMLDataFormat);
507    }
508
509    /**
510     * Uses the Jackson XML data format
511     *
512     * @param unmarshalType
513     *            unmarshal type for xml jackson type
514     * @param jsonView
515     *            the view type for xml jackson type
516     * @param include
517     *            include such as <tt>ALWAYS</tt>, <tt>NON_NULL</tt>, etc.
518     * @param prettyPrint
519     *            turn pretty printing on or off
520     */
521    public T jacksonxml(Class<?> unmarshalType, Class<?> jsonView, String include, boolean prettyPrint) {
522        JacksonXMLDataFormat jacksonXMLDataFormat = new JacksonXMLDataFormat();
523        jacksonXMLDataFormat.setUnmarshalType(unmarshalType);
524        jacksonXMLDataFormat.setJsonView(jsonView);
525        jacksonXMLDataFormat.setInclude(include);
526        jacksonXMLDataFormat.setPrettyPrint(prettyPrint);
527        return dataFormat(jacksonXMLDataFormat);
528    }
529
530    /**
531     * Uses the JAXB data format
532     */
533    public T jaxb() {
534        return dataFormat(new JaxbDataFormat());
535    }
536
537    /**
538     * Uses the JAXB data format with context path
539     */
540    public T jaxb(String contextPath) {
541        JaxbDataFormat dataFormat = new JaxbDataFormat();
542        dataFormat.setContextPath(contextPath);
543        return dataFormat(dataFormat);
544    }
545
546    /**
547     * Uses the JAXB data format turning pretty printing on or off
548     */
549    public T jaxb(boolean prettyPrint) {
550        return dataFormat(new JaxbDataFormat(prettyPrint));
551    }
552
553    /**
554     * Uses the JiBX data format.
555     */
556    public T jibx() {
557        return dataFormat(new JibxDataFormat());
558    }
559
560    /**
561     * Uses the JiBX data format with unmarshall class.
562     */
563    public T jibx(Class<?> unmarshallClass) {
564        return dataFormat(new JibxDataFormat(unmarshallClass));
565    }
566
567    /**
568     * Uses the JSON data format using the XStream json library
569     */
570    public T json() {
571        return dataFormat(new JsonDataFormat());
572    }
573
574    /**
575     * Uses the JSON data format using the XStream json library turning pretty printing on or off
576     * 
577     * @param prettyPrint turn pretty printing on or off
578     */
579    public T json(boolean prettyPrint) {
580        JsonDataFormat json = new JsonDataFormat();
581        json.setPrettyPrint(prettyPrint);
582        return dataFormat(json);
583    }
584
585    /**
586     * Uses the JSON data format
587     *
588     * @param library the json library to use
589     */
590    public T json(JsonLibrary library) {
591        return dataFormat(new JsonDataFormat(library));
592    }
593
594    /**
595     * Uses the JSON data format
596     *
597     * @param library     the json library to use
598     * @param prettyPrint turn pretty printing on or off
599     */
600    public T json(JsonLibrary library, boolean prettyPrint) {
601        JsonDataFormat json = new JsonDataFormat(library);
602        json.setPrettyPrint(prettyPrint);
603        return dataFormat(json);
604    }
605
606    /**
607     * Uses the JSON data format
608     *
609     * @param type          the json type to use
610     * @param unmarshalType unmarshal type for json jackson type
611     */
612    public T json(JsonLibrary type, Class<?> unmarshalType) {
613        JsonDataFormat json = new JsonDataFormat(type);
614        json.setUnmarshalType(unmarshalType);
615        return dataFormat(json);
616    }
617
618    /**
619     * Uses the JSON data format
620     *
621     * @param type          the json type to use
622     * @param unmarshalType unmarshal type for json jackson type
623     * @param prettyPrint   turn pretty printing on or off
624     */
625    public T json(JsonLibrary type, Class<?> unmarshalType, boolean prettyPrint) {
626        JsonDataFormat json = new JsonDataFormat(type);
627        json.setUnmarshalType(unmarshalType);
628        json.setPrettyPrint(prettyPrint);
629        return dataFormat(json);
630    }
631
632    /**
633     * Uses the Jackson JSON data format
634     *
635     * @param unmarshalType unmarshal type for json jackson type
636     * @param jsonView      the view type for json jackson type
637     */
638    public T json(Class<?> unmarshalType, Class<?> jsonView) {
639        JsonDataFormat json = new JsonDataFormat(JsonLibrary.Jackson);
640        json.setUnmarshalType(unmarshalType);
641        json.setJsonView(jsonView);
642        return dataFormat(json);
643    }
644
645    /**
646     * Uses the Jackson JSON data format
647     *
648     * @param unmarshalType unmarshal type for json jackson type
649     * @param jsonView      the view type for json jackson type
650     * @param prettyPrint   turn pretty printing on or off
651     */
652    public T json(Class<?> unmarshalType, Class<?> jsonView, boolean prettyPrint) {
653        JsonDataFormat json = new JsonDataFormat(JsonLibrary.Jackson);
654        json.setUnmarshalType(unmarshalType);
655        json.setJsonView(jsonView);
656        json.setPrettyPrint(prettyPrint);
657        return dataFormat(json);
658    }
659
660    /**
661     * Uses the Jackson JSON data format
662     *
663     * @param unmarshalType unmarshal type for json jackson type
664     * @param jsonView      the view type for json jackson type
665     * @param include       include such as <tt>ALWAYS</tt>, <tt>NON_NULL</tt>, etc.
666     */
667    public T json(Class<?> unmarshalType, Class<?> jsonView, String include) {
668        JsonDataFormat json = new JsonDataFormat(JsonLibrary.Jackson);
669        json.setUnmarshalType(unmarshalType);
670        json.setJsonView(jsonView);
671        json.setInclude(include);
672        return dataFormat(json);
673    }
674
675    /**
676     * Uses the Jackson JSON data format
677     *
678     * @param unmarshalType unmarshal type for json jackson type
679     * @param jsonView      the view type for json jackson type
680     * @param include       include such as <tt>ALWAYS</tt>, <tt>NON_NULL</tt>, etc.
681      * @param prettyPrint  turn pretty printing on or off
682     */
683    public T json(Class<?> unmarshalType, Class<?> jsonView, String include, boolean prettyPrint) {
684        JsonDataFormat json = new JsonDataFormat(JsonLibrary.Jackson);
685        json.setUnmarshalType(unmarshalType);
686        json.setJsonView(jsonView);
687        json.setInclude(include);
688        json.setPrettyPrint(prettyPrint);
689        return dataFormat(json);
690    }
691
692    /**
693     * Uses the protobuf data format
694     */
695    public T protobuf() {
696        return dataFormat(new ProtobufDataFormat());
697    }
698
699    public T protobuf(Object defaultInstance) {
700        ProtobufDataFormat dataFormat = new ProtobufDataFormat();
701        dataFormat.setDefaultInstance(defaultInstance);
702        return dataFormat(dataFormat);
703    }
704
705    public T protobuf(String instanceClassName) {
706        return dataFormat(new ProtobufDataFormat(instanceClassName));
707    }
708
709    /**
710     * Uses the RSS data format
711     */
712    public T rss() {
713        return dataFormat(new RssDataFormat());
714    }
715
716    /**
717     * Uses the Java Serialization data format
718     */
719    public T serialization() {
720        return dataFormat(new SerializationDataFormat());
721    }
722
723    /**
724     * Uses the Soap 1.1 JAXB data format
725     */
726    public T soapjaxb() {
727        return dataFormat(new SoapJaxbDataFormat());
728    }
729
730    /**
731     * Uses the Soap 1.1 JAXB data format
732     */
733    public T soapjaxb(String contextPath) {
734        return dataFormat(new SoapJaxbDataFormat(contextPath));
735    }
736
737    /**
738     * Uses the Soap 1.1 JAXB data format
739     */
740    public T soapjaxb(String contextPath, String elementNameStrategyRef) {
741        return dataFormat(new SoapJaxbDataFormat(contextPath, elementNameStrategyRef));
742    }
743
744    /**
745     * Uses the Soap 1.1 JAXB data format
746     */
747    public T soapjaxb(String contextPath, Object elementNameStrategy) {
748        return dataFormat(new SoapJaxbDataFormat(contextPath, elementNameStrategy));
749    }
750
751    /**
752     * Uses the Soap 1.2 JAXB data format
753     */
754    public T soapjaxb12() {
755        SoapJaxbDataFormat soap = new SoapJaxbDataFormat();
756        soap.setVersion("1.2");
757        return dataFormat(soap);
758    }
759
760    /**
761     * Uses the Soap 1.2 JAXB data format
762     */
763    public T soapjaxb12(String contextPath) {
764        SoapJaxbDataFormat soap = new SoapJaxbDataFormat(contextPath);
765        soap.setVersion("1.2");
766        return dataFormat(soap);
767    }
768
769    /**
770     * Uses the Soap 1.2 JAXB data format
771     */
772    public T soapjaxb12(String contextPath, String elementNameStrategyRef) {
773        SoapJaxbDataFormat soap = new SoapJaxbDataFormat(contextPath, elementNameStrategyRef);
774        soap.setVersion("1.2");
775        return dataFormat(soap);
776    }
777
778    /**
779     * Uses the Soap JAXB data format
780     */
781    public T soapjaxb12(String contextPath, Object elementNameStrategy) {
782        SoapJaxbDataFormat soap = new SoapJaxbDataFormat(contextPath, elementNameStrategy);
783        soap.setVersion("1.2");
784        return dataFormat(soap);
785    }
786
787    /**
788     * Uses the String data format
789     */
790    public T string() {
791        return string(null);
792    }
793
794    /**
795     * Uses the String data format supporting encoding using given charset
796     */
797    public T string(String charset) {
798        StringDataFormat sdf = new StringDataFormat();
799        sdf.setCharset(charset);
800        return dataFormat(sdf);
801    }
802
803    /**
804     * Uses the Syslog data format
805     */
806    public T syslog() {
807        return dataFormat(new SyslogDataFormat());
808    }
809
810    /**
811     * Return WellFormed HTML (an XML Document) either
812     * {@link java.lang.String} or {@link org.w3c.dom.Node}
813     */
814    public T tidyMarkup(Class<?> dataObjectType) {
815        return dataFormat(new TidyMarkupDataFormat(dataObjectType));
816    }
817
818    /**
819     * Return TidyMarkup in the default format
820     * as {@link org.w3c.dom.Node}
821     */
822    public T tidyMarkup() {
823        return dataFormat(new TidyMarkupDataFormat(Node.class));
824    }
825
826    /**
827     * Uses the XStream data format.
828     * <p/>
829     * Favor using {@link #xstream(String)} to pass in a permission
830     */
831    public T xstream() {
832        return dataFormat(new XStreamDataFormat());
833    }
834
835    /**
836     * Uses the xstream by setting the encoding or permission
837     *
838     * @param encodingOrPermission is either an encoding or permission syntax
839     */
840    public T xstream(String encodingOrPermission) {
841        // is it an encoding? if not we assume its a permission
842        if (Charset.isSupported(encodingOrPermission)) {
843            return xstream(encodingOrPermission, (String) null);
844        } else {
845            return xstream(null, encodingOrPermission);
846        }
847    }
848
849    /**
850     * Uses the xstream by setting the encoding
851     */
852    public T xstream(String encoding, String permission) {
853        XStreamDataFormat xdf = new XStreamDataFormat();
854        xdf.setPermissions(permission);
855        xdf.setEncoding(encoding);
856        return dataFormat(xdf);
857    }
858
859    /**
860     * Uses the xstream by permitting the java type
861     *
862     * @param type the pojo xstream should use as allowed permission
863     */
864    public T xstream(Class<?> type) {
865        return xstream(null, type);
866    }
867
868    /**
869     * Uses the xstream by permitting the java type
870     *
871     * @param encoding encoding to use
872     * @param type the pojo class(es) xstream should use as allowed permission
873     */
874    public T xstream(String encoding, Class<?>... type) {
875        CollectionStringBuffer csb = new CollectionStringBuffer(",");
876        for (Class<?> clazz : type) {
877            csb.append("+");
878            csb.append(clazz.getName());
879        }
880        return xstream(encoding, csb.toString());
881    }
882
883    /**
884     * Uses the YAML data format
885     *
886     * @param library the yaml library to use
887     */
888    public T yaml(YAMLLibrary library) {
889        return dataFormat(new YAMLDataFormat(library));
890    }
891
892    /**
893     * Uses the YAML data format
894     *
895     * @param type          the yaml type to use
896     * @param type          the type for json snakeyaml type
897     */
898    public T yaml(YAMLLibrary library, Class<?> type) {
899        return dataFormat(new YAMLDataFormat(library, type));
900    }
901
902    /**
903     * Uses the XML Security data format
904     */
905    public T secureXML() {
906        XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat();
907        return dataFormat(xsdf);
908    }
909
910    /**
911     * Uses the XML Security data format
912     */
913    public T secureXML(String secureTag, boolean secureTagContents) {
914        XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents);
915        return dataFormat(xsdf);
916    }
917    
918    /**
919     * Uses the XML Security data format
920     */
921    public T secureXML(String secureTag, Map<String, String> namespaces, boolean secureTagContents) {
922        XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, namespaces, secureTagContents);
923        return dataFormat(xsdf);
924    }
925
926    /**
927     * Uses the XML Security data format
928     */
929    public T secureXML(String secureTag, boolean secureTagContents, String passPhrase) {
930        XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents, passPhrase);
931        return dataFormat(xsdf);
932    }
933    
934    /**
935     * Uses the XML Security data format
936     */
937    public T secureXML(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String passPhrase) {
938        XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, namespaces, secureTagContents, passPhrase);
939        return dataFormat(xsdf);
940    }
941    
942    /**
943     * Uses the XML Security data format
944     */
945    public T secureXML(String secureTag, boolean secureTagContents, String passPhrase, String xmlCipherAlgorithm) {
946        XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents, passPhrase, xmlCipherAlgorithm);
947        return dataFormat(xsdf);
948    }
949    
950    
951    /**
952     * Uses the XML Security data format
953     */
954    public T secureXML(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String passPhrase, String xmlCipherAlgorithm) {
955        XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, namespaces, secureTagContents, passPhrase, xmlCipherAlgorithm);
956        return dataFormat(xsdf);
957    }
958    
959    /**
960     * @deprecated Use {@link #secureXML(String, Map, boolean, String, String, String, String)} instead.
961     * Uses the XML Security data format
962     */
963    @Deprecated
964    public T secureXML(String secureTag, boolean secureTagContents, String recipientKeyAlias, String xmlCipherAlgorithm, 
965            String keyCipherAlgorithm) {
966        XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents, recipientKeyAlias, xmlCipherAlgorithm, keyCipherAlgorithm);
967        return dataFormat(xsdf);
968    }
969    
970    /**
971     * Uses the XML Security data format
972     */
973    public T secureXML(String secureTag, boolean secureTagContents, String recipientKeyAlias, String xmlCipherAlgorithm, 
974            String keyCipherAlgorithm, String keyOrTrustStoreParametersId) {
975        XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents, recipientKeyAlias, xmlCipherAlgorithm, 
976            keyCipherAlgorithm, keyOrTrustStoreParametersId);
977        return dataFormat(xsdf);
978    }
979    
980    /**
981     * Uses the XML Security data format
982     */
983    public T secureXML(String secureTag, boolean secureTagContents, String recipientKeyAlias, String xmlCipherAlgorithm, 
984            String keyCipherAlgorithm, String keyOrTrustStoreParametersId, String keyPassword) {
985        XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents, recipientKeyAlias, xmlCipherAlgorithm, 
986            keyCipherAlgorithm, keyOrTrustStoreParametersId, keyPassword);
987        return dataFormat(xsdf);
988    }    
989    
990    /**
991     * Uses the XML Security data format
992     */
993    public T secureXML(String secureTag, boolean secureTagContents, String recipientKeyAlias, String xmlCipherAlgorithm, 
994            String keyCipherAlgorithm, KeyStoreParameters keyOrTrustStoreParameters) {
995        XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents, recipientKeyAlias, xmlCipherAlgorithm, 
996            keyCipherAlgorithm, keyOrTrustStoreParameters);
997        return dataFormat(xsdf);
998    }
999    
1000    /**
1001     * Uses the XML Security data format
1002     */
1003    public T secureXML(String secureTag, boolean secureTagContents, String recipientKeyAlias, String xmlCipherAlgorithm, 
1004            String keyCipherAlgorithm, KeyStoreParameters keyOrTrustStoreParameters, String keyPassword) {
1005        XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents, recipientKeyAlias, xmlCipherAlgorithm, 
1006            keyCipherAlgorithm, keyOrTrustStoreParameters, keyPassword);
1007        return dataFormat(xsdf);
1008    }    
1009    
1010    /**
1011     * Uses the XML Security data format
1012     */
1013    public T secureXML(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String recipientKeyAlias, 
1014            String xmlCipherAlgorithm, String keyCipherAlgorithm, String keyOrTrustStoreParametersId) {
1015        XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, namespaces, secureTagContents, recipientKeyAlias, xmlCipherAlgorithm, 
1016                keyCipherAlgorithm, keyOrTrustStoreParametersId);
1017        return dataFormat(xsdf);
1018    }
1019    
1020    /**
1021     * Uses the XML Security data format
1022     */
1023    public T secureXML(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String recipientKeyAlias, 
1024            String xmlCipherAlgorithm, String keyCipherAlgorithm, String keyOrTrustStoreParametersId, String keyPassword) {
1025        XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, namespaces, secureTagContents, recipientKeyAlias, xmlCipherAlgorithm, 
1026                keyCipherAlgorithm, keyOrTrustStoreParametersId, keyPassword);
1027        return dataFormat(xsdf);
1028    }    
1029    
1030    /**
1031     * Uses the XML Security data format
1032     */
1033    public T secureXML(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String recipientKeyAlias, 
1034            String xmlCipherAlgorithm, String keyCipherAlgorithm, KeyStoreParameters keyOrTrustStoreParameters) {
1035        XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, namespaces, secureTagContents, recipientKeyAlias, xmlCipherAlgorithm, 
1036                keyCipherAlgorithm, keyOrTrustStoreParameters);
1037        return dataFormat(xsdf);
1038    }
1039    
1040    /**
1041     * Uses the XML Security data format
1042     */
1043    public T secureXML(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String recipientKeyAlias, 
1044            String xmlCipherAlgorithm, String keyCipherAlgorithm, KeyStoreParameters keyOrTrustStoreParameters, String keyPassword) {
1045        XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, namespaces, secureTagContents, recipientKeyAlias, xmlCipherAlgorithm, 
1046                keyCipherAlgorithm, keyOrTrustStoreParameters, keyPassword);
1047        return dataFormat(xsdf);
1048    }   
1049    
1050    /**
1051     * Uses the XML Security data format
1052     */
1053    public T secureXML(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String recipientKeyAlias, 
1054            String xmlCipherAlgorithm, String keyCipherAlgorithm, KeyStoreParameters keyOrTrustStoreParameters, String keyPassword,
1055            String digestAlgorithm) {
1056        XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, namespaces, secureTagContents, recipientKeyAlias, xmlCipherAlgorithm, 
1057                keyCipherAlgorithm, keyOrTrustStoreParameters, keyPassword, digestAlgorithm);
1058        return dataFormat(xsdf);
1059    }   
1060    
1061    /**
1062     * Uses the xmlBeans data format
1063     */
1064    public T xmlBeans() {
1065        return dataFormat(new XMLBeansDataFormat());
1066    }
1067
1068    /**
1069     * Uses the xmljson dataformat, based on json-lib
1070     */
1071    public T xmljson() {
1072        return dataFormat(new XmlJsonDataFormat());
1073    }
1074    
1075    /**
1076     * Uses the xmljson dataformat, based on json-lib, initializing custom options with a Map
1077     */
1078    public T xmljson(Map<String, String> options) {
1079        return dataFormat(new XmlJsonDataFormat(options));
1080    }
1081    
1082    /**
1083     * Uses the ZIP deflater data format
1084     */
1085    public T zip() {
1086        ZipDataFormat zdf = new ZipDataFormat(Deflater.DEFAULT_COMPRESSION);
1087        return dataFormat(zdf);
1088    }
1089
1090    /**
1091     * Uses the ZIP deflater data format
1092     */
1093    public T zip(int compressionLevel) {
1094        ZipDataFormat zdf = new ZipDataFormat(compressionLevel);
1095        return dataFormat(zdf);
1096    }
1097
1098    /**
1099     * Uses the ZIP file data format
1100     */
1101    public T zipFile() {
1102        ZipFileDataFormat zfdf = new ZipFileDataFormat();
1103        return dataFormat(zfdf);
1104    }
1105
1106    @SuppressWarnings("unchecked")
1107    private T dataFormat(DataFormatDefinition dataFormatType) {
1108        switch (operation) {
1109        case Unmarshal:
1110            return (T) processorType.unmarshal(dataFormatType);
1111        case Marshal:
1112            return (T) processorType.marshal(dataFormatType);
1113        default:
1114            throw new IllegalArgumentException("Unknown DataFormat operation: " + operation);
1115        }
1116    }
1117}