1 36 package com.sslexplorer.agent.client.util; 37 38 import java.io.IOException ; 39 import java.io.Serializable ; 40 import java.text.MessageFormat ; 41 42 62 public class URI 63 implements Serializable { 64 65 70 public static class MalformedURIException 71 extends IOException { 72 73 76 public MalformedURIException() { 77 super(); 78 } 79 80 85 public MalformedURIException(String p_msg) { 86 super(p_msg); 87 } 88 } 89 90 91 private static final String RESERVED_CHARACTERS = ";/?:@&=+$,"; 93 96 private static final String MARK_CHARACTERS = "-_.!~*'() "; 98 99 private static final String SCHEME_CHARACTERS = "+-."; 101 104 private static final String USERINFO_CHARACTERS = ";:&=+$,"; 106 111 private String m_scheme = null; 112 113 118 private String m_userinfo = null; 119 120 125 private String m_host = null; 126 127 132 private int m_port = -1; 133 134 139 private String m_path = null; 140 141 146 private String m_queryString = null; 147 148 153 private String m_fragment = null; 154 155 158 public URI() { 159 } 160 161 166 public URI(URI p_other) { 167 initialize(p_other); 168 } 169 170 180 public URI(String p_uriSpec) throws MalformedURIException { 181 this( (URI)null, p_uriSpec); 182 } 183 184 192 public URI(URI p_base, String p_uriSpec) throws MalformedURIException { 193 initialize(p_base, p_uriSpec); 194 } 195 196 205 public URI(String p_scheme, String p_schemeSpecificPart) throws 206 MalformedURIException { 207 208 if (p_scheme == null || p_scheme.trim().length() == 0) { 209 throw new MalformedURIException( 210 Messages.getString("URI.cannotConstructURIWithNullEmptyScheme")); } 212 213 if (p_schemeSpecificPart == null || 214 p_schemeSpecificPart.trim().length() == 0) { 215 throw new MalformedURIException( 216 Messages.getString("URI.cannotConstructURIWIthNullEmptySchemeSpecificPart")); } 218 219 setScheme(p_scheme); 220 setPath(p_schemeSpecificPart); 221 } 222 223 237 public URI(String p_scheme, String p_host, String p_path, 238 String p_queryString, String p_fragment) throws 239 MalformedURIException { 240 this(p_scheme, null, p_host, -1, p_path, p_queryString, p_fragment); 241 } 242 243 259 public URI( 260 String p_scheme, 261 String p_userinfo, 262 String p_host, 263 int p_port, 264 String p_path, 265 String p_queryString, 266 String p_fragment) throws MalformedURIException { 267 268 if (p_scheme == null || p_scheme.trim().length() == 0) { 269 throw new MalformedURIException(Messages.getString("URI.schemeIsRequired")); } 271 272 if (p_host == null) { 273 if (p_userinfo != null) { 274 throw new MalformedURIException( 275 Messages.getString("URI.userinfoMayNotBeSpecirfiedIfHostIsNotSpecified")); } 277 278 if (p_port != -1) { 279 throw new MalformedURIException( 280 Messages.getString("URI.portMayNotBeSpecifiedIfHostIsNotSpecified")); } 282 } 283 284 if (p_path != null) { 285 if (p_path.indexOf('?') != -1 && p_queryString != null) { 286 throw new MalformedURIException( 287 Messages.getString("URI.queryStringCannotBeSpecifiedInPathAndQueryString")); } 289 290 if (p_path.indexOf('#') != -1 && p_fragment != null) { 291 throw new MalformedURIException( 292 Messages.getString("URI.fragmentCannotBeSpecifiedInBothPathAndFragment")); } 294 } 295 296 setScheme(p_scheme); 297 setHost(p_host); 298 setPort(p_port); 299 setUserinfo(p_userinfo); 300 setPath(p_path); 301 setQueryString(p_queryString); 302 setFragment(p_fragment); 303 } 304 305 310 private void initialize(URI p_other) { 311 312 m_scheme = p_other.getScheme(); 313 m_userinfo = p_other.getUserinfo(); 314 m_host = p_other.getHost(); 315 m_port = p_other.getPort(); 316 m_path = p_other.getPath(); 317 m_queryString = p_other.getQueryString(); 318 m_fragment = p_other.getFragment(); 319 } 320 321 330 private void initialize(URI p_base, String p_uriSpec) throws 331 MalformedURIException { 332 333 if (p_base == null && (p_uriSpec == null || p_uriSpec.trim().length() == 0)) { 334 throw new MalformedURIException( 335 Messages.getString("URI.cannotInitialiseURIWithEmptyParameters")); } 337 338 if (p_uriSpec == null || p_uriSpec.trim().length() == 0) { 340 initialize(p_base); 341 342 return; 343 } 344 345 String uriSpec = p_uriSpec.trim(); 346 int uriSpecLen = uriSpec.length(); 347 int index = 0; 348 349 if (uriSpec.indexOf(':') == -1) { 351 if (p_base == null) { 352 throw new MalformedURIException(MessageFormat.format(Messages.getString("URI.noSchemeFoundInURI"), new Object [] { uriSpec } ) ); } 354 } 355 else { 356 initializeScheme(uriSpec); 357 358 index = m_scheme.length() + 1; 359 } 360 361 if ( ( (index + 1) < uriSpecLen) && 363 (uriSpec.substring(index).startsWith("//"))) { index += 2; 365 366 int startPos = index; 367 368 char testChar = '\0'; 370 371 while (index < uriSpecLen) { 372 testChar = uriSpec.charAt(index); 373 374 if (testChar == '/' || testChar == '?' || testChar == '#') { 375 break; 376 } 377 378 index++; 379 } 380 381 if (index > startPos) { 384 initializeAuthority(uriSpec.substring(startPos, index)); 385 } 386 else { 387 m_host = ""; } 389 } 390 391 initializePath(uriSpec.substring(index)); 392 393 if (p_base != null) { 399 400 if (m_path.length() == 0 && m_scheme == null && m_host == null) { 408 m_scheme = p_base.getScheme(); 409 m_userinfo = p_base.getUserinfo(); 410 m_host = p_base.getHost(); 411 m_port = p_base.getPort(); 412 m_path = p_base.getPath(); 413 414 if (m_queryString == null) { 415 m_queryString = p_base.getQueryString(); 416 } 417 418 return; 419 } 420 421 if (m_scheme == null) { 424 m_scheme = p_base.getScheme(); 425 } 426 else { 427 return; 428 } 429 430 if (m_host == null) { 433 m_userinfo = p_base.getUserinfo(); 434 m_host = p_base.getHost(); 435 m_port = p_base.getPort(); 436 } 437 else { 438 return; 439 } 440 441 if (m_path.length() > 0 && m_path.startsWith("/")) { return; 444 } 445 446 String path = new String (); 449 String basePath = p_base.getPath(); 450 451 if (basePath != null) { 453 int lastSlash = basePath.lastIndexOf('/'); 454 455 if (lastSlash != -1) { 456 path = basePath.substring(0, lastSlash + 1); 457 } 458 } 459 460 path = path.concat(m_path); 462 463 index = -1; 465 466 while ( (index = path.indexOf("/./")) != -1) { path = path.substring(0, index + 1).concat(path.substring(index + 3)); 468 } 469 470 if (path.endsWith("/.")) { path = path.substring(0, path.length() - 1); 473 } 474 475 index = -1; 478 479 int segIndex = -1; 480 String tempString = null; 481 482 while ( (index = path.indexOf("/../")) > 0) { tempString = path.substring(0, path.indexOf("/../")); segIndex = tempString.lastIndexOf('/'); 485 486 if (segIndex != -1) { 487 if (!tempString.substring(segIndex++).equals("..")) { path = path.substring(0, segIndex).concat(path.substring(index + 4)); 489 } 490 } 491 } 492 493 if (path.endsWith("/..")) { tempString = path.substring(0, path.length() - 3); 497 segIndex = tempString.lastIndexOf('/'); 498 499 if (segIndex != -1) { 500 path = path.substring(0, segIndex + 1); 501 } 502 } 503 504 m_path = path; 505 } 506 } 507 508 515 private void initializeScheme(String p_uriSpec) throws MalformedURIException { 516 517 int uriSpecLen = p_uriSpec.length(); 518 int index = 0; 519 String scheme = null; 520 char testChar = '\0'; 521 522 while (index < uriSpecLen) { 523 testChar = p_uriSpec.charAt(index); 524 525 if (testChar == ':' || testChar == '/' || testChar == '?' || 526 testChar == '#') { 527 break; 528 } 529 530 index++; 531 } 532 533 scheme = p_uriSpec.substring(0, index); 534 535 if (scheme.length() == 0) { 536 throw new MalformedURIException(Messages.getString("URI.noSchemeFoundInURIShort")); } 538 else { 539 setScheme(scheme); 540 } 541 } 542 543 550 private void initializeAuthority(String p_uriSpec) throws 551 MalformedURIException { 552 553 int index = 0; 554 int start = 0; 555 int end = p_uriSpec.length(); 556 char testChar = '\0'; 557 String userinfo = null; 558 559 if (p_uriSpec.indexOf('@', start) != -1) { 561 while (index < end) { 562 testChar = p_uriSpec.charAt(index); 563 564 if (testChar == '@') { 565 break; 566 } 567 568 index++; 569 } 570 571 userinfo = p_uriSpec.substring(start, index); 572 573 index++; 574 } 575 576 String host = null; 578 579 start = index; 580 581 while (index < end) { 582 testChar = p_uriSpec.charAt(index); 583 584 if (testChar == ':') { 585 break; 586 } 587 588 index++; 589 } 590 591 host = p_uriSpec.substring(start, index); 592 593 int port = -1; 594 595 if (host.length() > 0) { 596 597 if (testChar == ':') { 599 index++; 600 601 start = index; 602 603 while (index < end) { 604 index++; 605 } 606 607 String portStr = p_uriSpec.substring(start, index); 608 609 if (portStr.length() > 0) { 610 for (int i = 0; i < portStr.length(); i++) { 611 if (!isDigit(portStr.charAt(i))) { 612 throw new MalformedURIException( 613 MessageFormat.format(Messages.getString("URI.portIsInvalid"), new Object [] { portStr } ) ); } 615 } 616 617 try { 618 port = Integer.parseInt(portStr); 619 } 620 catch (NumberFormatException nfe) { 621 622 } 624 } 625 } 626 } 627 628 setHost(host); 629 setPort(port); 630 setUserinfo(userinfo); 631 } 632 633 640 private void initializePath(String p_uriSpec) throws MalformedURIException { 641 642 if (p_uriSpec == null) { 643 throw new MalformedURIException( 644 Messages.getString("URI.cannotInitialisePathFromNullString")); } 646 647 int index = 0; 648 int start = 0; 649 int end = p_uriSpec.length(); 650 char testChar = '\0'; 651 652 while (index < end) { 654 testChar = p_uriSpec.charAt(index); 655 656 if (testChar == '?' || testChar == '#') { 657 break; 658 } 659 660 if (testChar == '%') { 662 if (index + 2 >= end || !isHex(p_uriSpec.charAt(index + 1)) || 663 !isHex(p_uriSpec.charAt(index + 2))) { 664 throw new MalformedURIException( 665 Messages.getString("URI.pathContainsInvalidEscapeSequence")); } 667 } 668 else 669 if (!isReservedCharacter(testChar) && !isUnreservedCharacter(testChar)) { 670 if ('\\' != testChar) { 671 throw new MalformedURIException(MessageFormat.format(Messages.getString("URI.pathContainsInvalidCharacter"), new Object [] { String.valueOf(testChar) } ) ); } 673 } 674 675 index++; 676 } 677 678 m_path = p_uriSpec.substring(start, index); 679 680 if (testChar == '?') { 682 index++; 683 684 start = index; 685 686 while (index < end) { 687 testChar = p_uriSpec.charAt(index); 688 689 if (testChar == '#') { 690 break; 691 } 692 693 if (testChar == '%') { 694 if (index + 2 >= end || !isHex(p_uriSpec.charAt(index + 1)) || 695 !isHex(p_uriSpec.charAt(index + 2))) { 696 throw new MalformedURIException( 697 Messages.getString("URI.queryContainsInvalidEscapeSequence")); } 699 } 700 else 701 if (!isReservedCharacter(testChar) && !isUnreservedCharacter(testChar)) { 702 throw new MalformedURIException( 703 MessageFormat.format(Messages.getString("URI.queryContainsInvalidCharacter"), new Object [] { String.valueOf(testChar) } ) ); } 705 706 index++; 707 } 708 709 m_queryString = p_uriSpec.substring(start, index); 710 } 711 712 if (testChar == '#') { 714 index++; 715 716 start = index; 717 718 while (index < end) { 719 testChar = p_uriSpec.charAt(index); 720 721 if (testChar == '%') { 722 if (index + 2 >= end || !isHex(p_uriSpec.charAt(index + 1)) || 723 !isHex(p_uriSpec.charAt(index + 2))) { 724 throw new MalformedURIException( 725 Messages.getString("URI.fragmentContainsInvalidEscapeSequence")); } 727 } 728 else 729 if (!isReservedCharacter(testChar) && !isUnreservedCharacter(testChar)) { 730 throw new MalformedURIException( 731 MessageFormat.format(Messages.getString("URI.fragmentContainsInvalidCharacter"), new Object [] { String.valueOf(testChar) } ) ); } 733 734 index++; 735 } 736 737 m_fragment = p_uriSpec.substring(start, index); 738 } 739 } 740 741 746 public String getScheme() { 747 return m_scheme; 748 } 749 750 756 public String getSchemeSpecificPart() { 757 758 StringBuffer schemespec = new StringBuffer (); 759 760 if (m_userinfo != null || m_host != null || m_port != -1) { 761 schemespec.append("//"); } 763 764 if (m_userinfo != null) { 765 schemespec.append(m_userinfo); 766 schemespec.append('@'); 767 } 768 769 if (m_host != null) { 770 schemespec.append(m_host); 771 } 772 773 if (m_port != -1) { 774 schemespec.append(':'); 775 schemespec.append(m_port); 776 } 777 778 if (m_path != null) { 779 schemespec.append( (m_path)); 780 } 781 782 if (m_queryString != null) { 783 schemespec.append('?'); 784 schemespec.append(m_queryString); 785 } 786 787 if (m_fragment != null) { 788 schemespec.append('#'); 789 schemespec.append(m_fragment); 790 } 791 792 return schemespec.toString(); 793 } 794 795 800 public String getUserinfo() { 801 return m_userinfo; 802 } 803 804 809 public String getHost() { 810 return m_host; 811 } 812 813 818 public int getPort() { 819 return m_port; 820 } 821 822 830 public String getPath(boolean p_includeQueryString, boolean p_includeFragment) { 831 832 StringBuffer pathString = new StringBuffer (m_path); 833 834 if (p_includeQueryString && m_queryString != null) { 835 pathString.append('?'); 836 pathString.append(m_queryString); 837 } 838 839 if (p_includeFragment && m_fragment != null) { 840 pathString.append('#'); 841 pathString.append(m_fragment); 842 } 843 844 return pathString.toString(); 845 } 846 847 852 public String getPath() { 853 return m_path; 854 } 855 856 862 public String getQueryString() { 863 return m_queryString; 864 } 865 866 872 public String getFragment() { 873 return m_fragment; 874 } 875 876 883 public void setScheme(String p_scheme) throws MalformedURIException { 884 885 if (p_scheme == null) { 886 throw new MalformedURIException(Messages.getString("URI.cannotSetSchemeFromNullString")); } 888 889 if (!isConformantSchemeName(p_scheme)) { 890 throw new MalformedURIException(Messages.getString("URI.theSchemeIsNonConformant")); } 892 893 m_scheme = p_scheme.toLowerCase(); 894 } 895 896 903 public void setUserinfo(String p_userinfo) throws MalformedURIException { 904 905 if (p_userinfo == null) { 906 m_userinfo = null; 907 } 908 else { 909 if (m_host == null) { 910 throw new MalformedURIException( 911 Messages.getString("URI.userinfoCannotBeSetWhenHostIsNull")); } 913 914 int index = 0; 917 int end = p_userinfo.length(); 918 char testChar = '\0'; 919 920 while (index < end) { 921 testChar = p_userinfo.charAt(index); 922 923 if (testChar == '%') { 924 if (index + 2 >= end || !isHex(p_userinfo.charAt(index + 1)) || 925 !isHex(p_userinfo.charAt(index + 2))) { 926 throw new MalformedURIException( 927 Messages.getString("URI.userinfoContainsInvalidEscapeSequence")); } 929 } 930 else 931 if (!isUnreservedCharacter(testChar) && 932 USERINFO_CHARACTERS.indexOf(testChar) == -1) { 933 throw new MalformedURIException( 934 MessageFormat.format(Messages.getString("URI.userinfoContainsInvalidCharacter"), new Object [] { String.valueOf(testChar) } ) ); } 936 937 index++; 938 } 939 } 940 941 m_userinfo = p_userinfo; 942 } 943 944 951 public void setHost(String p_host) throws MalformedURIException { 952 953 if (p_host == null || p_host.trim().length() == 0) { 954 m_host = p_host; 955 m_userinfo = null; 956 m_port = -1; 957 } 958 else 959 if (!isWellFormedAddress(p_host)) { 960 throw new MalformedURIException(Messages.getString("URI.hostIsNotWellFormed")); } 962 963 m_host = p_host; 964 } 965 966 974 public void setPort(int p_port) throws MalformedURIException { 975 976 if (p_port >= 0 && p_port <= 65535) { 977 if (m_host == null) { 978 throw new MalformedURIException(Messages.getString("URI.portCannotBeSetWhenHostIsNull")); } 980 } 981 else 982 if (p_port != -1) { 983 throw new MalformedURIException(Messages.getString("URI.invalidPortNumber")); } 985 986 m_port = p_port; 987 } 988 989 999 public void setPath(String p_path) throws MalformedURIException { 1000 1001 if (p_path == null) { 1002 m_path = null; 1003 m_queryString = null; 1004 m_fragment = null; 1005 } 1006 else { 1007 initializePath(p_path); 1008 } 1009 } 1010 1011 1020 public void appendPath(String p_addToPath) throws MalformedURIException { 1021 1022 if (p_addToPath == null || p_addToPath.trim().length() == 0) { 1023 return; 1024 } 1025 1026 if (!isURIString(p_addToPath)) { 1027 throw new MalformedURIException(Messages.getString("URI.pathContainsInvalidCharacterShort")); } 1029 1030 if (m_path == null || m_path.trim().length() == 0) { 1031 if (p_addToPath.startsWith("/")) { m_path = p_addToPath; 1033 } 1034 else { 1035 m_path = "/" + p_addToPath; } 1037 } 1038 else 1039 if (m_path.endsWith("/")) { if (p_addToPath.startsWith("/")) { m_path = m_path.concat(p_addToPath.substring(1)); 1042 } 1043 else { 1044 m_path = m_path.concat(p_addToPath); 1045 } 1046 } 1047 else { 1048 if (p_addToPath.startsWith("/")) { m_path = m_path.concat(p_addToPath); 1050 } 1051 else { 1052 m_path = m_path.concat("/" + p_addToPath); } 1054 } 1055 } 1056 1057 1066 public void setQueryString(String p_queryString) throws MalformedURIException { 1067 1068 if (p_queryString == null) { 1069 m_queryString = null; 1070 } 1071 else 1072 if (!isGenericURI()) { 1073 throw new MalformedURIException( 1074 Messages.getString("URI.queryStringCanOnlyBeSetForGenericURI")); } 1076 else 1077 if (getPath() == null) { 1078 throw new MalformedURIException( 1079 Messages.getString("URI.queryStringCannotBeSetWhenPathIsNull")); } 1081 else 1082 if (!isURIString(p_queryString)) { 1083 throw new MalformedURIException( 1084 Messages.getString("URI.queryStringContainsInvalidCharacter")); } 1086 else { 1087 m_queryString = p_queryString; 1088 } 1089 } 1090 1091 1100 public void setFragment(String p_fragment) throws MalformedURIException { 1101 1102 if (p_fragment == null) { 1103 m_fragment = null; 1104 } 1105 else 1106 if (!isGenericURI()) { 1107 throw new MalformedURIException( 1108 Messages.getString("URI.fragmentCanOnlyBeSetForAGenericURI")); } 1110 else 1111 if (getPath() == null) { 1112 throw new MalformedURIException( 1113 Messages.getString("URI.fragmentCannotBeSetWhenPathIsNull")); } 1115 else 1116 if (!isURIString(p_fragment)) { 1117 throw new MalformedURIException(Messages.getString("URI.fragmentContainsInvalidCharacter")); } 1119 else { 1120 m_fragment = p_fragment; 1121 } 1122 } 1123 1124 1131 public boolean equals(Object p_test) { 1132 1133 if (p_test instanceof URI) { 1134 URI testURI = (URI) p_test; 1135 1136 if ( ( (m_scheme == null && testURI.m_scheme == null) 1137 || 1138 (m_scheme != null && testURI.m_scheme != null && 1139 m_scheme.equals(testURI.m_scheme))) 1140 && ( (m_userinfo == null && testURI.m_userinfo == null) 1141 || 1142 (m_userinfo != null && testURI.m_userinfo != null && 1143 m_userinfo.equals(testURI.m_userinfo))) 1144 && ( (m_host == null && testURI.m_host == null) 1145 || 1146 (m_host != null && testURI.m_host != null && 1147 m_host.equals(testURI.m_host))) 1148 && m_port == testURI.m_port 1149 && ( (m_path == null && testURI.m_path == null) 1150 || 1151 (m_path != null && testURI.m_path != null && 1152 m_path.equals(testURI.m_path))) 1153 && ( (m_queryString == null && testURI.m_queryString == null) 1154 || 1155 (m_queryString != null && testURI.m_queryString != null && 1156 m_queryString.equals(testURI.m_queryString))) 1157 && ( (m_fragment == null && testURI.m_fragment == null) 1158 || 1159 (m_fragment != null && testURI.m_fragment != null && 1160 m_fragment.equals(testURI.m_fragment)))) { 1161 return true; 1162 } 1163 } 1164 1165 return false; 1166 } 1167 1168 1173 public String toString() { 1174 1175 StringBuffer uriSpecString = new StringBuffer (); 1176 1177 if (m_scheme != null) { 1178 uriSpecString.append(m_scheme); 1179 uriSpecString.append(':'); 1180 } 1181 1182 uriSpecString.append(getSchemeSpecificPart()); 1183 1184 return uriSpecString.toString(); 1185 } 1186 1187 1192 public boolean isGenericURI() { 1193 1194 return (m_host != null); 1197 } 1198 1199 1206 public static boolean isConformantSchemeName(String p_scheme) { 1207 1208 if (p_scheme == null || p_scheme.trim().length() == 0) { 1209 return false; 1210 } 1211 1212 if (!isAlpha(p_scheme.charAt(0))) { 1213 return false; 1214 } 1215 1216 char testChar; 1217 1218 for (int i = 1; i < p_scheme.length(); i++) { 1219 testChar = p_scheme.charAt(i); 1220 1221 if (!isAlphanum(testChar) && SCHEME_CHARACTERS.indexOf(testChar) == -1) { 1222 return false; 1223 } 1224 } 1225 1226 return true; 1227 } 1228 1229 1238 public static boolean isWellFormedAddress(String p_address) { 1239 1240 if (p_address == null) { 1241 return false; 1242 } 1243 1244 String address = p_address.trim(); 1245 int addrLength = address.length(); 1246 1247 if (addrLength == 0 || addrLength > 255) { 1248 return false; 1249 } 1250 1251 if (address.startsWith(".") || address.startsWith("-")) { return false; 1253 } 1254 1255 int index = address.lastIndexOf('.'); 1259 1260 if (address.endsWith(".")) { index = address.substring(0, index).lastIndexOf('.'); 1262 } 1263 1264 if (index + 1 < addrLength && isDigit(p_address.charAt(index + 1))) { 1265 char testChar; 1266 int numDots = 0; 1267 1268 for (int i = 0; i < addrLength; i++) { 1272 testChar = address.charAt(i); 1273 1274 if (testChar == '.') { 1275 if (!isDigit(address.charAt(i - 1)) || 1276 (i + 1 < addrLength && !isDigit(address.charAt(i + 1)))) { 1277 return false; 1278 } 1279 1280 numDots++; 1281 } 1282 else 1283 if (!isDigit(testChar)) { 1284 return false; 1285 } 1286 } 1287 1288 if (numDots != 3) { 1289 return false; 1290 } 1291 } 1292 else { 1293 1294 char testChar; 1297 1298 for (int i = 0; i < addrLength; i++) { 1299 testChar = address.charAt(i); 1300 1301 if (testChar == '.') { 1302 if (!isAlphanum(address.charAt(i - 1))) { 1303 return false; 1304 } 1305 1306 if (i + 1 < addrLength && !isAlphanum(address.charAt(i + 1))) { 1307 return false; 1308 } 1309 } 1310 else 1311 if (!isAlphanum(testChar) && testChar != '-') { 1312 return false; 1313 } 1314 } 1315 } 1316 1317 return true; 1318 } 1319 1320 1326 private static boolean isDigit(char p_char) { 1327 return p_char >= '0' && p_char <= '9'; 1328 } 1329 1330 1336 private static boolean isHex(char p_char) { 1337 return (isDigit(p_char) || (p_char >= 'a' && p_char <= 'f') || 1338 (p_char >= 'A' && p_char <= 'F')); 1339 } 1340 1341 1347 private static boolean isAlpha(char p_char) { 1348 return ( (p_char >= 'a' && p_char <= 'z') || 1349 (p_char >= 'A' && p_char <= 'Z')); 1350 } 1351 1352 1358 private static boolean isAlphanum(char p_char) { 1359 return (isAlpha(p_char) || isDigit(p_char)); 1360 } 1361 1362 1368 private static boolean isReservedCharacter(char p_char) { 1369 return RESERVED_CHARACTERS.indexOf(p_char) != -1; 1370 } 1371 1372 1378 private static boolean isUnreservedCharacter(char p_char) { 1379 return (isAlphanum(p_char) || MARK_CHARACTERS.indexOf(p_char) != -1); 1380 } 1381 1382 1389 private static boolean isURIString(String p_uric) { 1390 1391 if (p_uric == null) { 1392 return false; 1393 } 1394 1395 int end = p_uric.length(); 1396 char testChar = '\0'; 1397 1398 for (int i = 0; i < end; i++) { 1399 testChar = p_uric.charAt(i); 1400 1401 if (testChar == '%') { 1402 if (i + 2 >= end || !isHex(p_uric.charAt(i + 1)) || 1403 !isHex(p_uric.charAt(i + 2))) { 1404 return false; 1405 } 1406 else { 1407 i += 2; 1408 1409 continue; 1410 } 1411 } 1412 1413 if (isReservedCharacter(testChar) || isUnreservedCharacter(testChar)) { 1414 continue; 1415 } 1416 else { 1417 return false; 1418 } 1419 } 1420 1421 return true; 1422 } 1423} 1424 | Popular Tags |