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