001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.builder;
018    
019    import java.util.zip.Deflater;
020    
021    import org.w3c.dom.Node;
022    
023    import org.apache.camel.model.DataFormatDefinition;
024    import org.apache.camel.model.ProcessorDefinition;
025    import org.apache.camel.model.dataformat.ArtixDSContentType;
026    import org.apache.camel.model.dataformat.ArtixDSDataFormat;
027    import org.apache.camel.model.dataformat.CsvDataFormat;
028    import org.apache.camel.model.dataformat.GzipDataFormat;
029    import org.apache.camel.model.dataformat.HL7DataFormat;
030    import org.apache.camel.model.dataformat.JaxbDataFormat;
031    import org.apache.camel.model.dataformat.JsonDataFormat;
032    import org.apache.camel.model.dataformat.JsonLibrary;
033    import org.apache.camel.model.dataformat.RssDataFormat;
034    import org.apache.camel.model.dataformat.SerializationDataFormat;
035    import org.apache.camel.model.dataformat.StringDataFormat;
036    import org.apache.camel.model.dataformat.TidyMarkupDataFormat;
037    import org.apache.camel.model.dataformat.XMLBeansDataFormat;
038    import org.apache.camel.model.dataformat.XMLSecurityDataFormat;
039    import org.apache.camel.model.dataformat.XStreamDataFormat;
040    import org.apache.camel.model.dataformat.ZipDataFormat;
041    
042    /**
043     * An expression for constructing the different possible {@link org.apache.camel.spi.DataFormat}
044     * options.
045     *
046     * @version $Revision: 782535 $
047     */
048    public class DataFormatClause<T extends ProcessorDefinition> {
049        private final T processorType;
050        private final Operation operation;
051    
052        /**
053         * {@link org.apache.camel.spi.DataFormat} operations.
054         */
055        public enum Operation {
056            Marshal, Unmarshal
057        }
058    
059        public DataFormatClause(T processorType, Operation operation) {
060            this.processorType = processorType;
061            this.operation = operation;
062        }
063    
064        /**
065         * Uses the
066         * <a href="http://camel.apache.org/artix-data-services.html">Artix Data Services</a>
067         * data format for dealing with lots of different message formats such as SWIFT etc.
068         */
069        public T artixDS() {
070            return dataFormat(new ArtixDSDataFormat());
071        }
072    
073        /**
074         * Uses the
075         * <a href="http://camel.apache.org/artix-data-services.html">Artix Data Services</a>
076         * data format with the specified type of ComplexDataObject
077         * for marshalling and unmarshalling messages using the dataObject's default Source and Sink.
078         */
079        public T artixDS(Class<?> dataObjectType) {
080            return dataFormat(new ArtixDSDataFormat(dataObjectType));
081        }
082    
083    
084        /**
085         * Uses the
086         * <a href="http://camel.apache.org/artix-data-services.html">Artix Data Services</a>
087         * data format with the specified type of ComplexDataObject
088         * for marshalling and unmarshalling messages using the dataObject's default Source and Sink.
089         */
090        public T artixDS(Class<?> elementType, ArtixDSContentType contentType) {
091            return dataFormat(new ArtixDSDataFormat(elementType, contentType));
092        }
093    
094        /**
095         * Uses the
096         * <a href="http://camel.apache.org/artix-data-services.html">Artix Data Services</a>
097         * data format with the specified content type
098         * for marshalling and unmarshalling messages
099         */
100        public T artixDS(ArtixDSContentType contentType) {
101            return dataFormat(new ArtixDSDataFormat(contentType));
102        }
103    
104        /**
105         * Uses the CSV data format
106         */
107        public T csv() {
108            return dataFormat(new CsvDataFormat());
109        }
110    
111        /**
112         * Uses the HL7 data format
113         */
114        public T hl7() {
115            return dataFormat(new HL7DataFormat());
116        }
117    
118        /**
119         * Uses the JAXB data format
120         */
121        public T jaxb() {
122            return dataFormat(new JaxbDataFormat());
123        }
124    
125        /**
126         * Uses the JAXB data format turning pretty printing on or off
127         */
128        public T jaxb(boolean prettyPrint) {
129            return dataFormat(new JaxbDataFormat(prettyPrint));
130        }
131    
132        /**
133         * Uses the RSS data format
134         */
135        public T rss() {
136            return dataFormat(new RssDataFormat());
137        }    
138        
139        /**
140         * Uses the Java Serialization data format
141         */
142        public T serialization() {
143            return dataFormat(new SerializationDataFormat());
144        }
145    
146        /**
147         * Uses the String data format
148         */
149        public T string() {
150            return string(null);
151        }
152    
153        /**
154         * Uses the String data format supporting encoding using given charset
155         */
156        public T string(String charset) {
157            StringDataFormat sdf = new StringDataFormat();
158            sdf.setCharset(charset);
159            return dataFormat(sdf);
160        }
161    
162        /**
163         * Uses the JAXB data format
164         */
165        public T xmlBeans() {
166            return dataFormat(new XMLBeansDataFormat());
167        }
168    
169        /**
170         * Return WellFormed HTML (an XML Document) either 
171         * {@link java.lang.String} or {@link org.w3c.dom.Node}
172         */
173        public T tidyMarkup(Class<?> dataObjectType) {
174            return dataFormat(new TidyMarkupDataFormat(dataObjectType));
175        }
176    
177        /**
178         * Return TidyMarkup in the default format 
179         *  as {@link org.w3c.dom.Node}
180         */
181        public T tidyMarkup() {
182            return dataFormat(new TidyMarkupDataFormat(Node.class));
183        }
184    
185        
186        /**
187         * Uses the XStream data format
188         */
189        public T xstream() {
190            return dataFormat(new XStreamDataFormat());
191        }
192        
193        /**
194         * Uses the JSON data format using the XStream json library
195         */
196        public T json() {
197            return dataFormat(new JsonDataFormat());
198        }
199    
200        /**
201         * Uses the JSON data format
202         *
203         * @param library the json library to use
204         */
205        public T json(JsonLibrary library) {
206            return dataFormat(new JsonDataFormat(library));
207        }
208    
209        /**
210         * Uses the JSON data format
211         *
212         * @param type the json type to use
213         * @param unmarshalType unmarshal type for json jackson type
214         */
215        public T json(JsonLibrary type, Class<?> unmarshalType) {
216            JsonDataFormat json = new JsonDataFormat(type);
217            json.setUnmarshalType(unmarshalType);
218            return dataFormat(json);
219        }
220    
221        /**
222         * Uses the XML Security data format
223         */
224        public T secureXML() {
225            XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat();
226            return dataFormat(xsdf);
227        }
228    
229        /**
230         * Uses the XML Security data format
231         */
232        public T secureXML(String secureTag, boolean secureTagContents) {
233            XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents);
234            return dataFormat(xsdf);
235        }
236    
237        /**
238         * Uses the XML Security data format
239         */
240        public T secureXML(String secureTag, boolean secureTagContents, String passPhrase) {
241            XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents, passPhrase);
242            return dataFormat(xsdf);
243        }
244    
245        /**
246         * Uses the XML Security data format
247         */
248        public T secureXML(String secureTag, boolean secureTagContents, String passPhrase, String xmlCipherAlgorithm) {
249            XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents, passPhrase, xmlCipherAlgorithm);
250            return dataFormat(xsdf);
251        }
252    
253        /**
254         * Uses the ZIP deflater data format
255         */
256        public T zip() {
257            ZipDataFormat zdf = new ZipDataFormat(Deflater.DEFAULT_COMPRESSION);
258            return dataFormat(zdf);
259        }
260    
261        /**
262         * Uses the ZIP deflater data format
263         */
264        public T zip(int compressionLevel) {
265            ZipDataFormat zdf = new ZipDataFormat(compressionLevel);
266            return dataFormat(zdf);
267        }
268        
269        /**
270         * Uses the GZIP deflater data format
271         */
272        public T gzip() {
273            GzipDataFormat gzdf = new GzipDataFormat();
274            return dataFormat(gzdf);
275        }
276    
277        @SuppressWarnings("unchecked")
278        private T dataFormat(DataFormatDefinition dataFormatType) {
279            switch (operation) {
280            case Unmarshal:
281                return (T)processorType.unmarshal(dataFormatType);
282            case Marshal:
283                return (T)processorType.marshal(dataFormatType);
284            default:
285                throw new IllegalArgumentException("Unknown DataFormat operation: " + operation);
286            }
287        }
288    }