    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
    018    package org.apache.commons.configuration;
    019
    020    import java.util.ArrayList;
    021    import java.util.Iterator;
    022    import java.util.List;
    023    import java.util.Map;
    024    import java.util.Properties;
    025
    026    import org.apache.commons.collections.ExtendedProperties;
    027    import org.apache.commons.lang.StringUtils;
    028
    029    /**
    030     * Configuration converter. Helper class to convert between Configuration,
    031     * ExtendedProperties and standard Properties.
    032     *
    033     * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
    034     * @version $Id: ConfigurationConverter.java 1208788 2011-11-30 21:15:37Z oheger $
    035     */
    036    public final class ConfigurationConverter
    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 ExtendedProperties class into a Configuration class.
    048         *
    049         * @param eprops ExtendedProperties object to convert
    050         * @return Configuration created from the ExtendedProperties
    051         */
    052        public static Configuration getConfiguration(ExtendedProperties eprops)
    053        {
    054            return new MapConfiguration(eprops);
    055        }
    056
    057        /**
    058         * Convert a standard Properties class into a configuration class.
    059         *
    060         * @param props properties object to convert
    061         * @return Configuration configuration created from the Properties
    062         */
    063        public static Configuration getConfiguration(Properties props)
    064        {
    065            return new MapConfiguration(props);
    066        }
    067
    068        /**
    069         * Convert a Configuration class into a ExtendedProperties class.
    070         *
    071         * @param config Configuration object to convert
    072         * @return ExtendedProperties created from the Configuration
    073         */
    074        public static ExtendedProperties getExtendedProperties(Configuration config)
    075        {
    076            ExtendedProperties props = new ExtendedProperties();
    077
    078            for (Iterator<String> keys = config.getKeys(); keys.hasNext();)
    079            {
    080                String key = keys.next();
    081                Object property = config.getProperty(key);
    082
    083                // turn lists into vectors
    084                if (property instanceof List)
    085                {
    086                    property = new ArrayList<Object>((List<?>) property);
    087                }
    088
    089                props.setProperty(key, property);
    090            }
    091
    092            return props;
    093        }
    094
    095        /**
    096         * Convert a Configuration class into a Properties class. List properties
    097         * are joined into a string using the delimiter of the configuration if it
    098         * extends AbstractConfiguration, and a comma otherwise.
    099         *
    100         * @param config Configuration object to convert
    101         * @return Properties created from the Configuration
    102         */
    103        public static Properties getProperties(Configuration config)
    104        {
    105            Properties props = new Properties();
    106
    107            char delimiter = (config instanceof AbstractConfiguration)
    108                ? ((AbstractConfiguration) config).getListDelimiter() : ',';
    109
    110            for (Iterator<String> keys = config.getKeys(); keys.hasNext();)
    111            {
    112                String key = keys.next();
    113                List<Object> list = config.getList(key);
    114
    115                // turn the list into a string
    116                props.setProperty(key, StringUtils.join(list.iterator(), delimiter));
    117            }
    118
    119            return props;
    120        }
    121
    122        /**
    123         * Convert a Configuration class into a Map class.
    124         *
    125         * @param config Configuration object to convert
    126         * @return Map created from the Configuration
    127         */
    128        public static Map<Object, Object> getMap(Configuration config)
    129        {
    130            return new ConfigurationMap(config);
    131        }
    132
    133    }