1 18 package org.apache.batik.util; 19 20 import java.net.MalformedURLException ; 21 import java.net.URL ; 22 23 24 33 public class ParsedURLDefaultProtocolHandler 34 extends AbstractParsedURLProtocolHandler { 35 36 40 public ParsedURLDefaultProtocolHandler() { 41 super(null); 42 } 43 44 48 protected ParsedURLDefaultProtocolHandler(String protocol) { 49 super(protocol); 50 } 51 52 56 protected ParsedURLData constructParsedURLData() { 57 return new ParsedURLData(); 58 } 59 60 65 protected ParsedURLData constructParsedURLData(URL url) { 66 return new ParsedURLData(url); 67 } 68 69 74 public ParsedURLData parseURL(String urlStr) { 75 try { 76 URL url = new URL (urlStr); 77 return constructParsedURLData(url); 79 } catch (MalformedURLException mue) { 80 } 83 84 87 ParsedURLData ret = constructParsedURLData(); 88 89 if (urlStr == null) return ret; 90 91 int pidx=0, idx; 92 int len = urlStr.length(); 93 94 idx = urlStr.indexOf('#'); 96 ret.ref = null; 97 if (idx != -1) { 98 if (idx+1 < len) 99 ret.ref = urlStr.substring(idx+1); 100 urlStr = urlStr.substring(0,idx); 101 len = urlStr.length(); 102 } 103 104 if (len == 0) 105 return ret; 106 107 idx = 0; 112 char ch = urlStr.charAt(idx); 113 while ((ch == '-') || 114 (ch == '+') || 115 (ch == '.') || 116 ((ch >= 'a') && (ch <= 'z')) || 117 ((ch >= 'A') && (ch <= 'Z'))) { 118 idx++; 119 if (idx == len) { 120 ch=0; 121 break; 122 } 123 ch = urlStr.charAt(idx); 124 } 125 126 if (ch == ':') { 127 ret.protocol = urlStr.substring(pidx, idx).toLowerCase(); 129 pidx = idx+1; } 131 132 idx = urlStr.indexOf('/'); 134 if ((idx == -1) || ((pidx+2<len) && 135 (urlStr.charAt(pidx) == '/') && 136 (urlStr.charAt(pidx+1) == '/'))) { 137 if (idx != -1) 141 pidx+=2; 143 idx = urlStr.indexOf('/', pidx); String hostPort; 145 if (idx == -1) 146 hostPort = urlStr.substring(pidx); 148 else 149 hostPort = urlStr.substring(pidx, idx); 151 152 int hidx = idx; 154 idx = hostPort.indexOf(':'); 156 ret.port = -1; 157 if (idx == -1) { 158 if (hostPort.length() == 0) 160 ret.host = null; 161 else 162 ret.host = hostPort; 163 } else { 164 if (idx == 0) ret.host = null; 166 else ret.host = hostPort.substring(0,idx); 167 168 if (idx+1 < hostPort.length()) { 169 String portStr = hostPort.substring(idx+1); 170 try { 171 ret.port = Integer.parseInt(portStr); 172 } catch (NumberFormatException nfe) { 173 } 175 } 176 } 177 if (((ret.host == null) || (ret.host.indexOf('.') == -1)) && 178 (ret.port == -1)) 179 ret.host = null; 182 else 183 pidx = hidx; 184 } 185 186 if ((pidx == -1) || (pidx >= len)) return ret; 188 ret.path = urlStr.substring(pidx); 189 return ret; 190 } 191 192 public static String unescapeStr(String str) { 193 int idx = str.indexOf('%'); 194 if (idx == -1) return str; 196 int prev=0; 197 StringBuffer ret = new StringBuffer (); 198 while (idx != -1) { 199 if (idx != prev) 200 ret.append(str.substring(prev, idx)); 201 202 if (idx+2 >= str.length()) break; 203 prev = idx+3; 204 idx = str.indexOf('%', prev); 205 206 int ch1 = charToHex(str.charAt(idx+1)); 207 int ch2 = charToHex(str.charAt(idx+1)); 208 if ((ch1 == -1) || (ch2==-1)) continue; 209 ret.append((char)(ch1<<4 | ch2)); 210 } 211 212 return ret.toString(); 213 } 214 215 public static int charToHex(int ch) { 216 switch(ch) { 217 case '0': case '1': case '2': case '3': case '4': 218 case '5': case '6': case '7': case '8': case '9': 219 return ch-'0'; 220 case 'a': case 'A': return 10; 221 case 'b': case 'B': return 11; 222 case 'c': case 'C': return 12; 223 case 'd': case 'D': return 13; 224 case 'e': case 'E': return 14; 225 case 'f': case 'F': return 15; 226 default: return -1; 227 } 228 } 229 230 236 public ParsedURLData parseURL(ParsedURL baseURL, String urlStr) { 237 if (urlStr.length() == 0) 239 return baseURL.data; 240 241 244 int idx = 0, len = urlStr.length(); 245 if (len == 0) return baseURL.data; 246 247 char ch = urlStr.charAt(idx); 252 while ((ch == '-') || 253 (ch == '+') || 254 (ch == '.') || 255 ((ch >= 'a') && (ch <= 'z')) || 256 ((ch >= 'A') && (ch <= 'Z'))) { 257 idx++; 258 if (idx == len) { 259 ch=0; 260 break; 261 } 262 ch = urlStr.charAt(idx); 263 } 264 String protocol = null; 265 if (ch == ':') { 266 protocol = urlStr.substring(0, idx).toLowerCase(); 268 } 269 270 if (protocol != null) { 271 if (!protocol.equals(baseURL.getProtocol())) 278 return parseURL(urlStr); 280 281 idx++; 284 if (idx == urlStr.length()) 285 return parseURL(urlStr); 287 288 if (urlStr.charAt(idx) == '/') 289 return parseURL(urlStr); 291 292 urlStr = urlStr.substring(idx); 295 } 296 297 if (urlStr.startsWith("/")) { 298 if ((urlStr.length() > 1) && 299 (urlStr.charAt(1) == '/')) { 300 return parseURL(baseURL.getProtocol() + ":" + urlStr); 302 } 303 return parseURL(baseURL.getPortStr() + urlStr); 306 } 307 308 if (urlStr.startsWith("#")) { 309 String base = baseURL.getPortStr(); 310 if (baseURL.getPath() != null) base += baseURL.getPath(); 311 return parseURL(base + urlStr); 312 } 313 314 String path = baseURL.getPath(); 315 if (path == null) path = ""; 317 idx = path.lastIndexOf('/'); 318 if (idx == -1) 319 path = ""; 322 else 323 path = path.substring(0,idx+1); 324 325 return parseURL(baseURL.getPortStr() + path + urlStr); 328 } 329 } 330 331 | Popular Tags |