1 25 26 package org.objectweb.easybeans.util.url; 27 28 import java.io.File ; 29 import java.net.MalformedURLException ; 30 import java.net.URI ; 31 import java.net.URISyntaxException ; 32 import java.net.URL ; 33 34 46 public final class URLUtils { 47 48 51 public static final String FILE_PROTOCOL = "file"; 52 53 56 private URLUtils() { 57 58 } 59 60 65 public static URL fileToURL(final File file) { 66 try { 67 return fileToURL2(file); 68 } catch (URLUtilsException e) { 69 throw new IllegalArgumentException ("Cannot get URL from the given file '" + file + "'.", e); 70 } 71 } 72 73 79 public static URL fileToURL2(final File file) throws URLUtilsException { 80 if (file == null) { 82 throw new URLUtilsException("Invalid File. It is null"); 83 } 84 85 try { 87 return file.toURI().toURL(); 88 } catch (MalformedURLException e) { 89 throw new URLUtilsException("Cannot get URL from the given file '" + file + "'.", e); 90 } 91 } 92 93 98 public static File urlToFile(final URL url) { 99 try { 100 return urlToFile2(url); 101 } catch (URLUtilsException e) { 102 throw new IllegalArgumentException ("Cannot get File from the given url '" + url + "'.", e); 103 } 104 } 105 106 112 public static File urlToFile2(final URL url) throws URLUtilsException { 113 if (url == null) { 115 throw new URLUtilsException("Invalid URL. It is null"); 116 } 117 118 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 (new URI (url.toString())); 126 } catch (URISyntaxException e) { 127 throw new URLUtilsException("Cannot get File from the given url '" + url + "'.", e); 128 } 129 } 130 } 131 | Popular Tags |