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