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 java.util.Arrays; 020 import java.util.List; 021 import java.util.Map; 022 023 import javax.xml.bind.annotation.XmlAccessType; 024 import javax.xml.bind.annotation.XmlAccessorType; 025 import javax.xml.bind.annotation.XmlAttribute; 026 import javax.xml.bind.annotation.XmlList; 027 import javax.xml.bind.annotation.XmlRootElement; 028 029 import org.apache.camel.model.DataFormatDefinition; 030 import org.apache.camel.spi.DataFormat; 031 032 /** 033 * Represents a <a href="http://camel.apache.org/xmljson.html">XML-JSON</a> {@link org.apache.camel.spi.DataFormat}. 034 * 035 * @version 036 */ 037 @XmlRootElement(name = "xmljson") 038 @XmlAccessorType(XmlAccessType.FIELD) 039 public class XmlJsonDataFormat extends DataFormatDefinition { 040 041 public static final String TYPE_HINTS = "typeHints"; 042 public static final String REMOVE_NAMESPACE_PREFIXES = "removeNamespacePrefixes"; 043 public static final String SKIP_NAMESPACES = "skipNamespaces"; 044 public static final String TRIM_SPACES = "trimSpaces"; 045 public static final String SKIP_WHITESPACE = "skipWhitespace"; 046 public static final String EXPANDABLE_PROPERTIES = "expandableProperties"; 047 public static final String ARRAY_NAME = "arrayName"; 048 public static final String ELEMENT_NAME = "elementName"; 049 public static final String ROOT_NAME = "rootName"; 050 public static final String NAMESPACE_LENIENT = "namespaceLenient"; 051 public static final String FORCE_TOP_LEVEL_OBJECT = "forceTopLevelObject"; 052 public static final String ENCODING = "encoding"; 053 054 @XmlAttribute 055 private String encoding; 056 @XmlAttribute 057 private String elementName; 058 @XmlAttribute 059 private String arrayName; 060 @XmlAttribute 061 private Boolean forceTopLevelObject; 062 @XmlAttribute 063 private Boolean namespaceLenient; 064 @XmlAttribute 065 private String rootName; 066 @XmlAttribute 067 private Boolean skipWhitespace; 068 @XmlAttribute 069 private Boolean trimSpaces; 070 @XmlAttribute 071 private Boolean skipNamespaces; 072 @XmlAttribute 073 private Boolean removeNamespacePrefixes; 074 @XmlAttribute @XmlList 075 private List<String> expandableProperties; 076 @XmlAttribute 077 private String typeHints; 078 079 public XmlJsonDataFormat() { 080 super("xmljson"); 081 } 082 083 public XmlJsonDataFormat(Map<String, String> options) { 084 super("xmljson"); 085 if (options.containsKey(ENCODING)) { 086 encoding = options.get(ENCODING); 087 } 088 if (options.containsKey(FORCE_TOP_LEVEL_OBJECT)) { 089 forceTopLevelObject = Boolean.parseBoolean(options.get(FORCE_TOP_LEVEL_OBJECT)); 090 } 091 if (options.containsKey(NAMESPACE_LENIENT)) { 092 namespaceLenient = Boolean.parseBoolean(options.get(NAMESPACE_LENIENT)); 093 } 094 if (options.containsKey(ROOT_NAME)) { 095 rootName = options.get(ROOT_NAME); 096 } 097 if (options.containsKey(ELEMENT_NAME)) { 098 encoding = options.get(ELEMENT_NAME); 099 } 100 if (options.containsKey(ARRAY_NAME)) { 101 elementName = options.get(ARRAY_NAME); 102 } 103 if (options.containsKey(EXPANDABLE_PROPERTIES)) { 104 expandableProperties = Arrays.asList(options.get(EXPANDABLE_PROPERTIES).split(" ")); 105 } 106 if (options.containsKey(SKIP_WHITESPACE)) { 107 skipWhitespace = Boolean.parseBoolean(options.get(SKIP_WHITESPACE)); 108 } 109 if (options.containsKey(TRIM_SPACES)) { 110 trimSpaces = Boolean.parseBoolean(options.get(TRIM_SPACES)); 111 } 112 if (options.containsKey(SKIP_NAMESPACES)) { 113 skipNamespaces = Boolean.parseBoolean(options.get(SKIP_NAMESPACES)); 114 } 115 if (options.containsKey(REMOVE_NAMESPACE_PREFIXES)) { 116 removeNamespacePrefixes = Boolean.parseBoolean(options.get(REMOVE_NAMESPACE_PREFIXES)); 117 } 118 if (options.containsKey(TYPE_HINTS)) { 119 typeHints = options.get(TYPE_HINTS); 120 } 121 } 122 123 @Override 124 protected void configureDataFormat(DataFormat dataFormat) { 125 if (encoding != null) { 126 setProperty(dataFormat, ENCODING, encoding); 127 } 128 129 if (forceTopLevelObject != null) { 130 setProperty(dataFormat, FORCE_TOP_LEVEL_OBJECT, forceTopLevelObject); 131 } 132 133 if (namespaceLenient != null) { 134 setProperty(dataFormat, NAMESPACE_LENIENT, namespaceLenient); 135 } 136 137 if (rootName != null) { 138 setProperty(dataFormat, ROOT_NAME, rootName); 139 } 140 141 if (elementName != null) { 142 setProperty(dataFormat, ELEMENT_NAME, elementName); 143 } 144 145 if (arrayName != null) { 146 setProperty(dataFormat, ARRAY_NAME, arrayName); 147 } 148 149 if (expandableProperties != null && expandableProperties.size() != 0) { 150 setProperty(dataFormat, EXPANDABLE_PROPERTIES, expandableProperties); 151 } 152 153 if (skipWhitespace != null) { 154 setProperty(dataFormat, SKIP_WHITESPACE, skipWhitespace); 155 } 156 157 if (trimSpaces != null) { 158 setProperty(dataFormat, TRIM_SPACES, trimSpaces); 159 } 160 161 if (skipNamespaces != null) { 162 setProperty(dataFormat, SKIP_NAMESPACES, skipNamespaces); 163 } 164 165 if (removeNamespacePrefixes != null) { 166 setProperty(dataFormat, REMOVE_NAMESPACE_PREFIXES, removeNamespacePrefixes); 167 } 168 169 // will end up calling the setTypeHints(String s) which does the parsing from the Enum String key to the Enum value 170 if (typeHints != null) { 171 setProperty(typeHints, TYPE_HINTS, typeHints); 172 } 173 174 //TODO: xmljson: element-namespace mapping is not implemented in the XML DSL 175 // depending on adoption rate of this data format, we'll make this data format NamespaceAware so that it gets 176 // the prefix-namespaceURI mappings from the context, and with a new attribute called "namespacedElements", 177 // we'll associate named elements with prefixes following a format "element1:prefix1,element2:prefix2,..." 178 } 179 180 public String getEncoding() { 181 return encoding; 182 } 183 184 public void setEncoding(String encoding) { 185 this.encoding = encoding; 186 } 187 188 public String getElementName() { 189 return elementName; 190 } 191 192 public void setElementName(String elementName) { 193 this.elementName = elementName; 194 } 195 196 public String getArrayName() { 197 return arrayName; 198 } 199 200 public void setArrayName(String arrayName) { 201 this.arrayName = arrayName; 202 } 203 204 public Boolean getForceTopLevelObject() { 205 return forceTopLevelObject; 206 } 207 208 public void setForceTopLevelObject(Boolean forceTopLevelObject) { 209 this.forceTopLevelObject = forceTopLevelObject; 210 } 211 212 public Boolean getNamespaceLenient() { 213 return namespaceLenient; 214 } 215 216 public void setNamespaceLenient(Boolean namespaceLenient) { 217 this.namespaceLenient = namespaceLenient; 218 } 219 220 public String getRootName() { 221 return rootName; 222 } 223 224 public void setRootName(String rootName) { 225 this.rootName = rootName; 226 } 227 228 public Boolean getSkipWhitespace() { 229 return skipWhitespace; 230 } 231 232 public void setSkipWhitespace(Boolean skipWhitespace) { 233 this.skipWhitespace = skipWhitespace; 234 } 235 236 public Boolean getTrimSpaces() { 237 return trimSpaces; 238 } 239 240 public void setTrimSpaces(Boolean trimSpaces) { 241 this.trimSpaces = trimSpaces; 242 } 243 244 public Boolean getSkipNamespaces() { 245 return skipNamespaces; 246 } 247 248 public void setSkipNamespaces(Boolean skipNamespaces) { 249 this.skipNamespaces = skipNamespaces; 250 } 251 252 public Boolean getRemoveNamespacePrefixes() { 253 return removeNamespacePrefixes; 254 } 255 256 public void setRemoveNamespacePrefixes(Boolean removeNamespacePrefixes) { 257 this.removeNamespacePrefixes = removeNamespacePrefixes; 258 } 259 260 public List<String> getExpandableProperties() { 261 return expandableProperties; 262 } 263 264 public void setExpandableProperties(List<String> expandableProperties) { 265 this.expandableProperties = expandableProperties; 266 } 267 268 public String getTypeHints() { 269 return typeHints; 270 } 271 272 public void setTypeHints(String typeHints) { 273 this.typeHints = typeHints; 274 } 275 276 }