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