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