1 11 package org.eclipse.core.internal.runtime.auth; 12 13 import java.net.MalformedURLException ; 14 import java.net.URL ; 15 import org.eclipse.core.runtime.Assert; 16 17 20 public class URLTool { 21 22 41 public static URL getParent(URL url) { 42 String file = url.getFile(); 43 int len = file.length(); 44 if (len == 0 || len == 1 && file.charAt(0) == '/') 45 return null; 46 int lastSlashIndex = -1; 47 for (int i = len - 2; lastSlashIndex == -1 && i >= 0; --i) { 48 if (file.charAt(i) == '/') 49 lastSlashIndex = i; 50 } 51 if (lastSlashIndex == -1) 52 file = ""; else 54 file = file.substring(0, lastSlashIndex + 1); 55 56 try { 57 url = new URL (url.getProtocol(), url.getHost(), url.getPort(), file); 58 } catch (MalformedURLException e) { 59 Assert.isTrue(false, e.getMessage()); 60 } 61 return url; 62 } 63 } 64 | Popular Tags |