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