1 11 package org.eclipse.help.internal.toc; 12 13 import org.eclipse.core.runtime.Path; 14 15 public class HrefUtil { 16 19 public static String normalizeDirectoryHref(String pluginID, String dir) { 20 if (dir == null || dir.length() <= 0) 22 return null; 23 if (".".equals(dir)) dir = ""; if (dir.length() > 0 && dir.lastIndexOf('/') == dir.length() - 1) { 28 dir = dir.substring(0, dir.length() - 1); 29 } 30 return normalizeHref(pluginID, dir); 31 } 32 33 44 public final static String normalizeHref(String pluginID, String href) { 45 if (href == null) 46 return null; 47 if (href.startsWith("http:") || href.startsWith("https:") || href.startsWith("file:") || href.startsWith("jar:")) return href; 53 href = normalizeDirectoryPath(href); 54 if (href.startsWith("/")) return href; 57 if (href.startsWith("../")) { return href.substring(2); 59 } 60 if (href.length() > 0) { 61 StringBuffer buf = new StringBuffer (2 + pluginID.length() 62 + href.length()); 63 buf.append('/').append(pluginID); 64 buf.append('/').append(href); 65 return buf.toString(); 66 } 67 return "/" + pluginID; } 69 76 public static String getPluginIDFromHref(String href) { 77 if (href == null || href.length() < 2 || href.charAt(0) != '/') 78 return null; 79 int secondSlashIx = href.indexOf("/", 1); if (secondSlashIx < 0) return href.substring(1); 82 return href.substring(1, secondSlashIx); 84 } 85 86 93 public static String getResourcePathFromHref(String href) { 94 if (href == null) 95 return null; 96 int anchorIx = href.lastIndexOf("#"); if (anchorIx >= 0) href = href.substring(0, anchorIx); 100 if (href.length() < 2 || href.charAt(0) != '/') 101 return null; 102 int secondSlashIx = href.indexOf("/", 1); if (secondSlashIx < 0) return null; 105 if (secondSlashIx + 1 < href.length()) return href.substring(secondSlashIx + 1); 107 return ""; } 110 111 119 public static String normalizeDirectoryPath(String href) { 120 if (href != null) { 121 return new Path(href).toString(); 122 } 123 return null; 124 } 125 } 126 | Popular Tags |