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 
033     */
034    @XmlRootElement(name = "jaxb")
035    @XmlAccessorType(XmlAccessType.FIELD)
036    public class JaxbDataFormat extends DataFormatDefinition {
037        @XmlAttribute(required = true)
038        private String contextPath;
039        @XmlAttribute
040        private Boolean prettyPrint;
041        @XmlAttribute
042        private Boolean ignoreJAXBElement;
043        @XmlAttribute
044        private Boolean filterNonXmlChars;
045        @XmlAttribute
046        private String encoding;
047        @XmlAttribute
048        private Boolean fragment;
049        // Partial encoding
050        @XmlAttribute
051        private String partClass;
052        @XmlAttribute
053        private String partNamespace;
054    
055        public JaxbDataFormat() {
056            super("jaxb");
057        }
058    
059        public JaxbDataFormat(boolean prettyPrint) {
060            this();
061            setPrettyPrint(prettyPrint);
062        }
063    
064        public String getContextPath() {
065            return contextPath;
066        }
067    
068        public void setContextPath(String contextPath) {
069            this.contextPath = contextPath;
070        }
071    
072        public Boolean getPrettyPrint() {
073            return prettyPrint;
074        }
075    
076        public void setPrettyPrint(Boolean prettyPrint) {
077            this.prettyPrint = prettyPrint;
078        }
079    
080        public Boolean getIgnoreJAXBElement() {
081            return ignoreJAXBElement;
082        }
083    
084        public void setIgnoreJAXBElement(Boolean ignoreJAXBElement) {
085            this.ignoreJAXBElement = ignoreJAXBElement;
086        }
087        
088        public void setFragment(Boolean fragment) {
089            this.fragment = fragment;
090        }
091        
092        public Boolean getFragment() {
093            return fragment;
094        }
095    
096        public Boolean getFilterNonXmlChars() {
097            return filterNonXmlChars;
098        }
099    
100        public void setFilterNonXmlChars(Boolean filterNonXmlChars) {
101            this.filterNonXmlChars = filterNonXmlChars;
102        }
103    
104        public String getEncoding() {
105            return encoding;
106        }
107    
108        public void setEncoding(String encoding) {
109            this.encoding = encoding;
110        }
111    
112        public String getPartClass() {
113            return partClass;
114        }
115    
116        public void setPartClass(String partClass) {
117            this.partClass = partClass;
118        }
119    
120        public String getPartNamespace() {
121            return partNamespace;
122        }
123    
124        public void setPartNamespace(String partNamespace) {
125            this.partNamespace = partNamespace;
126        }
127    
128        @Override
129        protected void configureDataFormat(DataFormat dataFormat) {
130            Boolean answer = ObjectHelper.toBoolean(getPrettyPrint());
131            if (answer != null && !answer) {
132                setProperty(dataFormat, "prettyPrint", Boolean.FALSE);
133            } else { // the default value is true
134                setProperty(dataFormat, "prettyPrint", Boolean.TRUE);
135            }
136            answer = ObjectHelper.toBoolean(getIgnoreJAXBElement());
137            if (answer != null && !answer) {
138                setProperty(dataFormat, "ignoreJAXBElement", Boolean.FALSE);
139            } else { // the default value is true
140                setProperty(dataFormat, "ignoreJAXBElement", Boolean.TRUE);
141            }
142            answer = ObjectHelper.toBoolean(getFilterNonXmlChars());
143            if (answer != null && answer) {
144                setProperty(dataFormat, "filterNonXmlChars", Boolean.TRUE);
145            } else { // the default value is false
146                setProperty(dataFormat, "filterNonXmlChars", Boolean.FALSE);
147            }
148            answer = ObjectHelper.toBoolean(getFragment());
149            if (answer != null && answer) {
150                setProperty(dataFormat, "fragment", Boolean.TRUE);
151            } else { // the default value is false
152                setProperty(dataFormat, "fragment", Boolean.FALSE);
153            }
154            if (partClass != null) {
155                setProperty(dataFormat, "partClass", partClass);
156            }
157            if (partNamespace != null) {
158                setProperty(dataFormat, "partNamespace", QName.valueOf(partNamespace));
159            }
160            if (encoding != null) {
161                setProperty(dataFormat, "encoding", encoding);
162            }
163            setProperty(dataFormat, "contextPath", contextPath);
164        }
165    
166    }