001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: URLUtils.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.util.url;
025:
026: import java.io.File;
027: import java.net.MalformedURLException;
028: import java.net.URI;
029: import java.net.URISyntaxException;
030: import java.net.URL;
031:
032: /**
033: * This class is used to :<br>
034: * <ul>
035: * <li> - get a File from an URL</li>
036: * <li> - get an URL from a file</li>
037: * </ul>
038: * In fact, when there are spaces in a directory name, file.toURL() doesn't
039: * escape spaces into %20, this is why this class will help.<br>
040: * This class has to be used both for encoding and decoding URL.<br>
041: * There are methods with checked exceptions or unchecked exceptions.
042: * @author Florent Benoit
043: */
044: public final class URLUtils {
045:
046: /**
047: * File protocol.
048: */
049: public static final String FILE_PROTOCOL = "file";
050:
051: /**
052: * Utility class.
053: */
054: private URLUtils() {
055:
056: }
057:
058: /**
059: * Gets an URL from a given file.(throws only runtime exception).
060: * @param file the given file
061: * @return the URL that has been built
062: */
063: public static URL fileToURL(final File file) {
064: try {
065: return fileToURL2(file);
066: } catch (URLUtilsException e) {
067: throw new IllegalArgumentException(
068: "Cannot get URL from the given file '" + file
069: + "'.", e);
070: }
071: }
072:
073: /**
074: * Gets an URL from a given file.
075: * @param file the given file
076: * @return the URL that has been built
077: * @throws URLUtilsException if the URL cannot be get from file.
078: */
079: public static URL fileToURL2(final File file)
080: throws URLUtilsException {
081: // check
082: if (file == null) {
083: throw new URLUtilsException("Invalid File. It is null");
084: }
085:
086: // transform
087: try {
088: return file.toURI().toURL();
089: } catch (MalformedURLException e) {
090: throw new URLUtilsException(
091: "Cannot get URL from the given file '" + file
092: + "'.", e);
093: }
094: }
095:
096: /**
097: * Gets a File object for the given URL.
098: * @param url the given url.
099: * @return File object
100: */
101: public static File urlToFile(final URL url) {
102: try {
103: return urlToFile2(url);
104: } catch (URLUtilsException e) {
105: throw new IllegalArgumentException(
106: "Cannot get File from the given url '" + url + "'.",
107: e);
108: }
109: }
110:
111: /**
112: * Gets a File object for the given URL.
113: * @param url the given url.
114: * @return File object
115: * @throws URLUtilsException if File cannot be get from URL
116: */
117: public static File urlToFile2(final URL url)
118: throws URLUtilsException {
119: // check
120: if (url == null) {
121: throw new URLUtilsException("Invalid URL. It is null");
122: }
123:
124: // Validate protocol
125: if (!url.getProtocol().equals(FILE_PROTOCOL)) {
126: throw new URLUtilsException("Invalid protocol named '"
127: + url.getProtocol() + "'. Protocol should be '"
128: + FILE_PROTOCOL + "'.");
129: }
130:
131: try {
132: return new File(new URI(url.toString()));
133: } catch (URISyntaxException e) {
134: throw new URLUtilsException(
135: "Cannot get File from the given url '" + url + "'.",
136: e);
137: }
138: }
139: }
|