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.Iterator;
021    import java.util.List;
022    import java.util.Map;
023    import java.util.Properties;
024    import java.util.Vector;
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 $Revision: 439648 $, $Date: 2006-09-02 22:42:10 +0200 (Sa, 02. Sep 2006) $
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            Iterator keys = config.getKeys();
079    
080            while (keys.hasNext())
081            {
082                String key = (String) keys.next();
083                Object property = config.getProperty(key);
084    
085                // turn lists into vectors
086                if (property instanceof List)
087                {
088                    property = new Vector((List) property);
089                }
090    
091                props.setProperty(key, property);
092            }
093    
094            return props;
095        }
096    
097        /**
098         * Convert a Configuration class into a Properties class. List properties
099         * are joined into a string using the delimiter of the configuration if it
100         * extends AbstractConfiguration, and a comma otherwise.
101         *
102         * @param config Configuration object to convert
103         * @return Properties created from the Configuration
104         */
105        public static Properties getProperties(Configuration config)
106        {
107            Properties props = new Properties();
108    
109            char delimiter = (config instanceof AbstractConfiguration)
110                ? ((AbstractConfiguration) config).getListDelimiter() : ',';
111    
112            Iterator keys = config.getKeys();
113            while (keys.hasNext())
114            {
115                String key = (String) keys.next();
116                List list = config.getList(key);
117    
118                // turn the list into a string
119                props.setProperty(key, StringUtils.join(list.iterator(), delimiter));
120            }
121    
122            return props;
123        }
124    
125        /**
126         * Convert a Configuration class into a Map class.
127         *
128         * @param config Configuration object to convert
129         * @return Map created from the Configuration
130         */
131        public static Map getMap(Configuration config)
132        {
133            return new ConfigurationMap(config);
134        }
135    
136    }