1 112 113 114 115 116 117 package org.exolab.jms.net.uri; 118 119 120 121 import java.io.IOException ; 122 123 import java.io.Serializable ; 124 125 126 127 128 129 192 193 public final class URI 194 195 { 196 197 198 199 200 201 210 211 public static class MalformedURIException 212 213 extends IOException 214 215 { 216 217 218 219 220 221 228 229 public MalformedURIException() 230 231 { 232 233 super(); 234 235 } 236 237 238 239 240 241 252 253 public MalformedURIException( String message ) 254 255 { 256 257 super( message ); 258 259 } 260 261 262 263 264 265 } 266 267 268 269 270 271 276 277 private static final String RESERVED_CHARACTERS = ";/?:@&=+$,"; 278 279 280 281 282 283 290 291 private static final String MARK_CHARACTERS = "-_.!~*'() "; 292 293 294 295 296 297 302 303 private static final String SCHEME_CHARACTERS = "+-."; 304 305 306 307 308 309 316 317 private static final String USERINFO_CHARACTERS = ";:&=+$,"; 318 319 320 321 322 323 328 329 private String _scheme = null; 330 331 332 333 334 335 340 341 private String _userinfo = null; 342 343 344 345 346 347 352 353 private String _host = null; 354 355 356 357 358 359 364 365 private int _port = -1; 366 367 368 369 370 371 376 377 private String _path = null; 378 379 380 381 382 383 390 391 private String _queryString = null; 392 393 394 395 396 397 402 403 private String _fragment = null; 404 405 406 407 408 409 414 415 private static boolean DEBUG = false; 416 417 418 419 420 421 426 427 public URI() 428 429 { 430 431 } 432 433 434 435 436 437 448 449 public URI( URI other) 450 451 { 452 453 initialize( other ); 454 455 } 456 457 458 459 460 461 484 485 public URI( String uriSpec ) 486 487 throws MalformedURIException 488 489 { 490 491 this( (URI) null, uriSpec); 492 493 } 494 495 496 497 498 499 516 517 public URI( URI base, String uriSpec) 518 519 throws MalformedURIException 520 521 { 522 523 initialize( base, uriSpec ); 524 525 } 526 527 528 529 530 531 550 551 public URI( String scheme, String schemeSpecificPart ) 552 553 throws MalformedURIException 554 555 { 556 557 if ( scheme == null || scheme.trim().length() == 0 ) 558 559 throw new MalformedURIException( "Argument scheme is null or an empty string" ); 560 561 if ( schemeSpecificPart == null || schemeSpecificPart.trim().length() == 0 ) 562 563 throw new MalformedURIException( "Argument schemeSpecificPart is null or an empty string" ); 564 565 setScheme( scheme ); 566 567 setPath( schemeSpecificPart ); 568 569 } 570 571 572 573 574 575 610 611 public URI( String scheme, String host, String path, 612 613 String queryString, String fragment ) 614 615 throws MalformedURIException 616 617 { 618 619 this( scheme, null, host, -1, path, queryString, fragment ); 620 621 } 622 623 624 625 626 627 668 669 public URI( String scheme, String userinfo, String host, int port, 670 671 String path, String queryString, String fragment ) 672 673 throws MalformedURIException 674 675 { 676 677 if ( scheme == null || scheme.trim().length() == 0 ) 678 679 throw new MalformedURIException( "Argument scheme is null or an empty string" ); 680 681 if ( host == null ) { 682 683 if ( userinfo != null ) 684 685 throw new MalformedURIException( "Argument userInfo must be null if host is null" ); 686 687 if ( port != -1 ) 688 689 throw new MalformedURIException( "Argument port must be null if host is null" ); 690 691 } else if ( host.trim().length() == 0 ) 692 693 throw new IllegalArgumentException ( "Argument host is an empty string" ); 694 695 if ( path != null ) { 696 697 if ( path.indexOf('?') != -1 && queryString != null ) 698 699 throw new MalformedURIException( "Argument queryString is illegal if path includes query string" ); 700 701 if ( path.indexOf('#') != -1 && fragment != null ) 702 703 throw new MalformedURIException( "Argument fragment is illegal if path includes fragment identifier" ); 704 705 } else if ( path.trim().length() == 0 ) 706 707 throw new IllegalArgumentException ( "Argument path is an empty string" ); 708 709 setScheme( scheme ); 710 711 setHost( host ); 712 713 setPort( port ); 714 715 setUserinfo( userinfo ); 716 717 setPath( path ); 718 719 setQueryString( queryString ); 720 721 setFragment( fragment ); 722 723 } 724 725 726 727 728 729 738 739 private void initialize( URI other ) 740 741 { 742 743 _scheme = other.getScheme(); 744 745 _userinfo = other.getUserinfo(); 746 747 _host = other.getHost(); 748 749 _port = other.getPort(); 750 751 _path = other.getPath(); 752 753 _queryString = other.getQueryString(); 754 755 _fragment = other.getFragment(); 756 757 } 758 759 760 761 762 763 786 787 private void initialize( URI base, String uriSpec ) 788 789 throws MalformedURIException 790 791 { 792 793 int uriSpecLen; 794 795 int index; 796 797 int startPos; 798 799 char testChar; 800 801 802 803 if ( base == null && ( uriSpec == null || uriSpec.trim().length() == 0) ) 804 805 throw new MalformedURIException( "Argument base is null and argument uriSpec is null or an empty string" ); 806 807 809 if ( uriSpec == null || uriSpec.trim().length() == 0 ) { 810 811 initialize( base ); 812 813 return; 814 815 } 816 817 818 819 uriSpec = uriSpec.trim(); 820 821 uriSpecLen = uriSpec.length(); 822 823 index = 0; 824 825 826 827 829 if ( uriSpec.indexOf( ':' ) == -1 ) { 830 831 if ( base == null ) 832 833 throw new MalformedURIException( "No scheme found in URI." ); 834 835 } else { 836 837 initializeScheme( uriSpec ); 838 839 index = _scheme.length() + 1; 840 841 } 842 843 844 845 847 if ( ( index + 1 < uriSpecLen ) && ( uriSpec.substring( index ).startsWith( "//" ) ) ) { 848 849 index += 2; 850 851 startPos = index; 852 853 855 testChar = '\0'; 856 857 while ( index < uriSpecLen ) { 858 859 testChar = uriSpec.charAt( index ); 860 861 if ( testChar == '/' || testChar == '?' || testChar == '#' ) 862 863 break; 864 865 index++; 866 867 } 868 869 870 871 873 875 if ( index > startPos ) 876 877 initializeAuthority( uriSpec.substring( startPos, index ) ); 878 879 else 880 881 _host = ""; 882 883 } 884 885 886 887 initializePath( uriSpec.substring( index ) ); 888 889 890 891 893 895 897 899 901 if ( base != null ) { 902 903 904 905 907 909 911 913 915 917 919 if ( _path.length() == 0 && _scheme == null && _host == null ) { 920 921 _scheme = base.getScheme(); 922 923 _userinfo = base.getUserinfo(); 924 925 _host = base.getHost(); 926 927 _port = base.getPort(); 928 929 _path = base.getPath(); 930 931 if ( _queryString == null ) 932 933 _queryString = base.getQueryString(); 934 935 return; 936 937 } 938 939 940 941 943 945 if ( _scheme == null ) 946 947 _scheme = base.getScheme(); 948 949 else 950 951 return; 952 953 954 955 957 959 if ( _host == null ) { 960 961 _userinfo = base.getUserinfo(); 962 963 _host = base.getHost(); 964 965 _port = base.getPort(); 966 967 } else 968 969 return; 970 971 972 973 975 if ( _path.length() > 0 && _path.startsWith( "/" ) ) 976 977 return; 978 979 980 981 983 985 String tmpPath = new String (); 986 987 String basePath = base.getPath(); 988 989 990 991 993 if ( basePath != null ) { 994 995 int lastSlash = basePath.lastIndexOf( '/' ); 996 997 if ( lastSlash != -1 ) 998 999 tmpPath = basePath.substring( 0, lastSlash + 1 ); 1000 1001 } 1002 1003 1004 1005 1007 tmpPath = tmpPath.concat( tmpPath ); 1008 1009 1011 index = -1; 1012 1013 while ( ( index = tmpPath.indexOf( "/./" ) ) != -1 ) 1014 1015 tmpPath = tmpPath.substring( 0, index + 1 ).concat( tmpPath.substring( index + 3 ) ); 1016 1017 1018 1019 1021 if ( tmpPath.endsWith("/.") ) 1022 1023 tmpPath = tmpPath.substring( 0, tmpPath.length() - 1 ); 1024 1025 1026 1027 1029 1031 index = -1; 1032 1033 1034 1035 int segIndex = -1; 1036 1037 String tempString = null; 1038 1039 1040 1041 while ( ( index = tmpPath.indexOf( "/../" ) ) > 0 ) { 1042 1043 tempString = tmpPath.substring( 0, tmpPath.indexOf( "/../" ) ); 1044 1045 segIndex = tempString.lastIndexOf( '/' ); 1046 1047 if ( segIndex != -1 ) 1048 1049 if ( !tempString.substring( segIndex++ ).equals( ".." ) ) 1050 1051 tmpPath = tmpPath.substring( 0, segIndex ).concat( tmpPath.substring( index + 4 ) ); 1052 1053 } 1054 1055 1056 1057 1059 1061 if ( tmpPath.endsWith( "/.." ) ) { 1062 1063 tempString = tmpPath.substring( 0, tmpPath.length() - 3 ); 1064 1065 segIndex = tempString.lastIndexOf( '/' ); 1066 1067 if ( segIndex != -1 ) 1068 1069 tmpPath = tmpPath.substring( 0, segIndex + 1 ); 1070 1071 } 1072 1073 _path = tmpPath; 1074 1075 } 1076 1077 } 1078 1079 1080 1081 1082 1083 1094 1095 private void initializeScheme( String uriSpec ) 1096 1097 throws MalformedURIException 1098 1099 { 1100 1101 int uriSpecLen = uriSpec.length(); 1102 1103 int index = 0; 1104 1105 String scheme = null; 1106 1107 char testChar = '\0'; 1108 1109 1110 1111 while ( index < uriSpecLen ) { 1112 1113 testChar = uriSpec.charAt( index ); 1114 1115 if ( testChar == ':' || testChar == '/' || testChar == '?' || testChar == '#' ) 1116 1117 break; 1118 1119 index++; 1120 1121 } 1122 1123 scheme = uriSpec.substring( 0, index ); 1124 1125 if ( scheme.length() == 0 ) 1126 1127 throw new MalformedURIException( "No scheme found in URI." ); 1128 1129 else 1130 1131 setScheme( scheme ); 1132 1133 } 1134 1135 1136 1137 1138 1139 1152 1153 private void initializeAuthority( String uriSpec ) 1154 1155 throws MalformedURIException 1156 1157 { 1158 1159 int index = 0; 1160 1161 int start = 0; 1162 1163 int end = uriSpec.length(); 1164 1165 char testChar = '\0'; 1166 1167 String userinfo = null; 1168 1169 1170 1171 1173 if ( uriSpec.indexOf( '@', start ) != -1 ) { 1174 1175 while ( index < end ) { 1176 1177 testChar = uriSpec.charAt( index ); 1178 1179 if ( testChar == '@' ) 1180 1181 break; 1182 1183 index++; 1184 1185 } 1186 1187 userinfo = uriSpec.substring( start, index ); 1188 1189 index++; 1190 1191 } 1192 1193 1194 1195 1197 String host = null; 1198 1199 1200 1201 start = index; 1202 1203 while ( index < end ) { 1204 1205 testChar = uriSpec.charAt( index ); 1206 1207 if ( testChar == ':' ) 1208 1209 break; 1210 1211 index++; 1212 1213 } 1214 1215 host = uriSpec.substring( start, index ); 1216 1217 1218 1219 int port = -1; 1220 1221 1222 1223 if ( host.length() > 0 ) { 1224 1225 1227 if ( testChar == ':' ) { 1228 1229 index++; 1230 1231 start = index; 1232 1233 while ( index < end ) 1234 1235 index++; 1236 1237 1238 1239 String portStr = uriSpec.substring( start, index ); 1240 1241 if ( portStr.length() > 0 ) { 1242 1243 for ( int i = 0 ; i < portStr.length() ; i++ ) 1244 1245 if ( !isDigit( portStr.charAt( i ) ) ) 1246 1247 throw new MalformedURIException( portStr + " is invalid. Port should only contain digits!" ); 1248 1249 try { 1250 1251 port = Integer.parseInt( portStr ); 1252 1253 } catch ( NumberFormatException nfe ) { 1254 1255 1257 } 1258 1259 } 1260 1261 } 1262 1263 } 1264 1265 1266 1267 setHost( host ); 1268 1269 setPort( port ); 1270 1271 setUserinfo( userinfo ); 1272 1273 } 1274 1275 1276 1277 1278 1279 1290 1291 private void initializePath( String uriSpec ) 1292 1293 throws MalformedURIException 1294 1295 { 1296 1297 if ( uriSpec == null ) 1298 1299 throw new MalformedURIException( "Argument uriSpec is null" ); 1300 1301 1302 1303 int index = 0; 1304 1305 int start = 0; 1306 1307 int end = uriSpec.length(); 1308 1309 char testChar = '\0'; 1310 1311 1312 1313 1315 while ( index < end ) { 1316 1317 testChar = uriSpec.charAt( index ); 1318 1319 if ( testChar == '?' || testChar == '#' ) 1320 1321 break; 1322 1323 1325 if ( testChar == '%' ) { 1326 1327 if ( index + 2 >= end || ! isHex( uriSpec.charAt( index + 1 ) ) || 1328 1329 ! isHex( uriSpec.charAt( index + 2 ) ) ) 1330 1331 throw new MalformedURIException( "Path contains invalid escape sequence!" ); 1332 1333 } else if ( ! isReservedCharacter( testChar ) && 1334 1335 ! isUnreservedCharacter( testChar ) ) { 1336 1337 if ( '\\' != testChar ) 1338 1339 throw new MalformedURIException( "Path contains invalid character: " + testChar ); 1340 1341 } 1342 1343 index++; 1344 1345 } 1346 1347 _path = uriSpec.substring( start, index ); 1348 1349 1350 1351 1353 if ( testChar == '?' ) { 1354 1355 index++; 1356 1357 start = index; 1358 1359 while ( index < end ) { 1360 1361 testChar = uriSpec.charAt( index ); 1362 1363 if ( testChar == '#' ) 1364 1365 break; 1366 1367 if ( testChar == '%' ) { 1368 1369 if ( index + 2 >= end || ! isHex( uriSpec.charAt( index + 1 ) ) || 1370 1371 ! isHex( uriSpec.charAt( index + 2 ) ) ) 1372 1373 throw new MalformedURIException( "Query string contains invalid escape sequence!" ); 1374 1375 } else if ( ! isReservedCharacter( testChar ) && 1376 1377 ! isUnreservedCharacter( testChar ) ) 1378 1379 throw new MalformedURIException( "Query string contains invalid character:" + testChar ); 1380 1381 index++; 1382 1383 } 1384 1385 _queryString = uriSpec.substring( start, index ); 1386 1387 } 1388 1389 1390 1391 1393 if ( testChar == '#' ) { 1394 1395 index++; 1396 1397 start = index; 1398 1399 while ( index < end ) { 1400 1401 testChar = uriSpec.charAt( index ); 1402 1403 if ( testChar == '%' ) { 1404 1405 if ( index + 2 >= end || ! isHex( uriSpec.charAt( index + 1 ) ) || 1406 1407 !isHex( uriSpec.charAt( index + 2 ) ) ) 1408 1409 throw new MalformedURIException( "Fragment contains invalid escape sequence!" ); 1410 1411 } else if ( ! isReservedCharacter( testChar ) && 1412 1413 ! isUnreservedCharacter( testChar ) ) 1414 1415 throw new MalformedURIException( "Fragment contains invalid character:" + testChar ); 1416 1417 index++; 1418 1419 } 1420 1421 _fragment = uriSpec.substring( start, index ); 1422 1423 } 1424 1425 } 1426 1427 1428 1429 1430 1431 1440 1441 public String getScheme() 1442 1443 { 1444 1445 return _scheme; 1446 1447 } 1448 1449 1450 1451 1452 1453 1464 1465 public String getSchemeSpecificPart() 1466 1467 { 1468 1469 StringBuffer schemespec = new StringBuffer (); 1470 1471 if ( _userinfo != null || _host != null || _port != -1 ) { 1472 1473 schemespec.append( "//" ); 1474 1475 if ( _userinfo != null) { 1476 1477 schemespec.append( _userinfo ); 1478 1479 schemespec.append( '@' ); 1480 1481 } 1482 1483 if ( _host != null ) 1484 1485 schemespec.append( _host ); 1486 1487 if ( _port != -1 ) { 1488 1489 schemespec.append( ':' ); 1490 1491 schemespec.append( _port ); 1492 1493 } 1494 1495 } 1496 1497 if ( _path != null ) 1498 1499 schemespec.append( _path ); 1500 1501 if ( _queryString != null ) { 1502 1503 schemespec.append( '?' ); 1504 1505 schemespec.append( _queryString ); 1506 1507 } 1508 1509 if ( _fragment != null ) { 1510 1511 schemespec.append( '#' ); 1512 1513 schemespec.append( _fragment ); 1514 1515 } 1516 1517 return schemespec.toString(); 1518 1519 } 1520 1521 1522 1523 1524 1525 1534 1535 public String getUserinfo() 1536 1537 { 1538 1539 return _userinfo; 1540 1541 } 1542 1543 1544 1545 1546 1547 1556 1557 public String getHost() 1558 1559 { 1560 1561 return _host; 1562 1563 } 1564 1565 1566 1567 1568 1569 1578 1579 public int getPort() 1580 1581 { 1582 1583 return _port; 1584 1585 } 1586 1587 1588 1589 1590 1591 1610 1611 public String getPath( boolean includeQueryString, 1612 1613 boolean includeFragment ) 1614 1615 { 1616 1617 StringBuffer pathString = new StringBuffer ( _path ); 1618 1619 if ( includeQueryString && _queryString != null ) { 1620 1621 pathString.append( '?' ); 1622 1623 pathString.append( _queryString ); 1624 1625 } 1626 1627 if ( includeFragment && _fragment != null ) { 1628 1629 pathString.append( '#' ); 1630 1631 pathString.append( _fragment ); 1632 1633 } 1634 1635 return pathString.toString(); 1636 1637 } 1638 1639 1640 1641 1642 1643 1654 1655 public String getPath() 1656 1657 { 1658 1659 return _path; 1660 1661 } 1662 1663 1664 1665 1666 1667 1680 1681 public String getQueryString() 1682 1683 { 1684 1685 return _queryString; 1686 1687 } 1688 1689 1690 1691 1692 1693 1706 1707 public String getFragment() 1708 1709 { 1710 1711 return _fragment; 1712 1713 } 1714 1715 1716 1717 1718 1719 1732 1733 public void setScheme( String scheme ) 1734 1735 throws MalformedURIException 1736 1737 { 1738 1739 if ( scheme == null ) 1740 1741 throw new MalformedURIException( "Argument scheme is null" ); 1742 1743 if ( ! isConformantSchemeName( scheme ) ) 1744 1745 throw new MalformedURIException( "The scheme is not conformant." ); 1746 1747 _scheme = scheme.toLowerCase(); 1748 1749 } 1750 1751 1752 1753 1754 1755 1768 1769 public void setUserinfo( String userinfo ) 1770 1771 throws MalformedURIException 1772 1773 { 1774 1775 if ( userinfo == null ) 1776 1777 _userinfo = null; 1778 1779 else { 1780 1781 if ( _host == null) 1782 1783 throw new MalformedURIException( "Userinfo cannot be set when host is null!" ); 1784 1785 1787 1789 int index = 0; 1790 1791 int end = userinfo.length(); 1792 1793 char testChar = '\0'; 1794 1795 1796 1797 while ( index < end ) { 1798 1799 testChar = userinfo.charAt( index ); 1800 1801 if ( testChar == '%' ) { 1802 1803 if ( index + 2 >= end || ! isHex( userinfo.charAt( index + 1 ) ) || 1804 1805 ! isHex( userinfo.charAt( index + 2 ) ) ) 1806 1807 throw new MalformedURIException( "Userinfo contains invalid escape sequence!" ); 1808 1809 } else if ( ! isUnreservedCharacter( testChar ) && USERINFO_CHARACTERS.indexOf( testChar ) == -1 ) 1810 1811 throw new MalformedURIException( "Userinfo contains invalid character:" + testChar ); 1812 1813 index++; 1814 1815 } 1816 1817 } 1818 1819 _userinfo = userinfo; 1820 1821 } 1822 1823 1824 1825 1826 1827 1840 1841 public void setHost( String host ) 1842 1843 throws MalformedURIException 1844 1845 { 1846 1847 if ( host == null || host.trim().length() == 0 ) { 1848 1849 _host = host; 1850 1851 _userinfo = null; 1852 1853 _port = -1; 1854 1855 } else if ( ! isWellFormedAddress( host ) ) 1856 1857 throw new MalformedURIException( "Host is not a well formed address!" ); 1858 1859 _host = host; 1860 1861 } 1862 1863 1864 1865 1866 1867 1884 1885 public void setPort( int port ) 1886 1887 throws MalformedURIException 1888 1889 { 1890 1891 if ( port >= 0 && port <= 65535 ) { 1892 1893 if ( _host == null ) 1894 1895 throw new MalformedURIException( "Port cannot be set when host is null!" ); 1896 1897 } else if ( port != -1 ) 1898 1899 throw new MalformedURIException( "Invalid port number!" ); 1900 1901 _port = port; 1902 1903 } 1904 1905 1906 1907 1908 1909 1932 1933 public void setPath( String path ) 1934 1935 throws MalformedURIException 1936 1937 { 1938 1939 if ( path == null ) { 1940 1941 _path = null; 1942 1943 _queryString = null; 1944 1945 _fragment = null; 1946 1947 } else 1948 1949 initializePath( path ); 1950 1951 } 1952 1953 1954 1955 1956 1957 1978 1979 public void appendPath( String addToPath ) 1980 1981 throws MalformedURIException 1982 1983 { 1984 1985 if ( addToPath == null || addToPath.trim().length() == 0 ) 1986 1987 return; 1988 1989 if ( ! isURIString( addToPath ) ) 1990 1991 throw new MalformedURIException( "Path contains invalid character!" ); 1992 1993 if ( _path == null || _path.trim().length() == 0 ) { 1994 1995 if ( addToPath.startsWith( "/" ) ) 1996 1997 _path = addToPath; 1998 1999 else 2000 2001 _path = "/" + addToPath; 2002 2003 } else if ( _path.endsWith( "/" ) ) { 2004 2005 if ( addToPath.startsWith( "/" ) ) 2006 2007 _path = _path.concat( addToPath.substring( 1 ) ); 2008 2009 else 2010 2011 _path = _path.concat( addToPath ); 2012 2013 } else { 2014 2015 if ( addToPath.startsWith( "/" ) ) 2016 2017 _path = _path.concat( addToPath ); 2018 2019 else 2020 2021 _path = _path.concat( "/" + addToPath ); 2022 2023 } 2024 2025 } 2026 2027 2028 2029 2030 2031 2048 2049 public void setQueryString( String queryString ) 2050 2051 throws MalformedURIException 2052 2053 { 2054 2055 if ( queryString == null ) 2056 2057 _queryString = null; 2058 2059 else if ( ! isGenericURI() ) 2060 2061 throw new MalformedURIException( "Query string can only be set for a generic URI!" ); 2062 2063 else if ( getPath() == null ) 2064 2065 throw new MalformedURIException( "Query string cannot be set when path is null!" ); 2066 2067 else if ( ! isURIString( queryString ) ) 2068 2069 throw new MalformedURIException( "Query string contains invalid character!" ); 2070 2071 else 2072 2073 _queryString = queryString; 2074 2075 } 2076 2077 2078 2079 2080 2081 2098 2099 public void setFragment( String fragment ) 2100 2101 throws MalformedURIException 2102 2103 { 2104 2105 if ( fragment == null ) 2106 2107 _fragment = null; 2108 2109 else if ( ! isGenericURI() ) 2110 2111 throw new MalformedURIException( "Fragment can only be set for a generic URI!" ); 2112 2113 else if ( getPath() == null ) 2114 2115 throw new MalformedURIException( "Fragment cannot be set when path is null!" ); 2116 2117 else if ( ! isURIString( fragment ) ) 2118 2119 throw new MalformedURIException( "Fragment contains invalid character!" ); 2120 2121 else 2122 2123 _fragment = fragment; 2124 2125 } 2126 2127 2128 2129 2130 2131 2144 2145 public boolean equals( Object test ) 2146 2147 { 2148 2149 if ( test instanceof URI ) { 2150 2151 URI testURI = (URI) test; 2152 2153 return ( ( ( _scheme == null && testURI._scheme == null ) || 2154 2155 ( _scheme != null && testURI._scheme != null && _scheme.equals( testURI._scheme) ) ) && 2156 2157 ( ( _userinfo == null && testURI._userinfo == null ) || 2158 2159 ( _userinfo != null && testURI._userinfo != null && _userinfo.equals( testURI._userinfo ) ) ) && 2160 2161 ( ( _host == null && testURI._host == null ) || 2162 2163 ( _host != null && testURI._host != null && _host.equals( testURI._host ) ) ) && 2164 2165 _port == testURI._port && 2166 2167 ( ( _path == null && testURI._path == null ) || 2168 2169 ( _path != null && testURI._path != null && _path.equals( testURI._path ) ) ) && 2170 2171 ( ( _queryString == null && testURI._queryString == null ) || 2172 2173 ( _queryString != null && testURI._queryString != null && 2174 2175 _queryString.equals( testURI._queryString ) ) ) && 2176 2177 ( ( _fragment == null && testURI._fragment == null ) || 2178 2179 ( _fragment != null && testURI._fragment != null && _fragment.equals( testURI._fragment ) ) ) ); 2180 2181 } 2182 2183 return false; 2184 2185 } 2186 2187 2188 2189 2190 2191 2200 2201 public String toString() 2202 2203 { 2204 2205 StringBuffer uriSpecString = new StringBuffer (); 2206 2207 2208 2209 if ( _scheme != null ) { 2210 2211 uriSpecString.append( _scheme ); 2212 2213 uriSpecString.append( ':' ); 2214 2215 } 2216 2217 uriSpecString.append( getSchemeSpecificPart() ); 2218 2219 return uriSpecString.toString(); 2220 2221 } 2222 2223 2228 public int hashCode() { 2229 return toString().hashCode(); 2230 } 2231 2232 2243 2244 public boolean isGenericURI() 2245 2246 { 2247 2248 2250 2252 return ( _host != null ); 2253 2254 } 2255 2256 2257 2258 2259 2260 2277 2278 public static boolean isConformantSchemeName( String scheme ) 2279 2280 { 2281 2282 if ( scheme == null || scheme.trim().length() == 0 ) 2283 2284 return false; 2285 2286 if ( ! isAlpha( scheme.charAt( 0 ) ) ) 2287 2288 return false; 2289 2290 char testChar; 2291 2292 for ( int i = 1 ; i < scheme.length() ; i++ ) { 2293 2294 testChar = scheme.charAt( i ); 2295 2296 if ( ! isAlphanum( testChar ) && SCHEME_CHARACTERS.indexOf( testChar ) == -1 ) 2297 2298 return false; 2299 2300 } 2301 2302 return true; 2303 2304 } 2305 2306 2307 2308 2309 2310 2331 2332 public static boolean isWellFormedAddress( String address ) 2333 2334 { 2335 2336 char testChar; 2337 2338 2339 2340 if ( address == null ) 2341 2342 return false; 2343 2344 address = address.trim(); 2345 2346 int addrLength = address.length(); 2347 2348 2349 2350 if ( addrLength == 0 || addrLength > 255 ) 2351 2352 return false; 2353 2354 2355 2356 if ( address.startsWith( "." ) || address.startsWith( "-" ) ) 2357 2358 return false; 2359 2360 2361 2362 2364 2366 2368 int index = address.lastIndexOf( '.' ); 2369 2370 if ( address.endsWith( "." ) ) 2371 2372 index = address.substring( 0, index ).lastIndexOf( '.' ); 2373 2374 if ( index + 1 < addrLength && isDigit( address.charAt( index + 1 ) ) ) { 2375 2376 int numDots = 0; 2377 2378 2379 2380 2382 2384 2386 for ( int i = 0 ; i < addrLength ; i++) { 2387 2388 testChar = address.charAt( i ); 2389 2390 if ( testChar == '.' ) { 2391 2392 if ( ! isDigit( address.charAt( i - 1 ) ) || 2393 2394 ( i + 1 < addrLength && ! isDigit( address.charAt( i + 1 ) ) ) ) 2395 2396 return false; 2397 2398 2399 2400 numDots++; 2401 2402 } else if ( ! isDigit( testChar ) ) 2403 2404 return false; 2405 2406 } 2407 2408 if ( numDots != 3 ) 2409 2410 return false; 2411 2412 } else { 2413 2414 2416 2418 for ( int i = 0 ; i < addrLength ; i++ ) { 2419 2420 testChar = address.charAt( i ); 2421 2422 if ( testChar == '.' ) { 2423 2424 if ( ! isAlphanum( address.charAt( i - 1 ) ) ) 2425 2426 return false; 2427 2428 if ( i + 1 < addrLength && ! isAlphanum( address.charAt( i + 1 ) ) ) 2429 2430 return false; 2431 2432 } else if ( ! isAlphanum( testChar ) && testChar != '-' ) 2433 2434 return false; 2435 2436 } 2437 2438 } 2439 2440 return true; 2441 2442 } 2443 2444 2445 2446 2447 2448 2459 2460 private static boolean isDigit( char ch ) 2461 2462 { 2463 2464 return ch >= '0' && ch <= '9'; 2465 2466 } 2467 2468 2469 2470 2471 2472 2485 2486 private static boolean isHex( char ch ) 2487 2488 { 2489 2490 return ( isDigit( ch ) || ( ch >= 'a' && ch <= 'f' ) || 2491 2492 ( ch >= 'A' && ch <= 'F' ) ); 2493 2494 } 2495 2496 2497 2498 2499 2500 2511 2512 private static boolean isAlpha( char ch ) 2513 2514 { 2515 2516 return ( ( ch >= 'a' && ch <= 'z' ) || 2517 2518 ( ch >= 'A' && ch <= 'Z' ) ); 2519 2520 } 2521 2522 2523 2524 2525 2526 2537 2538 private static boolean isAlphanum( char ch ) 2539 2540 { 2541 2542 return ( isAlpha( ch ) || isDigit( ch ) ); 2543 2544 } 2545 2546 2547 2548 2549 2550 2563 2564 private static boolean isReservedCharacter( char ch ) 2565 2566 { 2567 2568 return RESERVED_CHARACTERS.indexOf( ch ) != -1; 2569 2570 } 2571 2572 2573 2574 2575 2576 2587 2588 private static boolean isUnreservedCharacter( char ch ) 2589 2590 { 2591 2592 return ( isAlphanum( ch ) || MARK_CHARACTERS.indexOf( ch ) != -1 ); 2593 2594 } 2595 2596 2597 2598 2599 2600 2615 2616 private static boolean isURIString( String uric ) 2617 2618 { 2619 2620 if ( uric == null ) 2621 2622 return false; 2623 2624 int end = uric.length(); 2625 2626 char testChar = '\0'; 2627 2628 for ( int i = 0 ; i < end ; i++ ) { 2629 2630 testChar = uric.charAt( i ); 2631 2632 if ( testChar == '%' ) { 2633 2634 if ( i + 2 >= end || ! isHex( uric.charAt( i + 1 ) ) || 2635 2636 ! isHex( uric.charAt( i + 2 ) ) ) 2637 2638 return false; 2639 2640 else { 2641 2642 i += 2; 2643 2644 continue; 2645 2646 } 2647 2648 } 2649 2650 if ( isReservedCharacter( testChar ) || isUnreservedCharacter( testChar ) ) 2651 2652 continue; 2653 2654 else 2655 2656 return false; 2657 2658 } 2659 2660 return true; 2661 2662 } 2663 2664 2665 2666 2667 2668} 2669 2670 | Popular Tags |