2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
18
package org.apache.commons.configuration;
20
import java.util.ArrayList;
21
import java.util.Iterator;
22
import java.util.List;
24
import java.util.Properties;
26
import org.apache.commons.collections.ExtendedProperties;
27
import org.apache.commons.lang.StringUtils;
30
* Configuration converter. Helper class to convert between Configuration,
31
* ExtendedProperties and standard Properties.
33
* @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
34
* @version $Id: ConfigurationConverter.java 1208788 2011-11-30 21:15:37Z oheger $
36
public final class ConfigurationConverter
39
* Private constructor prevents instances from being created.
41
private ConfigurationConverter()
43
// to prevent instanciation...
47
* Convert a ExtendedProperties class into a Configuration class.
49
* @param eprops ExtendedProperties object to convert
50
* @return Configuration created from the ExtendedProperties
52
public static Configuration getConfiguration(ExtendedProperties eprops)
54
return new MapConfiguration(eprops);
58
* Convert a standard Properties class into a configuration class.
60
* @param props properties object to convert
61
* @return Configuration configuration created from the Properties
63
public static Configuration getConfiguration(Properties props)
65
return new MapConfiguration(props);
69
* Convert a Configuration class into a ExtendedProperties class.
71
* @param config Configuration object to convert
72
* @return ExtendedProperties created from the Configuration
74
public static ExtendedProperties getExtendedProperties(Configuration config)
76
ExtendedProperties props = new ExtendedProperties();
78
for (Iterator<String> keys = config.getKeys(); keys.hasNext();)
80
String key = keys.next();
81
Object property = config.getProperty(key);
83
// turn lists into vectors
84
if (property instanceof List)
86
property = new ArrayList<Object>((List<?>) property);
89
props.setProperty(key, property);
96
* Convert a Configuration class into a Properties class. List properties
97
* are joined into a string using the delimiter of the configuration if it
98
* extends AbstractConfiguration, and a comma otherwise.
100
* @param config Configuration object to convert
101
* @return Properties created from the Configuration
103
public static Properties getProperties(Configuration config)
105
Properties props = new Properties();
107
char delimiter = (config instanceof AbstractConfiguration)
108
? ((AbstractConfiguration) config).getListDelimiter() : ',';
110
for (Iterator<String> keys = config.getKeys(); keys.hasNext();)
112
String key = keys.next();
113
List<Object> list = config.getList(key);
115
// turn the list into a string
116
props.setProperty(key, StringUtils.join(list.iterator(), delimiter));
123
* Convert a Configuration class into a Map class.
125
* @param config Configuration object to convert
126
* @return Map created from the Configuration
128
public static Map<Object, Object> getMap(Configuration config)
130
return new ConfigurationMap(config);