1 11 package org.eclipse.core.runtime.internal.adaptor; 12 13 import java.io.File ; 14 import java.net.MalformedURLException ; 15 import java.net.URL ; 16 17 20 public class LocationHelper { 21 27 public static URL buildURL(String spec, boolean trailingSlash) { 28 if (spec == null) 29 return null; 30 boolean isFile = spec.startsWith("file:"); try { 32 if (isFile) 33 return adjustTrailingSlash(new File (spec.substring(5)).toURL(), trailingSlash); 34 return new URL (spec); 35 } catch (MalformedURLException e) { 36 if (isFile) 39 return null; 40 try { 41 return adjustTrailingSlash(new File (spec).toURL(), trailingSlash); 42 } catch (MalformedURLException e1) { 43 return null; 44 } 45 } 46 } 47 48 private static URL adjustTrailingSlash(URL url, boolean trailingSlash) throws MalformedURLException { 49 String file = url.getFile(); 50 if (trailingSlash == (file.endsWith("/"))) return url; 52 file = trailingSlash ? file + "/" : file.substring(0, file.length() - 1); return new URL (url.getProtocol(), url.getHost(), file); 54 } 55 56 } 57 | Popular Tags |