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.Enumeration;
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
28 /***
29 * Configuration converter. Helper class to convert between Configuration,
30 * ExtendedProperties and standard Properties.
31 *
32 * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
33 * @version $Revision: 1.7 $, $Date: 2004/08/12 16:06:01 $
34 */
35 public final class ConfigurationConverter
36 {
37 private ConfigurationConverter()
38 {
39
40 }
41
42 /***
43 * Convert a ExtendedProperties class into a Configuration class.
44 *
45 * @param eprops ExtendedProperties object to convert
46 * @return Configuration created from the ExtendedProperties
47 */
48 public static Configuration getConfiguration(ExtendedProperties eprops)
49 {
50 Configuration config = new BaseConfiguration();
51
52 Iterator keys = eprops.getKeys();
53
54 while (keys.hasNext())
55 {
56 String key = (String) keys.next();
57 config.setProperty(key, eprops.getProperty(key));
58 }
59
60 return config;
61 }
62
63 /***
64 * Convert a standard Properties class into a configuration class.
65 *
66 * @param props properties object to convert
67 * @return Configuration configuration created from the Properties
68 */
69 public static Configuration getConfiguration(Properties props)
70 {
71 Configuration config = new BaseConfiguration();
72
73 Enumeration keys = props.keys();
74
75 while (keys.hasMoreElements())
76 {
77 String key = (String) keys.nextElement();
78 config.setProperty(key, props.getProperty(key));
79 }
80
81 return config;
82 }
83
84 /***
85 * Convert a Configuration class into a ExtendedProperties class.
86 *
87 * @param config Configuration object to convert
88 * @return ExtendedProperties created from the Configuration
89 */
90 public static ExtendedProperties getExtendedProperties(Configuration config)
91 {
92 ExtendedProperties props = new ExtendedProperties();
93
94 Iterator keys = config.getKeys();
95
96 while (keys.hasNext())
97 {
98 String key = (String) keys.next();
99 Object property = config.getProperty(key);
100
101
102 if (property instanceof List)
103 {
104 property = new Vector((List) property);
105 }
106
107 props.setProperty(key, property);
108 }
109
110 return props;
111 }
112
113 /***
114 * Convert a Configuration class into a Properties class. Multivalue keys
115 * will be collapsed into comma separated values.
116 *
117 * @param config Configuration object to convert
118 * @return Properties created from the Configuration
119 */
120 public static Properties getProperties(Configuration config)
121 {
122 Properties props = new Properties();
123
124 Iterator keys = config.getKeys();
125
126 while (keys.hasNext())
127 {
128 String key = (String) keys.next();
129 List list = config.getList(key);
130
131
132 StringBuffer property = new StringBuffer();
133 Iterator it = list.iterator();
134 while (it.hasNext())
135 {
136 property.append(String.valueOf(it.next()));
137 if (it.hasNext())
138 {
139 property.append(", ");
140 }
141 }
142
143 props.setProperty(key, property.toString());
144 }
145
146 return props;
147 }
148
149 /***
150 * Convert a Configuration class into a Map class.
151 *
152 * @param config Configuration object to convert
153 * @return Map created from the Configuration
154 */
155 public static Map getMap(Configuration config) {
156 return new ConfigurationMap(config);
157 }
158
159 }