1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.configuration;
19
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Properties;
24 import java.util.Vector;
25
26 import org.apache.commons.collections.ExtendedProperties;
27 import org.apache.commons.lang.StringUtils;
28
29 /***
30 * Configuration converter. Helper class to convert between Configuration,
31 * ExtendedProperties and standard Properties.
32 *
33 * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
34 * @version $Revision: 439648 $, $Date: 2006-09-02 22:42:10 +0200 (Sa, 02 Sep 2006) $
35 */
36 public final class ConfigurationConverter
37 {
38 /***
39 * Private constructor prevents instances from being created.
40 */
41 private ConfigurationConverter()
42 {
43
44 }
45
46 /***
47 * Convert a ExtendedProperties class into a Configuration class.
48 *
49 * @param eprops ExtendedProperties object to convert
50 * @return Configuration created from the ExtendedProperties
51 */
52 public static Configuration getConfiguration(ExtendedProperties eprops)
53 {
54 return new MapConfiguration(eprops);
55 }
56
57 /***
58 * Convert a standard Properties class into a configuration class.
59 *
60 * @param props properties object to convert
61 * @return Configuration configuration created from the Properties
62 */
63 public static Configuration getConfiguration(Properties props)
64 {
65 return new MapConfiguration(props);
66 }
67
68 /***
69 * Convert a Configuration class into a ExtendedProperties class.
70 *
71 * @param config Configuration object to convert
72 * @return ExtendedProperties created from the Configuration
73 */
74 public static ExtendedProperties getExtendedProperties(Configuration config)
75 {
76 ExtendedProperties props = new ExtendedProperties();
77
78 Iterator keys = config.getKeys();
79
80 while (keys.hasNext())
81 {
82 String key = (String) keys.next();
83 Object property = config.getProperty(key);
84
85
86 if (property instanceof List)
87 {
88 property = new Vector((List) property);
89 }
90
91 props.setProperty(key, property);
92 }
93
94 return props;
95 }
96
97 /***
98 * Convert a Configuration class into a Properties class. List properties
99 * 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
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 }