1 17 18 19 20 package org.apache.lenya.util; 21 22 23 26 public class URLUtil { 27 32 public static void main(String [] args) { 33 System.out.println(URLUtil.complete("http://www.apache.org/download/index.html", 34 "../images/lenya.jpeg")); 35 System.out.println(URLUtil.complete("http://www.apache.org/download/index.html", 36 "/images/lenya.jpeg")); 37 } 38 39 47 public static String complete(String parent, String child) { 48 String url = child; 49 String urlLowCase = child.toLowerCase(); 50 String currentURLPath = parent.substring(0, parent.lastIndexOf("/")); 51 String rootURL = parent.substring(0, parent.indexOf("/", 8)); 52 53 if (urlLowCase.startsWith("/")) { 54 url = rootURL + url; 55 } else if (urlLowCase.startsWith("./")) { 56 url = currentURLPath + url.substring(1, url.length()); 57 } else if (urlLowCase.startsWith("../")) { 58 int back = 1; 59 60 while (urlLowCase.indexOf("../", back * 3) != -1) 61 back++; 62 63 int pos = currentURLPath.length(); 64 int count = back; 65 66 while (count-- > 0) { 67 pos = currentURLPath.lastIndexOf("/", pos) - 1; 68 } 69 70 url = currentURLPath.substring(0, pos + 2) + url.substring(3 * back, url.length()); 71 } else if (urlLowCase.startsWith("javascript:")) { 72 System.err.println(".parseHREF(): parseJavaScript is not implemented yet"); 74 } else if (urlLowCase.startsWith("#")) { 75 url = null; 77 } else if (urlLowCase.startsWith("mailto:")) { 78 url = null; 80 } else { 81 url = currentURLPath + "/" + url; 82 } 83 84 if (url != null) { 88 int i; 89 90 if ((i = url.indexOf("#")) != -1) { 91 url = url.substring(0, i); 92 } 93 } 94 95 return url; 96 } 97 } 98 | Popular Tags |