1 27 package org.htmlparser.util; 28 29 import java.io.Serializable ; 30 import java.net.MalformedURLException ; 31 import java.net.URL ; 32 33 37 public class LinkProcessor 38 implements 39 Serializable 40 { 41 45 private String baseUrl; 46 47 50 public LinkProcessor () 51 { 52 baseUrl = null; 53 } 54 55 62 public String extract (String link, String base) 63 { 64 String ret; 65 66 try 67 { 68 if (null == link) 69 link = ""; 70 else 71 link = stripQuotes (link); 72 if (null != getBaseUrl ()) 73 base = getBaseUrl (); 74 if ((null == base) || ("".equals (link))) 75 ret = link; 76 else 77 { 78 URL url = constructUrl(link, base); 79 ret = url.toExternalForm (); 80 } 81 } 82 catch (MalformedURLException murle) 83 { 84 ret = link; 85 } 86 87 return (Translate.decode (ret)); 88 } 89 90 93 public String stripQuotes (String string) 94 { 95 if (string.startsWith ("\"") && string.endsWith ("\"") && (1 < string.length ())) 97 string = string.substring (1, string.length () - 1); 98 99 if (string.startsWith ("'") && string.endsWith ("'") && (1 < string.length ())) 101 string = string.substring (1, string.length () - 1); 102 103 return (string); 104 } 105 106 109 public URL constructUrl(String link, String base) 110 throws MalformedURLException { 111 String path; 112 boolean modified; 113 boolean absolute; 114 int index; 115 URL url; url = new URL (new URL (base), link); 117 path = url.getFile (); 118 modified = false; 119 absolute = link.startsWith ("/"); 120 if (!absolute) { while (path.startsWith ("/.")) { 123 if (path.startsWith ("/../")) { 124 path = path.substring (3); 125 modified = true; 126 } 127 else if (path.startsWith ("/./") || path.startsWith("/.")) { 128 path = path.substring (2); 129 modified = true; 130 } else break; 131 } 132 } 133 while (-1 != (index = path.indexOf ("/\\"))) { 135 path = path.substring (0, index + 1) + path.substring (index + 2); 136 modified = true; 137 } 138 if (modified) 139 url = new URL (url, path); 140 return url; 141 } 142 143 149 public static String fixSpaces (String url) 150 { 151 int index; 152 int length; 153 char ch; 154 StringBuffer returnURL; 155 156 index = url.indexOf (' '); 157 if (-1 != index) 158 { 159 length = url.length (); 160 returnURL = new StringBuffer (length * 3); 161 returnURL.append (url.substring (0, index)); 162 for (int i = index; i < length; i++) 163 { 164 ch = url.charAt (i); 165 if (ch==' ') 166 returnURL.append ("%20"); 167 else 168 returnURL.append (ch); 169 } 170 url = returnURL.toString (); 171 } 172 173 return (url); 174 } 175 176 181 public static boolean isURL (String resourceLocn) { 182 boolean ret; 183 184 try 185 { 186 new URL (resourceLocn); 187 ret = true; 188 } 189 catch (MalformedURLException murle) 190 { 191 ret = false; 192 } 193 194 return (ret); 195 } 196 197 201 public String getBaseUrl () 202 { 203 return baseUrl; 204 } 205 206 210 public void setBaseUrl (String baseUrl) 211 { 212 this.baseUrl = baseUrl; 213 } 214 215 218 public static String removeLastSlash(String baseUrl) { 219 if(baseUrl.charAt(baseUrl.length()-1)=='/') 220 { 221 return baseUrl.substring(0,baseUrl.length()-1); 222 } 223 else 224 { 225 return baseUrl; 226 } 227 } 228 229 } 230 | Popular Tags |