1 16 19 package org.apache.xml.utils; 20 21 import java.io.IOException ; 22 import java.io.Serializable ; 23 24 import org.apache.xml.res.XMLErrorResources; 25 import org.apache.xml.res.XMLMessages; 26 27 58 public class URI implements Serializable 59 { 60 61 67 public static class MalformedURIException extends IOException 68 { 69 70 74 public MalformedURIException() 75 { 76 super(); 77 } 78 79 85 public MalformedURIException(String p_msg) 86 { 87 super(p_msg); 88 } 89 } 90 91 92 private static final String RESERVED_CHARACTERS = ";/?:@&=+$,"; 93 94 98 private static final String MARK_CHARACTERS = "-_.!~*'() "; 99 100 101 private static final String SCHEME_CHARACTERS = "+-."; 102 103 107 private static final String USERINFO_CHARACTERS = ";:&=+$,"; 108 109 111 private String m_scheme = null; 112 113 115 private String m_userinfo = null; 116 117 119 private String m_host = null; 120 121 123 private int m_port = -1; 124 125 127 private String m_path = null; 128 129 134 private String m_queryString = null; 135 136 138 private String m_fragment = null; 139 140 141 private static boolean DEBUG = false; 142 143 146 public URI(){} 147 148 154 public URI(URI p_other) 155 { 156 initialize(p_other); 157 } 158 159 174 public URI(String p_uriSpec) throws MalformedURIException 175 { 176 this((URI) null, p_uriSpec); 177 } 178 179 191 public URI(URI p_base, String p_uriSpec) throws MalformedURIException 192 { 193 initialize(p_base, p_uriSpec); 194 } 195 196 208 public URI(String p_scheme, String p_schemeSpecificPart) 209 throws MalformedURIException 210 { 211 212 if (p_scheme == null || p_scheme.trim().length() == 0) 213 { 214 throw new MalformedURIException( 215 "Cannot construct URI with null/empty scheme!"); 216 } 217 218 if (p_schemeSpecificPart == null 219 || p_schemeSpecificPart.trim().length() == 0) 220 { 221 throw new MalformedURIException( 222 "Cannot construct URI with null/empty scheme-specific part!"); 223 } 224 225 setScheme(p_scheme); 226 setPath(p_schemeSpecificPart); 227 } 228 229 250 public URI(String p_scheme, String p_host, String p_path, String p_queryString, String p_fragment) 251 throws MalformedURIException 252 { 253 this(p_scheme, null, p_host, -1, p_path, p_queryString, p_fragment); 254 } 255 256 281 public URI(String p_scheme, String p_userinfo, String p_host, int p_port, String p_path, String p_queryString, String p_fragment) 282 throws MalformedURIException 283 { 284 285 if (p_scheme == null || p_scheme.trim().length() == 0) 286 { 287 throw new MalformedURIException(XMLMessages.createXMLMessage(XMLErrorResources.ER_SCHEME_REQUIRED, null)); } 289 290 if (p_host == null) 291 { 292 if (p_userinfo != null) 293 { 294 throw new MalformedURIException( 295 XMLMessages.createXMLMessage(XMLErrorResources.ER_NO_USERINFO_IF_NO_HOST, null)); } 297 298 if (p_port != -1) 299 { 300 throw new MalformedURIException( 301 XMLMessages.createXMLMessage(XMLErrorResources.ER_NO_PORT_IF_NO_HOST, null)); } 303 } 304 305 if (p_path != null) 306 { 307 if (p_path.indexOf('?') != -1 && p_queryString != null) 308 { 309 throw new MalformedURIException( 310 XMLMessages.createXMLMessage(XMLErrorResources.ER_NO_QUERY_STRING_IN_PATH, null)); } 312 313 if (p_path.indexOf('#') != -1 && p_fragment != null) 314 { 315 throw new MalformedURIException( 316 XMLMessages.createXMLMessage(XMLErrorResources.ER_NO_FRAGMENT_STRING_IN_PATH, null)); } 318 } 319 320 setScheme(p_scheme); 321 setHost(p_host); 322 setPort(p_port); 323 setUserinfo(p_userinfo); 324 setPath(p_path); 325 setQueryString(p_queryString); 326 setFragment(p_fragment); 327 } 328 329 334 private void initialize(URI p_other) 335 { 336 337 m_scheme = p_other.getScheme(); 338 m_userinfo = p_other.getUserinfo(); 339 m_host = p_other.getHost(); 340 m_port = p_other.getPort(); 341 m_path = p_other.getPath(); 342 m_queryString = p_other.getQueryString(); 343 m_fragment = p_other.getFragment(); 344 } 345 346 362 private void initialize(URI p_base, String p_uriSpec) 363 throws MalformedURIException 364 { 365 366 if (p_base == null 367 && (p_uriSpec == null || p_uriSpec.trim().length() == 0)) 368 { 369 throw new MalformedURIException( 370 XMLMessages.createXMLMessage(XMLErrorResources.ER_CANNOT_INIT_URI_EMPTY_PARMS, null)); } 372 373 if (p_uriSpec == null || p_uriSpec.trim().length() == 0) 375 { 376 initialize(p_base); 377 378 return; 379 } 380 381 String uriSpec = p_uriSpec.trim(); 382 int uriSpecLen = uriSpec.length(); 383 int index = 0; 384 385 int colonIndex = uriSpec.indexOf(':'); 387 if (colonIndex < 0) 388 { 389 if (p_base == null) 390 { 391 throw new MalformedURIException(XMLMessages.createXMLMessage(XMLErrorResources.ER_NO_SCHEME_IN_URI, new Object []{uriSpec})); } 393 } 394 else 395 { 396 initializeScheme(uriSpec); 397 uriSpec = uriSpec.substring(colonIndex+1); 398 uriSpecLen = uriSpec.length(); 399 } 400 401 if (((index + 1) < uriSpecLen) 403 && (uriSpec.substring(index).startsWith("//"))) 404 { 405 index += 2; 406 407 int startPos = index; 408 409 char testChar = '\0'; 411 412 while (index < uriSpecLen) 413 { 414 testChar = uriSpec.charAt(index); 415 416 if (testChar == '/' || testChar == '?' || testChar == '#') 417 { 418 break; 419 } 420 421 index++; 422 } 423 424 if (index > startPos) 427 { 428 initializeAuthority(uriSpec.substring(startPos, index)); 429 } 430 else 431 { 432 m_host = ""; 433 } 434 } 435 436 initializePath(uriSpec.substring(index)); 437 438 if (p_base != null) 444 { 445 446 if (m_path.length() == 0 && m_scheme == null && m_host == null) 454 { 455 m_scheme = p_base.getScheme(); 456 m_userinfo = p_base.getUserinfo(); 457 m_host = p_base.getHost(); 458 m_port = p_base.getPort(); 459 m_path = p_base.getPath(); 460 461 if (m_queryString == null) 462 { 463 m_queryString = p_base.getQueryString(); 464 } 465 466 return; 467 } 468 469 if (m_scheme == null) 472 { 473 m_scheme = p_base.getScheme(); 474 } 475 476 if (m_host == null) 479 { 480 m_userinfo = p_base.getUserinfo(); 481 m_host = p_base.getHost(); 482 m_port = p_base.getPort(); 483 } 484 else 485 { 486 return; 487 } 488 489 if (m_path.length() > 0 && m_path.startsWith("/")) 491 { 492 return; 493 } 494 495 String path = new String (); 498 String basePath = p_base.getPath(); 499 500 if (basePath != null) 502 { 503 int lastSlash = basePath.lastIndexOf('/'); 504 505 if (lastSlash != -1) 506 { 507 path = basePath.substring(0, lastSlash + 1); 508 } 509 } 510 511 path = path.concat(m_path); 513 514 index = -1; 516 517 while ((index = path.indexOf("/./")) != -1) 518 { 519 path = path.substring(0, index + 1).concat(path.substring(index + 3)); 520 } 521 522 if (path.endsWith("/.")) 524 { 525 path = path.substring(0, path.length() - 1); 526 } 527 528 index = -1; 531 532 int segIndex = -1; 533 String tempString = null; 534 535 while ((index = path.indexOf("/../")) > 0) 536 { 537 tempString = path.substring(0, path.indexOf("/../")); 538 segIndex = tempString.lastIndexOf('/'); 539 540 if (segIndex != -1) 541 { 542 if (!tempString.substring(segIndex++).equals("..")) 543 { 544 path = path.substring(0, segIndex).concat(path.substring(index 545 + 4)); 546 } 547 } 548 } 549 550 if (path.endsWith("/..")) 553 { 554 tempString = path.substring(0, path.length() - 3); 555 segIndex = tempString.lastIndexOf('/'); 556 557 if (segIndex != -1) 558 { 559 path = path.substring(0, segIndex + 1); 560 } 561 } 562 563 m_path = path; 564 } 565 } 566 567 575 private void initializeScheme(String p_uriSpec) throws MalformedURIException 576 { 577 578 int uriSpecLen = p_uriSpec.length(); 579 int index = 0; 580 String scheme = null; 581 char testChar = '\0'; 582 583 while (index < uriSpecLen) 584 { 585 testChar = p_uriSpec.charAt(index); 586 587 if (testChar == ':' || testChar == '/' || testChar == '?' 588 || testChar == '#') 589 { 590 break; 591 } 592 593 index++; 594 } 595 596 scheme = p_uriSpec.substring(0, index); 597 598 if (scheme.length() == 0) 599 { 600 throw new MalformedURIException(XMLMessages.createXMLMessage(XMLErrorResources.ER_NO_SCHEME_INURI, null)); } 602 else 603 { 604 setScheme(scheme); 605 } 606 } 607 608 616 private void initializeAuthority(String p_uriSpec) 617 throws MalformedURIException 618 { 619 620 int index = 0; 621 int start = 0; 622 int end = p_uriSpec.length(); 623 char testChar = '\0'; 624 String userinfo = null; 625 626 if (p_uriSpec.indexOf('@', start) != -1) 628 { 629 while (index < end) 630 { 631 testChar = p_uriSpec.charAt(index); 632 633 if (testChar == '@') 634 { 635 break; 636 } 637 638 index++; 639 } 640 641 userinfo = p_uriSpec.substring(start, index); 642 643 index++; 644 } 645 646 String host = null; 648 649 start = index; 650 651 while (index < end) 652 { 653 testChar = p_uriSpec.charAt(index); 654 655 if (testChar == ':') 656 { 657 break; 658 } 659 660 index++; 661 } 662 663 host = p_uriSpec.substring(start, index); 664 665 int port = -1; 666 667 if (host.length() > 0) 668 { 669 670 if (testChar == ':') 672 { 673 index++; 674 675 start = index; 676 677 while (index < end) 678 { 679 index++; 680 } 681 682 String portStr = p_uriSpec.substring(start, index); 683 684 if (portStr.length() > 0) 685 { 686 for (int i = 0; i < portStr.length(); i++) 687 { 688 if (!isDigit(portStr.charAt(i))) 689 { 690 throw new MalformedURIException( 691 portStr + " is invalid. Port should only contain digits!"); 692 } 693 } 694 695 try 696 { 697 port = Integer.parseInt(portStr); 698 } 699 catch (NumberFormatException nfe) 700 { 701 702 } 704 } 705 } 706 } 707 708 setHost(host); 709 setPort(port); 710 setUserinfo(userinfo); 711 } 712 713 720 private void initializePath(String p_uriSpec) throws MalformedURIException 721 { 722 723 if (p_uriSpec == null) 724 { 725 throw new MalformedURIException( 726 "Cannot initialize path from null string!"); 727 } 728 729 int index = 0; 730 int start = 0; 731 int end = p_uriSpec.length(); 732 char testChar = '\0'; 733 734 while (index < end) 736 { 737 testChar = p_uriSpec.charAt(index); 738 739 if (testChar == '?' || testChar == '#') 740 { 741 break; 742 } 743 744 if (testChar == '%') 746 { 747 if (index + 2 >= end ||!isHex(p_uriSpec.charAt(index + 1)) 748 ||!isHex(p_uriSpec.charAt(index + 2))) 749 { 750 throw new MalformedURIException( 751 XMLMessages.createXMLMessage(XMLErrorResources.ER_PATH_CONTAINS_INVALID_ESCAPE_SEQUENCE, null)); } 753 } 754 else if (!isReservedCharacter(testChar) 755 &&!isUnreservedCharacter(testChar)) 756 { 757 if ('\\' != testChar) 758 throw new MalformedURIException(XMLMessages.createXMLMessage(XMLErrorResources.ER_PATH_INVALID_CHAR, new Object []{String.valueOf(testChar)})); } 761 762 index++; 763 } 764 765 m_path = p_uriSpec.substring(start, index); 766 767 if (testChar == '?') 769 { 770 index++; 771 772 start = index; 773 774 while (index < end) 775 { 776 testChar = p_uriSpec.charAt(index); 777 778 if (testChar == '#') 779 { 780 break; 781 } 782 783 if (testChar == '%') 784 { 785 if (index + 2 >= end ||!isHex(p_uriSpec.charAt(index + 1)) 786 ||!isHex(p_uriSpec.charAt(index + 2))) 787 { 788 throw new MalformedURIException( 789 "Query string contains invalid escape sequence!"); 790 } 791 } 792 else if (!isReservedCharacter(testChar) 793 &&!isUnreservedCharacter(testChar)) 794 { 795 throw new MalformedURIException( 796 "Query string contains invalid character:" + testChar); 797 } 798 799 index++; 800 } 801 802 m_queryString = p_uriSpec.substring(start, index); 803 } 804 805 if (testChar == '#') 807 { 808 index++; 809 810 start = index; 811 812 while (index < end) 813 { 814 testChar = p_uriSpec.charAt(index); 815 816 if (testChar == '%') 817 { 818 if (index + 2 >= end ||!isHex(p_uriSpec.charAt(index + 1)) 819 ||!isHex(p_uriSpec.charAt(index + 2))) 820 { 821 throw new MalformedURIException( 822 "Fragment contains invalid escape sequence!"); 823 } 824 } 825 else if (!isReservedCharacter(testChar) 826 &&!isUnreservedCharacter(testChar)) 827 { 828 throw new MalformedURIException( 829 "Fragment contains invalid character:" + testChar); 830 } 831 832 index++; 833 } 834 835 m_fragment = p_uriSpec.substring(start, index); 836 } 837 } 838 839 844 public String getScheme() 845 { 846 return m_scheme; 847 } 848 849 855 public String getSchemeSpecificPart() 856 { 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 946 public String getPath(boolean p_includeQueryString, 947 boolean p_includeFragment) 948 { 949 950 StringBuffer pathString = new StringBuffer (m_path); 951 952 if (p_includeQueryString && m_queryString != null) 953 { 954 pathString.append('?'); 955 pathString.append(m_queryString); 956 } 957 958 if (p_includeFragment && m_fragment != null) 959 { 960 pathString.append('#'); 961 pathString.append(m_fragment); 962 } 963 964 return pathString.toString(); 965 } 966 967 973 public String getPath() 974 { 975 return m_path; 976 } 977 978 985 public String getQueryString() 986 { 987 return m_queryString; 988 } 989 990 997 public String getFragment() 998 { 999 return m_fragment; 1000 } 1001 1002 1011 public void setScheme(String p_scheme) throws MalformedURIException 1012 { 1013 1014 if (p_scheme == null) 1015 { 1016 throw new MalformedURIException(XMLMessages.createXMLMessage(XMLErrorResources.ER_SCHEME_FROM_NULL_STRING, null)); } 1018 1019 if (!isConformantSchemeName(p_scheme)) 1020 { 1021 throw new MalformedURIException(XMLMessages.createXMLMessage(XMLErrorResources.ER_SCHEME_NOT_CONFORMANT, null)); } 1023 1024 m_scheme = p_scheme.toLowerCase(); 1025 } 1026 1027 1036 public void setUserinfo(String p_userinfo) throws MalformedURIException 1037 { 1038 1039 if (p_userinfo == null) 1040 { 1041 m_userinfo = null; 1042 } 1043 else 1044 { 1045 if (m_host == null) 1046 { 1047 throw new MalformedURIException( 1048 "Userinfo cannot be set when host is null!"); 1049 } 1050 1051 int index = 0; 1054 int end = p_userinfo.length(); 1055 char testChar = '\0'; 1056 1057 while (index < end) 1058 { 1059 testChar = p_userinfo.charAt(index); 1060 1061 if (testChar == '%') 1062 { 1063 if (index + 2 >= end ||!isHex(p_userinfo.charAt(index + 1)) 1064 ||!isHex(p_userinfo.charAt(index + 2))) 1065 { 1066 throw new MalformedURIException( 1067 "Userinfo contains invalid escape sequence!"); 1068 } 1069 } 1070 else if (!isUnreservedCharacter(testChar) 1071 && USERINFO_CHARACTERS.indexOf(testChar) == -1) 1072 { 1073 throw new MalformedURIException( 1074 "Userinfo contains invalid character:" + testChar); 1075 } 1076 1077 index++; 1078 } 1079 } 1080 1081 m_userinfo = p_userinfo; 1082 } 1083 1084 1093 public void setHost(String p_host) throws MalformedURIException 1094 { 1095 1096 if (p_host == null || p_host.trim().length() == 0) 1097 { 1098 m_host = p_host; 1099 m_userinfo = null; 1100 m_port = -1; 1101 } 1102 else if (!isWellFormedAddress(p_host)) 1103 { 1104 throw new MalformedURIException(XMLMessages.createXMLMessage(XMLErrorResources.ER_HOST_ADDRESS_NOT_WELLFORMED, null)); } 1106 1107 m_host = p_host; 1108 } 1109 1110 1121 public void setPort(int p_port) throws MalformedURIException 1122 { 1123 1124 if (p_port >= 0 && p_port <= 65535) 1125 { 1126 if (m_host == null) 1127 { 1128 throw new MalformedURIException( 1129 XMLMessages.createXMLMessage(XMLErrorResources.ER_PORT_WHEN_HOST_NULL, null)); } 1131 } 1132 else if (p_port != -1) 1133 { 1134 throw new MalformedURIException(XMLMessages.createXMLMessage(XMLErrorResources.ER_INVALID_PORT, null)); } 1136 1137 m_port = p_port; 1138 } 1139 1140 1154 public void setPath(String p_path) throws MalformedURIException 1155 { 1156 1157 if (p_path == null) 1158 { 1159 m_path = null; 1160 m_queryString = null; 1161 m_fragment = null; 1162 } 1163 else 1164 { 1165 initializePath(p_path); 1166 } 1167 } 1168 1169 1182 public void appendPath(String p_addToPath) throws MalformedURIException 1183 { 1184 1185 if (p_addToPath == null || p_addToPath.trim().length() == 0) 1186 { 1187 return; 1188 } 1189 1190 if (!isURIString(p_addToPath)) 1191 { 1192 throw new MalformedURIException(XMLMessages.createXMLMessage(XMLErrorResources.ER_PATH_INVALID_CHAR, new Object []{p_addToPath})); } 1194 1195 if (m_path == null || m_path.trim().length() == 0) 1196 { 1197 if (p_addToPath.startsWith("/")) 1198 { 1199 m_path = p_addToPath; 1200 } 1201 else 1202 { 1203 m_path = "/" + p_addToPath; 1204 } 1205 } 1206 else if (m_path.endsWith("/")) 1207 { 1208 if (p_addToPath.startsWith("/")) 1209 { 1210 m_path = m_path.concat(p_addToPath.substring(1)); 1211 } 1212 else 1213 { 1214 m_path = m_path.concat(p_addToPath); 1215 } 1216 } 1217 else 1218 { 1219 if (p_addToPath.startsWith("/")) 1220 { 1221 m_path = m_path.concat(p_addToPath); 1222 } 1223 else 1224 { 1225 m_path = m_path.concat("/" + p_addToPath); 1226 } 1227 } 1228 } 1229 1230 1241 public void setQueryString(String p_queryString) 1242 throws MalformedURIException 1243 { 1244 1245 if (p_queryString == null) 1246 { 1247 m_queryString = null; 1248 } 1249 else if (!isGenericURI()) 1250 { 1251 throw new MalformedURIException( 1252 "Query string can only be set for a generic URI!"); 1253 } 1254 else if (getPath() == null) 1255 { 1256 throw new MalformedURIException( 1257 "Query string cannot be set when path is null!"); 1258 } 1259 else if (!isURIString(p_queryString)) 1260 { 1261 throw new MalformedURIException( 1262 "Query string contains invalid character!"); 1263 } 1264 else 1265 { 1266 m_queryString = p_queryString; 1267 } 1268 } 1269 1270 1281 public void setFragment(String p_fragment) throws MalformedURIException 1282 { 1283 1284 if (p_fragment == null) 1285 { 1286 m_fragment = null; 1287 } 1288 else if (!isGenericURI()) 1289 { 1290 throw new MalformedURIException( 1291 XMLMessages.createXMLMessage(XMLErrorResources.ER_FRAG_FOR_GENERIC_URI, null)); } 1293 else if (getPath() == null) 1294 { 1295 throw new MalformedURIException( 1296 XMLMessages.createXMLMessage(XMLErrorResources.ER_FRAG_WHEN_PATH_NULL, null)); } 1298 else if (!isURIString(p_fragment)) 1299 { 1300 throw new MalformedURIException(XMLMessages.createXMLMessage(XMLErrorResources.ER_FRAG_INVALID_CHAR, null)); } 1302 else 1303 { 1304 m_fragment = p_fragment; 1305 } 1306 } 1307 1308 1316 public boolean equals(Object p_test) 1317 { 1318 1319 if (p_test instanceof URI) 1320 { 1321 URI testURI = (URI) p_test; 1322 1323 if (((m_scheme == null && testURI.m_scheme == null) || (m_scheme != null && testURI.m_scheme != null && m_scheme.equals( 1324 testURI.m_scheme))) && ((m_userinfo == null && testURI.m_userinfo == null) || (m_userinfo != null && testURI.m_userinfo != null && m_userinfo.equals( 1325 testURI.m_userinfo))) && ((m_host == null && testURI.m_host == null) || (m_host != null && testURI.m_host != null && m_host.equals( 1326 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( 1327 testURI.m_path))) && ((m_queryString == null && testURI.m_queryString == null) || (m_queryString != null && testURI.m_queryString != null && m_queryString.equals( 1328 testURI.m_queryString))) && ((m_fragment == null && testURI.m_fragment == null) || (m_fragment != null && testURI.m_fragment != null && m_fragment.equals( 1329 testURI.m_fragment)))) 1330 { 1331 return true; 1332 } 1333 } 1334 1335 return false; 1336 } 1337 1338 1343 public String toString() 1344 { 1345 1346 StringBuffer uriSpecString = new StringBuffer (); 1347 1348 if (m_scheme != null) 1349 { 1350 uriSpecString.append(m_scheme); 1351 uriSpecString.append(':'); 1352 } 1353 1354 uriSpecString.append(getSchemeSpecificPart()); 1355 1356 return uriSpecString.toString(); 1357 } 1358 1359 1366 public boolean isGenericURI() 1367 { 1368 1369 return (m_host != null); 1372 } 1373 1374 1383 public static boolean isConformantSchemeName(String p_scheme) 1384 { 1385 1386 if (p_scheme == null || p_scheme.trim().length() == 0) 1387 { 1388 return false; 1389 } 1390 1391 if (!isAlpha(p_scheme.charAt(0))) 1392 { 1393 return false; 1394 } 1395 1396 char testChar; 1397 1398 for (int i = 1; i < p_scheme.length(); i++) 1399 { 1400 testChar = p_scheme.charAt(i); 1401 1402 if (!isAlphanum(testChar) && SCHEME_CHARACTERS.indexOf(testChar) == -1) 1403 { 1404 return false; 1405 } 1406 } 1407 1408 return true; 1409 } 1410 1411 1424 public static boolean isWellFormedAddress(String p_address) 1425 { 1426 1427 if (p_address == null) 1428 { 1429 return false; 1430 } 1431 1432 String address = p_address.trim(); 1433 int addrLength = address.length(); 1434 1435 if (addrLength == 0 || addrLength > 255) 1436 { 1437 return false; 1438 } 1439 1440 if (address.startsWith(".") || address.startsWith("-")) 1441 { 1442 return false; 1443 } 1444 1445 int index = address.lastIndexOf('.'); 1449 1450 if (address.endsWith(".")) 1451 { 1452 index = address.substring(0, index).lastIndexOf('.'); 1453 } 1454 1455 if (index + 1 < addrLength && isDigit(p_address.charAt(index + 1))) 1456 { 1457 char testChar; 1458 int numDots = 0; 1459 1460 for (int i = 0; i < addrLength; i++) 1464 { 1465 testChar = address.charAt(i); 1466 1467 if (testChar == '.') 1468 { 1469 if (!isDigit(address.charAt(i - 1)) 1470 || (i + 1 < addrLength &&!isDigit(address.charAt(i + 1)))) 1471 { 1472 return false; 1473 } 1474 1475 numDots++; 1476 } 1477 else if (!isDigit(testChar)) 1478 { 1479 return false; 1480 } 1481 } 1482 1483 if (numDots != 3) 1484 { 1485 return false; 1486 } 1487 } 1488 else 1489 { 1490 1491 char testChar; 1494 1495 for (int i = 0; i < addrLength; i++) 1496 { 1497 testChar = address.charAt(i); 1498 1499 if (testChar == '.') 1500 { 1501 if (!isAlphanum(address.charAt(i - 1))) 1502 { 1503 return false; 1504 } 1505 1506 if (i + 1 < addrLength &&!isAlphanum(address.charAt(i + 1))) 1507 { 1508 return false; 1509 } 1510 } 1511 else if (!isAlphanum(testChar) && testChar != '-') 1512 { 1513 return false; 1514 } 1515 } 1516 } 1517 1518 return true; 1519 } 1520 1521 1528 private static boolean isDigit(char p_char) 1529 { 1530 return p_char >= '0' && p_char <= '9'; 1531 } 1532 1533 1541 private static boolean isHex(char p_char) 1542 { 1543 return (isDigit(p_char) || (p_char >= 'a' && p_char <= 'f') 1544 || (p_char >= 'A' && p_char <= 'F')); 1545 } 1546 1547 1554 private static boolean isAlpha(char p_char) 1555 { 1556 return ((p_char >= 'a' && p_char <= 'z') 1557 || (p_char >= 'A' && p_char <= 'Z')); 1558 } 1559 1560 1567 private static boolean isAlphanum(char p_char) 1568 { 1569 return (isAlpha(p_char) || isDigit(p_char)); 1570 } 1571 1572 1580 private static boolean isReservedCharacter(char p_char) 1581 { 1582 return RESERVED_CHARACTERS.indexOf(p_char) != -1; 1583 } 1584 1585 1592 private static boolean isUnreservedCharacter(char p_char) 1593 { 1594 return (isAlphanum(p_char) || MARK_CHARACTERS.indexOf(p_char) != -1); 1595 } 1596 1597 1606 private static boolean isURIString(String p_uric) 1607 { 1608 1609 if (p_uric == null) 1610 { 1611 return false; 1612 } 1613 1614 int end = p_uric.length(); 1615 char testChar = '\0'; 1616 1617 for (int i = 0; i < end; i++) 1618 { 1619 testChar = p_uric.charAt(i); 1620 1621 if (testChar == '%') 1622 { 1623 if (i + 2 >= end ||!isHex(p_uric.charAt(i + 1)) 1624 ||!isHex(p_uric.charAt(i + 2))) 1625 { 1626 return false; 1627 } 1628 else 1629 { 1630 i += 2; 1631 1632 continue; 1633 } 1634 } 1635 1636 if (isReservedCharacter(testChar) || isUnreservedCharacter(testChar)) 1637 { 1638 continue; 1639 } 1640 else 1641 { 1642 return false; 1643 } 1644 } 1645 1646 return true; 1647 } 1648} 1649 | Popular Tags |