    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 */
    017
    018package org.apache.commons.configuration2;
    019
    020import java.util.Iterator;
    021import java.util.List;
    022import java.util.Map;
    023import java.util.Properties;
    024
    025import org.apache.commons.configuration2.convert.ListDelimiterHandler;
    026import org.apache.commons.lang3.StringUtils;
    027
    028/**
    029 * Configuration converter. Helper class to convert between Configuration,
    030 * ExtendedProperties and standard Properties.
    031 *
    032 * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
    033 * @version $Id: ConfigurationConverter.java 1842194 2018-09-27 22:24:23Z ggregory $
    034 */
    035public final class ConfigurationConverter
    036{
    037    /** Constant for the default separator for properties with multiple values. */
    038    private static final char DEFAULT_SEPARATOR = ',';
    039
    040    /**
    041     * Private constructor prevents instances from being created.
    042     */
    043    private ConfigurationConverter()
    044    {
    045        // to prevent instanciation...
    046    }
    047
    048    /**
    049     * Convert a standard Properties class into a configuration class.
    050     *
    051     * @param props properties object to convert
    052     * @return Configuration configuration created from the Properties
    053     */
    054    public static Configuration getConfiguration(final Properties props)
    055    {
    056        return new MapConfiguration(props);
    057    }
    058
    059    /**
    060     * Convert a Configuration class into a Properties class. List properties
    061     * are joined into a string using either the list delimiter handler of the
    062     * configuration (if it extends AbstractConfiguration) or with a comma as
    063     * delimiter otherwise.
    064     *
    065     * @param config ImmutableConfiguration object to convert
    066     * @return Properties created from the Configuration
    067     * @since 2.2
    068     */
    069    public static Properties getProperties(final ImmutableConfiguration config)
    070    {
    071        final Properties props = new Properties();
    072        ListDelimiterHandler listHandler;
    073        boolean useDelimiterHandler;
    074
    075        if (config instanceof AbstractConfiguration)
    076        {
    077            listHandler = ((AbstractConfiguration) config).getListDelimiterHandler();
    078            useDelimiterHandler = true;
    079        }
    080        else
    081        {
    082            listHandler = null;
    083            useDelimiterHandler = false;
    084        }
    085
    086        for (final Iterator<String> keys = config.getKeys(); keys.hasNext();)
    087        {
    088            final String key = keys.next();
    089            final List<Object> list = config.getList(key);
    090
    091            String propValue;
    092            if (useDelimiterHandler)
    093            {
    094                try
    095                {
    096                    propValue =
    097                            String.valueOf(listHandler.escapeList(list,
    098                                    ListDelimiterHandler.NOOP_TRANSFORMER));
    099                }
    100                catch (final Exception ex)
    101                {
    102                    // obviously, the list handler does not support splitting
    103                    useDelimiterHandler = false;
    104                    propValue = listToString(list);
    105                }
    106            }
    107            else
    108            {
    109                propValue = listToString(list);
    110            }
    111
    112            props.setProperty(key, propValue);
    113        }
    114
    115        return props;
    116    }
    117
    118    /**
    119     * Convert a Configuration class into a Properties class. List properties
    120     * are joined into a string using either the list delimiter handler of the
    121     * configuration (if it extends AbstractConfiguration) or with a comma as
    122     * delimiter otherwise.
    123     * This version of the method exists only for backwards compatibility reason.
    124     *
    125     * @param config Configuration object to convert
    126     * @return Properties created from the Configuration
    127     */
    128    public static Properties getProperties(final Configuration config)
    129    {
    130        return getProperties((ImmutableConfiguration) config);
    131    }
    132
    133    /**
    134     * Convert a Configuration class into a Map class.
    135     *
    136     * @param config Configuration object to convert
    137     * @return Map created from the Configuration
    138     */
    139    public static Map<Object, Object> getMap(final Configuration config)
    140    {
    141        return new ConfigurationMap(config);
    142    }
    143
    144    /**
    145     * Helper method for joining all elements of a list to a string using the
    146     * default value separator.
    147     *
    148     * @param list the list
    149     * @return the resulting string
    150     */
    151    private static String listToString(final List<?> list)
    152    {
    153        return StringUtils.join(list, DEFAULT_SEPARATOR);
    154    }
    155}