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 java.util.List;
020
021import javax.xml.bind.annotation.XmlAccessType;
022import javax.xml.bind.annotation.XmlAccessorType;
023import javax.xml.bind.annotation.XmlAttribute;
024import javax.xml.bind.annotation.XmlElement;
025import javax.xml.bind.annotation.XmlRootElement;
026import javax.xml.bind.annotation.XmlTransient;
027
028import org.apache.camel.model.DataFormatDefinition;
029import org.apache.camel.spi.Metadata;
030
031/**
032 * YAML is a data format to marshal and unmarshal Java objects to and from YAML.
033 */
034@Metadata(firstVersion = "2.17.0", label = "dataformat,transformation,yaml", title = "YAML")
035@XmlRootElement(name = "yaml")
036@XmlAccessorType(XmlAccessType.FIELD)
037public class YAMLDataFormat extends DataFormatDefinition {
038    @XmlAttribute
039    @Metadata(defaultValue = "SnakeYAML")
040    private YAMLLibrary library = YAMLLibrary.SnakeYAML;
041    @XmlTransient
042    private ClassLoader classLoader;
043    @XmlTransient
044    private Class<?> unmarshalType;
045    @XmlAttribute
046    private String unmarshalTypeName;
047    @XmlAttribute
048    private String constructor;
049    @XmlAttribute
050    private String representer;
051    @XmlAttribute
052    private String dumperOptions;
053    @XmlAttribute
054    private String resolver;
055    @XmlAttribute
056    @Metadata(javaType = "java.lang.Boolean", defaultValue = "true")
057    private String useApplicationContextClassLoader = Boolean.toString(true);
058    @XmlAttribute
059    @Metadata(javaType = "java.lang.Boolean", defaultValue = "false")
060    private String prettyFlow = Boolean.toString(false);
061    @XmlAttribute
062    @Metadata(javaType = "java.lang.Boolean", defaultValue = "false")
063    private String allowAnyType = Boolean.toString(false);
064    @XmlElement(name = "typeFilter")
065    private List<YAMLTypeFilterDefinition> typeFilters;
066    @XmlAttribute
067    @Metadata(javaType = "java.lang.Integer", defaultValue = "50")
068    private String maxAliasesForCollections = "50";
069    @XmlAttribute
070    @Metadata(javaType = "java.lang.Boolean")
071    private String allowRecursiveKeys;
072
073    public YAMLDataFormat() {
074        this(YAMLLibrary.SnakeYAML);
075    }
076
077    public YAMLDataFormat(YAMLLibrary library) {
078        super("yaml-" + library.name().toLowerCase());
079        this.library = library;
080    }
081
082    public YAMLDataFormat(YAMLLibrary library, Class<?> unmarshalType) {
083        super("yaml-" + library.name().toLowerCase());
084        this.library = library;
085        this.unmarshalType = unmarshalType;
086    }
087
088    public YAMLLibrary getLibrary() {
089        return library;
090    }
091
092    /**
093     * Which yaml library to use.
094     * <p/>
095     * By default it is SnakeYAML
096     */
097    public void setLibrary(YAMLLibrary library) {
098        this.library = library;
099        setDataFormatName("yaml-" + library.name().toLowerCase());
100    }
101
102    public Class<?> getUnmarshalType() {
103        return unmarshalType;
104    }
105
106    /**
107     * Class of the object to be created
108     */
109    public void setUnmarshalType(Class<?> type) {
110        this.unmarshalType = type;
111    }
112
113    public String getUnmarshalTypeName() {
114        return unmarshalTypeName;
115    }
116
117    /**
118     * Class name of the java type to use when unarmshalling
119     */
120    public void setUnmarshalTypeName(String unmarshalTypeName) {
121        this.unmarshalTypeName = unmarshalTypeName;
122    }
123
124    public ClassLoader getClassLoader() {
125        return classLoader;
126    }
127
128    /**
129     * Set a custom classloader
130     */
131    public void setClassLoader(ClassLoader classLoader) {
132        this.classLoader = classLoader;
133    }
134
135    public String getConstructor() {
136        return constructor;
137    }
138
139    /**
140     * BaseConstructor to construct incoming documents.
141     */
142    public void setConstructor(String constructor) {
143        this.constructor = constructor;
144    }
145
146    public String getRepresenter() {
147        return representer;
148    }
149
150    /**
151     * Representer to emit outgoing objects.
152     */
153    public void setRepresenter(String representer) {
154        this.representer = representer;
155    }
156
157    public String getDumperOptions() {
158        return dumperOptions;
159    }
160
161    /**
162     * DumperOptions to configure outgoing objects.
163     */
164    public void setDumperOptions(String dumperOptions) {
165        this.dumperOptions = dumperOptions;
166    }
167
168    public String getResolver() {
169        return resolver;
170    }
171
172    /**
173     * Resolver to detect implicit type
174     */
175    public void setResolver(String resolver) {
176        this.resolver = resolver;
177    }
178
179    public String getUseApplicationContextClassLoader() {
180        return useApplicationContextClassLoader;
181    }
182
183    /**
184     * Use ApplicationContextClassLoader as custom ClassLoader
185     */
186    public void setUseApplicationContextClassLoader(String useApplicationContextClassLoader) {
187        this.useApplicationContextClassLoader = useApplicationContextClassLoader;
188    }
189
190    public String getPrettyFlow() {
191        return prettyFlow;
192    }
193
194    /**
195     * Force the emitter to produce a pretty YAML document when using the flow
196     * style.
197     */
198    public void setPrettyFlow(String prettyFlow) {
199        this.prettyFlow = prettyFlow;
200    }
201
202    public String getAllowAnyType() {
203        return allowAnyType;
204    }
205
206    /**
207     * Allow any class to be un-marshaled
208     */
209    public void setAllowAnyType(String allowAnyType) {
210        this.allowAnyType = allowAnyType;
211    }
212
213    public List<YAMLTypeFilterDefinition> getTypeFilters() {
214        return typeFilters;
215    }
216
217    /**
218     * Set the types SnakeYAML is allowed to un-marshall
219     */
220    public void setTypeFilters(List<YAMLTypeFilterDefinition> typeFilters) {
221        this.typeFilters = typeFilters;
222    }
223
224    public String getMaxAliasesForCollections() {
225        return maxAliasesForCollections;
226    }
227
228    /**
229     * Set the maximum amount of aliases allowed for collections.
230     */
231    public void setMaxAliasesForCollections(String maxAliasesForCollections) {
232        this.maxAliasesForCollections = maxAliasesForCollections;
233    }
234
235    public String getAllowRecursiveKeys() {
236        return allowRecursiveKeys;
237    }
238
239    /**
240     * Set whether recursive keys are allowed.
241     */
242    public void setAllowRecursiveKeys(String allowRecursiveKeys) {
243        this.allowRecursiveKeys = allowRecursiveKeys;
244    }
245}