KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
58 import java.io.PrintStream JavaDoc;
59 import java.io.PrintWriter JavaDoc;
60 import java.io.StringWriter JavaDoc;
61 import java.net.MalformedURLException JavaDoc;
62 import java.net.URL JavaDoc;
63 import java.util.Iterator JavaDoc;
64
65 import org.apache.commons.lang.StringUtils;
66
67 /**
68  * Miscellaneous utility methods for configurations.
69  *
70  * @author <a HREF="mailto:herve.quiroz@esil.univ-mrs.fr">Herve Quiroz</a>
71  * @author <a HREF="mailto:oliver.heger@t-online.de">Oliver Heger</a>
72  */

73 public class ConfigurationUtils
74 {
75     /** File separator. */
76     protected static String JavaDoc fileSeparator = System.getProperty("file.separator");
77
78     private ConfigurationUtils()
79     {
80         // to prevent instanciation...
81
}
82
83     /**
84      * Dump the configuration key/value mappings to some ouput stream.
85      *
86      * @param configuration the configuration
87      * @param out the output stream to dump the configuration to
88      */

89     public static void dump(Configuration configuration, PrintStream JavaDoc out)
90     {
91         for (Iterator JavaDoc i = configuration.getKeys(); i.hasNext(); )
92         {
93             String JavaDoc key = (String JavaDoc) i.next();
94             Object JavaDoc value = configuration.getProperty(key);
95             out.print(key);
96             out.print("=");
97             out.print(value);
98             if (i.hasNext())
99             {
100                 out.println();
101             }
102         }
103     }
104
105     /**
106      * Dump the configuration key/value mappings to some writer.
107      *
108      * @param configuration the configuration
109      * @param out the writer to dump the configuration to
110      */

111     public static void dump(Configuration configuration, PrintWriter JavaDoc out)
112     {
113         for (Iterator JavaDoc i = configuration.getKeys(); i.hasNext();)
114         {
115             String JavaDoc key = (String JavaDoc) i.next();
116             Object JavaDoc value = configuration.getProperty(key);
117             out.print(key);
118             out.print("=");
119             out.print(value);
120
121             if (i.hasNext())
122             {
123                 out.println();
124             }
125         }
126     }
127
128     /**
129      * Get a string representation of the key/value mappings of a
130      * configuration.
131      *
132      * @param configuration the configuration
133      * @return a string representation of the configuration
134      */

135     public static String JavaDoc toString(Configuration configuration)
136     {
137         StringWriter JavaDoc writer = new StringWriter JavaDoc();
138         dump(configuration, new PrintWriter JavaDoc(writer));
139         return writer.toString();
140     }
141
142     /**
143      * Constructs a URL from a base path and a file name. The file name can
144      * be absolute, relative or a full URL. If necessary the base path URL is
145      * applied.
146      * @param basePath the base path URL (can be <b>null</b>)
147      * @param file the file name
148      * @return the resulting URL
149      * @throws MalformedURLException if URLs are invalid
150      */

151     public static URL JavaDoc getURL(String JavaDoc basePath, String JavaDoc file)
152     throws MalformedURLException JavaDoc
153     {
154         File JavaDoc f = new File JavaDoc(file);
155         if(f.isAbsolute()) // already absolute?
156
{
157             return f.toURL();
158         } /* if */
159
160         try
161         {
162             if(basePath == null)
163             {
164                 return new URL JavaDoc(file);
165             } /* if */
166             else
167             {
168                 URL JavaDoc base = new URL JavaDoc(basePath);
169                 return new URL JavaDoc(base, file);
170             } /* else */
171         } /* try */
172         catch(MalformedURLException JavaDoc uex)
173         {
174             return constructFile(basePath, file).toURL();
175         } /* catch */
176     }
177
178     /**
179      * Helper method for constructing a file object from a base path and a
180      * file name. This method is called if the base path passed to
181      * <code>getURL()</code> does not seem to be a valid URL.
182      * @param basePath the base path
183      * @param fileName the file name
184      * @return the resulting file
185      */

186     static File JavaDoc constructFile(String JavaDoc basePath, String JavaDoc fileName)
187     {
188         // code from DOM4JConfiguration
189
File JavaDoc file = null;
190         if (StringUtils.isEmpty(basePath))
191         {
192             // Good luck... This will fail 99 out of 100 times.
193
file = new File JavaDoc(fileName);
194         }
195         else
196         {
197             StringBuffer JavaDoc fName = new StringBuffer JavaDoc();
198             fName.append(basePath);
199
200             // My best friend. Paranoia.
201
if (!basePath.endsWith(fileSeparator))
202             {
203                 fName.append(fileSeparator);
204             }
205
206             //
207
// We have a relative path, and we have
208
// two possible forms here. If we have the
209
// "./" form then just strip that off first
210
// before continuing.
211
//
212
if (fileName.startsWith("." + fileSeparator))
213             {
214                 fName.append(fileName.substring(2));
215             }
216             else
217             {
218                 fName.append(fileName);
219             }
220
221             file = new File JavaDoc(fName.toString());
222         }
223         return file;
224     }
225 }
226
Popular Tags