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; 023import javax.xml.namespace.QName; 024 025import org.apache.camel.CamelContext; 026import org.apache.camel.model.DataFormatDefinition; 027import org.apache.camel.spi.DataFormat; 028import org.apache.camel.spi.Metadata; 029import org.apache.camel.util.ObjectHelper; 030 031/** 032 * JAXB data format 033 * 034 * @version 035 */ 036@Metadata(label = "dataformat,transformation", title = "JAXB") 037@XmlRootElement(name = "jaxb") 038@XmlAccessorType(XmlAccessType.FIELD) 039public class JaxbDataFormat extends DataFormatDefinition { 040 @XmlAttribute(required = true) 041 private String contextPath; 042 @XmlAttribute 043 private String schema; 044 @XmlAttribute 045 private Boolean prettyPrint; 046 @XmlAttribute 047 private Boolean ignoreJAXBElement; 048 @XmlAttribute 049 private Boolean mustBeJAXBElement; 050 @XmlAttribute 051 private Boolean filterNonXmlChars; 052 @XmlAttribute 053 private String encoding; 054 @XmlAttribute 055 private Boolean fragment; 056 // Partial encoding 057 @XmlAttribute 058 private String partClass; 059 @XmlAttribute 060 private String partNamespace; 061 @XmlAttribute 062 private String namespacePrefixRef; 063 @XmlAttribute 064 private String xmlStreamWriterWrapper; 065 @XmlAttribute 066 private String schemaLocation; 067 068 public JaxbDataFormat() { 069 super("jaxb"); 070 } 071 072 public JaxbDataFormat(boolean prettyPrint) { 073 this(); 074 setPrettyPrint(prettyPrint); 075 } 076 077 public String getContextPath() { 078 return contextPath; 079 } 080 081 /** 082 * Package name where your JAXB classes are located. 083 */ 084 public void setContextPath(String contextPath) { 085 this.contextPath = contextPath; 086 } 087 088 public String getSchema() { 089 return schema; 090 } 091 092 /** 093 * To validate against an existing schema. 094 * Your can use the prefix classpath:, file:* or *http: to specify how the resource should by resolved. 095 * You can separate multiple schema files by using the ',' character. 096 */ 097 public void setSchema(String schema) { 098 this.schema = schema; 099 } 100 101 public Boolean getPrettyPrint() { 102 return prettyPrint; 103 } 104 105 /** 106 * To enable pretty printing output nicely formatted. 107 * <p/> 108 * Is by default false. 109 */ 110 public void setPrettyPrint(Boolean prettyPrint) { 111 this.prettyPrint = prettyPrint; 112 } 113 114 public Boolean getIgnoreJAXBElement() { 115 return ignoreJAXBElement; 116 } 117 118 /** 119 * Whether to ignore JAXBElement elements - only needed to be set to false in very special use-cases. 120 */ 121 public void setIgnoreJAXBElement(Boolean ignoreJAXBElement) { 122 this.ignoreJAXBElement = ignoreJAXBElement; 123 } 124 125 public Boolean getMustBeJAXBElement() { 126 return mustBeJAXBElement; 127 } 128 129 /** 130 * Whether marhsalling must be java objects with JAXB annotations. And if not then it fails. 131 * This option can be set to false to relax that, such as when the data is already in XML format. 132 */ 133 public void setMustBeJAXBElement(Boolean mustBeJAXBElement) { 134 this.mustBeJAXBElement = mustBeJAXBElement; 135 } 136 137 /** 138 * To turn on marshalling XML fragment trees. 139 * By default JAXB looks for @XmlRootElement annotation on given class to operate on whole XML tree. 140 * This is useful but not always - sometimes generated code does not have @XmlRootElement annotation, 141 * sometimes you need unmarshall only part of tree. 142 * In that case you can use partial unmarshalling. To enable this behaviours you need set property partClass. 143 * Camel will pass this class to JAXB's unmarshaler. 144 */ 145 public void setFragment(Boolean fragment) { 146 this.fragment = fragment; 147 } 148 149 public Boolean getFragment() { 150 return fragment; 151 } 152 153 public Boolean getFilterNonXmlChars() { 154 return filterNonXmlChars; 155 } 156 157 /** 158 * To ignore non xml characheters and replace them with an empty space. 159 */ 160 public void setFilterNonXmlChars(Boolean filterNonXmlChars) { 161 this.filterNonXmlChars = filterNonXmlChars; 162 } 163 164 public String getEncoding() { 165 return encoding; 166 } 167 168 /** 169 * To overrule and use a specific encoding 170 */ 171 public void setEncoding(String encoding) { 172 this.encoding = encoding; 173 } 174 175 public String getPartClass() { 176 return partClass; 177 } 178 179 /** 180 * Name of class used for fragment parsing. 181 * <p/> 182 * See more details at the fragment option. 183 */ 184 public void setPartClass(String partClass) { 185 this.partClass = partClass; 186 } 187 188 public String getPartNamespace() { 189 return partNamespace; 190 } 191 192 /** 193 * XML namespace to use for fragment parsing. 194 * <p/> 195 * See more details at the fragment option. 196 */ 197 public void setPartNamespace(String partNamespace) { 198 this.partNamespace = partNamespace; 199 } 200 201 public String getNamespacePrefixRef() { 202 return namespacePrefixRef; 203 } 204 205 /** 206 * When marshalling using JAXB or SOAP then the JAXB implementation will automatic assign namespace prefixes, 207 * such as ns2, ns3, ns4 etc. To control this mapping, Camel allows you to refer to a map which contains the desired mapping. 208 */ 209 public void setNamespacePrefixRef(String namespacePrefixRef) { 210 this.namespacePrefixRef = namespacePrefixRef; 211 } 212 213 public String getXmlStreamWriterWrapper() { 214 return xmlStreamWriterWrapper; 215 } 216 217 /** 218 * To use a custom xml stream writer. 219 */ 220 public void setXmlStreamWriterWrapper(String xmlStreamWriterWrapperRef) { 221 this.xmlStreamWriterWrapper = xmlStreamWriterWrapperRef; 222 } 223 224 public String getSchemaLocation() { 225 return schemaLocation; 226 } 227 228 /** 229 * To define the location of the schema 230 */ 231 public void setSchemaLocation(String schemaLocation) { 232 this.schemaLocation = schemaLocation; 233 } 234 235 @Override 236 protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) { 237 Boolean answer = ObjectHelper.toBoolean(getPrettyPrint()); 238 if (answer != null && !answer) { 239 setProperty(camelContext, dataFormat, "prettyPrint", Boolean.FALSE); 240 } else { // the default value is true 241 setProperty(camelContext, dataFormat, "prettyPrint", Boolean.TRUE); 242 } 243 answer = ObjectHelper.toBoolean(getIgnoreJAXBElement()); 244 if (answer != null && !answer) { 245 setProperty(camelContext, dataFormat, "ignoreJAXBElement", Boolean.FALSE); 246 } else { // the default value is true 247 setProperty(camelContext, dataFormat, "ignoreJAXBElement", Boolean.TRUE); 248 } 249 answer = ObjectHelper.toBoolean(getMustBeJAXBElement()); 250 if (answer != null && answer) { 251 setProperty(camelContext, dataFormat, "mustBeJAXBElement", Boolean.TRUE); 252 } else { // the default value is false 253 setProperty(camelContext, dataFormat, "mustBeJAXBElement", Boolean.FALSE); 254 } 255 answer = ObjectHelper.toBoolean(getFilterNonXmlChars()); 256 if (answer != null && answer) { 257 setProperty(camelContext, dataFormat, "filterNonXmlChars", Boolean.TRUE); 258 } else { // the default value is false 259 setProperty(camelContext, dataFormat, "filterNonXmlChars", Boolean.FALSE); 260 } 261 answer = ObjectHelper.toBoolean(getFragment()); 262 if (answer != null && answer) { 263 setProperty(camelContext, dataFormat, "fragment", Boolean.TRUE); 264 } else { // the default value is false 265 setProperty(camelContext, dataFormat, "fragment", Boolean.FALSE); 266 } 267 if (partClass != null) { 268 setProperty(camelContext, dataFormat, "partClass", partClass); 269 } 270 if (partNamespace != null) { 271 setProperty(camelContext, dataFormat, "partNamespace", QName.valueOf(partNamespace)); 272 } 273 if (encoding != null) { 274 setProperty(camelContext, dataFormat, "encoding", encoding); 275 } 276 if (namespacePrefixRef != null) { 277 setProperty(camelContext, dataFormat, "namespacePrefixRef", namespacePrefixRef); 278 } 279 setProperty(camelContext, dataFormat, "contextPath", contextPath); 280 if (schema != null) { 281 setProperty(camelContext, dataFormat, "schema", schema); 282 } 283 if (xmlStreamWriterWrapper != null) { 284 setProperty(camelContext, dataFormat, "xmlStreamWriterWrapper", xmlStreamWriterWrapper); 285 } 286 if (schemaLocation != null) { 287 setProperty(camelContext, dataFormat, "schemaLocation", schemaLocation); 288 } 289 } 290}