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.CamelContext;
026    import org.apache.camel.model.DataFormatDefinition;
027    import org.apache.camel.spi.DataFormat;
028    import org.apache.camel.util.ObjectHelper;
029    
030    /**
031     * Represents the JAXB2 XML {@link DataFormat}
032     *
033     * @version 
034     */
035    @XmlRootElement(name = "jaxb")
036    @XmlAccessorType(XmlAccessType.FIELD)
037    public class JaxbDataFormat extends DataFormatDefinition {
038        @XmlAttribute(required = true)
039        private String contextPath;
040        @XmlAttribute
041        private String schema;
042        @XmlAttribute
043        private Boolean prettyPrint;
044        @XmlAttribute
045        private Boolean ignoreJAXBElement;
046        @XmlAttribute
047        private Boolean filterNonXmlChars;
048        @XmlAttribute
049        private String encoding;
050        @XmlAttribute
051        private Boolean fragment;
052        // Partial encoding
053        @XmlAttribute
054        private String partClass;
055        @XmlAttribute
056        private String partNamespace;
057        @XmlAttribute
058        private String namespacePrefixRef;
059        @XmlAttribute
060        private String xmlStreamWriterWrapper;
061    
062        public JaxbDataFormat() {
063            super("jaxb");
064        }
065    
066        public JaxbDataFormat(boolean prettyPrint) {
067            this();
068            setPrettyPrint(prettyPrint);
069        }
070    
071        public String getContextPath() {
072            return contextPath;
073        }
074    
075        public void setContextPath(String contextPath) {
076            this.contextPath = contextPath;
077        }
078    
079        public String getSchema() {
080            return schema;
081        }
082    
083        public void setSchema(String schema) {
084            this.schema = schema;
085        }
086    
087        public Boolean getPrettyPrint() {
088            return prettyPrint;
089        }
090    
091        public void setPrettyPrint(Boolean prettyPrint) {
092            this.prettyPrint = prettyPrint;
093        }
094    
095        public Boolean getIgnoreJAXBElement() {
096            return ignoreJAXBElement;
097        }
098    
099        public void setIgnoreJAXBElement(Boolean ignoreJAXBElement) {
100            this.ignoreJAXBElement = ignoreJAXBElement;
101        }
102        
103        public void setFragment(Boolean fragment) {
104            this.fragment = fragment;
105        }
106        
107        public Boolean getFragment() {
108            return fragment;
109        }
110    
111        public Boolean getFilterNonXmlChars() {
112            return filterNonXmlChars;
113        }
114    
115        public void setFilterNonXmlChars(Boolean filterNonXmlChars) {
116            this.filterNonXmlChars = filterNonXmlChars;
117        }
118    
119        public String getEncoding() {
120            return encoding;
121        }
122    
123        public void setEncoding(String encoding) {
124            this.encoding = encoding;
125        }
126    
127        public String getPartClass() {
128            return partClass;
129        }
130    
131        public void setPartClass(String partClass) {
132            this.partClass = partClass;
133        }
134    
135        public String getPartNamespace() {
136            return partNamespace;
137        }
138    
139        public void setPartNamespace(String partNamespace) {
140            this.partNamespace = partNamespace;
141        }
142    
143        public String getNamespacePrefixRef() {
144            return namespacePrefixRef;
145        }
146    
147        public void setNamespacePrefixRef(String namespacePrefixRef) {
148            this.namespacePrefixRef = namespacePrefixRef;
149        }
150    
151        public String getXmlStreamWriterWrapper() {
152            return xmlStreamWriterWrapper;
153        }
154    
155        public void setXmlStreamWriterWrapper(String xmlStreamWriterWrapperRef) {
156            this.xmlStreamWriterWrapper = xmlStreamWriterWrapperRef;
157        }
158    
159        @Override
160        protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) {
161            Boolean answer = ObjectHelper.toBoolean(getPrettyPrint());
162            if (answer != null && !answer) {
163                setProperty(camelContext, dataFormat, "prettyPrint", Boolean.FALSE);
164            } else { // the default value is true
165                setProperty(camelContext, dataFormat, "prettyPrint", Boolean.TRUE);
166            }
167            answer = ObjectHelper.toBoolean(getIgnoreJAXBElement());
168            if (answer != null && !answer) {
169                setProperty(camelContext, dataFormat, "ignoreJAXBElement", Boolean.FALSE);
170            } else { // the default value is true
171                setProperty(camelContext, dataFormat, "ignoreJAXBElement", Boolean.TRUE);
172            }
173            answer = ObjectHelper.toBoolean(getFilterNonXmlChars());
174            if (answer != null && answer) {
175                setProperty(camelContext, dataFormat, "filterNonXmlChars", Boolean.TRUE);
176            } else { // the default value is false
177                setProperty(camelContext, dataFormat, "filterNonXmlChars", Boolean.FALSE);
178            }
179            answer = ObjectHelper.toBoolean(getFragment());
180            if (answer != null && answer) {
181                setProperty(camelContext, dataFormat, "fragment", Boolean.TRUE);
182            } else { // the default value is false
183                setProperty(camelContext, dataFormat, "fragment", Boolean.FALSE);
184            }
185            if (partClass != null) {
186                setProperty(camelContext, dataFormat, "partClass", partClass);
187            }
188            if (partNamespace != null) {
189                setProperty(camelContext, dataFormat, "partNamespace", QName.valueOf(partNamespace));
190            }
191            if (encoding != null) {
192                setProperty(camelContext, dataFormat, "encoding", encoding);
193            }
194            if (namespacePrefixRef != null) {
195                setProperty(camelContext, dataFormat, "namespacePrefixRef", namespacePrefixRef);
196            }
197            setProperty(camelContext, dataFormat, "contextPath", contextPath);
198            if (schema != null) {
199                setProperty(camelContext, dataFormat, "schema", schema);
200            }
201            if (xmlStreamWriterWrapper != null) {
202                setProperty(camelContext, dataFormat, "xmlStreamWriterWrapper", xmlStreamWriterWrapper);
203            }
204        }
205    }