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.ArtixDSDataFormat;
028 import org.apache.camel.model.dataformat.CsvDataFormat;
029 import org.apache.camel.model.dataformat.DataFormatType;
030 import org.apache.camel.model.dataformat.JaxbDataFormat;
031 import org.apache.camel.model.dataformat.SerializationDataFormat;
032 import org.apache.camel.model.dataformat.StringDataFormat;
033 import org.apache.camel.model.dataformat.XMLBeansDataFormat;
034 import org.apache.camel.processor.MarshalProcessor;
035 import org.apache.camel.spi.DataFormat;
036 import org.apache.camel.spi.RouteContext;
037
038 /**
039 * Marshals to a binary payload using the given {@link DataFormatType}
040 *
041 * @version $Revision: 671918 $
042 */
043 @XmlRootElement(name = "marshal")
044 @XmlAccessorType(XmlAccessType.FIELD)
045 public class MarshalType extends OutputType<ProcessorType> {
046 @XmlAttribute(required = false)
047 private String ref;
048 // TODO cannot use @XmlElementRef as it doesn't allow optional properties
049 // @XmlElementRef
050 @XmlElements({
051 @XmlElement(required = false, name = "artixDS", type = ArtixDSDataFormat.class),
052 @XmlElement(required = false, name = "csv", type = CsvDataFormat.class),
053 @XmlElement(required = false, name = "jaxb", type = JaxbDataFormat.class),
054 @XmlElement(required = false, name = "serialization", type = SerializationDataFormat.class),
055 @XmlElement(required = false, name = "string", type = StringDataFormat.class),
056 @XmlElement(required = false, name = "xmlBeans", type = XMLBeansDataFormat.class)}
057 )
058 private DataFormatType dataFormatType;
059
060 public MarshalType() {
061 }
062
063 public MarshalType(DataFormatType dataFormatType) {
064 this.dataFormatType = dataFormatType;
065 }
066
067 public MarshalType(String ref) {
068 this.ref = ref;
069 }
070
071 @Override
072 public String toString() {
073 if (dataFormatType != null) {
074 return "Marshal[" + dataFormatType + "]";
075 } else {
076 return "Marshal[ref: " + ref + "]";
077 }
078 }
079
080 @Override
081 public String getShortName() {
082 return "marshal";
083 }
084
085 public String getRef() {
086 return ref;
087 }
088
089 public void setRef(String ref) {
090 this.ref = ref;
091 }
092
093 public DataFormatType getDataFormatType() {
094 return dataFormatType;
095 }
096
097 public void setDataFormatType(DataFormatType dataFormatType) {
098 this.dataFormatType = dataFormatType;
099 }
100
101 @Override
102 public Processor createProcessor(RouteContext routeContext) {
103 DataFormat dataFormat = DataFormatType.getDataFormat(routeContext, getDataFormatType(), ref);
104 return new MarshalProcessor(dataFormat);
105 }
106 }