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