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 * https://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 */ 017 018package org.apache.commons.configuration2; 019 020import java.util.ArrayList; 021import java.util.Collection; 022import java.util.Collections; 023import java.util.HashMap; 024import java.util.List; 025import java.util.Map; 026import java.util.stream.Collectors; 027 028import org.apache.commons.configuration2.ex.ConfigurationException; 029import org.apache.commons.configuration2.io.ConfigurationLogger; 030import org.apache.commons.configuration2.tree.ImmutableNode; 031 032/** 033 * <p> 034 * A base class for configuration implementations based on YAML structures. 035 * </p> 036 * <p> 037 * This base class offers functionality related to YAML-like data structures based on maps. Such a map has strings as 038 * keys and arbitrary objects as values. The class offers methods to transform such a map into a hierarchy of 039 * {@link ImmutableNode} objects and vice versa. 040 * </p> 041 * 042 * @since 2.2 043 */ 044public class AbstractYAMLBasedConfiguration extends BaseHierarchicalConfiguration { 045 046 /** 047 * Adds a key value pair to a map, taking list structures into account. If a key is added which is already present in 048 * the map, this method ensures that a list is created. 049 * 050 * @param map the map 051 * @param key the key 052 * @param value the value 053 */ 054 private static void addEntry(final Map<String, Object> map, final String key, final Object value) { 055 final Object oldValue = map.get(key); 056 if (oldValue == null) { 057 map.put(key, value); 058 } else if (oldValue instanceof Collection) { 059 // safe case because the collection was created by ourselves 060 @SuppressWarnings("unchecked") 061 final Collection<Object> values = (Collection<Object>) oldValue; 062 values.add(value); 063 } else { 064 final Collection<Object> values = new ArrayList<>(); 065 values.add(oldValue); 066 values.add(value); 067 map.put(key, values); 068 } 069 } 070 071 /** 072 * Creates a part of the hierarchical nodes structure of the resulting configuration. The passed in element is converted 073 * into one or multiple configuration nodes. (If list structures are involved, multiple nodes are returned.) 074 * 075 * @param key the key of the new node(s) 076 * @param elem the element to be processed 077 * @return a list with configuration nodes representing the element 078 */ 079 private static List<ImmutableNode> constructHierarchy(final String key, final Object elem) { 080 if (elem instanceof Map) { 081 return parseMap((Map<String, Object>) elem, key); 082 } 083 if (elem instanceof Collection) { 084 return parseCollection((Collection<Object>) elem, key); 085 } 086 return Collections.singletonList(new ImmutableNode.Builder().name(key).value(elem).create()); 087 } 088 089 /** 090 * Parses a collection structure. The elements of the collection are processed recursively. 091 * 092 * @param col the collection to be processed 093 * @param key the key under which this collection is to be stored 094 * @return a node representing this collection 095 */ 096 private static List<ImmutableNode> parseCollection(final Collection<Object> col, final String key) { 097 return col.stream().flatMap(elem -> constructHierarchy(key, elem).stream()).collect(Collectors.toList()); 098 } 099 100 /** 101 * Parses a map structure. The single keys of the map are processed recursively. 102 * 103 * @param map the map to be processed 104 * @param key the key under which this map is to be stored 105 * @return a node representing this map 106 */ 107 private static List<ImmutableNode> parseMap(final Map<String, Object> map, final String key) { 108 final ImmutableNode.Builder subtree = new ImmutableNode.Builder().name(key); 109 map.forEach((k, v) -> constructHierarchy(k, v).forEach(subtree::addChild)); 110 return Collections.singletonList(subtree.create()); 111 } 112 113 /** 114 * Internal helper method to wrap an exception in a {@code ConfigurationException}. 115 * 116 * @param e the exception to be wrapped 117 * @throws ConfigurationException the resulting exception 118 */ 119 static void rethrowException(final Exception e) throws ConfigurationException { 120 if (e instanceof ClassCastException) { 121 throw new ConfigurationException("Error parsing", e); 122 } 123 throw new ConfigurationException("Unable to load the configuration", e); 124 } 125 126 /** 127 * Creates a new instance of {@code AbstractYAMLBasedConfiguration}. 128 */ 129 protected AbstractYAMLBasedConfiguration() { 130 initLogger(new ConfigurationLogger(getClass())); 131 } 132 133 /** 134 * Creates a new instance of {@code AbstractYAMLBasedConfiguration} as a copy of the specified configuration. 135 * 136 * @param c the configuration to be copied 137 */ 138 protected AbstractYAMLBasedConfiguration(final HierarchicalConfiguration<ImmutableNode> c) { 139 super(c); 140 initLogger(new ConfigurationLogger(getClass())); 141 } 142 143 /** 144 * Constructs a YAML map, i.e. String -> Object from a given configuration node. 145 * 146 * @param node The configuration node to create a map from. 147 * @return A Map that contains the configuration node information. 148 */ 149 protected Map<String, Object> constructMap(final ImmutableNode node) { 150 final Map<String, Object> map = new HashMap<>(node.getChildren().size()); 151 node.forEach(cNode -> addEntry(map, cNode.getNodeName(), cNode.getChildren().isEmpty() ? cNode.getValue() : constructMap(cNode))); 152 return map; 153 } 154 155 /** 156 * Loads this configuration from the content of the specified map. The data in the map is transformed into a hierarchy 157 * of {@link ImmutableNode} objects. 158 * 159 * @param map the map to be processed 160 */ 161 protected void load(final Map<String, Object> map) { 162 final List<ImmutableNode> roots = constructHierarchy("", map); 163 getNodeModel().setRootNode(roots.get(0)); 164 } 165}