KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > configuration > ConfigurationConverter


1 /*
2  * Copyright 2001-2004 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 JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Properties JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 import org.apache.commons.collections.ExtendedProperties;
26
27 /**
28  * Configuration converter. Helper class to convert between Configuration,
29  * ExtendedProperties and standard Properties.
30  *
31  * @author <a HREF="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
32  * @version $Revision: 155408 $, $Date: 2005-02-26 13:56:39 +0100 (Sa, 26 Feb 2005) $
33  */

34 public final class ConfigurationConverter
35 {
36     private ConfigurationConverter()
37     {
38         // to prevent instanciation...
39
}
40
41     /**
42      * Convert a ExtendedProperties class into a Configuration class.
43      *
44      * @param eprops ExtendedProperties object to convert
45      * @return Configuration created from the ExtendedProperties
46      */

47     public static Configuration getConfiguration(ExtendedProperties eprops)
48     {
49         return new MapConfiguration(eprops);
50     }
51
52     /**
53      * Convert a standard Properties class into a configuration class.
54      *
55      * @param props properties object to convert
56      * @return Configuration configuration created from the Properties
57      */

58     public static Configuration getConfiguration(Properties JavaDoc props)
59     {
60         return new MapConfiguration(props);
61     }
62
63     /**
64      * Convert a Configuration class into a ExtendedProperties class.
65      *
66      * @param config Configuration object to convert
67      * @return ExtendedProperties created from the Configuration
68      */

69     public static ExtendedProperties getExtendedProperties(Configuration config)
70     {
71         ExtendedProperties props = new ExtendedProperties();
72
73         Iterator JavaDoc keys = config.getKeys();
74
75         while (keys.hasNext())
76         {
77             String JavaDoc key = (String JavaDoc) keys.next();
78             Object JavaDoc property = config.getProperty(key);
79
80             // turn lists into vectors
81
if (property instanceof List JavaDoc)
82             {
83                 property = new Vector JavaDoc((List JavaDoc) property);
84             }
85
86             props.setProperty(key, property);
87         }
88
89         return props;
90     }
91
92     /**
93      * Convert a Configuration class into a Properties class. Multivalue keys
94      * will be collapsed into comma separated values.
95      *
96      * @param config Configuration object to convert
97      * @return Properties created from the Configuration
98      */

99     public static Properties JavaDoc getProperties(Configuration config)
100     {
101         Properties JavaDoc props = new Properties JavaDoc();
102
103         Iterator JavaDoc keys = config.getKeys();
104
105         while (keys.hasNext())
106         {
107             String JavaDoc key = (String JavaDoc) keys.next();
108             List JavaDoc list = config.getList(key);
109
110             // turn lists into a string
111
StringBuffer JavaDoc property = new StringBuffer JavaDoc();
112             Iterator JavaDoc it = list.iterator();
113             while (it.hasNext())
114             {
115                 property.append(String.valueOf(it.next()));
116                 if (it.hasNext())
117                 {
118                     property.append(", ");
119                 }
120             }
121
122             props.setProperty(key, property.toString());
123         }
124
125         return props;
126     }
127
128     /**
129      * Convert a Configuration class into a Map class.
130      *
131      * @param config Configuration object to convert
132      * @return Map created from the Configuration
133      */

134     public static Map JavaDoc getMap(Configuration config)
135     {
136         return new ConfigurationMap(config);
137     }
138
139 }
140
Popular Tags