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.apache.camel.Processor;
022    import org.apache.camel.model.ProcessorType;
023    import org.apache.camel.model.dataformat.ArtixDSContentType;
024    import org.apache.camel.model.dataformat.ArtixDSDataFormat;
025    import org.apache.camel.model.dataformat.CsvDataFormat;
026    import org.apache.camel.model.dataformat.DataFormatType;
027    import org.apache.camel.model.dataformat.HL7DataFormat;
028    import org.apache.camel.model.dataformat.JaxbDataFormat;
029    import org.apache.camel.model.dataformat.JsonDataFormat;
030    import org.apache.camel.model.dataformat.SerializationDataFormat;
031    import org.apache.camel.model.dataformat.StringDataFormat;
032    import org.apache.camel.model.dataformat.XMLBeansDataFormat;
033    import org.apache.camel.model.dataformat.XStreamDataFormat;
034    import org.apache.camel.model.dataformat.ZipDataFormat;
035    import org.apache.camel.spi.DataFormat;
036    
037    /**
038     * An expression for constructing the different possible {@link DataFormat}
039     * options.
040     *
041     * @version $Revision: 63424 $
042     */
043    public class DataFormatClause<T extends ProcessorType> {
044        private final T processorType;
045        private final Operation operation;
046    
047        /**
048         * {@link DataFormat} operations.
049         */
050        public enum Operation {
051            Marshal, Unmarshal
052        }
053    
054        public DataFormatClause(T processorType, Operation operation) {
055            this.processorType = processorType;
056            this.operation = operation;
057        }
058    
059        /**
060         * Uses the
061         * <a href="http://activemq.apache.org/camel/artix-data-services.html">Artix Data Services</a>
062         * data format for dealing with lots of different message formats such as SWIFT etc.
063         */
064        public T artixDS() {
065            return dataFormat(new ArtixDSDataFormat());
066        }
067    
068        /**
069         * Uses the
070         * <a href="http://activemq.apache.org/camel/artix-data-services.html">Artix Data Services</a>
071         * data format with the specified type of ComplexDataObject
072         * for marshalling and unmarshalling messages using the dataObject's default Source and Sink.
073         */
074        public T artixDS(Class<?> dataObjectType) {
075            return dataFormat(new ArtixDSDataFormat(dataObjectType));
076        }
077    
078    
079        /**
080         * Uses the
081         * <a href="http://activemq.apache.org/camel/artix-data-services.html">Artix Data Services</a>
082         * data format with the specified type of ComplexDataObject
083         * for marshalling and unmarshalling messages using the dataObject's default Source and Sink.
084         */
085        public T artixDS(Class<?> elementType, ArtixDSContentType contentType) {
086            return dataFormat(new ArtixDSDataFormat(elementType, contentType));
087        }
088    
089        /**
090         * Uses the
091         * <a href="http://activemq.apache.org/camel/artix-data-services.html">Artix Data Services</a>
092         * data format with the specified content type
093         * for marshalling and unmarshalling messages
094         */
095        public T artixDS(ArtixDSContentType contentType) {
096            return dataFormat(new ArtixDSDataFormat(contentType));
097        }
098    
099        /**
100         * Uses the CSV data format
101         */
102        public T csv() {
103            return dataFormat(new CsvDataFormat());
104        }
105    
106        /**
107         * Uses the HL7 data format
108         */
109        public T hl7() {
110            return dataFormat(new HL7DataFormat());
111        }
112    
113        /**
114         * Uses the JAXB data format
115         */
116        public T jaxb() {
117            return dataFormat(new JaxbDataFormat());
118        }
119    
120        /**
121         * Uses the JAXB data format turning pretty printing on or off
122         */
123        public T jaxb(boolean prettyPrint) {
124            return dataFormat(new JaxbDataFormat(prettyPrint));
125        }
126    
127        /**
128         * Uses the Java Serialization data format
129         */
130        public T serialization() {
131            return dataFormat(new SerializationDataFormat());
132        }
133    
134        /**
135         * Uses the String data format
136         */
137        public T string() {
138            return string(null);
139        }
140    
141        /**
142         * Uses the String data format supporting encoding using given charset
143         */
144        public T string(String charset) {
145            StringDataFormat sdf = new StringDataFormat();
146            sdf.setCharset(charset);
147            return dataFormat(sdf);
148        }
149    
150        /**
151         * Uses the JAXB data format
152         */
153        public T xmlBeans() {
154            return dataFormat(new XMLBeansDataFormat());
155        }
156    
157        /**
158         * Uses the XStream data format
159         */
160        public T xstream() {
161            return dataFormat(new XStreamDataFormat());
162        }
163        
164        /**
165         * Uses the JSON data format
166         */
167        public T json() {
168            return dataFormat(new JsonDataFormat());
169        }
170    
171        private T dataFormat(DataFormatType dataFormatType) {
172            switch (operation) {
173            case Unmarshal:
174                return (T)processorType.unmarshal(dataFormatType);
175            case Marshal:
176                return (T)processorType.marshal(dataFormatType);
177            default:
178                throw new IllegalArgumentException("Unknown DataFormat operation: " + operation);
179            }
180        }
181    
182        public T zip() {
183            ZipDataFormat zdf = new ZipDataFormat(Deflater.DEFAULT_COMPRESSION);
184            return dataFormat(zdf);
185        }
186    
187        public T zip(int compressionLevel) {
188            ZipDataFormat zdf = new ZipDataFormat(compressionLevel);
189            return dataFormat(zdf);
190        }
191    }