1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.configuration;
18
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Properties;
23 import java.util.Vector;
24
25 import org.apache.commons.collections.ExtendedProperties;
26
27 /***
28 * Configuration converter. Helper class to convert between Configuration,
29 * ExtendedProperties and standard Properties.
30 *
31 * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
32 * @version $Revision: 155408 $, $Date: 2005-02-26 13:56:39 +0100 (Sa, 26 Feb 2005) $
33 */
34 public final class ConfigurationConverter
35 {
36 private ConfigurationConverter()
37 {
38
39 }
40
41 /***
42 * Convert a ExtendedProperties class into a Configuration class.
43 *
44 * @param eprops ExtendedProperties object to convert
45 * @return Configuration created from the ExtendedProperties
46 */
47 public static Configuration getConfiguration(ExtendedProperties eprops)
48 {
49 return new MapConfiguration(eprops);
50 }
51
52 /***
53 * Convert a standard Properties class into a configuration class.
54 *
55 * @param props properties object to convert
56 * @return Configuration configuration created from the Properties
57 */
58 public static Configuration getConfiguration(Properties props)
59 {
60 return new MapConfiguration(props);
61 }
62
63 /***
64 * Convert a Configuration class into a ExtendedProperties class.
65 *
66 * @param config Configuration object to convert
67 * @return ExtendedProperties created from the Configuration
68 */
69 public static ExtendedProperties getExtendedProperties(Configuration config)
70 {
71 ExtendedProperties props = new ExtendedProperties();
72
73 Iterator keys = config.getKeys();
74
75 while (keys.hasNext())
76 {
77 String key = (String) keys.next();
78 Object property = config.getProperty(key);
79
80
81 if (property instanceof List)
82 {
83 property = new Vector((List) property);
84 }
85
86 props.setProperty(key, property);
87 }
88
89 return props;
90 }
91
92 /***
93 * Convert a Configuration class into a Properties class. Multivalue keys
94 * will be collapsed into comma separated values.
95 *
96 * @param config Configuration object to convert
97 * @return Properties created from the Configuration
98 */
99 public static Properties getProperties(Configuration config)
100 {
101 Properties props = new Properties();
102
103 Iterator keys = config.getKeys();
104
105 while (keys.hasNext())
106 {
107 String key = (String) keys.next();
108 List list = config.getList(key);
109
110
111 StringBuffer property = new StringBuffer();
112 Iterator it = list.iterator();
113 while (it.hasNext())
114 {
115 property.append(String.valueOf(it.next()));
116 if (it.hasNext())
117 {
118 property.append(", ");
119 }
120 }
121
122 props.setProperty(key, property.toString());
123 }
124
125 return props;
126 }
127
128 /***
129 * Convert a Configuration class into a Map class.
130 *
131 * @param config Configuration object to convert
132 * @return Map created from the Configuration
133 */
134 public static Map getMap(Configuration config)
135 {
136 return new ConfigurationMap(config);
137 }
138
139 }