%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.commons.configuration.ConfigurationConverter |
|
|
1 | /* |
|
2 | * Copyright 2001-2004 The Apache Software Foundation. |
|
3 | * |
|
4 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
5 | * you may not use this file except in compliance with the License. |
|
6 | * You may obtain a copy of the License at |
|
7 | * |
|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 | * |
|
10 | * Unless required by applicable law or agreed to in writing, software |
|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 | * See the License for the specific language governing permissions and |
|
14 | * limitations under the License. |
|
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 | 0 | { |
38 | // to prevent instanciation... |
|
39 | 0 | } |
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 | 23 | 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 | 69 | 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 | 23 | ExtendedProperties props = new ExtendedProperties(); |
72 | ||
73 | 23 | Iterator keys = config.getKeys(); |
74 | ||
75 | 92 | while (keys.hasNext()) |
76 | { |
|
77 | 69 | String key = (String) keys.next(); |
78 | 69 | Object property = config.getProperty(key); |
79 | ||
80 | // turn lists into vectors |
|
81 | 69 | if (property instanceof List) |
82 | { |
|
83 | 23 | property = new Vector((List) property); |
84 | } |
|
85 | ||
86 | 69 | props.setProperty(key, property); |
87 | } |
|
88 | ||
89 | 23 | 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 | 23 | Properties props = new Properties(); |
102 | ||
103 | 23 | Iterator keys = config.getKeys(); |
104 | ||
105 | 69 | while (keys.hasNext()) |
106 | { |
|
107 | 46 | String key = (String) keys.next(); |
108 | 46 | List list = config.getList(key); |
109 | ||
110 | // turn lists into a string |
|
111 | 46 | StringBuffer property = new StringBuffer(); |
112 | 46 | Iterator it = list.iterator(); |
113 | 115 | while (it.hasNext()) |
114 | { |
|
115 | 69 | property.append(String.valueOf(it.next())); |
116 | 69 | if (it.hasNext()) |
117 | { |
|
118 | 23 | property.append(", "); |
119 | } |
|
120 | } |
|
121 | ||
122 | 46 | props.setProperty(key, property.toString()); |
123 | } |
|
124 | ||
125 | 23 | 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 | 23 | return new ConfigurationMap(config); |
137 | } |
|
138 | ||
139 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |