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