ConfigurationConverter.java

  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. package org.apache.commons.configuration2;

  18. import java.util.Iterator;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.Properties;

  22. import org.apache.commons.configuration2.convert.ListDelimiterHandler;
  23. import org.apache.commons.lang3.StringUtils;

  24. /**
  25.  * Configuration converter. Helper class to convert between Configuration, ExtendedProperties and standard Properties.
  26.  */
  27. public final class ConfigurationConverter {
  28.     /** Constant for the default separator for properties with multiple values. */
  29.     private static final char DEFAULT_SEPARATOR = ',';

  30.     /**
  31.      * Convert a standard Properties class into a configuration class.
  32.      *
  33.      * @param props properties object to convert
  34.      * @return Configuration configuration created from the Properties
  35.      */
  36.     public static Configuration getConfiguration(final Properties props) {
  37.         return new MapConfiguration(props);
  38.     }

  39.     /**
  40.      * Convert a Configuration class into a Map class.
  41.      *
  42.      * @param config Configuration object to convert
  43.      * @return Map created from the Configuration
  44.      */
  45.     public static Map<Object, Object> getMap(final Configuration config) {
  46.         return new ConfigurationMap(config);
  47.     }

  48.     /**
  49.      * Convert a Configuration class into a Properties class. List properties are joined into a string using either the list
  50.      * delimiter handler of the configuration (if it extends AbstractConfiguration) or with a comma as delimiter otherwise.
  51.      * This version of the method exists only for backwards compatibility reason.
  52.      *
  53.      * @param config Configuration object to convert
  54.      * @return Properties created from the Configuration
  55.      */
  56.     public static Properties getProperties(final Configuration config) {
  57.         return getProperties((ImmutableConfiguration) config);
  58.     }

  59.     /**
  60.      * Convert a Configuration class into a Properties class. List properties are joined into a string using either the list
  61.      * delimiter handler of the configuration (if it extends AbstractConfiguration) or with a comma as delimiter otherwise.
  62.      *
  63.      * @param config ImmutableConfiguration object to convert
  64.      * @return Properties created from the Configuration
  65.      * @since 2.2
  66.      */
  67.     public static Properties getProperties(final ImmutableConfiguration config) {
  68.         final Properties props = new Properties();
  69.         final ListDelimiterHandler listHandler;
  70.         boolean useDelimiterHandler;

  71.         if (config instanceof AbstractConfiguration) {
  72.             listHandler = ((AbstractConfiguration) config).getListDelimiterHandler();
  73.             useDelimiterHandler = true;
  74.         } else {
  75.             listHandler = null;
  76.             useDelimiterHandler = false;
  77.         }

  78.         for (final Iterator<String> keys = config.getKeys(); keys.hasNext();) {
  79.             final String key = keys.next();
  80.             final List<Object> list = config.getList(key);

  81.             String propValue;
  82.             if (useDelimiterHandler) {
  83.                 try {
  84.                     propValue = String.valueOf(listHandler.escapeList(list, ListDelimiterHandler.NOOP_TRANSFORMER));
  85.                 } catch (final Exception ex) {
  86.                     // obviously, the list handler does not support splitting
  87.                     useDelimiterHandler = false;
  88.                     propValue = listToString(list);
  89.                 }
  90.             } else {
  91.                 propValue = listToString(list);
  92.             }

  93.             props.setProperty(key, propValue);
  94.         }

  95.         return props;
  96.     }

  97.     /**
  98.      * Helper method for joining all elements of a list to a string using the default value separator.
  99.      *
  100.      * @param list the list
  101.      * @return the resulting string
  102.      */
  103.     private static String listToString(final List<?> list) {
  104.         return StringUtils.join(list, DEFAULT_SEPARATOR);
  105.     }

  106.     /**
  107.      * Private constructor prevents instances from being created.
  108.      */
  109.     private ConfigurationConverter() {
  110.         // to prevent instantiation
  111.     }
  112. }