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.dataformat;
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.XmlRootElement;
023    
024    import org.apache.camel.model.DataFormatDefinition;
025    import org.apache.camel.spi.DataFormat;
026    import org.apache.camel.util.ObjectHelper;
027    
028    /**
029     * Represents the JAXB2 XML {@link DataFormat}
030     *
031     * @version $Revision: 782535 $
032     */
033    @XmlRootElement(name = "jaxb")
034    @XmlAccessorType(XmlAccessType.FIELD)
035    public class JaxbDataFormat extends DataFormatDefinition {
036        @XmlAttribute(required = false)
037        private String contextPath;
038        @XmlAttribute(required = false)
039        private Boolean prettyPrint;
040        @XmlAttribute(required = false)
041        private Boolean ignoreJAXBElement;
042        @XmlAttribute(required = false)
043        private String encoding;
044    
045        public JaxbDataFormat() {
046            super("org.apache.camel.converter.jaxb.JaxbDataFormat");
047        }
048    
049        public JaxbDataFormat(boolean prettyPrint) {
050            this();
051            setPrettyPrint(prettyPrint);
052        }
053    
054        public String getContextPath() {
055            return contextPath;
056        }
057    
058        public void setContextPath(String contextPath) {
059            this.contextPath = contextPath;
060        }
061    
062        public Boolean getPrettyPrint() {
063            return prettyPrint;
064        }
065    
066        public void setPrettyPrint(Boolean prettyPrint) {
067            this.prettyPrint = prettyPrint;
068        }
069        
070        public Boolean getIgnoreJAXBElement() {
071            return ignoreJAXBElement;
072        }
073        
074        public void setIgnoreJAXBElement(Boolean ignoreJAXBElement) {
075            this.ignoreJAXBElement = ignoreJAXBElement;
076        }
077        
078        @Override
079        protected void configureDataFormat(DataFormat dataFormat) {
080            Boolean answer = ObjectHelper.toBoolean(getPrettyPrint());
081            if (answer != null && !answer) {
082                setProperty(dataFormat, "prettyPrint", Boolean.FALSE);
083            } else { // the default value is true
084                setProperty(dataFormat, "prettyPrint", Boolean.TRUE);
085            }
086            answer = ObjectHelper.toBoolean(getIgnoreJAXBElement());
087            if (answer != null && !answer) {
088                setProperty(dataFormat, "ignoreJAXBElement", Boolean.FALSE);
089            } else { // the default value is true
090                setProperty(dataFormat, "ignoreJAXBElement", Boolean.TRUE);
091            }
092            if (encoding != null) {
093                setProperty(dataFormat, "encoding", encoding);
094            }
095            setProperty(dataFormat, "contextPath", contextPath);
096        }
097    }