1 56 57 package org.jboss.axis.types; 58 59 import java.io.IOException ; 60 61 97 public class URI 98 { 99 107 public static class MalformedURIException extends IOException 108 { 109 110 116 public MalformedURIException() 117 { 118 super(); 119 } 120 121 129 public MalformedURIException(String p_msg) 130 { 131 super(p_msg); 132 } 133 } 134 135 138 private static final String RESERVED_CHARACTERS = ";/?:@&=+$,[]"; 141 142 146 private static final String MARK_CHARACTERS = "-_.!~*'()"; 147 148 151 private static final String SCHEME_CHARACTERS = "+-."; 152 153 157 private static final String USERINFO_CHARACTERS = ";:&=+$,"; 158 159 162 private String m_scheme = null; 163 164 167 private String m_userinfo = null; 168 169 172 private String m_host = null; 173 174 177 private int m_port = -1; 178 179 182 private String m_path = null; 183 184 188 private String m_queryString = null; 189 190 193 private String m_fragment = null; 194 195 private static boolean DEBUG = false; 196 197 200 public URI() 201 { 202 } 203 204 210 public URI(URI p_other) 211 { 212 initialize(p_other); 213 } 214 215 229 public URI(String p_uriSpec) throws MalformedURIException 230 { 231 this((URI)null, p_uriSpec); 232 } 233 234 245 public URI(URI p_base, String p_uriSpec) throws MalformedURIException 246 { 247 initialize(p_base, p_uriSpec); 248 } 249 250 261 public URI(String p_scheme, String p_schemeSpecificPart) 262 throws MalformedURIException 263 { 264 if (p_scheme == null || p_scheme.trim().length() == 0) 265 { 266 throw new MalformedURIException("Cannot construct URI with null/empty scheme!"); 267 } 268 if (p_schemeSpecificPart == null || 269 p_schemeSpecificPart.trim().length() == 0) 270 { 271 throw new MalformedURIException("Cannot construct URI with null/empty scheme-specific part!"); 272 } 273 setScheme(p_scheme); 274 setPath(p_schemeSpecificPart); 275 } 276 277 297 public URI(String p_scheme, String p_host, String p_path, 298 String p_queryString, String p_fragment) 299 throws MalformedURIException 300 { 301 this(p_scheme, null, p_host, -1, p_path, p_queryString, p_fragment); 302 } 303 304 328 public URI(String p_scheme, String p_userinfo, 329 String p_host, int p_port, String p_path, 330 String p_queryString, String p_fragment) 331 throws MalformedURIException 332 { 333 if (p_scheme == null || p_scheme.trim().length() == 0) 334 { 335 throw new MalformedURIException("Scheme is required!"); 336 } 337 338 if (p_host == null) 339 { 340 if (p_userinfo != null) 341 { 342 throw new MalformedURIException("Userinfo may not be specified if host is not specified!"); 343 } 344 if (p_port != -1) 345 { 346 throw new MalformedURIException("Port may not be specified if host is not specified!"); 347 } 348 } 349 350 if (p_path != null) 351 { 352 if (p_path.indexOf('?') != -1 && p_queryString != null) 353 { 354 throw new MalformedURIException("Query string cannot be specified in path and query string!"); 355 } 356 357 if (p_path.indexOf('#') != -1 && p_fragment != null) 358 { 359 throw new MalformedURIException("Fragment cannot be specified in both the path and fragment!"); 360 } 361 } 362 363 setScheme(p_scheme); 364 setHost(p_host); 365 setPort(p_port); 366 setUserinfo(p_userinfo); 367 setPath(p_path); 368 setQueryString(p_queryString); 369 setFragment(p_fragment); 370 } 371 372 377 private void initialize(URI p_other) 378 { 379 m_scheme = p_other.getScheme(); 380 m_userinfo = p_other.getUserinfo(); 381 m_host = p_other.getHost(); 382 m_port = p_other.getPort(); 383 m_path = p_other.getPath(); 384 m_queryString = p_other.getQueryString(); 385 m_fragment = p_other.getFragment(); 386 } 387 388 403 private void initialize(URI p_base, String p_uriSpec) 404 throws MalformedURIException 405 { 406 if (p_base == null && 407 (p_uriSpec == null || p_uriSpec.trim().length() == 0)) 408 { 409 throw new MalformedURIException("Cannot initialize URI with empty parameters."); 410 } 411 412 if (p_uriSpec == null || p_uriSpec.trim().length() == 0) 414 { 415 initialize(p_base); 416 return; 417 } 418 419 String uriSpec = p_uriSpec.trim(); 420 int uriSpecLen = uriSpec.length(); 421 int index = 0; 422 423 int colonIdx = uriSpec.indexOf(':'); 427 int slashIdx = uriSpec.indexOf('/'); 428 int queryIdx = uriSpec.indexOf('?'); 429 int fragmentIdx = uriSpec.indexOf('#'); 430 431 if ((colonIdx < 2) || 432 (colonIdx > slashIdx && slashIdx != -1) || 433 (colonIdx > queryIdx && queryIdx != -1) || 434 (colonIdx > fragmentIdx && fragmentIdx != -1)) 435 { 436 if (p_base == null && fragmentIdx != 0) 438 { 439 throw new MalformedURIException("No scheme found in URI."); 440 } 441 } 442 else 443 { 444 initializeScheme(uriSpec); 445 index = m_scheme.length() + 1; 446 } 447 448 if (((index + 1) < uriSpecLen) && 450 (uriSpec.substring(index).startsWith("//"))) 451 { 452 index += 2; 453 int startPos = index; 454 455 char testChar = '\0'; 457 while (index < uriSpecLen) 458 { 459 testChar = uriSpec.charAt(index); 460 if (testChar == '/' || testChar == '?' || testChar == '#') 461 { 462 break; 463 } 464 index++; 465 } 466 467 if (index > startPos) 470 { 471 initializeAuthority(uriSpec.substring(startPos, index)); 472 } 473 else 474 { 475 m_host = ""; 476 } 477 } 478 479 initializePath(uriSpec.substring(index)); 480 481 if (p_base != null) 487 { 488 489 if (m_path.length() == 0 && m_scheme == null && 497 m_host == null) 498 { 499 m_scheme = p_base.getScheme(); 500 m_userinfo = p_base.getUserinfo(); 501 m_host = p_base.getHost(); 502 m_port = p_base.getPort(); 503 m_path = p_base.getPath(); 504 505 if (m_queryString == null) 506 { 507 m_queryString = p_base.getQueryString(); 508 } 509 return; 510 } 511 512 if (m_scheme == null) 515 { 516 m_scheme = p_base.getScheme(); 517 } 518 else 519 { 520 return; 521 } 522 523 if (m_host == null) 526 { 527 m_userinfo = p_base.getUserinfo(); 528 m_host = p_base.getHost(); 529 m_port = p_base.getPort(); 530 } 531 else 532 { 533 return; 534 } 535 536 if (m_path.length() > 0 && 538 m_path.startsWith("/")) 539 { 540 return; 541 } 542 543 String path = new String (); 546 String basePath = p_base.getPath(); 547 548 if (basePath != null) 550 { 551 int lastSlash = basePath.lastIndexOf('/'); 552 if (lastSlash != -1) 553 { 554 path = basePath.substring(0, lastSlash + 1); 555 } 556 } 557 558 path = path.concat(m_path); 560 561 index = -1; 563 while ((index = path.indexOf("/./")) != -1) 564 { 565 path = path.substring(0, index + 1).concat(path.substring(index + 3)); 566 } 567 568 if (path.endsWith("/.")) 570 { 571 path = path.substring(0, path.length() - 1); 572 } 573 574 index = 1; 577 int segIndex = -1; 578 String tempString = null; 579 580 while ((index = path.indexOf("/../", index)) > 0) 581 { 582 tempString = path.substring(0, path.indexOf("/../")); 583 segIndex = tempString.lastIndexOf('/'); 584 if (segIndex != -1) 585 { 586 if (!tempString.substring(segIndex).equals("..")) 587 { 588 path = path.substring(0, segIndex + 1).concat(path.substring(index + 4)); 589 index = segIndex; 590 } 591 else 592 index += 4; 593 } 594 else 595 index += 4; 596 } 597 598 if (path.endsWith("/..")) 601 { 602 tempString = path.substring(0, path.length() - 3); 603 segIndex = tempString.lastIndexOf('/'); 604 if (segIndex != -1) 605 { 606 path = path.substring(0, segIndex + 1); 607 } 608 } 609 m_path = path; 610 } 611 } 612 613 620 private void initializeScheme(String p_uriSpec) 621 throws MalformedURIException 622 { 623 int uriSpecLen = p_uriSpec.length(); 624 int index = 0; 625 String scheme = null; 626 char testChar = '\0'; 627 628 while (index < uriSpecLen) 629 { 630 testChar = p_uriSpec.charAt(index); 631 if (testChar == ':' || testChar == '/' || 632 testChar == '?' || testChar == '#') 633 { 634 break; 635 } 636 index++; 637 } 638 scheme = p_uriSpec.substring(0, index); 639 640 if (scheme.length() == 0) 641 { 642 throw new MalformedURIException("No scheme found in URI."); 643 } 644 else 645 { 646 setScheme(scheme); 647 } 648 } 649 650 657 private void initializeAuthority(String p_uriSpec) 658 throws MalformedURIException 659 { 660 int index = 0; 661 int start = 0; 662 int end = p_uriSpec.length(); 663 char testChar = '\0'; 664 String userinfo = null; 665 666 if (p_uriSpec.indexOf('@', start) != -1) 668 { 669 while (index < end) 670 { 671 testChar = p_uriSpec.charAt(index); 672 if (testChar == '@') 673 { 674 break; 675 } 676 index++; 677 } 678 userinfo = p_uriSpec.substring(start, index); 679 index++; 680 } 681 682 String host = null; 684 start = index; 685 while (index < end) 686 { 687 testChar = p_uriSpec.charAt(index); 688 if (testChar == ':') 689 { 690 break; 691 } 692 index++; 693 } 694 host = p_uriSpec.substring(start, index); 695 int port = -1; 696 if (host.length() > 0) 697 { 698 if (testChar == ':') 700 { 701 index++; 702 start = index; 703 while (index < end) 704 { 705 index++; 706 } 707 String portStr = p_uriSpec.substring(start, index); 708 if (portStr.length() > 0) 709 { 710 for (int i = 0; i < portStr.length(); i++) 711 { 712 if (!isDigit(portStr.charAt(i))) 713 { 714 throw new MalformedURIException(portStr + 715 " is invalid. Port should only contain digits!"); 716 } 717 } 718 try 719 { 720 port = Integer.parseInt(portStr); 721 } 722 catch (NumberFormatException nfe) 723 { 724 } 726 } 727 } 728 } 729 setHost(host); 730 setPort(port); 731 setUserinfo(userinfo); 732 } 733 734 740 private void initializePath(String p_uriSpec) 741 throws MalformedURIException 742 { 743 if (p_uriSpec == null) 744 { 745 throw new MalformedURIException("Cannot initialize path from null string!"); 746 } 747 748 int index = 0; 749 int start = 0; 750 int end = p_uriSpec.length(); 751 char testChar = '\0'; 752 753 while (index < end) 755 { 756 testChar = p_uriSpec.charAt(index); 757 if (testChar == '?' || testChar == '#') 758 { 759 break; 760 } 761 if (testChar == '%') 763 { 764 if (index + 2 >= end || 765 !isHex(p_uriSpec.charAt(index + 1)) || 766 !isHex(p_uriSpec.charAt(index + 2))) 767 { 768 throw new MalformedURIException("Path contains invalid escape sequence!"); 769 } 770 } 771 else if (!isReservedCharacter(testChar) && 772 !isUnreservedCharacter(testChar)) 773 { 774 throw new MalformedURIException("Path contains invalid character: " + testChar); 775 } 776 index++; 777 } 778 m_path = p_uriSpec.substring(start, index); 779 780 if (testChar == '?') 782 { 783 index++; 784 start = index; 785 while (index < end) 786 { 787 testChar = p_uriSpec.charAt(index); 788 if (testChar == '#') 789 { 790 break; 791 } 792 if (testChar == '%') 793 { 794 if (index + 2 >= end || 795 !isHex(p_uriSpec.charAt(index + 1)) || 796 !isHex(p_uriSpec.charAt(index + 2))) 797 { 798 throw new MalformedURIException("Query string contains invalid escape sequence!"); 799 } 800 } 801 else if (!isReservedCharacter(testChar) && 802 !isUnreservedCharacter(testChar)) 803 { 804 throw new MalformedURIException("Query string contains invalid character:" + testChar); 805 } 806 index++; 807 } 808 m_queryString = p_uriSpec.substring(start, index); 809 } 810 811 if (testChar == '#') 813 { 814 index++; 815 start = index; 816 while (index < end) 817 { 818 testChar = p_uriSpec.charAt(index); 819 820 if (testChar == '%') 821 { 822 if (index + 2 >= end || 823 !isHex(p_uriSpec.charAt(index + 1)) || 824 !isHex(p_uriSpec.charAt(index + 2))) 825 { 826 throw new MalformedURIException("Fragment contains invalid escape sequence!"); 827 } 828 } 829 else if (!isReservedCharacter(testChar) && 830 !isUnreservedCharacter(testChar)) 831 { 832 throw new MalformedURIException("Fragment contains invalid character:" + testChar); 833 } 834 index++; 835 } 836 m_fragment = p_uriSpec.substring(start, index); 837 } 838 } 839 840 845 public String getScheme() 846 { 847 return m_scheme; 848 } 849 850 856 public String getSchemeSpecificPart() 857 { 858 StringBuffer schemespec = new StringBuffer (); 859 860 if (m_userinfo != null || m_host != null || m_port != -1) 861 { 862 schemespec.append("//"); 863 } 864 865 if (m_userinfo != null) 866 { 867 schemespec.append(m_userinfo); 868 schemespec.append('@'); 869 } 870 871 if (m_host != null) 872 { 873 schemespec.append(m_host); 874 } 875 876 if (m_port != -1) 877 { 878 schemespec.append(':'); 879 schemespec.append(m_port); 880 } 881 882 if (m_path != null) 883 { 884 schemespec.append((m_path)); 885 } 886 887 if (m_queryString != null) 888 { 889 schemespec.append('?'); 890 schemespec.append(m_queryString); 891 } 892 893 if (m_fragment != null) 894 { 895 schemespec.append('#'); 896 schemespec.append(m_fragment); 897 } 898 899 return schemespec.toString(); 900 } 901 902 907 public String getUserinfo() 908 { 909 return m_userinfo; 910 } 911 912 917 public String getHost() 918 { 919 return m_host; 920 } 921 922 927 public int getPort() 928 { 929 return m_port; 930 } 931 932 945 public String getPath(boolean p_includeQueryString, 946 boolean p_includeFragment) 947 { 948 StringBuffer pathString = new StringBuffer (m_path); 949 950 if (p_includeQueryString && m_queryString != null) 951 { 952 pathString.append('?'); 953 pathString.append(m_queryString); 954 } 955 956 if (p_includeFragment && m_fragment != null) 957 { 958 pathString.append('#'); 959 pathString.append(m_fragment); 960 } 961 return pathString.toString(); 962 } 963 964 970 public String getPath() 971 { 972 return m_path; 973 } 974 975 982 public String getQueryString() 983 { 984 return m_queryString; 985 } 986 987 994 public String getFragment() 995 { 996 return m_fragment; 997 } 998 999 1007 public void setScheme(String p_scheme) throws MalformedURIException 1008 { 1009 if (p_scheme == null) 1010 { 1011 throw new MalformedURIException("Cannot set scheme from null string!"); 1012 } 1013 if (!isConformantSchemeName(p_scheme)) 1014 { 1015 throw new MalformedURIException("The scheme is not conformant."); 1016 } 1017 1018 m_scheme = p_scheme.toLowerCase(); 1019 } 1020 1021 1029 public void setUserinfo(String p_userinfo) throws MalformedURIException 1030 { 1031 if (p_userinfo == null) 1032 { 1033 m_userinfo = null; 1034 } 1035 else 1036 { 1037 if (m_host == null) 1038 { 1039 throw new MalformedURIException("Userinfo cannot be set when host is null!"); 1040 } 1041 1042 int index = 0; 1045 int end = p_userinfo.length(); 1046 char testChar = '\0'; 1047 while (index < end) 1048 { 1049 testChar = p_userinfo.charAt(index); 1050 if (testChar == '%') 1051 { 1052 if (index + 2 >= end || 1053 !isHex(p_userinfo.charAt(index + 1)) || 1054 !isHex(p_userinfo.charAt(index + 2))) 1055 { 1056 throw new MalformedURIException("Userinfo contains invalid escape sequence!"); 1057 } 1058 } 1059 else if (!isUnreservedCharacter(testChar) && 1060 USERINFO_CHARACTERS.indexOf(testChar) == -1) 1061 { 1062 throw new MalformedURIException("Userinfo contains invalid character:" + testChar); 1063 } 1064 index++; 1065 } 1066 } 1067 m_userinfo = p_userinfo; 1068 } 1069 1070 1078 public void setHost(String p_host) throws MalformedURIException 1079 { 1080 if (p_host == null || p_host.trim().length() == 0) 1081 { 1082 m_host = p_host; 1083 m_userinfo = null; 1084 m_port = -1; 1085 } 1086 else if (!isWellFormedAddress(p_host)) 1087 { 1088 throw new MalformedURIException("Host is not a well formed address!"); 1089 } 1090 m_host = p_host; 1091 } 1092 1093 1103 public void setPort(int p_port) throws MalformedURIException 1104 { 1105 if (p_port >= 0 && p_port <= 65535) 1106 { 1107 if (m_host == null) 1108 { 1109 throw new MalformedURIException("Port cannot be set when host is null!"); 1110 } 1111 } 1112 else if (p_port != -1) 1113 { 1114 throw new MalformedURIException("Invalid port number!"); 1115 } 1116 m_port = p_port; 1117 } 1118 1119 1132 public void setPath(String p_path) throws MalformedURIException 1133 { 1134 if (p_path == null) 1135 { 1136 m_path = null; 1137 m_queryString = null; 1138 m_fragment = null; 1139 } 1140 else 1141 { 1142 initializePath(p_path); 1143 } 1144 } 1145 1146 1158 public void appendPath(String p_addToPath) 1159 throws MalformedURIException 1160 { 1161 if (p_addToPath == null || p_addToPath.trim().length() == 0) 1162 { 1163 return; 1164 } 1165 1166 if (!isURIString(p_addToPath)) 1167 { 1168 throw new MalformedURIException("Path contains invalid character!"); 1169 } 1170 1171 if (m_path == null || m_path.trim().length() == 0) 1172 { 1173 if (p_addToPath.startsWith("/")) 1174 { 1175 m_path = p_addToPath; 1176 } 1177 else 1178 { 1179 m_path = "/" + p_addToPath; 1180 } 1181 } 1182 else if (m_path.endsWith("/")) 1183 { 1184 if (p_addToPath.startsWith("/")) 1185 { 1186 m_path = m_path.concat(p_addToPath.substring(1)); 1187 } 1188 else 1189 { 1190 m_path = m_path.concat(p_addToPath); 1191 } 1192 } 1193 else 1194 { 1195 if (p_addToPath.startsWith("/")) 1196 { 1197 m_path = m_path.concat(p_addToPath); 1198 } 1199 else 1200 { 1201 m_path = m_path.concat("/" + p_addToPath); 1202 } 1203 } 1204 } 1205 1206 1216 public void setQueryString(String p_queryString) throws MalformedURIException 1217 { 1218 if (p_queryString == null) 1219 { 1220 m_queryString = null; 1221 } 1222 else if (!isGenericURI()) 1223 { 1224 throw new MalformedURIException("Query string can only be set for a generic URI!"); 1225 } 1226 else if (getPath() == null) 1227 { 1228 throw new MalformedURIException("Query string cannot be set when path is null!"); 1229 } 1230 else if (!isURIString(p_queryString)) 1231 { 1232 throw new MalformedURIException("Query string contains invalid character!"); 1233 } 1234 else 1235 { 1236 m_queryString = p_queryString; 1237 } 1238 } 1239 1240 1250 public void setFragment(String p_fragment) throws MalformedURIException 1251 { 1252 if (p_fragment == null) 1253 { 1254 m_fragment = null; 1255 } 1256 else if (!isGenericURI()) 1257 { 1258 throw new MalformedURIException("Fragment can only be set for a generic URI!"); 1259 } 1260 else if (getPath() == null) 1261 { 1262 throw new MalformedURIException("Fragment cannot be set when path is null!"); 1263 } 1264 else if (!isURIString(p_fragment)) 1265 { 1266 throw new MalformedURIException("Fragment contains invalid character!"); 1267 } 1268 else 1269 { 1270 m_fragment = p_fragment; 1271 } 1272 } 1273 1274 1281 public boolean equals(Object p_test) 1282 { 1283 if (p_test instanceof URI) 1284 { 1285 URI testURI = (URI)p_test; 1286 if (((m_scheme == null && testURI.m_scheme == null) || 1287 (m_scheme != null && testURI.m_scheme != null && 1288 m_scheme.equals(testURI.m_scheme))) && 1289 ((m_userinfo == null && testURI.m_userinfo == null) || 1290 (m_userinfo != null && testURI.m_userinfo != null && 1291 m_userinfo.equals(testURI.m_userinfo))) && 1292 ((m_host == null && testURI.m_host == null) || 1293 (m_host != null && testURI.m_host != null && 1294 m_host.equals(testURI.m_host))) && 1295 m_port == testURI.m_port && 1296 ((m_path == null && testURI.m_path == null) || 1297 (m_path != null && testURI.m_path != null && 1298 m_path.equals(testURI.m_path))) && 1299 ((m_queryString == null && testURI.m_queryString == null) || 1300 (m_queryString != null && testURI.m_queryString != null && 1301 m_queryString.equals(testURI.m_queryString))) && 1302 ((m_fragment == null && testURI.m_fragment == null) || 1303 (m_fragment != null && testURI.m_fragment != null && 1304 m_fragment.equals(testURI.m_fragment)))) 1305 { 1306 return true; 1307 } 1308 } 1309 return false; 1310 } 1311 1312 1317 public String toString() 1318 { 1319 StringBuffer uriSpecString = new StringBuffer (); 1320 1321 if (m_scheme != null) 1322 { 1323 uriSpecString.append(m_scheme); 1324 uriSpecString.append(':'); 1325 } 1326 uriSpecString.append(getSchemeSpecificPart()); 1327 return uriSpecString.toString(); 1328 } 1329 1330 1337 public boolean isGenericURI() 1338 { 1339 return (m_host != null); 1342 } 1343 1344 1351 public static boolean isConformantSchemeName(String p_scheme) 1352 { 1353 if (p_scheme == null || p_scheme.trim().length() == 0) 1354 { 1355 return false; 1356 } 1357 1358 if (!isAlpha(p_scheme.charAt(0))) 1359 { 1360 return false; 1361 } 1362 1363 char testChar; 1364 for (int i = 1; i < p_scheme.length(); i++) 1365 { 1366 testChar = p_scheme.charAt(i); 1367 if (!isAlphanum(testChar) && 1368 SCHEME_CHARACTERS.indexOf(testChar) == -1) 1369 { 1370 return false; 1371 } 1372 } 1373 1374 return true; 1375 } 1376 1377 1388 public static boolean isWellFormedAddress(String p_address) 1389 { 1390 if (p_address == null) 1391 { 1392 return false; 1393 } 1394 1395 String address = p_address.trim(); 1396 int addrLength = address.length(); 1397 if (addrLength == 0 || addrLength > 255) 1398 { 1399 return false; 1400 } 1401 1402 if (address.startsWith(".") || address.startsWith("-")) 1403 { 1404 return false; 1405 } 1406 1407 int index = address.lastIndexOf('.'); 1411 if (address.endsWith(".")) 1412 { 1413 index = address.substring(0, index).lastIndexOf('.'); 1414 } 1415 1416 if (index + 1 < addrLength && isDigit(p_address.charAt(index + 1))) 1417 { 1418 char testChar; 1419 int numDots = 0; 1420 1421 for (int i = 0; i < addrLength; i++) 1425 { 1426 testChar = address.charAt(i); 1427 if (testChar == '.') 1428 { 1429 if (!isDigit(address.charAt(i - 1)) || 1430 (i + 1 < addrLength && !isDigit(address.charAt(i + 1)))) 1431 { 1432 return false; 1433 } 1434 numDots++; 1435 } 1436 else if (!isDigit(testChar)) 1437 { 1438 return false; 1439 } 1440 } 1441 if (numDots != 3) 1442 { 1443 return false; 1444 } 1445 } 1446 else 1447 { 1448 char testChar; 1451 1452 for (int i = 0; i < addrLength; i++) 1453 { 1454 testChar = address.charAt(i); 1455 if (testChar == '.') 1456 { 1457 if (!isAlphanum(address.charAt(i - 1))) 1458 { 1459 return false; 1460 } 1461 if (i + 1 < addrLength && !isAlphanum(address.charAt(i + 1))) 1462 { 1463 return false; 1464 } 1465 } 1466 else if (!isAlphanum(testChar) && testChar != '-') 1467 { 1468 return false; 1469 } 1470 } 1471 } 1472 return true; 1473 } 1474 1475 1476 1481 private static boolean isDigit(char p_char) 1482 { 1483 return p_char >= '0' && p_char <= '9'; 1484 } 1485 1486 1492 private static boolean isHex(char p_char) 1493 { 1494 return (isDigit(p_char) || 1495 (p_char >= 'a' && p_char <= 'f') || 1496 (p_char >= 'A' && p_char <= 'F')); 1497 } 1498 1499 1504 private static boolean isAlpha(char p_char) 1505 { 1506 return ((p_char >= 'a' && p_char <= 'z') || 1507 (p_char >= 'A' && p_char <= 'Z')); 1508 } 1509 1510 1515 private static boolean isAlphanum(char p_char) 1516 { 1517 return (isAlpha(p_char) || isDigit(p_char)); 1518 } 1519 1520 1526 private static boolean isReservedCharacter(char p_char) 1527 { 1528 return RESERVED_CHARACTERS.indexOf(p_char) != -1; 1529 } 1530 1531 1536 private static boolean isUnreservedCharacter(char p_char) 1537 { 1538 return (isAlphanum(p_char) || 1539 MARK_CHARACTERS.indexOf(p_char) != -1); 1540 } 1541 1542 1549 private static boolean isURIString(String p_uric) 1550 { 1551 if (p_uric == null) 1552 { 1553 return false; 1554 } 1555 int end = p_uric.length(); 1556 char testChar = '\0'; 1557 for (int i = 0; i < end; i++) 1558 { 1559 testChar = p_uric.charAt(i); 1560 if (testChar == '%') 1561 { 1562 if (i + 2 >= end || 1563 !isHex(p_uric.charAt(i + 1)) || 1564 !isHex(p_uric.charAt(i + 2))) 1565 { 1566 return false; 1567 } 1568 else 1569 { 1570 i += 2; 1571 continue; 1572 } 1573 } 1574 if (isReservedCharacter(testChar) || 1575 isUnreservedCharacter(testChar)) 1576 { 1577 continue; 1578 } 1579 else 1580 { 1581 return false; 1582 } 1583 } 1584 return true; 1585 } 1586 1587 1594 public int hashCode() 1595 { 1596 return toString().toLowerCase().hashCode(); 1597 } 1598} 1599 | Popular Tags |