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    import javax.xml.namespace.QName;
024    
025    import org.apache.camel.model.DataFormatDefinition;
026    import org.apache.camel.spi.DataFormat;
027    import org.apache.camel.util.ObjectHelper;
028    
029    /**
030     * Represents the JAXB2 XML {@link DataFormat}
031     *
032     * @version $Revision: 901192 $
033     */
034    @XmlRootElement(name = "jaxb")
035    @XmlAccessorType(XmlAccessType.FIELD)
036    public class JaxbDataFormat extends DataFormatDefinition {
037        @XmlAttribute(required = false)
038        private String contextPath;
039        @XmlAttribute(required = false)
040        private Boolean prettyPrint;
041        @XmlAttribute(required = false)
042        private Boolean ignoreJAXBElement;
043        @XmlAttribute(required = false)
044        private Boolean filterNonXmlChars;
045        @XmlAttribute(required = false)
046        private String encoding;
047    
048        // Partial encoding
049        @XmlAttribute(required = false)
050        private String partClass;
051        @XmlAttribute(required = false)
052        private String partNamespace;
053    
054        public JaxbDataFormat() {
055            super("jaxb");
056        }
057    
058        public JaxbDataFormat(boolean prettyPrint) {
059            this();
060            setPrettyPrint(prettyPrint);
061        }
062    
063        public String getContextPath() {
064            return contextPath;
065        }
066    
067        public void setContextPath(String contextPath) {
068            this.contextPath = contextPath;
069        }
070    
071        public Boolean getPrettyPrint() {
072            return prettyPrint;
073        }
074    
075        public void setPrettyPrint(Boolean prettyPrint) {
076            this.prettyPrint = prettyPrint;
077        }
078        
079        public Boolean getIgnoreJAXBElement() {
080            return ignoreJAXBElement;
081        }
082        
083        public void setIgnoreJAXBElement(Boolean ignoreJAXBElement) {
084            this.ignoreJAXBElement = ignoreJAXBElement;
085        }
086        
087        public Boolean getFilterNonXmlChars() {
088            return filterNonXmlChars;
089        }
090        
091        public void setFilterNonXmlChars(Boolean filterNonXmlChars) {
092            this.filterNonXmlChars = filterNonXmlChars;
093        }
094    
095        public String getPartClass() {
096            return partClass;
097        }
098    
099        public void setPartClass(String partClass) {
100            this.partClass = partClass;
101        }
102    
103        public void setPartNamespace(String partNamespace) {
104            this.partNamespace = partNamespace;
105        }
106    
107        @Override
108        protected void configureDataFormat(DataFormat dataFormat) {
109            Boolean answer = ObjectHelper.toBoolean(getPrettyPrint());
110            if (answer != null && !answer) {
111                setProperty(dataFormat, "prettyPrint", Boolean.FALSE);
112            } else { // the default value is true
113                setProperty(dataFormat, "prettyPrint", Boolean.TRUE);
114            }
115            answer = ObjectHelper.toBoolean(getIgnoreJAXBElement());
116            if (answer != null && !answer) {
117                setProperty(dataFormat, "ignoreJAXBElement", Boolean.FALSE);
118            } else { // the default value is true
119                setProperty(dataFormat, "ignoreJAXBElement", Boolean.TRUE);
120            }
121            answer = ObjectHelper.toBoolean(getFilterNonXmlChars());
122            if (answer != null && answer) {
123                setProperty(dataFormat, "filterNonXmlChars", Boolean.TRUE);
124            } else { // the default value is false
125                setProperty(dataFormat, "filterNonXmlChars", Boolean.FALSE);
126            }
127            if (partClass != null) {
128                setProperty(dataFormat, "partClass", partClass);
129            }
130            if (partNamespace != null) {
131                setProperty(dataFormat, "partNamespace", QName.valueOf(partNamespace));
132            }
133            if (encoding != null) {
134                setProperty(dataFormat, "encoding", encoding);
135            }
136            setProperty(dataFormat, "contextPath", contextPath);
137        }
138    }