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 */
017package org.apache.camel.model.dataformat;
018
019import javax.xml.bind.annotation.XmlAccessType;
020import javax.xml.bind.annotation.XmlAccessorType;
021import javax.xml.bind.annotation.XmlAttribute;
022import javax.xml.bind.annotation.XmlRootElement;
023import javax.xml.bind.annotation.XmlTransient;
024
025import org.apache.camel.CamelContext;
026import org.apache.camel.model.DataFormatDefinition;
027import org.apache.camel.spi.DataFormat;
028import org.apache.camel.spi.Metadata;
029import org.apache.camel.spi.RouteContext;
030import org.apache.camel.util.ObjectHelper;
031
032/**
033 * JiBX data format is used for unmarshal a XML payload to POJO or to marshal POJO back to XML payload.
034 */
035@Metadata(firstVersion = "2.6.0", label = "dataformat,transformation,xml", title = "JiBX")
036@XmlRootElement(name = "jibx")
037@XmlAccessorType(XmlAccessType.NONE)
038public class JibxDataFormat extends DataFormatDefinition {
039    @XmlAttribute(name = "unmarshallClass")
040    private String unmarshallTypeName;
041    @XmlAttribute
042    private String bindingName;
043    @XmlTransient
044    private Class<?> unmarshallClass;
045
046    public JibxDataFormat() {
047        super("jibx");
048    }
049
050    public JibxDataFormat(Class<?> unmarshallClass) {
051        this();
052        setUnmarshallClass(unmarshallClass);
053    }
054
055    public Class<?> getUnmarshallClass() {
056        return unmarshallClass;
057    }
058
059    /**
060     * Class use when unmarshalling from XML to Java.
061     */
062    public void setUnmarshallClass(Class<?> unmarshallClass) {
063        this.unmarshallClass = unmarshallClass;
064    }
065
066    public String getUnmarshallTypeName() {
067        return unmarshallTypeName;
068    }
069
070    /**
071     * Class name to use when unmarshalling from XML to Java.
072     */
073    public void setUnmarshallTypeName(String unmarshallTypeName) {
074        this.unmarshallTypeName = unmarshallTypeName;
075    }
076
077    public String getBindingName() {
078        return bindingName;
079    }
080
081    /**
082     * To use a custom binding factory
083     */
084    public void setBindingName(String bindingName) {
085        this.bindingName = bindingName;
086    }
087
088    @Override
089    protected DataFormat createDataFormat(RouteContext routeContext) {
090        if (unmarshallClass == null && unmarshallTypeName != null) {
091            try {
092                unmarshallClass = routeContext.getCamelContext().getClassResolver().resolveMandatoryClass(unmarshallTypeName);
093            } catch (ClassNotFoundException e) {
094                throw ObjectHelper.wrapRuntimeCamelException(e);
095            }
096        }
097
098        return super.createDataFormat(routeContext);
099    }
100
101    @Override
102    protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) {
103        if (unmarshallClass != null) {
104            setProperty(camelContext, dataFormat, "unmarshallClass", unmarshallClass);
105        }
106        if (bindingName != null) {
107            setProperty(camelContext, dataFormat, "bindingName", bindingName);
108        }
109    }
110
111}