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