KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > util > url > URLUtils


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: URLUtils.java 1026 2006-08-04 14:54:10Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.util.url;
27
28 import java.io.File JavaDoc;
29 import java.net.MalformedURLException JavaDoc;
30 import java.net.URI JavaDoc;
31 import java.net.URISyntaxException JavaDoc;
32 import java.net.URL JavaDoc;
33
34 /**
35  * This class is used to :<br>
36  * <ul>
37  * <li> - get a File from an URL</li>
38  * <li> - get an URL from a file</li>
39  * </ul>
40  * In fact, when there are spaces in a directory name, file.toURL() doesn't
41  * escape spaces into %20, this is why this class will help.<br>
42  * This class has to be used both for encoding and decoding URL.<br>
43  * There are methods with checked exceptions or unchecked exceptions.
44  * @author Florent Benoit
45  */

46 public final class URLUtils {
47
48     /**
49      * File protocol.
50      */

51     public static final String JavaDoc FILE_PROTOCOL = "file";
52
53     /**
54      * Utility class.
55      */

56     private URLUtils() {
57
58     }
59
60     /**
61      * Gets an URL from a given file.(throws only runtime exception).
62      * @param file the given file
63      * @return the URL that has been built
64      */

65     public static URL JavaDoc fileToURL(final File JavaDoc file) {
66         try {
67             return fileToURL2(file);
68         } catch (URLUtilsException e) {
69             throw new IllegalArgumentException JavaDoc("Cannot get URL from the given file '" + file + "'.", e);
70         }
71     }
72
73     /**
74      * Gets an URL from a given file.
75      * @param file the given file
76      * @return the URL that has been built
77      * @throws URLUtilsException if the URL cannot be get from file.
78      */

79     public static URL JavaDoc fileToURL2(final File JavaDoc file) throws URLUtilsException {
80         // check
81
if (file == null) {
82             throw new URLUtilsException("Invalid File. It is null");
83         }
84
85         // transform
86
try {
87             return file.toURI().toURL();
88         } catch (MalformedURLException JavaDoc e) {
89             throw new URLUtilsException("Cannot get URL from the given file '" + file + "'.", e);
90         }
91     }
92
93     /**
94      * Gets a File object for the given URL.
95      * @param url the given url.
96      * @return File object
97      */

98     public static File JavaDoc urlToFile(final URL JavaDoc url) {
99         try {
100             return urlToFile2(url);
101         } catch (URLUtilsException e) {
102             throw new IllegalArgumentException JavaDoc("Cannot get File from the given url '" + url + "'.", e);
103         }
104     }
105
106     /**
107      * Gets a File object for the given URL.
108      * @param url the given url.
109      * @return File object
110      * @throws URLUtilsException if File cannot be get from URL
111      */

112     public static File JavaDoc urlToFile2(final URL JavaDoc url) throws URLUtilsException {
113         // check
114
if (url == null) {
115             throw new URLUtilsException("Invalid URL. It is null");
116         }
117
118         // Validate protocol
119
if (!url.getProtocol().equals(FILE_PROTOCOL)) {
120             throw new URLUtilsException("Invalid protocol named '" + url.getProtocol() + "'. Protocol should be '"
121                     + FILE_PROTOCOL + "'.");
122         }
123
124         try {
125             return new File JavaDoc(new URI JavaDoc(url.toString()));
126         } catch (URISyntaxException JavaDoc e) {
127             throw new URLUtilsException("Cannot get File from the given url '" + url + "'.", e);
128         }
129     }
130 }
131
Popular Tags