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;
021import java.util.Map;
022
023import org.apache.camel.CamelContext;
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;
029
030public class YAMLDataFormatReifier extends DataFormatReifier<YAMLDataFormat> {
031
032    public YAMLDataFormatReifier(CamelContext camelContext, DataFormatDefinition definition) {
033        super(camelContext, (YAMLDataFormat)definition);
034    }
035
036    @Override
037    protected void prepareDataFormatConfig(Map<String, Object> properties) {
038        if (definition.getLibrary() == YAMLLibrary.SnakeYAML) {
039            configureSnakeDataFormat(properties);
040        }
041    }
042
043    protected void configureSnakeDataFormat(Map<String, Object> properties) {
044        properties.put("unmarshalType", or(definition.getUnmarshalType(), definition.getUnmarshalTypeName()));
045        properties.put("classLoader", definition.getClassLoader());
046        properties.put("useApplicationContextClassLoader", definition.getUseApplicationContextClassLoader());
047        properties.put("prettyFlow", definition.getPrettyFlow());
048        properties.put("allowAnyType", definition.getAllowAnyType());
049        properties.put("typeFilterDefinitions", getTypeFilterDefinitions());
050        properties.put("constructor", definition.getConstructor());
051        properties.put("representer", definition.getRepresenter());
052        properties.put("dumperOptions", definition.getDumperOptions());
053        properties.put("resolver", definition.getResolver());
054        properties.put("maxAliasesForCollections", definition.getMaxAliasesForCollections());
055        properties.put("allowRecursiveKeys", definition.getAllowRecursiveKeys());
056    }
057
058    private List<String> getTypeFilterDefinitions() {
059        if (definition.getTypeFilters() != null && !definition.getTypeFilters().isEmpty()) {
060            List<String> typeFilterDefinitions = new ArrayList<>(definition.getTypeFilters().size());
061            for (YAMLTypeFilterDefinition definition : definition.getTypeFilters()) {
062                String value = parseString(definition.getValue());
063
064                if (!value.startsWith("type") && !value.startsWith("regexp")) {
065                    YAMLTypeFilterType type = parse(YAMLTypeFilterType.class, definition.getType());
066                    if (type == null) {
067                        type = YAMLTypeFilterType.type;
068                    }
069
070                    value = type.name() + ":" + value;
071                }
072
073                typeFilterDefinitions.add(value);
074            }
075            return typeFilterDefinitions;
076        } else {
077            return null;
078        }
079    }
080
081}