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