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 * https://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.configuration2;
19
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Properties;
24
25 import org.apache.commons.configuration2.convert.ListDelimiterHandler;
26 import org.apache.commons.lang3.StringUtils;
27
28 /**
29 * Configuration converter. Helper class to convert between Configuration, ExtendedProperties and standard Properties.
30 */
31 public final class ConfigurationConverter {
32
33 /** Constant for the default separator for properties with multiple values. */
34 private static final char DEFAULT_SEPARATOR = ',';
35
36 /**
37 * Convert a standard Properties class into a configuration class.
38 *
39 * @param props properties object to convert
40 * @return Configuration configuration created from the Properties
41 */
42 public static Configuration getConfiguration(final Properties props) {
43 return new MapConfiguration(props);
44 }
45
46 /**
47 * Convert a Configuration class into a Map class.
48 *
49 * @param config Configuration object to convert
50 * @return Map created from the Configuration
51 */
52 public static Map<Object, Object> getMap(final Configuration config) {
53 return new ConfigurationMap(config);
54 }
55
56 /**
57 * Convert a Configuration class into a Properties class. List properties are joined into a string using either the list
58 * delimiter handler of the configuration (if it extends AbstractConfiguration) or with a comma as delimiter otherwise.
59 * This version of the method exists only for backwards compatibility reason.
60 *
61 * @param config Configuration object to convert
62 * @return Properties created from the Configuration
63 */
64 public static Properties getProperties(final Configuration config) {
65 return getProperties((ImmutableConfiguration) config);
66 }
67
68 /**
69 * Convert a Configuration class into a Properties class. List properties are joined into a string using either the list
70 * delimiter handler of the configuration (if it extends AbstractConfiguration) or with a comma as delimiter otherwise.
71 *
72 * @param config ImmutableConfiguration object to convert
73 * @return Properties created from the Configuration
74 * @since 2.2
75 */
76 public static Properties getProperties(final ImmutableConfiguration config) {
77 final Properties props = new Properties();
78 final ListDelimiterHandler listHandler;
79 boolean useDelimiterHandler;
80
81 if (config instanceof AbstractConfiguration) {
82 listHandler = ((AbstractConfiguration) config).getListDelimiterHandler();
83 useDelimiterHandler = true;
84 } else {
85 listHandler = null;
86 useDelimiterHandler = false;
87 }
88
89 for (final Iterator<String> keys = config.getKeys(); keys.hasNext();) {
90 final String key = keys.next();
91 final List<Object> list = config.getList(key);
92
93 String propValue;
94 if (useDelimiterHandler) {
95 try {
96 propValue = String.valueOf(listHandler.escapeList(list, ListDelimiterHandler.NOOP_TRANSFORMER));
97 } catch (final Exception ex) {
98 // obviously, the list handler does not support splitting
99 useDelimiterHandler = false;
100 propValue = listToString(list);
101 }
102 } else {
103 propValue = listToString(list);
104 }
105
106 props.setProperty(key, propValue);
107 }
108
109 return props;
110 }
111
112 /**
113 * Helper method for joining all elements of a list to a string using the default value separator.
114 *
115 * @param list the list
116 * @return the resulting string
117 */
118 private static String listToString(final List<?> list) {
119 return StringUtils.join(list, DEFAULT_SEPARATOR);
120 }
121
122 /**
123 * Private constructor prevents instances from being created.
124 */
125 private ConfigurationConverter() {
126 // to prevent instantiation
127 }
128 }