##### Coverage Report - org.apache.commons.configuration.ConfigurationConverter

Classes in this File | Line Coverage | Branch Coverage | Complexity
ConfigurationConverter |
92% | 22/24
100% | 4/4
| 1.5;1,5

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.configuration;
19 |  |
20 |  |
     import java.util.Iterator;
21 |  |
     import java.util.List;
22 |  |
     import java.util.Map;
23 |  |
     import java.util.Properties;
24 |  |
     import java.util.Vector;
25 |  |
26 |  |
     import org.apache.commons.collections.ExtendedProperties;
27 |  |
     import org.apache.commons.lang.StringUtils;
28 |  |
29 |  |
     /**
30 |  |
      * Configuration converter. Helper class to convert between Configuration,
31 |  |
      * ExtendedProperties and standard Properties.
32 |  |
      *
33 |  |
      * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
34 |  |
      * @version $Revision: 439648 $, $Date: 2006-09-02 22:42:10 +0200 (Sa, 02 Sep 2006) $
35 |  |
      */
36 |  |
     public final class ConfigurationConverter
37 |  |
     {
38 |  |
         /**
39 |  |
          * Private constructor prevents instances from being created.
40 |  |
          */
41 |  |
         private ConfigurationConverter()
42 | 0 |
         {
43 |  |
             // to prevent instanciation...
44 | 0 |
         }
45 |  |
46 |  |
         /**
47 |  |
          * Convert a ExtendedProperties class into a Configuration class.
48 |  |
          *
49 |  |
          * @param eprops ExtendedProperties object to convert
50 |  |
          * @return Configuration created from the ExtendedProperties
51 |  |
          */
52 |  |
         public static Configuration getConfiguration(ExtendedProperties eprops)
53 |  |
         {
54 | 1 |
             return new MapConfiguration(eprops);
55 |  |
         }
56 |  |
57 |  |
         /**
58 |  |
          * Convert a standard Properties class into a configuration class.
59 |  |
          *
60 |  |
          * @param props properties object to convert
61 |  |
          * @return Configuration configuration created from the Properties
62 |  |
          */
63 |  |
         public static Configuration getConfiguration(Properties props)
64 |  |
         {
65 | 3 |
             return new MapConfiguration(props);
66 |  |
         }
67 |  |
68 |  |
         /**
69 |  |
          * Convert a Configuration class into a ExtendedProperties class.
70 |  |
          *
71 |  |
          * @param config Configuration object to convert
72 |  |
          * @return ExtendedProperties created from the Configuration
73 |  |
          */
74 |  |
         public static ExtendedProperties getExtendedProperties(Configuration config)
75 |  |
         {
76 | 1 |
             ExtendedProperties props = new ExtendedProperties();
77 |  |
78 | 1 |
             Iterator keys = config.getKeys();
79 |  |
80 | 4 |
             while (keys.hasNext())
81 |  |
             {
82 | 3 |
                 String key = (String) keys.next();
83 | 3 |
                 Object property = config.getProperty(key);
84 |  |
85 |  |
                 // turn lists into vectors
86 | 3 |
                 if (property instanceof List)
87 |  |
                 {
88 | 1 |
                     property = new Vector((List) property);
89 |  |
                 }
90 |  |
91 | 3 |
                 props.setProperty(key, property);
92 | 3 |
             }
93 |  |
94 | 1 |
             return props;
95 |  |
         }
96 |  |
97 |  |
         /**
98 |  |
          * Convert a Configuration class into a Properties class. List properties
99 |  |
          * are joined into a string using the delimiter of the configuration if it
100 |  |
          * extends AbstractConfiguration, and a comma otherwise.
101 |  |
          *
102 |  |
          * @param config Configuration object to convert
103 |  |
          * @return Properties created from the Configuration
104 |  |
          */
105 |  |
         public static Properties getProperties(Configuration config)
106 |  |
         {
107 | 2 |
             Properties props = new Properties();
108 |  |
109 | 2 |
             char delimiter = (config instanceof AbstractConfiguration)
110 |  |
                 ? ((AbstractConfiguration) config).getListDelimiter() : ',';
111 |  |
112 | 2 |
             Iterator keys = config.getKeys();
113 | 10 |
             while (keys.hasNext())
114 |  |
             {
115 | 8 |
                 String key = (String) keys.next();
116 | 8 |
                 List list = config.getList(key);
117 |  |
118 |  |
                 // turn the list into a string
119 | 8 |
                 props.setProperty(key, StringUtils.join(list.iterator(), delimiter));
120 | 8 |
             }
121 |  |
122 | 2 |
             return props;
123 |  |
         }
124 |  |
125 |  |
         /**
126 |  |
          * Convert a Configuration class into a Map class.
127 |  |
          *
128 |  |
          * @param config Configuration object to convert
129 |  |
          * @return Map created from the Configuration
130 |  |
          */
131 |  |
         public static Map getMap(Configuration config)
132 |  |
         {
133 | 1 |
             return new ConfigurationMap(config);
134 |  |
         }
135 |  |
136 |  |
     }