KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > myvietnam > mvncore > configuration > ConfigurationConverter


1 package net.myvietnam.mvncore.configuration;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowledgement:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgement may appear in the software itself,
26  * if and wherever such third-party acknowledgements normally appear.
27  *
28  * 4. The names "The Jakarta Project", "Commons", and "Apache Software
29  * Foundation" must not be used to endorse or promote products derived
30  * from this software without prior written permission. For written
31  * permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache"
34  * nor may "Apache" appear in their names without prior written
35  * permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import java.util.Enumeration JavaDoc;
58 import java.util.Iterator JavaDoc;
59 import java.util.Properties JavaDoc;
60 import org.apache.commons.collections.ExtendedProperties;
61
62
63 /**
64  * Configuration converter. <br>
65  * Helper class to convert between Configuration, ExtendedProperties and
66  * standard Properties.
67  *
68  * @author <a HREF="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
69  * @version $Id: ConfigurationConverter.java,v 1.2 2004/05/28 22:02:58 skoehler Exp $
70  */

71 public class ConfigurationConverter
72 {
73     /**
74      * Convert a ExtendedProperties class into a Configuration class.
75      *
76      * @param ep ExtendedProperties object to convert
77      * @return Configuration created from the ExtendedProperties
78      */

79     public static Configuration getConfiguration(ExtendedProperties ep)
80     {
81         Configuration config = new BaseConfiguration();
82         for (Iterator JavaDoc i = ep.getKeys(); i.hasNext();)
83         {
84             String JavaDoc key = (String JavaDoc) i.next();
85             config.setProperty(key, ep.getProperty(key));
86         }
87         return config;
88     }
89
90     /**
91      * Convert a standard properties class into a configuration class.
92      *
93      * @param p properties object to convert
94      * @return Configuration configuration created from the Properties
95      */

96     public static Configuration getConfiguration(Properties JavaDoc p)
97     {
98         Configuration config = new BaseConfiguration();
99         for (Enumeration JavaDoc e = p.keys(); e.hasMoreElements();)
100         {
101             String JavaDoc key = (String JavaDoc) e.nextElement();
102             config.setProperty(key, p.getProperty(key));
103         }
104         return config;
105     }
106
107     /**
108      * Convert a Configuration class into a ExtendedProperties class.
109      *
110      * @param c Configuration object to convert
111      * @return ExtendedProperties created from the Configuration
112      */

113     public static ExtendedProperties getExtendedProperties(Configuration c)
114     {
115         ExtendedProperties props = new ExtendedProperties();
116         for (Iterator JavaDoc i = c.getKeys(); i.hasNext();)
117         {
118             String JavaDoc key = (String JavaDoc) i.next();
119             props.setProperty(key, c.getProperty(key));
120         }
121         return props;
122     }
123
124     /**
125      * Convert a Configuration class into a Properties class. Multvalue keys
126      * will be collapsed by {@link Configuration#getString(String)}.
127      *
128      * @param c Configuration object to convert
129      * @return Properties created from the Configuration
130      */

131     public static Properties JavaDoc getProperties(Configuration c)
132     {
133         Properties JavaDoc props = new Properties JavaDoc();
134
135         Iterator JavaDoc iter = c.getKeys();
136
137         while (iter.hasNext())
138         {
139             String JavaDoc key = (String JavaDoc) iter.next();
140             props.setProperty(key, c.getString(key));
141         }
142
143         return props;
144     }
145 }
146
Popular Tags