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