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;
023
024import org.apache.camel.CamelContext;
025import org.apache.camel.model.DataFormatDefinition;
026import org.apache.camel.spi.DataFormat;
027import org.apache.camel.spi.Metadata;
028
029/**
030 * Castor data format is used for unmarshal a XML payload to POJO or to marshal POJO back to XML payload.
031 *
032 * @version 
033 */
034@Metadata(firstVersion = "2.1.0", label = "dataformat,transformation,xml", title = "Castor")
035@XmlRootElement(name = "castor")
036@XmlAccessorType(XmlAccessType.FIELD)
037public class CastorDataFormat extends DataFormatDefinition {
038    @XmlAttribute
039    private String mappingFile;
040    @XmlAttribute @Metadata(defaultValue = "true")
041    private Boolean validation;
042    @XmlAttribute @Metadata(defaultValue = "UTF-8")
043    private String encoding;
044    @XmlAttribute
045    private String[] packages;
046    @XmlAttribute
047    private String[] classes;
048
049    public CastorDataFormat() {
050        super("castor");
051    }
052
053    public Boolean getValidation() {
054        return validation;
055    }
056
057    /**
058     * Whether validation is turned on or off.
059     * <p/>
060     * Is by default true.
061     */
062    public void setValidation(Boolean validation) {
063        this.validation = validation;
064    }
065
066    public String getMappingFile() {
067        return mappingFile;
068    }
069
070    /**
071     * Path to a Castor mapping file to load from the classpath.
072     */
073    public void setMappingFile(String mappingFile) {
074        this.mappingFile = mappingFile;
075    }
076
077    public String[] getPackages() {
078        return packages;
079    }
080
081    /**
082     * Add additional packages to Castor XmlContext
083     */
084    public void setPackages(String[] packages) {
085        this.packages = packages;
086    }
087
088    public String[] getClasses() {
089        return classes;
090    }
091
092    /**
093     * Add additional class names to Castor XmlContext
094     */
095    public void setClasses(String[] classes) {
096        this.classes = classes;
097    }
098
099    public String getEncoding() {
100        return encoding;
101    }
102
103    /**
104     * Encoding to use when marshalling an Object to XML.
105     * <p/>
106     * Is by default UTF-8
107     */
108    public void setEncoding(String encoding) {
109        this.encoding = encoding;
110    }
111
112    @Override
113    protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) {
114        if (mappingFile != null) {
115            setProperty(camelContext, dataFormat, "mappingFile", mappingFile);
116        }
117        // should be true by default
118        boolean isValidation = getValidation() == null || getValidation();
119        setProperty(camelContext, dataFormat, "validation", isValidation);
120
121        if (encoding != null) {
122            setProperty(camelContext, dataFormat, "encoding", encoding);
123        }
124        if (packages != null) {
125            setProperty(camelContext, dataFormat, "packages", packages);
126        }
127        if (classes != null) {
128            setProperty(camelContext, dataFormat, "classes", classes);
129        }
130    }
131
132}