| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| org.apache.commons.configuration.ConfigurationConverter |
|
|
| 1 | /* |
|
| 2 | * Copyright 2001-2005 The Apache Software Foundation. |
|
| 3 | * |
|
| 4 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
| 5 | * you may not use this file except in compliance with the License. |
|
| 6 | * You may obtain a copy of the License at |
|
| 7 | * |
|
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
| 9 | * |
|
| 10 | * Unless required by applicable law or agreed to in writing, software |
|
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
| 13 | * See the License for the specific language governing permissions and |
|
| 14 | * limitations under the License. |
|
| 15 | */ |
|
| 16 | ||
| 17 | package org.apache.commons.configuration; |
|
| 18 | ||
| 19 | import java.util.Iterator; |
|
| 20 | import java.util.List; |
|
| 21 | import java.util.Map; |
|
| 22 | import java.util.Properties; |
|
| 23 | import java.util.Vector; |
|
| 24 | ||
| 25 | import org.apache.commons.collections.ExtendedProperties; |
|
| 26 | import org.apache.commons.lang.StringUtils; |
|
| 27 | ||
| 28 | /** |
|
| 29 | * Configuration converter. Helper class to convert between Configuration, |
|
| 30 | * ExtendedProperties and standard Properties. |
|
| 31 | * |
|
| 32 | * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a> |
|
| 33 | * @version $Revision$, $Date: 2005-10-03 20:12:54 +0200 (Mon, 03 Oct 2005) $ |
|
| 34 | */ |
|
| 35 | public final class ConfigurationConverter |
|
| 36 | { |
|
| 37 | /** |
|
| 38 | * Private constructor prevents instances from being created. |
|
| 39 | */ |
|
| 40 | private ConfigurationConverter() |
|
| 41 | 0 | { |
| 42 | // to prevent instanciation... |
|
| 43 | 0 | } |
| 44 | ||
| 45 | /** |
|
| 46 | * Convert a ExtendedProperties class into a Configuration class. |
|
| 47 | * |
|
| 48 | * @param eprops ExtendedProperties object to convert |
|
| 49 | * @return Configuration created from the ExtendedProperties |
|
| 50 | */ |
|
| 51 | public static Configuration getConfiguration(ExtendedProperties eprops) |
|
| 52 | { |
|
| 53 | 3 | return new MapConfiguration(eprops); |
| 54 | } |
|
| 55 | ||
| 56 | /** |
|
| 57 | * Convert a standard Properties class into a configuration class. |
|
| 58 | * |
|
| 59 | * @param props properties object to convert |
|
| 60 | * @return Configuration configuration created from the Properties |
|
| 61 | */ |
|
| 62 | public static Configuration getConfiguration(Properties props) |
|
| 63 | { |
|
| 64 | 9 | return new MapConfiguration(props); |
| 65 | } |
|
| 66 | ||
| 67 | /** |
|
| 68 | * Convert a Configuration class into a ExtendedProperties class. |
|
| 69 | * |
|
| 70 | * @param config Configuration object to convert |
|
| 71 | * @return ExtendedProperties created from the Configuration |
|
| 72 | */ |
|
| 73 | public static ExtendedProperties getExtendedProperties(Configuration config) |
|
| 74 | { |
|
| 75 | 3 | ExtendedProperties props = new ExtendedProperties(); |
| 76 | ||
| 77 | 3 | Iterator keys = config.getKeys(); |
| 78 | ||
| 79 | 15 | while (keys.hasNext()) |
| 80 | { |
|
| 81 | 9 | String key = (String) keys.next(); |
| 82 | 9 | Object property = config.getProperty(key); |
| 83 | ||
| 84 | // turn lists into vectors |
|
| 85 | 9 | if (property instanceof List) |
| 86 | { |
|
| 87 | 3 | property = new Vector((List) property); |
| 88 | } |
|
| 89 | ||
| 90 | 9 | props.setProperty(key, property); |
| 91 | } |
|
| 92 | ||
| 93 | 3 | return props; |
| 94 | } |
|
| 95 | ||
| 96 | /** |
|
| 97 | * Convert a Configuration class into a Properties class. List properties |
|
| 98 | * are joined into a string using the delimiter of the configuration if it |
|
| 99 | * extends AbstractConfiguration, and a comma otherwise. |
|
| 100 | * |
|
| 101 | * @param config Configuration object to convert |
|
| 102 | * @return Properties created from the Configuration |
|
| 103 | */ |
|
| 104 | public static Properties getProperties(Configuration config) |
|
| 105 | { |
|
| 106 | 6 | Properties props = new Properties(); |
| 107 | ||
| 108 | 6 | char delimiter = (config instanceof AbstractConfiguration) |
| 109 | ? ((AbstractConfiguration) config).getDelimiter() : ','; |
|
| 110 | ||
| 111 | 6 | Iterator keys = config.getKeys(); |
| 112 | 36 | while (keys.hasNext()) |
| 113 | { |
|
| 114 | 24 | String key = (String) keys.next(); |
| 115 | 24 | List list = config.getList(key); |
| 116 | ||
| 117 | // turn the list into a string |
|
| 118 | 24 | props.setProperty(key, StringUtils.join(list.iterator(), delimiter)); |
| 119 | } |
|
| 120 | ||
| 121 | 6 | return props; |
| 122 | } |
|
| 123 | ||
| 124 | /** |
|
| 125 | * Convert a Configuration class into a Map class. |
|
| 126 | * |
|
| 127 | * @param config Configuration object to convert |
|
| 128 | * @return Map created from the Configuration |
|
| 129 | */ |
|
| 130 | public static Map getMap(Configuration config) |
|
| 131 | { |
|
| 132 | 3 | return new ConfigurationMap(config); |
| 133 | } |
|
| 134 | ||
| 135 | } |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |