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.BindyDataFormat;
026    import org.apache.camel.model.dataformat.BindyType;
027    import org.apache.camel.model.dataformat.CastorDataFormat;
028    import org.apache.camel.model.dataformat.CsvDataFormat;
029    import org.apache.camel.model.dataformat.GzipDataFormat;
030    import org.apache.camel.model.dataformat.HL7DataFormat;
031    import org.apache.camel.model.dataformat.JaxbDataFormat;
032    import org.apache.camel.model.dataformat.JsonDataFormat;
033    import org.apache.camel.model.dataformat.JsonLibrary;
034    import org.apache.camel.model.dataformat.ProtobufDataFormat;
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: 904442 $
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 Bindy data format
068         *
069         * @param type the type of bindy data format to use
070         * @param packages packages to scan for Bindy annotated POJO classes
071         */
072        public T bindy(BindyType type,  String... packages) {
073            BindyDataFormat bindy = new BindyDataFormat();
074            bindy.setType(type);
075            bindy.setPackages(packages);
076            return dataFormat(bindy);
077        }
078    
079        /**
080         * Uses the CSV data format
081         */
082        public T csv() {
083            return dataFormat(new CsvDataFormat());
084        }
085    
086        /**
087         * Uses the Castor data format
088         */
089        public T castor() {
090            return dataFormat(new CastorDataFormat());
091        }
092    
093        /**
094         * Uses the Castor data format
095         *
096         * @param mappingFile name of mapping file to locate in classpath
097         */
098        public T castor(String mappingFile) {
099            CastorDataFormat castor = new CastorDataFormat();
100            castor.setMappingFile(mappingFile);
101            return dataFormat(castor);
102        }
103    
104        /**
105         * Uses the Castor data format
106         *
107         * @param mappingFile name of mapping file to locate in classpath
108         * @param validation whether validation is enabled or not
109         */
110        public T castor(String mappingFile, boolean validation) {
111            CastorDataFormat castor = new CastorDataFormat();
112            castor.setMappingFile(mappingFile);
113            castor.setValidation(validation);
114            return dataFormat(castor);
115        }
116    
117        /**
118         * Uses the GZIP deflater data format
119         */
120        public T gzip() {
121            GzipDataFormat gzdf = new GzipDataFormat();
122            return dataFormat(gzdf);
123        }
124    
125        /**
126         * Uses the HL7 data format
127         */
128        public T hl7() {
129            return dataFormat(new HL7DataFormat());
130        }
131    
132        /**
133         * Uses the HL7 data format
134         */
135        public T hl7(boolean validate) {
136            HL7DataFormat hl7 = new HL7DataFormat();
137            hl7.setValidate(validate);
138            return dataFormat(hl7);
139        }
140    
141        /**
142         * Uses the JAXB data format
143         */
144        public T jaxb() {
145            return dataFormat(new JaxbDataFormat());
146        }
147        
148        /**
149         * Uses the JAXB data format with context path
150         */
151        public T jaxb(String contextPath) {
152            JaxbDataFormat dataFormat = new JaxbDataFormat();
153            dataFormat.setContextPath(contextPath);
154            return dataFormat(dataFormat);
155        }
156    
157        /**
158         * Uses the JAXB data format turning pretty printing on or off
159         */
160        public T jaxb(boolean prettyPrint) {
161            return dataFormat(new JaxbDataFormat(prettyPrint));
162        }
163    
164        /**
165         * Uses the JSON data format using the XStream json library
166         */
167        public T json() {
168            return dataFormat(new JsonDataFormat());
169        }
170    
171        /**
172         * Uses the JSON data format
173         *
174         * @param library the json library to use
175         */
176        public T json(JsonLibrary library) {
177            return dataFormat(new JsonDataFormat(library));
178        }
179    
180        /**
181         * Uses the JSON data format
182         *
183         * @param type the json type to use
184         * @param unmarshalType unmarshal type for json jackson type
185         */
186        public T json(JsonLibrary type, Class<?> unmarshalType) {
187            JsonDataFormat json = new JsonDataFormat(type);
188            json.setUnmarshalType(unmarshalType);
189            return dataFormat(json);
190        }
191        
192        /**
193         * Uses the protobuf data format
194         */
195        public T protobuf() {
196            return dataFormat(new ProtobufDataFormat());
197        }
198        
199        public T protobuf(Object defaultInstance) {
200            ProtobufDataFormat dataFormat = new ProtobufDataFormat();
201            dataFormat.setDefaultInstance(defaultInstance);
202            return dataFormat(dataFormat);
203        }
204        
205        public T protobuf(String instanceClassName) {
206            return dataFormat(new ProtobufDataFormat(instanceClassName));
207        }
208    
209        /**
210         * Uses the RSS data format
211         */
212        public T rss() {
213            return dataFormat(new RssDataFormat());
214        }    
215        
216        /**
217         * Uses the Java Serialization data format
218         */
219        public T serialization() {
220            return dataFormat(new SerializationDataFormat());
221        }
222    
223        /**
224         * Uses the String data format
225         */
226        public T string() {
227            return string(null);
228        }
229    
230        /**
231         * Uses the String data format supporting encoding using given charset
232         */
233        public T string(String charset) {
234            StringDataFormat sdf = new StringDataFormat();
235            sdf.setCharset(charset);
236            return dataFormat(sdf);
237        }
238    
239        /**
240         * Return WellFormed HTML (an XML Document) either 
241         * {@link java.lang.String} or {@link org.w3c.dom.Node}
242         */
243        public T tidyMarkup(Class<?> dataObjectType) {
244            return dataFormat(new TidyMarkupDataFormat(dataObjectType));
245        }
246    
247        /**
248         * Return TidyMarkup in the default format 
249         *  as {@link org.w3c.dom.Node}
250         */
251        public T tidyMarkup() {
252            return dataFormat(new TidyMarkupDataFormat(Node.class));
253        }
254    
255        /**
256         * Uses the XStream data format
257         */
258        public T xstream() {
259            return dataFormat(new XStreamDataFormat());
260        }
261        
262        /**
263         * Uses the xstream by setting the encoding
264         */
265        public T xstream(String encoding) {
266            return dataFormat(new XStreamDataFormat(encoding));
267        }
268        
269        /**
270         * Uses the XML Security data format
271         */
272        public T secureXML() {
273            XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat();
274            return dataFormat(xsdf);
275        }
276    
277        /**
278         * Uses the XML Security data format
279         */
280        public T secureXML(String secureTag, boolean secureTagContents) {
281            XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents);
282            return dataFormat(xsdf);
283        }
284    
285        /**
286         * Uses the XML Security data format
287         */
288        public T secureXML(String secureTag, boolean secureTagContents, String passPhrase) {
289            XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents, passPhrase);
290            return dataFormat(xsdf);
291        }
292    
293        /**
294         * Uses the XML Security data format
295         */
296        public T secureXML(String secureTag, boolean secureTagContents, String passPhrase, String xmlCipherAlgorithm) {
297            XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents, passPhrase, xmlCipherAlgorithm);
298            return dataFormat(xsdf);
299        }
300    
301        /**
302         * Uses the xmlBeans data format
303         */
304        public T xmlBeans() {
305            return dataFormat(new XMLBeansDataFormat());
306        }
307    
308        /**
309         * Uses the ZIP deflater data format
310         */
311        public T zip() {
312            ZipDataFormat zdf = new ZipDataFormat(Deflater.DEFAULT_COMPRESSION);
313            return dataFormat(zdf);
314        }
315    
316        /**
317         * Uses the ZIP deflater data format
318         */
319        public T zip(int compressionLevel) {
320            ZipDataFormat zdf = new ZipDataFormat(compressionLevel);
321            return dataFormat(zdf);
322        }
323        
324        @SuppressWarnings("unchecked")
325        private T dataFormat(DataFormatDefinition dataFormatType) {
326            switch (operation) {
327            case Unmarshal:
328                return (T)processorType.unmarshal(dataFormatType);
329            case Marshal:
330                return (T)processorType.marshal(dataFormatType);
331            default:
332                throw new IllegalArgumentException("Unknown DataFormat operation: " + operation);
333            }
334        }
335    }