1 16 19 package com.sun.org.apache.xml.internal.serializer.utils; 20 21 import java.io.IOException ; 22 import java.io.Serializable ; 23 24 25 63 final class URI 64 { 65 71 public static class MalformedURIException extends IOException 72 { 73 74 78 public MalformedURIException() 79 { 80 super(); 81 } 82 83 89 public MalformedURIException(String p_msg) 90 { 91 super(p_msg); 92 } 93 } 94 95 96 private static final String RESERVED_CHARACTERS = ";/?:@&=+$,"; 97 98 102 private static final String MARK_CHARACTERS = "-_.!~*'() "; 103 104 105 private static final String SCHEME_CHARACTERS = "+-."; 106 107 111 private static final String USERINFO_CHARACTERS = ";:&=+$,"; 112 113 115 private String m_scheme = null; 116 117 119 private String m_userinfo = null; 120 121 123 private String m_host = null; 124 125 127 private int m_port = -1; 128 129 131 private String m_path = null; 132 133 138 private String m_queryString = null; 139 140 142 private String m_fragment = null; 143 144 145 private static boolean DEBUG = false; 146 147 150 public URI(){} 151 152 158 public URI(URI p_other) 159 { 160 initialize(p_other); 161 } 162 163 178 public URI(String p_uriSpec) throws MalformedURIException 179 { 180 this((URI) null, p_uriSpec); 181 } 182 183 195 public URI(URI p_base, String p_uriSpec) throws MalformedURIException 196 { 197 initialize(p_base, p_uriSpec); 198 } 199 200 212 public URI(String p_scheme, String p_schemeSpecificPart) 213 throws MalformedURIException 214 { 215 216 if (p_scheme == null || p_scheme.trim().length() == 0) 217 { 218 throw new MalformedURIException( 219 "Cannot construct URI with null/empty scheme!"); 220 } 221 222 if (p_schemeSpecificPart == null 223 || p_schemeSpecificPart.trim().length() == 0) 224 { 225 throw new MalformedURIException( 226 "Cannot construct URI with null/empty scheme-specific part!"); 227 } 228 229 setScheme(p_scheme); 230 setPath(p_schemeSpecificPart); 231 } 232 233 254 public URI(String p_scheme, String p_host, String p_path, String p_queryString, String p_fragment) 255 throws MalformedURIException 256 { 257 this(p_scheme, null, p_host, -1, p_path, p_queryString, p_fragment); 258 } 259 260 285 public URI(String p_scheme, String p_userinfo, String p_host, int p_port, String p_path, String p_queryString, String p_fragment) 286 throws MalformedURIException 287 { 288 289 if (p_scheme == null || p_scheme.trim().length() == 0) 290 { 291 throw new MalformedURIException(Utils.messages.createMessage(MsgKey.ER_SCHEME_REQUIRED, null)); } 293 294 if (p_host == null) 295 { 296 if (p_userinfo != null) 297 { 298 throw new MalformedURIException( 299 Utils.messages.createMessage(MsgKey.ER_NO_USERINFO_IF_NO_HOST, null)); } 301 302 if (p_port != -1) 303 { 304 throw new MalformedURIException( 305 Utils.messages.createMessage(MsgKey.ER_NO_PORT_IF_NO_HOST, null)); } 307 } 308 309 if (p_path != null) 310 { 311 if (p_path.indexOf('?') != -1 && p_queryString != null) 312 { 313 throw new MalformedURIException( 314 Utils.messages.createMessage(MsgKey.ER_NO_QUERY_STRING_IN_PATH, null)); } 316 317 if (p_path.indexOf('#') != -1 && p_fragment != null) 318 { 319 throw new MalformedURIException( 320 Utils.messages.createMessage(MsgKey.ER_NO_FRAGMENT_STRING_IN_PATH, null)); } 322 } 323 324 setScheme(p_scheme); 325 setHost(p_host); 326 setPort(p_port); 327 setUserinfo(p_userinfo); 328 setPath(p_path); 329 setQueryString(p_queryString); 330 setFragment(p_fragment); 331 } 332 333 338 private void initialize(URI p_other) 339 { 340 341 m_scheme = p_other.getScheme(); 342 m_userinfo = p_other.getUserinfo(); 343 m_host = p_other.getHost(); 344 m_port = p_other.getPort(); 345 m_path = p_other.getPath(); 346 m_queryString = p_other.getQueryString(); 347 m_fragment = p_other.getFragment(); 348 } 349 350 366 private void initialize(URI p_base, String p_uriSpec) 367 throws MalformedURIException 368 { 369 370 if (p_base == null 371 && (p_uriSpec == null || p_uriSpec.trim().length() == 0)) 372 { 373 throw new MalformedURIException( 374 Utils.messages.createMessage(MsgKey.ER_CANNOT_INIT_URI_EMPTY_PARMS, null)); } 376 377 if (p_uriSpec == null || p_uriSpec.trim().length() == 0) 379 { 380 initialize(p_base); 381 382 return; 383 } 384 385 String uriSpec = p_uriSpec.trim(); 386 int uriSpecLen = uriSpec.length(); 387 int index = 0; 388 389 int colonIndex = uriSpec.indexOf(':'); 391 if (colonIndex < 0) 392 { 393 if (p_base == null) 394 { 395 throw new MalformedURIException(Utils.messages.createMessage(MsgKey.ER_NO_SCHEME_IN_URI, new Object []{uriSpec})); } 397 } 398 else 399 { 400 initializeScheme(uriSpec); 401 uriSpec = uriSpec.substring(colonIndex+1); 402 uriSpecLen = uriSpec.length(); 403 } 404 405 if (((index + 1) < uriSpecLen) 407 && (uriSpec.substring(index).startsWith("//"))) 408 { 409 index += 2; 410 411 int startPos = index; 412 413 char testChar = '\0'; 415 416 while (index < uriSpecLen) 417 { 418 testChar = uriSpec.charAt(index); 419 420 if (testChar == '/' || testChar == '?' || testChar == '#') 421 { 422 break; 423 } 424 425 index++; 426 } 427 428 if (index > startPos) 431 { 432 initializeAuthority(uriSpec.substring(startPos, index)); 433 } 434 else 435 { 436 m_host = ""; 437 } 438 } 439 440 initializePath(uriSpec.substring(index)); 441 442 if (p_base != null) 448 { 449 450 if (m_path.length() == 0 && m_scheme == null && m_host == null) 458 { 459 m_scheme = p_base.getScheme(); 460 m_userinfo = p_base.getUserinfo(); 461 m_host = p_base.getHost(); 462 m_port = p_base.getPort(); 463 m_path = p_base.getPath(); 464 465 if (m_queryString == null) 466 { 467 m_queryString = p_base.getQueryString(); 468 } 469 470 return; 471 } 472 473 if (m_scheme == null) 476 { 477 m_scheme = p_base.getScheme(); 478 } 479 480 if (m_host == null) 483 { 484 m_userinfo = p_base.getUserinfo(); 485 m_host = p_base.getHost(); 486 m_port = p_base.getPort(); 487 } 488 else 489 { 490 return; 491 } 492 493 if (m_path.length() > 0 && m_path.startsWith("/")) 495 { 496 return; 497 } 498 499 String path = new String (); 502 String basePath = p_base.getPath(); 503 504 if (basePath != null) 506 { 507 int lastSlash = basePath.lastIndexOf('/'); 508 509 if (lastSlash != -1) 510 { 511 path = basePath.substring(0, lastSlash + 1); 512 } 513 } 514 515 path = path.concat(m_path); 517 518 index = -1; 520 521 while ((index = path.indexOf("/./")) != -1) 522 { 523 path = path.substring(0, index + 1).concat(path.substring(index + 3)); 524 } 525 526 if (path.endsWith("/.")) 528 { 529 path = path.substring(0, path.length() - 1); 530 } 531 532 index = -1; 535 536 int segIndex = -1; 537 String tempString = null; 538 539 while ((index = path.indexOf("/../")) > 0) 540 { 541 tempString = path.substring(0, path.indexOf("/../")); 542 segIndex = tempString.lastIndexOf('/'); 543 544 if (segIndex != -1) 545 { 546 if (!tempString.substring(segIndex++).equals("..")) 547 { 548 path = path.substring(0, segIndex).concat(path.substring(index 549 + 4)); 550 } 551 } 552 } 553 554 if (path.endsWith("/..")) 557 { 558 tempString = path.substring(0, path.length() - 3); 559 segIndex = tempString.lastIndexOf('/'); 560 561 if (segIndex != -1) 562 { 563 path = path.substring(0, segIndex + 1); 564 } 565 } 566 567 m_path = path; 568 } 569 } 570 571 579 private void initializeScheme(String p_uriSpec) throws MalformedURIException 580 { 581 582 int uriSpecLen = p_uriSpec.length(); 583 int index = 0; 584 String scheme = null; 585 char testChar = '\0'; 586 587 while (index < uriSpecLen) 588 { 589 testChar = p_uriSpec.charAt(index); 590 591 if (testChar == ':' || testChar == '/' || testChar == '?' 592 || testChar == '#') 593 { 594 break; 595 } 596 597 index++; 598 } 599 600 scheme = p_uriSpec.substring(0, index); 601 602 if (scheme.length() == 0) 603 { 604 throw new MalformedURIException(Utils.messages.createMessage(MsgKey.ER_NO_SCHEME_INURI, null)); } 606 else 607 { 608 setScheme(scheme); 609 } 610 } 611 612 620 private void initializeAuthority(String p_uriSpec) 621 throws MalformedURIException 622 { 623 624 int index = 0; 625 int start = 0; 626 int end = p_uriSpec.length(); 627 char testChar = '\0'; 628 String userinfo = null; 629 630 if (p_uriSpec.indexOf('@', start) != -1) 632 { 633 while (index < end) 634 { 635 testChar = p_uriSpec.charAt(index); 636 637 if (testChar == '@') 638 { 639 break; 640 } 641 642 index++; 643 } 644 645 userinfo = p_uriSpec.substring(start, index); 646 647 index++; 648 } 649 650 String host = null; 652 653 start = index; 654 655 while (index < end) 656 { 657 testChar = p_uriSpec.charAt(index); 658 659 if (testChar == ':') 660 { 661 break; 662 } 663 664 index++; 665 } 666 667 host = p_uriSpec.substring(start, index); 668 669 int port = -1; 670 671 if (host.length() > 0) 672 { 673 674 if (testChar == ':') 676 { 677 index++; 678 679 start = index; 680 681 while (index < end) 682 { 683 index++; 684 } 685 686 String portStr = p_uriSpec.substring(start, index); 687 688 if (portStr.length() > 0) 689 { 690 for (int i = 0; i < portStr.length(); i++) 691 { 692 if (!isDigit(portStr.charAt(i))) 693 { 694 throw new MalformedURIException( 695 portStr + " is invalid. Port should only contain digits!"); 696 } 697 } 698 699 try 700 { 701 port = Integer.parseInt(portStr); 702 } 703 catch (NumberFormatException nfe) 704 { 705 706 } 708 } 709 } 710 } 711 712 setHost(host); 713 setPort(port); 714 setUserinfo(userinfo); 715 } 716 717 724 private void initializePath(String p_uriSpec) throws MalformedURIException 725 { 726 727 if (p_uriSpec == null) 728 { 729 throw new MalformedURIException( 730 "Cannot initialize path from null string!"); 731 } 732 733 int index = 0; 734 int start = 0; 735 int end = p_uriSpec.length(); 736 char testChar = '\0'; 737 738 while (index < end) 740 { 741 testChar = p_uriSpec.charAt(index); 742 743 if (testChar == '?' || testChar == '#') 744 { 745 break; 746 } 747 748 if (testChar == '%') 750 { 751 if (index + 2 >= end ||!isHex(p_uriSpec.charAt(index + 1)) 752 ||!isHex(p_uriSpec.charAt(index + 2))) 753 { 754 throw new MalformedURIException( 755 Utils.messages.createMessage(MsgKey.ER_PATH_CONTAINS_INVALID_ESCAPE_SEQUENCE, null)); } 757 } 758 else if (!isReservedCharacter(testChar) 759 &&!isUnreservedCharacter(testChar)) 760 { 761 if ('\\' != testChar) 762 throw new MalformedURIException(Utils.messages.createMessage(MsgKey.ER_PATH_INVALID_CHAR, new Object []{String.valueOf(testChar)})); } 765 766 index++; 767 } 768 769 m_path = p_uriSpec.substring(start, index); 770 771 if (testChar == '?') 773 { 774 index++; 775 776 start = index; 777 778 while (index < end) 779 { 780 testChar = p_uriSpec.charAt(index); 781 782 if (testChar == '#') 783 { 784 break; 785 } 786 787 if (testChar == '%') 788 { 789 if (index + 2 >= end ||!isHex(p_uriSpec.charAt(index + 1)) 790 ||!isHex(p_uriSpec.charAt(index + 2))) 791 { 792 throw new MalformedURIException( 793 "Query string contains invalid escape sequence!"); 794 } 795 } 796 else if (!isReservedCharacter(testChar) 797 &&!isUnreservedCharacter(testChar)) 798 { 799 throw new MalformedURIException( 800 "Query string contains invalid character:" + testChar); 801 } 802 803 index++; 804 } 805 806 m_queryString = p_uriSpec.substring(start, index); 807 } 808 809 if (testChar == '#') 811 { 812 index++; 813 814 start = index; 815 816 while (index < end) 817 { 818 testChar = p_uriSpec.charAt(index); 819 820 if (testChar == '%') 821 { 822 if (index + 2 >= end ||!isHex(p_uriSpec.charAt(index + 1)) 823 ||!isHex(p_uriSpec.charAt(index + 2))) 824 { 825 throw new MalformedURIException( 826 "Fragment contains invalid escape sequence!"); 827 } 828 } 829 else if (!isReservedCharacter(testChar) 830 &&!isUnreservedCharacter(testChar)) 831 { 832 throw new MalformedURIException( 833 "Fragment contains invalid character:" + testChar); 834 } 835 836 index++; 837 } 838 839 m_fragment = p_uriSpec.substring(start, index); 840 } 841 } 842 843 848 public String getScheme() 849 { 850 return m_scheme; 851 } 852 853 859 public String getSchemeSpecificPart() 860 { 861 862 StringBuffer schemespec = new StringBuffer (); 863 864 if (m_userinfo != null || m_host != null || m_port != -1) 865 { 866 schemespec.append("//"); 867 } 868 869 if (m_userinfo != null) 870 { 871 schemespec.append(m_userinfo); 872 schemespec.append('@'); 873 } 874 875 if (m_host != null) 876 { 877 schemespec.append(m_host); 878 } 879 880 if (m_port != -1) 881 { 882 schemespec.append(':'); 883 schemespec.append(m_port); 884 } 885 886 if (m_path != null) 887 { 888 schemespec.append((m_path)); 889 } 890 891 if (m_queryString != null) 892 { 893 schemespec.append('?'); 894 schemespec.append(m_queryString); 895 } 896 897 if (m_fragment != null) 898 { 899 schemespec.append('#'); 900 schemespec.append(m_fragment); 901 } 902 903 return schemespec.toString(); 904 } 905 906 911 public String getUserinfo() 912 { 913 return m_userinfo; 914 } 915 916 921 public String getHost() 922 { 923 return m_host; 924 } 925 926 931 public int getPort() 932 { 933 return m_port; 934 } 935 936 950 public String getPath(boolean p_includeQueryString, 951 boolean p_includeFragment) 952 { 953 954 StringBuffer pathString = new StringBuffer (m_path); 955 956 if (p_includeQueryString && m_queryString != null) 957 { 958 pathString.append('?'); 959 pathString.append(m_queryString); 960 } 961 962 if (p_includeFragment && m_fragment != null) 963 { 964 pathString.append('#'); 965 pathString.append(m_fragment); 966 } 967 968 return pathString.toString(); 969 } 970 971 977 public String getPath() 978 { 979 return m_path; 980 } 981 982 989 public String getQueryString() 990 { 991 return m_queryString; 992 } 993 994 1001 public String getFragment() 1002 { 1003 return m_fragment; 1004 } 1005 1006 1015 public void setScheme(String p_scheme) throws MalformedURIException 1016 { 1017 1018 if (p_scheme == null) 1019 { 1020 throw new MalformedURIException(Utils.messages.createMessage(MsgKey.ER_SCHEME_FROM_NULL_STRING, null)); } 1022 1023 if (!isConformantSchemeName(p_scheme)) 1024 { 1025 throw new MalformedURIException(Utils.messages.createMessage(MsgKey.ER_SCHEME_NOT_CONFORMANT, null)); } 1027 1028 m_scheme = p_scheme.toLowerCase(); 1029 } 1030 1031 1040 public void setUserinfo(String p_userinfo) throws MalformedURIException 1041 { 1042 1043 if (p_userinfo == null) 1044 { 1045 m_userinfo = null; 1046 } 1047 else 1048 { 1049 if (m_host == null) 1050 { 1051 throw new MalformedURIException( 1052 "Userinfo cannot be set when host is null!"); 1053 } 1054 1055 int index = 0; 1058 int end = p_userinfo.length(); 1059 char testChar = '\0'; 1060 1061 while (index < end) 1062 { 1063 testChar = p_userinfo.charAt(index); 1064 1065 if (testChar == '%') 1066 { 1067 if (index + 2 >= end ||!isHex(p_userinfo.charAt(index + 1)) 1068 ||!isHex(p_userinfo.charAt(index + 2))) 1069 { 1070 throw new MalformedURIException( 1071 "Userinfo contains invalid escape sequence!"); 1072 } 1073 } 1074 else if (!isUnreservedCharacter(testChar) 1075 && USERINFO_CHARACTERS.indexOf(testChar) == -1) 1076 { 1077 throw new MalformedURIException( 1078 "Userinfo contains invalid character:" + testChar); 1079 } 1080 1081 index++; 1082 } 1083 } 1084 1085 m_userinfo = p_userinfo; 1086 } 1087 1088 1097 public void setHost(String p_host) throws MalformedURIException 1098 { 1099 1100 if (p_host == null || p_host.trim().length() == 0) 1101 { 1102 m_host = p_host; 1103 m_userinfo = null; 1104 m_port = -1; 1105 } 1106 else if (!isWellFormedAddress(p_host)) 1107 { 1108 throw new MalformedURIException(Utils.messages.createMessage(MsgKey.ER_HOST_ADDRESS_NOT_WELLFORMED, null)); } 1110 1111 m_host = p_host; 1112 } 1113 1114 1125 public void setPort(int p_port) throws MalformedURIException 1126 { 1127 1128 if (p_port >= 0 && p_port <= 65535) 1129 { 1130 if (m_host == null) 1131 { 1132 throw new MalformedURIException( 1133 Utils.messages.createMessage(MsgKey.ER_PORT_WHEN_HOST_NULL, null)); } 1135 } 1136 else if (p_port != -1) 1137 { 1138 throw new MalformedURIException(Utils.messages.createMessage(MsgKey.ER_INVALID_PORT, null)); } 1140 1141 m_port = p_port; 1142 } 1143 1144 1158 public void setPath(String p_path) throws MalformedURIException 1159 { 1160 1161 if (p_path == null) 1162 { 1163 m_path = null; 1164 m_queryString = null; 1165 m_fragment = null; 1166 } 1167 else 1168 { 1169 initializePath(p_path); 1170 } 1171 } 1172 1173 1186 public void appendPath(String p_addToPath) throws MalformedURIException 1187 { 1188 1189 if (p_addToPath == null || p_addToPath.trim().length() == 0) 1190 { 1191 return; 1192 } 1193 1194 if (!isURIString(p_addToPath)) 1195 { 1196 throw new MalformedURIException(Utils.messages.createMessage(MsgKey.ER_PATH_INVALID_CHAR, new Object []{p_addToPath})); } 1198 1199 if (m_path == null || m_path.trim().length() == 0) 1200 { 1201 if (p_addToPath.startsWith("/")) 1202 { 1203 m_path = p_addToPath; 1204 } 1205 else 1206 { 1207 m_path = "/" + p_addToPath; 1208 } 1209 } 1210 else if (m_path.endsWith("/")) 1211 { 1212 if (p_addToPath.startsWith("/")) 1213 { 1214 m_path = m_path.concat(p_addToPath.substring(1)); 1215 } 1216 else 1217 { 1218 m_path = m_path.concat(p_addToPath); 1219 } 1220 } 1221 else 1222 { 1223 if (p_addToPath.startsWith("/")) 1224 { 1225 m_path = m_path.concat(p_addToPath); 1226 } 1227 else 1228 { 1229 m_path = m_path.concat("/" + p_addToPath); 1230 } 1231 } 1232 } 1233 1234 1245 public void setQueryString(String p_queryString) 1246 throws MalformedURIException 1247 { 1248 1249 if (p_queryString == null) 1250 { 1251 m_queryString = null; 1252 } 1253 else if (!isGenericURI()) 1254 { 1255 throw new MalformedURIException( 1256 "Query string can only be set for a generic URI!"); 1257 } 1258 else if (getPath() == null) 1259 { 1260 throw new MalformedURIException( 1261 "Query string cannot be set when path is null!"); 1262 } 1263 else if (!isURIString(p_queryString)) 1264 { 1265 throw new MalformedURIException( 1266 "Query string contains invalid character!"); 1267 } 1268 else 1269 { 1270 m_queryString = p_queryString; 1271 } 1272 } 1273 1274 1285 public void setFragment(String p_fragment) throws MalformedURIException 1286 { 1287 1288 if (p_fragment == null) 1289 { 1290 m_fragment = null; 1291 } 1292 else if (!isGenericURI()) 1293 { 1294 throw new MalformedURIException( 1295 Utils.messages.createMessage(MsgKey.ER_FRAG_FOR_GENERIC_URI, null)); } 1297 else if (getPath() == null) 1298 { 1299 throw new MalformedURIException( 1300 Utils.messages.createMessage(MsgKey.ER_FRAG_WHEN_PATH_NULL, null)); } 1302 else if (!isURIString(p_fragment)) 1303 { 1304 throw new MalformedURIException(Utils.messages.createMessage(MsgKey.ER_FRAG_INVALID_CHAR, null)); } 1306 else 1307 { 1308 m_fragment = p_fragment; 1309 } 1310 } 1311 1312 1320 public boolean equals(Object p_test) 1321 { 1322 1323 if (p_test instanceof URI) 1324 { 1325 URI testURI = (URI) p_test; 1326 1327 if (((m_scheme == null && testURI.m_scheme == null) || (m_scheme != null && testURI.m_scheme != null && m_scheme.equals( 1328 testURI.m_scheme))) && ((m_userinfo == null && testURI.m_userinfo == null) || (m_userinfo != null && testURI.m_userinfo != null && m_userinfo.equals( 1329 testURI.m_userinfo))) && ((m_host == null && testURI.m_host == null) || (m_host != null && testURI.m_host != null && m_host.equals( 1330 testURI.m_host))) && m_port == testURI.m_port && ((m_path == null && testURI.m_path == null) || (m_path != null && testURI.m_path != null && m_path.equals( 1331 testURI.m_path))) && ((m_queryString == null && testURI.m_queryString == null) || (m_queryString != null && testURI.m_queryString != null && m_queryString.equals( 1332 testURI.m_queryString))) && ((m_fragment == null && testURI.m_fragment == null) || (m_fragment != null && testURI.m_fragment != null && m_fragment.equals( 1333 testURI.m_fragment)))) 1334 { 1335 return true; 1336 } 1337 } 1338 1339 return false; 1340 } 1341 1342 1347 public String toString() 1348 { 1349 1350 StringBuffer uriSpecString = new StringBuffer (); 1351 1352 if (m_scheme != null) 1353 { 1354 uriSpecString.append(m_scheme); 1355 uriSpecString.append(':'); 1356 } 1357 1358 uriSpecString.append(getSchemeSpecificPart()); 1359 1360 return uriSpecString.toString(); 1361 } 1362 1363 1370 public boolean isGenericURI() 1371 { 1372 1373 return (m_host != null); 1376 } 1377 1378 1387 public static boolean isConformantSchemeName(String p_scheme) 1388 { 1389 1390 if (p_scheme == null || p_scheme.trim().length() == 0) 1391 { 1392 return false; 1393 } 1394 1395 if (!isAlpha(p_scheme.charAt(0))) 1396 { 1397 return false; 1398 } 1399 1400 char testChar; 1401 1402 for (int i = 1; i < p_scheme.length(); i++) 1403 { 1404 testChar = p_scheme.charAt(i); 1405 1406 if (!isAlphanum(testChar) && SCHEME_CHARACTERS.indexOf(testChar) == -1) 1407 { 1408 return false; 1409 } 1410 } 1411 1412 return true; 1413 } 1414 1415 1428 public static boolean isWellFormedAddress(String p_address) 1429 { 1430 1431 if (p_address == null) 1432 { 1433 return false; 1434 } 1435 1436 String address = p_address.trim(); 1437 int addrLength = address.length(); 1438 1439 if (addrLength == 0 || addrLength > 255) 1440 { 1441 return false; 1442 } 1443 1444 if (address.startsWith(".") || address.startsWith("-")) 1445 { 1446 return false; 1447 } 1448 1449 int index = address.lastIndexOf('.'); 1453 1454 if (address.endsWith(".")) 1455 { 1456 index = address.substring(0, index).lastIndexOf('.'); 1457 } 1458 1459 if (index + 1 < addrLength && isDigit(p_address.charAt(index + 1))) 1460 { 1461 char testChar; 1462 int numDots = 0; 1463 1464 for (int i = 0; i < addrLength; i++) 1468 { 1469 testChar = address.charAt(i); 1470 1471 if (testChar == '.') 1472 { 1473 if (!isDigit(address.charAt(i - 1)) 1474 || (i + 1 < addrLength &&!isDigit(address.charAt(i + 1)))) 1475 { 1476 return false; 1477 } 1478 1479 numDots++; 1480 } 1481 else if (!isDigit(testChar)) 1482 { 1483 return false; 1484 } 1485 } 1486 1487 if (numDots != 3) 1488 { 1489 return false; 1490 } 1491 } 1492 else 1493 { 1494 1495 char testChar; 1498 1499 for (int i = 0; i < addrLength; i++) 1500 { 1501 testChar = address.charAt(i); 1502 1503 if (testChar == '.') 1504 { 1505 if (!isAlphanum(address.charAt(i - 1))) 1506 { 1507 return false; 1508 } 1509 1510 if (i + 1 < addrLength &&!isAlphanum(address.charAt(i + 1))) 1511 { 1512 return false; 1513 } 1514 } 1515 else if (!isAlphanum(testChar) && testChar != '-') 1516 { 1517 return false; 1518 } 1519 } 1520 } 1521 1522 return true; 1523 } 1524 1525 1532 private static boolean isDigit(char p_char) 1533 { 1534 return p_char >= '0' && p_char <= '9'; 1535 } 1536 1537 1545 private static boolean isHex(char p_char) 1546 { 1547 return (isDigit(p_char) || (p_char >= 'a' && p_char <= 'f') 1548 || (p_char >= 'A' && p_char <= 'F')); 1549 } 1550 1551 1558 private static boolean isAlpha(char p_char) 1559 { 1560 return ((p_char >= 'a' && p_char <= 'z') 1561 || (p_char >= 'A' && p_char <= 'Z')); 1562 } 1563 1564 1571 private static boolean isAlphanum(char p_char) 1572 { 1573 return (isAlpha(p_char) || isDigit(p_char)); 1574 } 1575 1576 1584 private static boolean isReservedCharacter(char p_char) 1585 { 1586 return RESERVED_CHARACTERS.indexOf(p_char) != -1; 1587 } 1588 1589 1596 private static boolean isUnreservedCharacter(char p_char) 1597 { 1598 return (isAlphanum(p_char) || MARK_CHARACTERS.indexOf(p_char) != -1); 1599 } 1600 1601 1610 private static boolean isURIString(String p_uric) 1611 { 1612 1613 if (p_uric == null) 1614 { 1615 return false; 1616 } 1617 1618 int end = p_uric.length(); 1619 char testChar = '\0'; 1620 1621 for (int i = 0; i < end; i++) 1622 { 1623 testChar = p_uric.charAt(i); 1624 1625 if (testChar == '%') 1626 { 1627 if (i + 2 >= end ||!isHex(p_uric.charAt(i + 1)) 1628 ||!isHex(p_uric.charAt(i + 2))) 1629 { 1630 return false; 1631 } 1632 else 1633 { 1634 i += 2; 1635 1636 continue; 1637 } 1638 } 1639 1640 if (isReservedCharacter(testChar) || isUnreservedCharacter(testChar)) 1641 { 1642 continue; 1643 } 1644 else 1645 { 1646 return false; 1647 } 1648 } 1649 1650 return true; 1651 } 1652} 1653 | Popular Tags |