1 31 32 package org.apache.commons.httpclient.util; 33 34 import java.io.UnsupportedEncodingException ; 35 import java.util.BitSet ; 36 import org.apache.commons.httpclient.URI; 37 import org.apache.commons.httpclient.URIException; 38 39 47 48 public class URIUtil { 49 50 52 protected static final BitSet empty = new BitSet (1); 53 54 56 62 public static String getName(String uri) { 63 if (uri == null || uri.length() == 0) { return uri; } 64 String path = URIUtil.getPath(uri); 65 int at = path.lastIndexOf("/"); 66 int to = path.length(); 67 return (at >= 0) ? path.substring(at + 1, to) : path; 68 } 69 70 71 77 public static String getQuery(String uri) { 78 if (uri == null || uri.length() == 0) { return null; } 79 int at = uri.indexOf("//"); 81 int from = uri.indexOf( 82 "/", 83 at >= 0 ? (uri.lastIndexOf("/", at - 1) >= 0 ? 0 : at + 2) : 0 84 ); 85 int to = uri.length(); 87 at = uri.indexOf("?", from); 89 if (at >= 0) { 90 from = at + 1; 91 } else { 92 return null; 93 } 94 if (uri.lastIndexOf("#") > from) { 96 to = uri.lastIndexOf("#"); 97 } 98 return (from < 0 || from == to) ? null : uri.substring(from, to); 100 } 101 102 103 109 public static String getPath(String uri) { 110 if (uri == null) { 111 return null; 112 } 113 int at = uri.indexOf("//"); 115 int from = uri.indexOf( 116 "/", 117 at >= 0 ? (uri.lastIndexOf("/", at - 1) >= 0 ? 0 : at + 2) : 0 118 ); 119 int to = uri.length(); 121 if (uri.indexOf('?', from) != -1) { 123 to = uri.indexOf('?', from); 124 } 125 if (uri.lastIndexOf("#") > from && uri.lastIndexOf("#") < to) { 127 to = uri.lastIndexOf("#"); 128 } 129 return (from < 0) ? (at >= 0 ? "/" : uri) : uri.substring(from, to); 131 } 132 133 134 140 public static String getPathQuery(String uri) { 141 if (uri == null) { 142 return null; 143 } 144 int at = uri.indexOf("//"); 146 int from = uri.indexOf( 147 "/", 148 at >= 0 ? (uri.lastIndexOf("/", at - 1) >= 0 ? 0 : at + 2) : 0 149 ); 150 int to = uri.length(); 152 if (uri.lastIndexOf("#") > from) { 155 to = uri.lastIndexOf("#"); 156 } 157 return (from < 0) ? (at >= 0 ? "/" : uri) : uri.substring(from, to); 159 } 160 161 162 168 public static String getFromPath(String uri) { 169 if (uri == null) { 170 return null; 171 } 172 int at = uri.indexOf("//"); 174 int from = uri.indexOf( 175 "/", 176 at >= 0 ? (uri.lastIndexOf("/", at - 1) >= 0 ? 0 : at + 2) : 0 177 ); 178 return (from < 0) ? (at >= 0 ? "/" : uri) : uri.substring(from); 180 } 181 182 184 197 public static String encodeAll(String unescaped) throws URIException { 198 return encodeAll(unescaped, URI.getDefaultProtocolCharset()); 199 } 200 201 202 215 public static String encodeAll(String unescaped, String charset) 216 throws URIException { 217 218 return encode(unescaped, empty, charset); 219 } 220 221 222 236 public static String encodeWithinAuthority(String unescaped) 237 throws URIException { 238 239 return encodeWithinAuthority(unescaped, URI.getDefaultProtocolCharset()); 240 } 241 242 243 257 public static String encodeWithinAuthority(String unescaped, String charset) 258 throws URIException { 259 260 return encode(unescaped, URI.allowed_within_authority, charset); 261 } 262 263 264 276 public static String encodePathQuery(String unescaped) throws URIException { 277 return encodePathQuery(unescaped, URI.getDefaultProtocolCharset()); 278 } 279 280 281 293 public static String encodePathQuery(String unescaped, String charset) 294 throws URIException { 295 296 int at = unescaped.indexOf('?'); 297 if (at < 0) { 298 return encode(unescaped, URI.allowed_abs_path, charset); 299 } 300 return encode(unescaped.substring(0, at), URI.allowed_abs_path, charset) 302 + '?' + encode(unescaped.substring(at + 1), URI.allowed_query, charset); 303 } 304 305 306 321 public static String encodeWithinPath(String unescaped) 322 throws URIException { 323 324 return encodeWithinPath(unescaped, URI.getDefaultProtocolCharset()); 325 } 326 327 328 343 public static String encodeWithinPath(String unescaped, String charset) 344 throws URIException { 345 346 return encode(unescaped, URI.allowed_within_path, charset); 347 } 348 349 350 362 public static String encodePath(String unescaped) throws URIException { 363 return encodePath(unescaped, URI.getDefaultProtocolCharset()); 364 } 365 366 367 379 public static String encodePath(String unescaped, String charset) 380 throws URIException { 381 382 return encode(unescaped, URI.allowed_abs_path, charset); 383 } 384 385 386 401 public static String encodeWithinQuery(String unescaped) 402 throws URIException { 403 404 return encodeWithinQuery(unescaped, URI.getDefaultProtocolCharset()); 405 } 406 407 408 423 public static String encodeWithinQuery(String unescaped, String charset) 424 throws URIException { 425 426 return encode(unescaped, URI.allowed_within_query, charset); 427 } 428 429 430 445 public static String encodeQuery(String unescaped) throws URIException { 446 return encodeQuery(unescaped, URI.getDefaultProtocolCharset()); 447 } 448 449 450 465 public static String encodeQuery(String unescaped, String charset) 466 throws URIException { 467 468 return encode(unescaped, URI.allowed_query, charset); 469 } 470 471 472 485 public static String encode(String unescaped, BitSet allowed) 486 throws URIException { 487 488 return encode(unescaped, allowed, URI.getDefaultProtocolCharset()); 489 } 490 491 492 505 public static String encode(String unescaped, BitSet allowed, 506 String charset) throws URIException { 507 508 return new String (Coder.encode(unescaped, allowed, charset)); 509 } 510 511 512 524 public static String decode(String escaped) throws URIException { 525 return Coder.decode(escaped.toCharArray(), URI.getDefaultProtocolCharset()); 526 } 527 528 529 540 public static String decode(String escaped, String charset) 541 throws URIException { 542 543 return Coder.decode(escaped.toCharArray(), charset); 544 } 545 546 548 561 public static String toProtocolCharset(String target) throws URIException { 562 return toUsingCharset( 563 target, 564 URI.getDefaultDocumentCharset(), 565 URI.getDefaultProtocolCharset()); 566 } 567 568 569 581 public static String toProtocolCharset(String target, String charset) 582 throws URIException { 583 584 return toUsingCharset(target, URI.getDefaultDocumentCharset(), charset); 585 } 586 587 588 601 public static String toDocumentCharset(String target) throws URIException { 602 return toUsingCharset(target, URI.getDefaultProtocolCharset(), 603 URI.getDefaultDocumentCharset()); 604 } 605 606 607 619 public static String toDocumentCharset(String target, String charset) 620 throws URIException { 621 622 return toUsingCharset(target, URI.getDefaultProtocolCharset(), charset); 623 } 624 625 626 644 645 public static String toUsingCharset(String target, String fromCharset, 646 String toCharset) throws URIException { 647 648 try { 649 return new String (target.getBytes(fromCharset), toCharset); 650 } catch (UnsupportedEncodingException error) { 651 throw new URIException(URIException.UNSUPPORTED_ENCODING, 652 error.getMessage()); 653 } 654 } 655 656 658 662 protected static class Coder extends URI { 663 664 675 public static char[] encode(String unescapedComponent, BitSet allowed, String charset) 676 throws URIException { 677 678 return URI.encode(unescapedComponent, allowed, charset); 679 } 680 681 682 691 public static String decode(char[] escapedComponent, String charset) 692 throws URIException { 693 694 return URI.decode(escapedComponent, charset); 695 } 696 697 698 704 public static boolean verifyEscaped(char[] original) { 705 for (int i = 0; i < original.length; i++) { 706 int c = original[i]; 707 if (c > 128) { 708 return false; 709 } else if (c == '%') { 710 if (Character.digit(original[++i], 16) == -1 711 || Character.digit(original[++i], 16) == -1) { 712 return false; 713 } 714 } 715 } 716 return true; 717 } 718 719 720 729 public static String replace(String original, char[] from, char[] to) { 730 for (int i = from.length; i > 0; --i) { 731 original = replace(original, from[i], to[i]); 732 } 733 return original.toString(); 734 } 735 736 737 745 public static String replace(String original, char from, char to) { 746 StringBuffer result = new StringBuffer (original.length()); 747 int at, saved = 0; 748 do { 749 at = original.indexOf(from); 750 if (at >= 0) { 751 result.append(original.substring(0, at)); 752 result.append(to); 753 } else { 754 result.append(original.substring(saved)); 755 } 756 saved = at; 757 } while (at >= 0); 758 return result.toString(); 759 } 760 } 761 762 } 763 764 | Popular Tags |