1 18 package org.apache.batik.util; 19 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.net.URL ; 23 import java.util.ArrayList ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Map ; 28 29 import org.apache.batik.Version; 30 31 55 public class ParsedURL { 56 57 60 ParsedURLData data; 61 62 65 String userAgent; 66 67 70 private static Map handlersMap = null; 71 72 77 private static ParsedURLProtocolHandler defaultHandler 78 = new ParsedURLDefaultProtocolHandler(); 79 80 private static String globalUserAgent = "Batik/"+Version.getVersion(); 81 82 public static String getGlobalUserAgent() { return globalUserAgent; } 83 84 public static void setGlobalUserAgent(String userAgent) { 85 globalUserAgent = userAgent; 86 } 87 88 94 private static synchronized Map getHandlersMap() { 95 if (handlersMap != null) return handlersMap; 96 97 handlersMap = new HashMap (); 98 registerHandler(new ParsedURLDataProtocolHandler()); 99 registerHandler(new ParsedURLJarProtocolHandler()); 100 101 Iterator iter = Service.providers(ParsedURLProtocolHandler.class); 102 while (iter.hasNext()) { 103 ParsedURLProtocolHandler handler; 104 handler = (ParsedURLProtocolHandler)iter.next(); 105 106 registerHandler(handler); 108 } 109 110 111 return handlersMap; 112 113 } 114 115 121 public static synchronized ParsedURLProtocolHandler getHandler 122 (String protocol) { 123 if (protocol == null) 124 return defaultHandler; 125 126 Map handlers = getHandlersMap(); 127 ParsedURLProtocolHandler ret; 128 ret = (ParsedURLProtocolHandler)handlers.get(protocol); 129 if (ret == null) 130 ret = defaultHandler; 131 return ret; 132 } 133 134 141 public static synchronized void registerHandler 142 (ParsedURLProtocolHandler handler) { 143 if (handler.getProtocolHandled() == null) { 144 defaultHandler = handler; 145 return; 146 } 147 148 Map handlers = getHandlersMap(); 149 handlers.put(handler.getProtocolHandled(), handler); 150 } 151 152 159 public static InputStream checkGZIP(InputStream is) 160 throws IOException { 161 return ParsedURLData.checkGZIP(is); 162 } 163 164 168 public ParsedURL(String urlStr) { 169 this.userAgent = getGlobalUserAgent(); 170 this.data = parseURL(urlStr); 171 } 172 173 182 public ParsedURL(URL url) { 183 this.userAgent = getGlobalUserAgent(); 184 this.data = new ParsedURLData(url); 185 } 186 187 193 public ParsedURL(String baseStr, String urlStr) { 194 this.userAgent = getGlobalUserAgent(); 195 if (baseStr != null) 196 this.data = parseURL(baseStr, urlStr); 197 else 198 this.data = parseURL(urlStr); 199 } 200 201 207 public ParsedURL(URL baseURL, String urlStr) { 208 this.userAgent = getGlobalUserAgent(); 209 210 if (baseURL != null) 211 this.data = parseURL(new ParsedURL(baseURL), urlStr); 212 else 213 this.data = parseURL(urlStr); 214 } 215 216 222 public ParsedURL(ParsedURL baseURL, String urlStr) { 223 this.userAgent = baseURL.getUserAgent(); 224 if (baseURL != null) 225 this.data = parseURL(baseURL, urlStr); 226 else 227 this.data = parseURL(urlStr); 228 } 229 230 234 public String toString() { 235 return data.toString(); 236 } 237 238 243 public boolean equals(Object obj) { 244 if (obj == null) return false; 245 if (! (obj instanceof ParsedURL)) 246 return false; 247 ParsedURL purl = (ParsedURL)obj; 248 return data.equals(purl.data); 249 } 250 251 256 public int hashCode() { 257 return data.hashCode(); 258 } 259 260 265 public boolean complete() { 266 return data.complete(); 267 } 268 269 273 public String getUserAgent() { 274 return userAgent; 275 } 276 280 public void setUserAgent(String userAgent) { 281 this.userAgent = userAgent; 282 } 283 284 288 public String getProtocol() { 289 if (data.protocol == null) return null; 290 return new String (data.protocol); 291 } 292 293 297 public String getHost() { 298 if (data.host == null) return null; 299 return new String (data.host); 300 } 301 302 306 public int getPort() { return data.port; } 307 308 314 public String getPath() { 315 if (data.path == null) return null; 316 return new String (data.path); 317 } 318 319 322 public String getRef() { 323 if (data.ref == null) return null; 324 return new String (data.ref); 325 } 326 327 328 332 public String getPortStr() { 333 return data.getPortStr(); 334 } 335 336 340 public String getContentType() { 341 return data.getContentType(userAgent); 342 } 343 344 348 public String getContentEncoding() { 349 return data.getContentEncoding(userAgent); 350 } 351 352 356 public InputStream openStream() throws IOException { 357 return data.openStream(userAgent, null); 358 } 359 360 367 public InputStream openStream(String mimeType) throws IOException { 368 List mt = new ArrayList (1); 369 mt.add(mimeType); 370 return data.openStream(userAgent, mt.iterator()); 371 } 372 373 380 public InputStream openStream(String [] mimeTypes) throws IOException { 381 List mt = new ArrayList (mimeTypes.length); 382 for (int i=0; i<mimeTypes.length; i++) 383 mt.add(mimeTypes[i]); 384 return data.openStream(userAgent, mt.iterator()); 385 } 386 387 395 public InputStream openStream(Iterator mimeTypes) throws IOException { 396 return data.openStream(userAgent, mimeTypes); 397 } 398 399 403 public InputStream openStreamRaw() throws IOException { 404 return data.openStreamRaw(userAgent, null); 405 } 406 407 414 public InputStream openStreamRaw(String mimeType) throws IOException { 415 List mt = new ArrayList (1); 416 mt.add(mimeType); 417 return data.openStreamRaw(userAgent, mt.iterator()); 418 } 419 420 427 public InputStream openStreamRaw(String [] mimeTypes) throws IOException { 428 List mt = new ArrayList (mimeTypes.length); 429 for (int i=0; i<mimeTypes.length; i++) 430 mt.add(mimeTypes[i]); 431 return data.openStreamRaw(userAgent, mt.iterator()); 432 } 433 434 442 public InputStream openStreamRaw(Iterator mimeTypes) throws IOException { 443 return data.openStreamRaw(userAgent, mimeTypes); 444 } 445 446 public boolean sameFile(ParsedURL other) { 447 return data.sameFile(other.data); 448 } 449 450 451 456 protected static String getProtocol(String urlStr) { 457 if (urlStr == null) return null; 458 int idx = 0, len = urlStr.length(); 459 460 if (len == 0) return null; 461 462 char ch = urlStr.charAt(idx); 467 while ((ch == '-') || 468 (ch == '+') || 469 (ch == '.') || 470 ((ch >= 'a') && (ch <= 'z')) || 471 ((ch >= 'A') && (ch <= 'Z'))) { 472 idx++; 473 if (idx == len) { 474 ch=0; 475 break; 476 } 477 ch = urlStr.charAt(idx); 478 } 479 if (ch == ':') { 480 return urlStr.substring(0, idx).toLowerCase(); 482 } 483 return null; 484 } 485 486 490 public static ParsedURLData parseURL(String urlStr) { 491 ParsedURLProtocolHandler handler = getHandler(getProtocol(urlStr)); 492 return handler.parseURL(urlStr); 493 } 494 495 501 public static ParsedURLData parseURL(String baseStr, String urlStr) { 502 if (baseStr == null) 503 return parseURL(urlStr); 504 505 ParsedURL purl = new ParsedURL(baseStr); 506 return parseURL(purl, urlStr); 507 } 508 509 515 public static ParsedURLData parseURL(ParsedURL baseURL, String urlStr) { 516 if (baseURL == null) 517 return parseURL(urlStr); 518 519 String protocol = getProtocol(urlStr); 520 if (protocol == null) 521 protocol = baseURL.getProtocol(); 522 ParsedURLProtocolHandler handler = getHandler(protocol); 523 return handler.parseURL(baseURL, urlStr); 524 } 525 } 526 | Popular Tags |