org.apache.commons.configuration2.AbstractYAMLBasedConfiguration Maven / Gradle / Ivy
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.commons.configuration2;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.commons.configuration2.io.ConfigurationLogger;
import org.apache.commons.configuration2.tree.ImmutableNode;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
* A base class for configuration implementations based on YAML structures.
*
*
* This base class offers functionality related to YAML-like data structures based on maps. Such a map has strings as
* keys and arbitrary objects as values. The class offers methods to transform such a map into a hierarchy of
* {@link ImmutableNode} objects and vice versa.
*
*
* @since 2.2
*/
public class AbstractYAMLBasedConfiguration extends BaseHierarchicalConfiguration {
/**
* Creates a new instance of {@code AbstractYAMLBasedConfiguration}.
*/
protected AbstractYAMLBasedConfiguration() {
initLogger(new ConfigurationLogger(getClass()));
}
/**
* Creates a new instance of {@code AbstractYAMLBasedConfiguration} as a copy of the specified configuration.
*
* @param c the configuration to be copied
*/
protected AbstractYAMLBasedConfiguration(final HierarchicalConfiguration c) {
super(c);
initLogger(new ConfigurationLogger(getClass()));
}
/**
* Loads this configuration from the content of the specified map. The data in the map is transformed into a hierarchy
* of {@link ImmutableNode} objects.
*
* @param map the map to be processed
*/
protected void load(final Map map) {
final List roots = constructHierarchy("", map);
getNodeModel().setRootNode(roots.get(0));
}
/**
* Constructs a YAML map, i.e. String -> Object from a given configuration node.
*
* @param node The configuration node to create a map from.
* @return A Map that contains the configuration node information.
*/
protected Map constructMap(final ImmutableNode node) {
final Map map = new HashMap<>(node.getChildren().size());
for (final ImmutableNode cNode : node) {
final Object value = cNode.getChildren().isEmpty() ? cNode.getValue() : constructMap(cNode);
addEntry(map, cNode.getNodeName(), value);
}
return map;
}
/**
* Adds a key value pair to a map, taking list structures into account. If a key is added which is already present in
* the map, this method ensures that a list is created.
*
* @param map the map
* @param key the key
* @param value the value
*/
private static void addEntry(final Map map, final String key, final Object value) {
final Object oldValue = map.get(key);
if (oldValue == null) {
map.put(key, value);
} else if (oldValue instanceof Collection) {
// safe case because the collection was created by ourselves
@SuppressWarnings("unchecked")
final Collection
© 2015 - 2025 Weber Informatics LLC | Privacy Policy