All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.boot.bind.YamlJavaBeanPropertyConstructor Maven / Gradle / Ivy

There is a newer version: 3.3.0
Show newest version
/*
 * Copyright 2012-2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.bind;

import java.beans.IntrospectionException;
import java.util.HashMap;
import java.util.Map;

import org.yaml.snakeyaml.constructor.Constructor;
import org.yaml.snakeyaml.introspector.Property;
import org.yaml.snakeyaml.introspector.PropertyUtils;
import org.yaml.snakeyaml.nodes.NodeId;

/**
 * Extended version of snakeyaml's Constructor class to facilitate mapping custom YAML
 * keys to JavaBean property names.
 *
 * @author Luke Taylor
 */
public class YamlJavaBeanPropertyConstructor extends Constructor {

	private final Map, Map> properties = new HashMap, Map>();

	private final PropertyUtils propertyUtils = new PropertyUtils();

	public YamlJavaBeanPropertyConstructor(Class theRoot) {
		super(theRoot);
		this.yamlClassConstructors.put(NodeId.mapping,
				new CustomPropertyConstructMapping());
	}

	public YamlJavaBeanPropertyConstructor(Class theRoot,
			Map, Map> propertyAliases) {
		this(theRoot);
		for (Class key : propertyAliases.keySet()) {
			Map map = propertyAliases.get(key);
			if (map != null) {
				for (String alias : map.keySet()) {
					addPropertyAlias(alias, key, map.get(alias));
				}
			}
		}
	}

	/**
	 * Adds an alias for a JavaBean property name on a particular type. The values of YAML
	 * keys with the alias name will be mapped to the JavaBean property.
	 * @param alias the alias to map
	 * @param type the type of property
	 * @param name the property name
	 */
	protected final void addPropertyAlias(String alias, Class type, String name) {
		Map typeMap = this.properties.get(type);

		if (typeMap == null) {
			typeMap = new HashMap();
			this.properties.put(type, typeMap);
		}

		try {
			typeMap.put(alias, this.propertyUtils.getProperty(type, name));
		}
		catch (IntrospectionException ex) {
			throw new RuntimeException(ex);
		}
	}

	/**
	 * Custom {@code ConstructMapping} to resolve properties.
	 */
	class CustomPropertyConstructMapping extends ConstructMapping {

		@Override
		protected Property getProperty(Class type, String name)
				throws IntrospectionException {
			Map forType = YamlJavaBeanPropertyConstructor.this.properties
					.get(type);
			Property property = (forType == null ? null : forType.get(name));
			return (property == null ? super.getProperty(type, name) : property);
		}

	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy