1 56 57 62 63 package com.hp.hpl.jena.rdf.arp; 64 65 import java.io.Serializable ; 66 import org.apache.commons.logging.Log; 67 import org.apache.commons.logging.LogFactory; 68 69 70 102 public class URI implements Serializable { 103 104 static Log logger = LogFactory.getLog(URI.class); 105 106 107 private static final String RESERVED_CHARACTERS = ";/?:@&=+$,[]"; 108 109 111 private static final String MARK_CHARACTERS = "-_.!~*'() "; 112 113 114 private static final String SCHEME_CHARACTERS = "+-."; 115 116 118 private static final String USERINFO_CHARACTERS = ";:&=+$,"; 119 120 121 private String m_scheme = null; 122 123 124 private String m_userinfo = null; 125 126 127 private String m_host = null; 128 129 130 private String m_port = null; 131 private int n_port = -1; 132 133 134 private String m_path = null; 135 139 private String m_subPaths[] = null; 140 141 143 private String m_queryString = null; 144 145 146 private String m_fragment = null; 147 148 private static boolean DEBUG = false; 149 150 153 public URI() { 154 } 155 156 162 public URI(URI p_other) { 163 initialize(p_other); 164 } 165 166 181 public URI(String p_uriSpec) throws MalformedURIException { 182 this((URI) null, p_uriSpec); 183 } 184 185 197 public URI(URI p_base, String p_uriSpec) throws MalformedURIException { 198 initialize(p_base, p_uriSpec); 199 } 200 201 213 public URI(String p_scheme, String p_schemeSpecificPart) 214 throws MalformedURIException { 215 if (p_scheme == null || p_scheme.length() == 0) { 216 throw new MalformedURIException("Cannot construct URI with null/empty scheme!"); 217 } 218 if (p_schemeSpecificPart == null 219 || p_schemeSpecificPart.length() == 0) { 220 throw new MalformedURIException("Cannot construct URI with null/empty scheme-specific part!"); 221 } 222 setScheme(p_scheme); 223 setPath(p_schemeSpecificPart); 224 } 225 226 247 public URI( 248 String p_scheme, 249 String p_host, 250 String p_path, 251 String p_queryString, 252 String p_fragment) 253 throws MalformedURIException { 254 this(p_scheme, null, p_host, -1, p_path, p_queryString, p_fragment); 255 } 256 257 282 public URI( 283 String p_scheme, 284 String p_userinfo, 285 String p_host, 286 int p_port, 287 String p_path, 288 String p_queryString, 289 String p_fragment) 290 throws MalformedURIException { 291 if (p_scheme == null || p_scheme.length() == 0) { 292 throw new MalformedURIException("Scheme is required!"); 293 } 294 295 if (p_host == null) { 296 if (p_userinfo != null) { 297 throw new MalformedURIException("Userinfo may not be specified if host is not specified!"); 298 } 299 if (p_port != -1) { 300 throw new MalformedURIException("Port may not be specified if host is not specified!"); 301 } 302 } 303 304 if (p_path != null) { 305 if (p_path.indexOf('?') != -1 && p_queryString != null) { 306 throw new MalformedURIException("Query string cannot be specified in path and query string!"); 307 } 308 309 if (p_path.indexOf('#') != -1 && p_fragment != null) { 310 throw new MalformedURIException("Fragment cannot be specified in both the path and fragment!"); 311 } 312 } 313 314 setScheme(p_scheme); 315 setHost(p_host); 316 setPort(p_port); 317 m_port = "" + p_port; 318 setUserinfo(p_userinfo); 319 setPath(p_path); 320 setQueryString(p_queryString); 321 setFragment(p_fragment); 322 } 323 324 329 private void initialize(URI p_other) { 330 m_scheme = p_other.getScheme(); 331 m_userinfo = p_other.getUserinfo(); 332 m_host = p_other.getHost(); 333 m_port = p_other.m_port; 334 n_port = p_other.n_port; 335 m_path = p_other.getPath(); 336 m_queryString = p_other.getQueryString(); 337 m_fragment = p_other.getFragment(); 338 } 339 340 356 private void initialize(URI p_base, String p_uriSpec) 357 throws MalformedURIException { 358 if (p_base == null && (p_uriSpec == null || p_uriSpec.length() == 0)) { 359 throw new RelativeURIException("Cannot initialize URI with empty parameters."); 360 } 361 362 if (p_uriSpec == null || p_uriSpec.length() == 0) { 364 initialize(p_base); 365 return; 366 } 367 368 String uriSpec = p_uriSpec; 369 int uriSpecLen = uriSpec.length(); 370 int index = 0; 371 372 int colonIdx = uriSpec.indexOf(':'); 376 int slashIdx = uriSpec.indexOf('/'); 377 int queryIdx = uriSpec.indexOf('?'); 378 int fragmentIdx = uriSpec.indexOf('#'); 379 380 if ((colonIdx < 2) 381 || (colonIdx > slashIdx && slashIdx != -1) 382 || (colonIdx > queryIdx && queryIdx != -1) 383 || (colonIdx > fragmentIdx && fragmentIdx != -1)) { 384 386 if (p_base == null ) { 392 throw new RelativeURIException("No scheme found in URI '" + p_uriSpec + "'" ); 394 } else { 395 if ((!p_base.isGenericURI()) && fragmentIdx != 0) 396 throw new MalformedURIException("Cannot apply relative URI to an opaque URI"); 398 } 399 } else { 400 initializeScheme(uriSpec); 401 index = m_scheme.length() + 1; 402 } 403 404 if (((index + 1) < uriSpecLen) 406 && (uriSpec.substring(index).startsWith("//"))) { 407 index += 2; 408 int startPos = index; 409 410 char testChar = '\0'; 412 while (index < uriSpecLen) { 413 testChar = uriSpec.charAt(index); 414 if (testChar == '/' || testChar == '?' || testChar == '#') { 415 break; 416 } 417 index++; 418 } 419 420 if (index > startPos) { 423 initializeAuthority(uriSpec.substring(startPos, index)); 424 } else { 425 m_host = ""; 426 } 427 } 428 429 initializePath(uriSpec.substring(index)); 430 431 if (p_base != null) { 437 438 if (m_path.length() == 0 && m_scheme == null && m_host == null) { 446 m_scheme = p_base.getScheme(); 447 m_userinfo = p_base.getUserinfo(); 448 m_host = p_base.getHost(); 449 m_port = p_base.m_port; 450 n_port = p_base.getPort(); 451 m_path = p_base.getPath(); 452 453 if (m_queryString == null) { 454 m_queryString = p_base.getQueryString(); 455 } 456 return; 457 } 458 459 if (m_scheme == null) { 462 m_scheme = p_base.getScheme(); 463 } else { 464 return; 465 } 466 467 if (m_host == null) { 470 m_userinfo = p_base.getUserinfo(); 471 m_host = p_base.getHost(); 472 m_port = p_base.m_port; 473 n_port = p_base.getPort(); 474 } else { 475 return; 476 } 477 478 if (m_path.length() > 0 && m_path.startsWith("/")) { 480 return; 481 } 482 483 String path = "/"; String basePath = p_base.getPath(); 488 489 if (basePath != null) { 491 int lastSlash = basePath.lastIndexOf('/'); 492 if (lastSlash != -1) { 493 path = basePath.substring(0, lastSlash + 1); 494 } 495 } 496 497 path = path.concat(m_path); 499 500 index = -1; 502 while ((index = path.indexOf("/./")) != -1) { 503 path = 504 path.substring(0, index + 1).concat( 505 path.substring(index + 3)); 506 } 507 508 if (path.endsWith("/.")) { 510 path = path.substring(0, path.length() - 1); 511 } 512 513 index = 1; 516 int segIndex = -1; 517 518 while ((index = path.indexOf("/../", index)) > 0) { 519 segIndex = path.lastIndexOf('/', index - 1); 520 if (segIndex != -1 521 && !path.substring(segIndex + 1, index).equals("..")) { 522 path = 523 path.substring(0, segIndex).concat( 524 path.substring(index + 3)); 525 index = segIndex; 526 } else { 527 index += 4; 528 } 529 } 530 531 if (path.endsWith("/..")) { 534 index = path.length() - 3; 535 segIndex = path.lastIndexOf('/', index - 1); 536 if (segIndex != -1 537 && !path.substring(segIndex + 1, index).equals("..")) { 538 path = path.substring(0, segIndex + 1); 539 } 540 } 541 542 m_path = path; 543 } 544 } 545 546 554 private void initializeScheme(String p_uriSpec) 555 throws MalformedURIException { 556 int uriSpecLen = p_uriSpec.length(); 557 int index = p_uriSpec.indexOf(':'); 558 559 if (index < 1) 560 throw new MalformedURIException("No scheme found in URI '" + p_uriSpec + "'" ); 561 562 if (index == uriSpecLen - 1) 563 throw new MalformedURIException( "A bare scheme name is not a URI: '" + p_uriSpec + "'" ); 564 565 setScheme(p_uriSpec.substring(0, index)); 566 } 567 568 576 private void initializeAuthority(String p_uriSpec) 577 throws MalformedURIException { 578 int index = 0; 579 int start = 0; 580 int end = p_uriSpec.length(); 581 char testChar = '\0'; 582 String userinfo = null; 583 584 if (p_uriSpec.indexOf('@', start) != -1) { 586 while (index < end) { 587 testChar = p_uriSpec.charAt(index); 588 if (testChar == '@') { 589 break; 590 } 591 index++; 592 } 593 userinfo = p_uriSpec.substring(start, index); 594 index++; 595 } 596 597 String host = null; 599 start = index; 600 while (index < end) { 601 testChar = p_uriSpec.charAt(index); 602 if (testChar == ':') { 603 break; 604 } 605 index++; 606 } 607 host = p_uriSpec.substring(start, index); 608 int port = -1; 609 if (host.length() > 0) { 610 if (testChar == ':') { 612 index++; 613 start = index; 614 while (index < end) { 615 index++; 616 } 617 String portStr = p_uriSpec.substring(start, index); 618 if (portStr.length() > 0) { 619 for (int i = 0; i < portStr.length(); i++) { 620 if (!isDigit(portStr.charAt(i))) { 621 throw new MalformedURIException( 622 portStr 623 + " is invalid. Port should only contain digits!"); 624 } 625 } 626 try { 627 port = Integer.parseInt(portStr); 628 m_port = portStr; 629 } catch (NumberFormatException nfe) { 630 } 632 } 633 } 634 } 635 setHost(host); 636 setPort(port); 637 setUserinfo(userinfo); 638 } 639 640 647 private void initializePath(String p_uriSpec) 648 throws MalformedURIException { 649 if (p_uriSpec == null) { 650 throw new MalformedURIException("Cannot initialize path from null string!"); 651 } 652 653 int index = 0; 654 int start = 0; 655 int end = p_uriSpec.length(); 656 char testChar = '\0'; 657 658 while (index < end) { 660 testChar = p_uriSpec.charAt(index); 661 if (testChar == '?' || testChar == '#') { 662 break; 663 } 664 if (testChar == '%') { 666 if (index + 2 >= end 667 || !isHex(p_uriSpec.charAt(index + 1)) 668 || !isHex(p_uriSpec.charAt(index + 2))) { 669 throw new MalformedURIException( "Path contains invalid escape sequence: " + p_uriSpec ); 670 } 671 } else if ( 672 !isReservedCharacter(testChar) 673 && !isUnreservedCharacter(testChar)) { 674 throw new MalformedURIException( 675 "Path '" + p_uriSpec + "' contains invalid character: " + testChar); 676 } 677 index++; 678 } 679 m_path = p_uriSpec.substring(start, index); 680 681 if (testChar == '?') { 683 index++; 684 start = index; 685 while (index < end) { 686 testChar = p_uriSpec.charAt(index); 687 if (testChar == '#') { 688 break; 689 } 690 if (testChar == '%') { 691 if (index + 2 >= end 692 || !isHex(p_uriSpec.charAt(index + 1)) 693 || !isHex(p_uriSpec.charAt(index + 2))) { 694 throw new MalformedURIException("Query string contains invalid escape sequence in '" + p_uriSpec + "'" ); 695 } 696 } else if ( 697 !isReservedCharacter(testChar) 698 && !isUnreservedCharacter(testChar)) { 699 throw new MalformedURIException( "Query string contains invalid character '" + testChar + "' in '" + p_uriSpec + "'" ); 700 } 701 index++; 702 } 703 m_queryString = p_uriSpec.substring(start, index); 704 } 705 706 if (testChar == '#') { 708 index++; 709 start = index; 710 while (index < end) { 711 testChar = p_uriSpec.charAt(index); 712 713 if (testChar == '%') { 714 if (index + 2 >= end 715 || !isHex(p_uriSpec.charAt(index + 1)) 716 || !isHex(p_uriSpec.charAt(index + 2))) { 717 throw new MalformedURIException( "Fragment contains invalid escape sequence in '" + p_uriSpec + "'" ); 718 } 719 } else if ( 720 !isReservedCharacter(testChar) 721 && !isUnreservedCharacter(testChar)) { 722 throw new MalformedURIException( 723 "Fragment contains invalid character '" + testChar + "' in '" + p_uriSpec + "'" ); 724 } 725 index++; 726 } 727 m_fragment = p_uriSpec.substring(start, index); 728 } 729 } 730 731 736 public String getScheme() { 737 return m_scheme; 738 } 739 740 746 public String getSchemeSpecificPart() { 747 StringBuffer schemespec = new StringBuffer (); 748 749 if (m_userinfo != null || m_host != null || m_port != null) { 750 schemespec.append("//"); 751 } 752 753 if (m_userinfo != null) { 754 schemespec.append(m_userinfo); 755 schemespec.append('@'); 756 } 757 758 if (m_host != null) { 759 schemespec.append(m_host); 760 } 761 762 if (m_port != null) { 763 schemespec.append(':'); 764 schemespec.append(m_port); 765 } 766 767 if (m_path != null) { 768 schemespec.append((m_path)); 769 } 770 771 if (m_queryString != null) { 772 schemespec.append('?'); 773 schemespec.append(m_queryString); 774 } 775 776 if (m_fragment != null) { 777 schemespec.append('#'); 778 schemespec.append(m_fragment); 779 } 780 781 return schemespec.toString(); 782 } 783 784 789 public String getUserinfo() { 790 return m_userinfo; 791 } 792 793 798 public String getHost() { 799 return m_host; 800 } 801 802 807 public int getPort() { 808 return n_port; 809 } 810 811 825 public String getPath( 826 boolean p_includeQueryString, 827 boolean p_includeFragment) { 828 StringBuffer pathString = new StringBuffer (m_path); 829 830 if (p_includeQueryString && m_queryString != null) { 831 pathString.append('?'); 832 pathString.append(m_queryString); 833 } 834 835 if (p_includeFragment && m_fragment != null) { 836 pathString.append('#'); 837 pathString.append(m_fragment); 838 } 839 return pathString.toString(); 840 } 841 842 848 public String getPath() { 849 return m_path; 850 } 851 852 859 public String getQueryString() { 860 return m_queryString; 861 } 862 863 870 public String getFragment() { 871 return m_fragment; 872 } 873 874 883 private void setScheme(String p_scheme) throws MalformedURIException { 884 if (p_scheme == null) { 885 throw new MalformedURIException("Cannot set scheme from null string!"); 886 } 887 if (!isConformantSchemeName(p_scheme)) { 888 throw new MalformedURIException("The scheme '" + p_scheme + "' is not conformant."); 889 } 890 891 m_scheme = p_scheme; } 893 894 903 private void setUserinfo(String p_userinfo) throws MalformedURIException { 904 if (p_userinfo == null) { 905 m_userinfo = null; 906 } else { 907 if (m_host == null) { 908 throw new MalformedURIException("Userinfo cannot be set when host is null!"); 909 } 910 911 int index = 0; 914 int end = p_userinfo.length(); 915 char testChar = '\0'; 916 while (index < end) { 917 testChar = p_userinfo.charAt(index); 918 if (testChar == '%') { 919 if (index + 2 >= end 920 || !isHex(p_userinfo.charAt(index + 1)) 921 || !isHex(p_userinfo.charAt(index + 2))) { 922 throw new MalformedURIException("Userinfo contains invalid escape sequence!"); 923 } 924 } else if ( 925 !isUnreservedCharacter(testChar) 926 && USERINFO_CHARACTERS.indexOf(testChar) == -1) { 927 throw new MalformedURIException( 928 "Userinfo contains invalid character:" + testChar); 929 } 930 index++; 931 } 932 } 933 m_userinfo = p_userinfo; 934 } 935 936 945 private void setHost(String p_host) throws MalformedURIException { 946 if (p_host == null || p_host.length() == 0) { 947 m_host = p_host; 948 m_userinfo = null; 949 m_port = null; 950 n_port = -1; 951 } else if (!isWellFormedAddress(p_host)) { 952 throw new MalformedURIException( "Host is not a well formed address in '" + p_host + "'" ); 953 } 954 m_host = p_host; 955 } 956 957 968 private void setPort(int p_port) throws MalformedURIException { 969 if (p_port >= 0 && p_port <= 65535) { 970 if (m_host == null) { 971 throw new MalformedURIException("Port cannot be set when host is null!"); 972 } 973 } else if (p_port != -1) { 974 throw new MalformedURIException("Invalid port number!"); 975 } 976 n_port = p_port; 977 978 } 979 980 994 private void setPath(String p_path) throws MalformedURIException { 995 if (p_path == null) { 996 m_path = null; 997 m_queryString = null; 998 m_fragment = null; 999 } else { 1000 initializePath(p_path); 1001 } 1002 } 1003 1004 1017 private void appendPath(String p_addToPath) throws MalformedURIException { 1018 if (p_addToPath == null || p_addToPath.length() == 0) { 1019 return; 1020 } 1021 1022 if (!isURIString(p_addToPath)) { 1023 throw new MalformedURIException("Path contains invalid character!"); 1024 } 1025 1026 if (m_path == null || m_path.length() == 0) { 1027 if (p_addToPath.startsWith("/")) { 1028 m_path = p_addToPath; 1029 } else { 1030 m_path = "/" + p_addToPath; 1031 } 1032 } else if (m_path.endsWith("/")) { 1033 if (p_addToPath.startsWith("/")) { 1034 m_path = m_path.concat(p_addToPath.substring(1)); 1035 } else { 1036 m_path = m_path.concat(p_addToPath); 1037 } 1038 } else { 1039 if (p_addToPath.startsWith("/")) { 1040 m_path = m_path.concat(p_addToPath); 1041 } else { 1042 m_path = m_path.concat("/" + p_addToPath); 1043 } 1044 } 1045 } 1046 1047 1058 private void setQueryString(String p_queryString) 1059 throws MalformedURIException { 1060 if (p_queryString == null) { 1061 m_queryString = null; 1062 } else if (!isGenericURI()) { 1063 throw new MalformedURIException("Query string can only be set for a generic URI!"); 1064 } else if (getPath() == null) { 1065 throw new MalformedURIException("Query string cannot be set when path is null!"); 1066 } else if (!isURIString(p_queryString)) { 1067 throw new MalformedURIException("Query string contains invalid character!"); 1068 } else { 1069 m_queryString = p_queryString; 1070 } 1071 } 1072 1073 1084 public void setFragment(String p_fragment) throws MalformedURIException { 1085 if (p_fragment == null) { 1086 m_fragment = null; 1087 } else if (!isGenericURI()) { 1088 throw new MalformedURIException("Fragment can only be set for a generic URI!"); 1089 } else if (getPath() == null) { 1090 throw new MalformedURIException("Fragment cannot be set when path is null!"); 1091 } else if (!isURIString(p_fragment)) { 1092 throw new MalformedURIException("Fragment contains invalid character!"); 1093 } else { 1094 m_fragment = p_fragment; 1095 } 1096 } 1097 1098 1106 public boolean equals(Object p_test) { 1107 if (p_test instanceof URI) { 1108 URI testURI = (URI) p_test; 1109 if (((m_scheme == null && testURI.m_scheme == null) 1110 || (m_scheme != null 1111 && testURI.m_scheme != null 1112 && m_scheme.equals(testURI.m_scheme))) 1113 && ((m_userinfo == null && testURI.m_userinfo == null) 1114 || (m_userinfo != null 1115 && testURI.m_userinfo != null 1116 && m_userinfo.equals(testURI.m_userinfo))) 1117 && ((m_host == null && testURI.m_host == null) 1118 || (m_host != null 1119 && testURI.m_host != null 1120 && m_host.equals(testURI.m_host))) 1121 && ((m_port == null && testURI.m_port == null) 1122 || (m_port != null 1123 && testURI.m_port != null 1124 && m_port.equals(testURI.m_port))) 1125 && ((m_path == null && testURI.m_path == null) 1126 || (m_path != null 1127 && testURI.m_path != null 1128 && m_path.equals(testURI.m_path))) 1129 && ((m_queryString == null && testURI.m_queryString == null) 1130 || (m_queryString != null 1131 && testURI.m_queryString != null 1132 && m_queryString.equals(testURI.m_queryString))) 1133 && ((m_fragment == null && testURI.m_fragment == null) 1134 || (m_fragment != null 1135 && testURI.m_fragment != null 1136 && m_fragment.equals(testURI.m_fragment)))) { 1137 return true; 1138 } 1139 } 1140 return false; 1141 } 1142 1143 1144 1147 public String toString() 1148 { return getURIString(); } 1149 1150 1155 public String getURIString() { 1156 StringBuffer uriSpecString = new StringBuffer (); 1157 1158 if (m_scheme != null) { 1159 uriSpecString.append(m_scheme); 1160 uriSpecString.append(':'); 1161 } 1162 uriSpecString.append(getSchemeSpecificPart()); 1163 return uriSpecString.toString(); 1164 } 1165 1166 public int hashCode() { 1167 return toString().hashCode(); 1168 } 1169 1170 1177 public boolean isGenericURI() { 1178 return (m_host != null); 1181 } 1182 1183 1190 public static boolean isConformantSchemeName(String p_scheme) { 1191 if (p_scheme == null || p_scheme.length() == 0) { 1192 return false; 1193 } 1194 1195 if (!isAlpha(p_scheme.charAt(0))) { 1196 return false; 1197 } 1198 1199 char testChar; 1200 for (int i = 1; i < p_scheme.length(); i++) { 1201 testChar = p_scheme.charAt(i); 1202 if (!isAlphanum(testChar) 1203 && SCHEME_CHARACTERS.indexOf(testChar) == -1) { 1204 return false; 1205 } 1206 } 1207 1208 return true; 1209 } 1210 1211 1222 public static boolean isWellFormedAddress(String p_address) { 1223 if (p_address == null) { 1224 return false; 1225 } 1226 1227 String address = p_address; 1228 int addrLength = address.length(); 1229 if (addrLength == 0 || addrLength > 255) { 1230 return false; 1231 } 1232 1233 if (address.startsWith(".") || address.startsWith("-")) { 1234 return false; 1235 } 1236 1237 int index = address.lastIndexOf('.'); 1241 if (address.endsWith(".")) { 1242 index = address.substring(0, index).lastIndexOf('.'); 1243 } 1244 1245 if (index + 1 < addrLength && isDigit(p_address.charAt(index + 1))) { 1246 char testChar; 1247 int numDots = 0; 1248 1249 for (int i = 0; i < addrLength; i++) { 1253 testChar = address.charAt(i); 1254 if (testChar == '.') { 1255 if (!isDigit(address.charAt(i - 1)) 1256 || (i + 1 < addrLength 1257 && !isDigit(address.charAt(i + 1)))) { 1258 return false; 1259 } 1260 numDots++; 1261 } else if (!isDigit(testChar)) { 1262 return false; 1263 } 1264 } 1265 if (numDots != 3) { 1266 return false; 1267 } 1268 } else { 1269 char testChar; 1272 1273 for (int i = 0; i < addrLength; i++) { 1274 testChar = address.charAt(i); 1275 if (testChar == '.') { 1276 if (!isAlphanum(address.charAt(i - 1))) { 1277 return false; 1278 } 1279 if (i + 1 < addrLength 1280 && !isAlphanum(address.charAt(i + 1))) { 1281 return false; 1282 } 1283 } else if (!isAlphanum(testChar) && testChar != '-') { 1284 return false; 1285 } 1286 } 1287 } 1288 return true; 1289 } 1290 1291 1296 private static boolean isDigit(char p_char) { 1297 return p_char >= '0' && p_char <= '9'; 1298 } 1299 1300 1306 private static boolean isHex(char p_char) { 1307 return ( 1308 isDigit(p_char) 1309 || (p_char >= 'a' && p_char <= 'f') 1310 || (p_char >= 'A' && p_char <= 'F')); 1311 } 1312 1313 1318 private static boolean isAlpha(char p_char) { 1319 return ( 1320 (p_char >= 'a' && p_char <= 'z') 1321 || (p_char >= 'A' && p_char <= 'Z')); 1322 } 1323 1324 1329 private static boolean isAlphanum(char p_char) { 1330 return (isAlpha(p_char) || isDigit(p_char)); 1331 } 1332 1333 1339 private static boolean isReservedCharacter(char p_char) { 1340 return RESERVED_CHARACTERS.indexOf(p_char) != -1; 1341 } 1342 1343 1348 private static boolean isUnreservedCharacter(char p_char) { 1349 return (!isReservedCharacter(p_char)) && "#%[]".indexOf(p_char) == -1; 1350 } 1353 1354 private boolean haveCheckedNFC = false; 1355 private boolean isNFC; 1356 1357 public boolean isNormalFormC() { 1358 if (!haveCheckedNFC) { 1359 isNFC = CharacterModel.isNormalFormC(toString()); 1360 haveCheckedNFC = true; 1361 } 1362 return isNFC; 1363 } 1364 1371 private static boolean isURIString(String p_uric) { 1372 if (p_uric == null) { 1373 return false; 1374 } 1375 int end = p_uric.length(); 1376 char testChar = '\0'; 1377 for (int i = 0; i < end; i++) { 1378 testChar = p_uric.charAt(i); 1379 if (testChar == '%') { 1380 if (i + 2 >= end 1381 || !isHex(p_uric.charAt(i + 1)) 1382 || !isHex(p_uric.charAt(i + 2))) { 1383 return false; 1384 } else { 1385 i += 2; 1386 continue; 1387 } 1388 } 1389 if (isReservedCharacter(testChar) 1390 || isUnreservedCharacter(testChar)) { 1391 continue; 1392 } else { 1393 return false; 1394 } 1395 } 1396 return true; 1397 } 1398 1399 1400 1401 static final public int SAMEDOCUMENT = 1; 1402 static final public int NETWORK = 2; 1403 static final public int ABSOLUTE = 4; 1404 static final public int RELATIVE = 8; 1405 static final public int PARENT = 16; 1406 static final public int GRANDPARENT = 32; 1407 1408 private boolean equal(String s1, String s2) { 1409 return s1 == null ? s2 == null : s1.equals(s2); 1410 } 1411 static private int prefs[][] = { 1412 { RELATIVE, RELATIVE|PARENT|GRANDPARENT }, 1413 { PARENT, PARENT|GRANDPARENT }, 1414 { GRANDPARENT, GRANDPARENT } 1415 }; 1416 static String exact[] = { ".", "..", "../.." }; 1417 static String sub[] = { "", "../", "../../" }; 1418 public String relativize(String abs, int flags) throws MalformedURIException { 1419 URI r; 1420 r = new URI(abs); 1421 if (r.isGenericURI()) { 1424 boolean net = equal(r.m_scheme, m_scheme); 1425 boolean absl = 1426 net 1427 && equal(r.m_host, m_host) 1428 && equal(m_userinfo, r.m_userinfo) 1429 && equal(m_port, r.m_port); 1430 boolean same = 1431 absl 1432 && equal(m_path, r.m_path) 1433 && equal(m_queryString, r.m_queryString); 1434 1435 String rslt = r.m_fragment == null ? "" : ("#" + r.m_fragment); 1436 1437 if (same && (flags & SAMEDOCUMENT) != 0) 1438 return rslt; 1439 if (r.m_queryString != null) { 1440 rslt = "?" + r.m_queryString + rslt; 1441 } 1442 if ( absl ) { 1443 if ( m_subPaths == null ) { 1444 m_subPaths = new String []{ 1445 m_path==null?null:(m_path + "a"), 1446 null, null, null }; 1447 } 1448 if ( m_subPaths[0] != null ) 1449 for (int i=0; i<3; i++) { 1450 if ( (flags & prefs[i][1]) == 0 ) 1451 break; 1452 if ( m_subPaths[i+1] == null ) 1453 m_subPaths[i+1] = getLastSlash(m_subPaths[i]); 1454 if ( m_subPaths[i+1].length() == 0 ) 1455 break; 1456 if ( (flags & prefs[i][0]) == 0 ) 1457 continue; 1458 if ( !r.m_path.startsWith(m_subPaths[i+1]) ) 1459 continue; 1460 int lg = m_subPaths[i+1].length(); 1462 if (lg == r.m_path.length()) { 1463 return exact[i] + rslt; 1464 } 1465 rslt = sub[i] + r.m_path.substring(lg) + rslt; 1466 1467 return rslt; 1469 } 1470 } 1471 rslt = r.m_path + rslt; 1472 if ( absl && (flags & ABSOLUTE ) != 0 ) { 1473 return rslt; 1474 } 1475 if ( net && ( flags & NETWORK ) != 0 ) { 1476 return "//" + 1477 ( r.m_userinfo == null ? "" : ( r.m_userinfo + "@") ) + 1478 r.m_host + 1479 ( r.m_port == null ? "" : ( ":" + r.m_port) ) + 1480 rslt; 1481 1482 } 1483 } 1484 return abs; 1485 } 1486 static private String getLastSlash(String s) { 1487 int ix = s.lastIndexOf('/',s.length()-2); 1488 return s.substring(0,ix+1); 1489 } 1490 1491} 1492 | Popular Tags |