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