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(defaultValue = "true") 057 private Boolean useApplicationContextClassLoader = true; 058 @XmlAttribute 059 @Metadata(defaultValue = "false") 060 private Boolean prettyFlow = false; 061 @XmlAttribute 062 @Metadata(defaultValue = "false") 063 private Boolean allowAnyType = false; 064 @XmlElement(name = "typeFilter") 065 private List<YAMLTypeFilterDefinition> typeFilters; 066 067 public YAMLDataFormat() { 068 this(YAMLLibrary.SnakeYAML); 069 } 070 071 public YAMLDataFormat(YAMLLibrary library) { 072 super("yaml-" + library.name().toLowerCase()); 073 this.library = library; 074 } 075 076 public YAMLDataFormat(YAMLLibrary library, Class<?> unmarshalType) { 077 super("yaml-" + library.name().toLowerCase()); 078 this.library = library; 079 this.unmarshalType = unmarshalType; 080 } 081 082 public YAMLLibrary getLibrary() { 083 return library; 084 } 085 086 /** 087 * Which yaml library to use. 088 * <p/> 089 * By default it is SnakeYAML 090 */ 091 public void setLibrary(YAMLLibrary library) { 092 this.library = library; 093 setDataFormatName("yaml-" + library.name().toLowerCase()); 094 } 095 096 public Class<?> getUnmarshalType() { 097 return unmarshalType; 098 } 099 100 /** 101 * Class of the object to be created 102 */ 103 public void setUnmarshalType(Class<?> type) { 104 this.unmarshalType = type; 105 } 106 107 public String getUnmarshalTypeName() { 108 return unmarshalTypeName; 109 } 110 111 /** 112 * Class name of the java type to use when unarmshalling 113 */ 114 public void setUnmarshalTypeName(String unmarshalTypeName) { 115 this.unmarshalTypeName = unmarshalTypeName; 116 } 117 118 public ClassLoader getClassLoader() { 119 return classLoader; 120 } 121 122 /** 123 * Set a custom classloader 124 */ 125 public void setClassLoader(ClassLoader classLoader) { 126 this.classLoader = classLoader; 127 } 128 129 public String getConstructor() { 130 return constructor; 131 } 132 133 /** 134 * BaseConstructor to construct incoming documents. 135 */ 136 public void setConstructor(String constructor) { 137 this.constructor = constructor; 138 } 139 140 public String getRepresenter() { 141 return representer; 142 } 143 144 /** 145 * Representer to emit outgoing objects. 146 */ 147 public void setRepresenter(String representer) { 148 this.representer = representer; 149 } 150 151 public String getDumperOptions() { 152 return dumperOptions; 153 } 154 155 /** 156 * DumperOptions to configure outgoing objects. 157 */ 158 public void setDumperOptions(String dumperOptions) { 159 this.dumperOptions = dumperOptions; 160 } 161 162 public String getResolver() { 163 return resolver; 164 } 165 166 /** 167 * Resolver to detect implicit type 168 */ 169 public void setResolver(String resolver) { 170 this.resolver = resolver; 171 } 172 173 public boolean isUseApplicationContextClassLoader() { 174 return useApplicationContextClassLoader; 175 } 176 177 /** 178 * Use ApplicationContextClassLoader as custom ClassLoader 179 */ 180 public void setUseApplicationContextClassLoader(boolean useApplicationContextClassLoader) { 181 this.useApplicationContextClassLoader = useApplicationContextClassLoader; 182 } 183 184 public boolean isPrettyFlow() { 185 return prettyFlow; 186 } 187 188 /** 189 * Force the emitter to produce a pretty YAML document when using the flow 190 * style. 191 */ 192 public void setPrettyFlow(boolean prettyFlow) { 193 this.prettyFlow = prettyFlow; 194 } 195 196 public boolean isAllowAnyType() { 197 return allowAnyType; 198 } 199 200 /** 201 * Allow any class to be un-marshaled 202 */ 203 public void setAllowAnyType(boolean allowAnyType) { 204 this.allowAnyType = allowAnyType; 205 } 206 207 public List<YAMLTypeFilterDefinition> getTypeFilters() { 208 return typeFilters; 209 } 210 211 /** 212 * Set the types SnakeYAML is allowed to un-marshall 213 */ 214 public void setTypeFilters(List<YAMLTypeFilterDefinition> typeFilters) { 215 this.typeFilters = typeFilters; 216 } 217 218}