Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ConfigurationConverter |
|
| 1.6666666666666667;1,667 |
1 | /* | |
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 | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
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. | |
16 | */ | |
17 | ||
18 | package org.apache.commons.configuration; | |
19 | ||
20 | import java.util.ArrayList; | |
21 | import java.util.Iterator; | |
22 | import java.util.List; | |
23 | import java.util.Map; | |
24 | import java.util.Properties; | |
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 $Id: ConfigurationConverter.java 1208788 2011-11-30 21:15:37Z oheger $ | |
35 | */ | |
36 | public final class ConfigurationConverter | |
37 | { | |
38 | /** | |
39 | * Private constructor prevents instances from being created. | |
40 | */ | |
41 | private ConfigurationConverter() | |
42 | 0 | { |
43 | // to prevent instanciation... | |
44 | 0 | } |
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 | 1 | 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 | 3 | 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 | 1 | ExtendedProperties props = new ExtendedProperties(); |
77 | ||
78 | 1 | for (Iterator<String> keys = config.getKeys(); keys.hasNext();) |
79 | { | |
80 | 3 | String key = keys.next(); |
81 | 3 | Object property = config.getProperty(key); |
82 | ||
83 | // turn lists into vectors | |
84 | 3 | if (property instanceof List) |
85 | { | |
86 | 1 | property = new ArrayList<Object>((List<?>) property); |
87 | } | |
88 | ||
89 | 3 | props.setProperty(key, property); |
90 | 3 | } |
91 | ||
92 | 1 | return props; |
93 | } | |
94 | ||
95 | /** | |
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. | |
99 | * | |
100 | * @param config Configuration object to convert | |
101 | * @return Properties created from the Configuration | |
102 | */ | |
103 | public static Properties getProperties(Configuration config) | |
104 | { | |
105 | 3 | Properties props = new Properties(); |
106 | ||
107 | 3 | char delimiter = (config instanceof AbstractConfiguration) |
108 | ? ((AbstractConfiguration) config).getListDelimiter() : ','; | |
109 | ||
110 | 3 | for (Iterator<String> keys = config.getKeys(); keys.hasNext();) |
111 | { | |
112 | 9 | String key = keys.next(); |
113 | 9 | List<Object> list = config.getList(key); |
114 | ||
115 | // turn the list into a string | |
116 | 9 | props.setProperty(key, StringUtils.join(list.iterator(), delimiter)); |
117 | 9 | } |
118 | ||
119 | 3 | return props; |
120 | } | |
121 | ||
122 | /** | |
123 | * Convert a Configuration class into a Map class. | |
124 | * | |
125 | * @param config Configuration object to convert | |
126 | * @return Map created from the Configuration | |
127 | */ | |
128 | public static Map<Object, Object> getMap(Configuration config) | |
129 | { | |
130 | 1 | return new ConfigurationMap(config); |
131 | } | |
132 | ||
133 | } |