001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.configuration2; 019 020import java.util.Iterator; 021import java.util.List; 022import java.util.Map; 023import java.util.Properties; 024 025import org.apache.commons.configuration2.convert.ListDelimiterHandler; 026import org.apache.commons.lang3.StringUtils; 027 028/** 029 * Configuration converter. Helper class to convert between Configuration, ExtendedProperties and standard Properties. 030 * 031 */ 032public final class ConfigurationConverter { 033 /** Constant for the default separator for properties with multiple values. */ 034 private static final char DEFAULT_SEPARATOR = ','; 035 036 /** 037 * Private constructor prevents instances from being created. 038 */ 039 private ConfigurationConverter() { 040 // to prevent instanciation... 041 } 042 043 /** 044 * Convert a standard Properties class into a configuration class. 045 * 046 * @param props properties object to convert 047 * @return Configuration configuration created from the Properties 048 */ 049 public static Configuration getConfiguration(final Properties props) { 050 return new MapConfiguration(props); 051 } 052 053 /** 054 * Convert a Configuration class into a Properties class. List properties are joined into a string using either the list 055 * delimiter handler of the configuration (if it extends AbstractConfiguration) or with a comma as delimiter otherwise. 056 * 057 * @param config ImmutableConfiguration object to convert 058 * @return Properties created from the Configuration 059 * @since 2.2 060 */ 061 public static Properties getProperties(final ImmutableConfiguration config) { 062 final Properties props = new Properties(); 063 final ListDelimiterHandler listHandler; 064 boolean useDelimiterHandler; 065 066 if (config instanceof AbstractConfiguration) { 067 listHandler = ((AbstractConfiguration) config).getListDelimiterHandler(); 068 useDelimiterHandler = true; 069 } else { 070 listHandler = null; 071 useDelimiterHandler = false; 072 } 073 074 for (final Iterator<String> keys = config.getKeys(); keys.hasNext();) { 075 final String key = keys.next(); 076 final List<Object> list = config.getList(key); 077 078 String propValue; 079 if (useDelimiterHandler) { 080 try { 081 propValue = String.valueOf(listHandler.escapeList(list, ListDelimiterHandler.NOOP_TRANSFORMER)); 082 } catch (final Exception ex) { 083 // obviously, the list handler does not support splitting 084 useDelimiterHandler = false; 085 propValue = listToString(list); 086 } 087 } else { 088 propValue = listToString(list); 089 } 090 091 props.setProperty(key, propValue); 092 } 093 094 return props; 095 } 096 097 /** 098 * Convert a Configuration class into a Properties class. List properties are joined into a string using either the list 099 * delimiter handler of the configuration (if it extends AbstractConfiguration) or with a comma as delimiter otherwise. 100 * This version of the method exists only for backwards compatibility reason. 101 * 102 * @param config Configuration object to convert 103 * @return Properties created from the Configuration 104 */ 105 public static Properties getProperties(final Configuration config) { 106 return getProperties((ImmutableConfiguration) config); 107 } 108 109 /** 110 * Convert a Configuration class into a Map class. 111 * 112 * @param config Configuration object to convert 113 * @return Map created from the Configuration 114 */ 115 public static Map<Object, Object> getMap(final Configuration config) { 116 return new ConfigurationMap(config); 117 } 118 119 /** 120 * Helper method for joining all elements of a list to a string using the default value separator. 121 * 122 * @param list the list 123 * @return the resulting string 124 */ 125 private static String listToString(final List<?> list) { 126 return StringUtils.join(list, DEFAULT_SEPARATOR); 127 } 128}