1 11 package org.eclipse.core.internal.runtime; 12 13 import java.net.MalformedURLException ; 14 import java.net.URL ; 15 import java.util.Vector ; 16 import org.eclipse.core.runtime.Assert; 17 18 21 public class URLTool { 22 23 43 public static URL appendTrailingSlash(URL url) { 44 String file = url.getFile(); 45 if (file.endsWith("/")) return url; 47 try { 48 return new URL (url.getProtocol(), url.getHost(), url.getPort(), file + "/"); } catch (MalformedURLException e) { 50 Assert.isTrue(false, "internal error"); } 52 return null; 53 } 54 55 61 public static URL getChild(URL parent, String member) { 62 String file = parent.getFile(); 63 if (!file.endsWith("/")) file = file + "/"; try { 66 return new URL (parent.getProtocol(), parent.getHost(), parent.getPort(), file + member); 67 } catch (MalformedURLException e) { 68 Assert.isTrue(false, "internal error"); } 70 return null; 71 } 72 73 93 public static Vector getElements(URL url) { 94 Vector result = new Vector (5); 95 String lastElement = null; 96 while ((lastElement = getLastElement(url)) != null) { 97 result.insertElementAt(lastElement, 0); 98 url = getParent(url); 99 } 100 return result; 101 } 102 103 125 public static String getLastElement(URL url) { 126 String file = url.getFile(); 127 int len = file.length(); 128 if (len == 0 || len == 1 && file.charAt(0) == '/') 129 return null; 130 131 int lastSlashIndex = -1; 132 for (int i = len - 2; lastSlashIndex == -1 && i >= 0; --i) { 133 if (file.charAt(i) == '/') 134 lastSlashIndex = i; 135 } 136 boolean isDirectory = file.charAt(len - 1) == '/'; 137 if (lastSlashIndex == -1) { 138 if (isDirectory) { 139 return file.substring(0, len - 1); 140 } else { 141 return file; 142 } 143 } else { 144 if (isDirectory) { 145 return file.substring(lastSlashIndex + 1, len - 1); 146 } else { 147 return file.substring(lastSlashIndex + 1, len); 148 } 149 } 150 } 151 152 171 public static URL getParent(URL url) { 172 String file = url.getFile(); 173 int len = file.length(); 174 if (len == 0 || len == 1 && file.charAt(0) == '/') 175 return null; 176 int lastSlashIndex = -1; 177 for (int i = len - 2; lastSlashIndex == -1 && i >= 0; --i) { 178 if (file.charAt(i) == '/') 179 lastSlashIndex = i; 180 } 181 if (lastSlashIndex == -1) 182 file = ""; else 184 file = file.substring(0, lastSlashIndex + 1); 185 186 try { 187 url = new URL (url.getProtocol(), url.getHost(), url.getPort(), file); 188 } catch (MalformedURLException e) { 189 Assert.isTrue(false, e.getMessage()); 190 } 191 return url; 192 } 193 194 213 public static URL getRoot(String urlString) throws MalformedURLException { 214 return getRoot(new URL (urlString)); 215 } 216 217 235 public static URL getRoot(URL url) { 236 try { 237 return new URL (url.getProtocol(), url.getHost(), url.getPort(), "/"); } catch (MalformedURLException e) { 239 Assert.isTrue(false, "internal error"); } 241 242 return null; 243 } 244 245 264 public static URL removeTrailingSlash(URL url) { 265 String file = url.getFile(); 266 267 if (file.endsWith("/")) { file = file.substring(0, file.length() - 1); 269 try { 270 return new URL (url.getProtocol(), url.getHost(), url.getPort(), file); 271 } catch (MalformedURLException e) { 272 Assert.isTrue(false, e.getMessage()); 273 } 274 } else { 275 return url; 276 } 277 278 return null; 279 } 280 281 314 public static boolean urlsOverlap(URL url1, URL url2) { 315 if (!getRoot(url1).equals(getRoot(url2))) { 316 return false; 317 } 318 319 Vector elements1 = URLTool.getElements(url1); 320 Vector elements2 = URLTool.getElements(url2); 321 322 for (int i = 0; i < elements1.size() && i < elements2.size(); ++i) { 323 String element1 = (String ) elements1.elementAt(i); 324 String element2 = (String ) elements2.elementAt(i); 325 if (!element1.equals(element2)) { 326 return false; 327 } 328 } 329 330 return true; 331 } 332 } 333 | Popular Tags |