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.model;
018    
019    import javax.xml.bind.annotation.XmlAccessType;
020    import javax.xml.bind.annotation.XmlAccessorType;
021    import javax.xml.bind.annotation.XmlAttribute;
022    import javax.xml.bind.annotation.XmlElement;
023    import javax.xml.bind.annotation.XmlElements;
024    import javax.xml.bind.annotation.XmlRootElement;
025    
026    import org.apache.camel.Processor;
027    import org.apache.camel.model.dataformat.BindyDataFormat;
028    import org.apache.camel.model.dataformat.CastorDataFormat;
029    import org.apache.camel.model.dataformat.CsvDataFormat;
030    import org.apache.camel.model.dataformat.FlatpackDataFormat;
031    import org.apache.camel.model.dataformat.GzipDataFormat;
032    import org.apache.camel.model.dataformat.HL7DataFormat;
033    import org.apache.camel.model.dataformat.JaxbDataFormat;
034    import org.apache.camel.model.dataformat.JsonDataFormat;
035    import org.apache.camel.model.dataformat.RssDataFormat;
036    import org.apache.camel.model.dataformat.SerializationDataFormat;
037    import org.apache.camel.model.dataformat.StringDataFormat;
038    import org.apache.camel.model.dataformat.TidyMarkupDataFormat;
039    import org.apache.camel.model.dataformat.XMLBeansDataFormat;
040    import org.apache.camel.model.dataformat.XMLSecurityDataFormat;
041    import org.apache.camel.model.dataformat.XStreamDataFormat;
042    import org.apache.camel.model.dataformat.ZipDataFormat;
043    import org.apache.camel.processor.UnmarshalProcessor;
044    import org.apache.camel.spi.DataFormat;
045    import org.apache.camel.spi.RouteContext;
046    
047    /**
048     * Unmarshals the binary payload using the given {@link DataFormatDefinition}
049     *
050     * @version $Revision: 881198 $
051     */
052    @XmlRootElement(name = "unmarshal")
053    @XmlAccessorType(XmlAccessType.FIELD)
054    public class UnmarshalDefinition extends OutputDefinition<ProcessorDefinition> {
055        @XmlAttribute(required = false)
056        private String ref;
057        // cannot use @XmlElementRef as it doesn't allow optional properties
058        @XmlElements({
059        @XmlElement(required = false, name = "bindy", type = BindyDataFormat.class),
060        @XmlElement(required = false, name = "castor", type = CastorDataFormat.class),
061        @XmlElement(required = false, name = "csv", type = CsvDataFormat.class),
062        @XmlElement(required = false, name = "flatpack", type = FlatpackDataFormat.class),
063        @XmlElement(required = false, name = "gzip", type = GzipDataFormat.class),
064        @XmlElement(required = false, name = "hl7", type = HL7DataFormat.class),
065        @XmlElement(required = false, name = "jaxb", type = JaxbDataFormat.class),
066        @XmlElement(required = false, name = "json", type = JsonDataFormat.class),
067        @XmlElement(required = false, name = "rss", type = RssDataFormat.class),
068        @XmlElement(required = false, name = "secureXML", type = XMLSecurityDataFormat.class),
069        @XmlElement(required = false, name = "serialization", type = SerializationDataFormat.class),
070        @XmlElement(required = false, name = "string", type = StringDataFormat.class),
071        @XmlElement(required = false, name = "tidyMarkup", type = TidyMarkupDataFormat.class),    
072        @XmlElement(required = false, name = "xmlBeans", type = XMLBeansDataFormat.class),
073        @XmlElement(required = false, name = "xstream", type = XStreamDataFormat.class),
074        @XmlElement(required = false, name = "zip", type = ZipDataFormat.class)}
075        )
076        private DataFormatDefinition dataFormatType;
077    
078        public UnmarshalDefinition() {
079        }
080    
081        public UnmarshalDefinition(DataFormatDefinition dataFormatType) {
082            this.dataFormatType = dataFormatType;
083        }
084    
085        public UnmarshalDefinition(String ref) {
086            this.ref = ref;
087        }
088    
089        @Override
090        public String toString() {
091            if (dataFormatType != null) {
092                return "Marshal[" + dataFormatType + "]";
093            } else {
094                return "Marshal[ref:" + ref + "]";
095            }
096        }
097    
098        @Override
099        public String getShortName() {
100            return "unmarshal";
101        }
102    
103        public String getRef() {
104            return ref;
105        }
106    
107        public void setRef(String ref) {
108            this.ref = ref;
109        }
110    
111        public DataFormatDefinition getDataFormatType() {
112            return dataFormatType;
113        }
114    
115        public void setDataFormatType(DataFormatDefinition dataFormatType) {
116            this.dataFormatType = dataFormatType;
117        }
118    
119        @Override
120        public Processor createProcessor(RouteContext routeContext) {
121            DataFormat dataFormat = DataFormatDefinition.getDataFormat(routeContext, getDataFormatType(), ref);
122            return new UnmarshalProcessor(dataFormat);
123        }
124    }