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.reifier.dataformat; 018 019import java.util.ArrayList; 020import java.util.List; 021 022import org.apache.camel.CamelContext; 023import org.apache.camel.RuntimeCamelException; 024import org.apache.camel.model.DataFormatDefinition; 025import org.apache.camel.model.dataformat.YAMLDataFormat; 026import org.apache.camel.model.dataformat.YAMLLibrary; 027import org.apache.camel.model.dataformat.YAMLTypeFilterDefinition; 028import org.apache.camel.model.dataformat.YAMLTypeFilterType; 029import org.apache.camel.spi.DataFormat; 030import org.apache.camel.util.ObjectHelper; 031 032public class YAMLDataFormatReifier extends DataFormatReifier<YAMLDataFormat> { 033 034 public YAMLDataFormatReifier(DataFormatDefinition definition) { 035 super((YAMLDataFormat)definition); 036 } 037 038 @Override 039 protected DataFormat doCreateDataFormat(CamelContext camelContext) { 040 if (definition.getLibrary() == YAMLLibrary.SnakeYAML) { 041 setProperty(camelContext, this, "dataFormatName", "yaml-snakeyaml"); 042 } 043 044 return super.doCreateDataFormat(camelContext); 045 } 046 047 @Override 048 protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) { 049 if (definition.getLibrary() == YAMLLibrary.SnakeYAML) { 050 configureSnakeDataFormat(dataFormat, camelContext); 051 } 052 } 053 054 protected void configureSnakeDataFormat(DataFormat dataFormat, CamelContext camelContext) { 055 Class<?> yamlUnmarshalType = definition.getUnmarshalType(); 056 if (yamlUnmarshalType == null && definition.getUnmarshalTypeName() != null) { 057 try { 058 yamlUnmarshalType = camelContext.getClassResolver().resolveMandatoryClass(definition.getUnmarshalTypeName()); 059 } catch (ClassNotFoundException e) { 060 throw RuntimeCamelException.wrapRuntimeCamelException(e); 061 } 062 } 063 064 setProperty(dataFormat, camelContext, "unmarshalType", yamlUnmarshalType); 065 setProperty(dataFormat, camelContext, "classLoader", definition.getClassLoader()); 066 setProperty(dataFormat, camelContext, "useApplicationContextClassLoader", definition.isUseApplicationContextClassLoader()); 067 setProperty(dataFormat, camelContext, "prettyFlow", definition.isPrettyFlow()); 068 setProperty(dataFormat, camelContext, "allowAnyType", definition.isAllowAnyType()); 069 070 if (definition.getTypeFilters() != null && !definition.getTypeFilters().isEmpty()) { 071 List<String> typeFilterDefinitions = new ArrayList<>(definition.getTypeFilters().size()); 072 for (YAMLTypeFilterDefinition definition : definition.getTypeFilters()) { 073 String value = definition.getValue(); 074 075 if (!value.startsWith("type") && !value.startsWith("regexp")) { 076 YAMLTypeFilterType type = definition.getType(); 077 if (type == null) { 078 type = YAMLTypeFilterType.type; 079 } 080 081 value = type.name() + ":" + value; 082 } 083 084 typeFilterDefinitions.add(value); 085 } 086 087 setProperty(dataFormat, camelContext, "typeFilterDefinitions", typeFilterDefinitions); 088 } 089 090 setPropertyRef(dataFormat, camelContext, "constructor", definition.getConstructor()); 091 setPropertyRef(dataFormat, camelContext, "representer", definition.getRepresenter()); 092 setPropertyRef(dataFormat, camelContext, "dumperOptions", definition.getDumperOptions()); 093 setPropertyRef(dataFormat, camelContext, "resolver", definition.getResolver()); 094 } 095 096 protected void setProperty(DataFormat dataFormat, CamelContext camelContext, String propertyName, Object propertyValue) { 097 if (ObjectHelper.isNotEmpty(propertyValue)) { 098 setProperty(camelContext, dataFormat, propertyName, propertyValue); 099 } 100 } 101 102 protected void setPropertyRef(DataFormat dataFormat, CamelContext camelContext, String propertyName, String propertyValue) { 103 if (ObjectHelper.isNotEmpty(propertyValue)) { 104 // must be a reference value 105 String ref = propertyValue.startsWith("#") ? propertyValue : "#" + propertyValue; 106 setProperty(camelContext, dataFormat, propertyName, ref); 107 } 108 } 109 110}