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