📄 uri.java
字号:
if ( addToPath.startsWith( "/" ) ) _path = _path.concat( addToPath.substring( 1 ) ); else _path = _path.concat( addToPath ); } else { if ( addToPath.startsWith( "/" ) ) _path = _path.concat( addToPath ); else _path = _path.concat( "/" + addToPath ); } } /** * Set the query string for this URI. A non-null value is valid only * if this is an URI conforming to the generic URI syntax and * the path value is not null. * * @param queryString the query string for this URI * @exception MalformedURIException queryString is not null and this * URI does not conform to the generic URI syntax or if the path is null */ public void setQueryString( String queryString ) throws MalformedURIException { if ( queryString == null ) _queryString = null; else if ( ! isGenericURI() ) throw new MalformedURIException( "Query string can only be set for a generic URI!" ); else if ( getPath() == null ) throw new MalformedURIException( "Query string cannot be set when path is null!" ); else if ( ! isURIString( queryString ) ) throw new MalformedURIException( "Query string contains invalid character!" ); else _queryString = queryString; } /** * Set the fragment for this URI. A non-null value is valid only * if this is a URI conforming to the generic URI syntax and * the path value is not null. * * @param fragment the fragment for this URI * @exception MalformedURIException fragment is not null and this * URI does not conform to the generic URI syntax or if the path is null */ public void setFragment( String fragment ) throws MalformedURIException { if ( fragment == null ) _fragment = null; else if ( ! isGenericURI() ) throw new MalformedURIException( "Fragment can only be set for a generic URI!" ); else if ( getPath() == null ) throw new MalformedURIException( "Fragment cannot be set when path is null!" ); else if ( ! isURIString( fragment ) ) throw new MalformedURIException( "Fragment contains invalid character!" ); else _fragment = fragment; } /** * Determines if the passed-in Object is equivalent to this URI. * * @param test the Object to test for equality. * @return true if test is a URI with all values equal to this * URI, false otherwise */ public boolean equals( Object test ) { if ( test instanceof URI ) { URI testURI = (URI) test; return ( ( ( _scheme == null && testURI._scheme == null ) || ( _scheme != null && testURI._scheme != null && _scheme.equals( testURI._scheme) ) ) && ( ( _userinfo == null && testURI._userinfo == null ) || ( _userinfo != null && testURI._userinfo != null && _userinfo.equals( testURI._userinfo ) ) ) && ( ( _host == null && testURI._host == null ) || ( _host != null && testURI._host != null && _host.equals( testURI._host ) ) ) && _port == testURI._port && ( ( _path == null && testURI._path == null ) || ( _path != null && testURI._path != null && _path.equals( testURI._path ) ) ) && ( ( _queryString == null && testURI._queryString == null ) || ( _queryString != null && testURI._queryString != null && _queryString.equals( testURI._queryString ) ) ) && ( ( _fragment == null && testURI._fragment == null ) || ( _fragment != null && testURI._fragment != null && _fragment.equals( testURI._fragment ) ) ) ); } return false; } /** * Get the URI as a string specification. See RFC 2396 Section 5.2. * * @return the URI string specification */ public String toString() { StringBuffer uriSpecString = new StringBuffer(); if ( _scheme != null ) { uriSpecString.append( _scheme ); uriSpecString.append( ':' ); } uriSpecString.append( getSchemeSpecificPart() ); return uriSpecString.toString(); } /** * Returns the hash code of this URI * * @return the hash code of this URI */ public int hashCode() { return toString().hashCode(); } /** * Get the indicator as to whether this URI uses the "generic URI" * syntax. * * @return true if this URI uses the "generic URI" syntax, false otherwise */ public boolean isGenericURI() { // presence of the host (whether valid or empty) means // double-slashes which means generic uri return ( _host != null ); } /** * Determine whether a scheme conforms to the rules for a scheme name. * A scheme is conformant if it starts with an alphanumeric, and * contains only alphanumerics, '+','-' and '.'. * * * @param scheme The sheme name to check * @return true if the scheme is conformant, false otherwise */ public static boolean isConformantSchemeName( String scheme ) { if ( scheme == null || scheme.trim().length() == 0 ) return false; if ( ! isAlpha( scheme.charAt( 0 ) ) ) return false; char testChar; for ( int i = 1 ; i < scheme.length() ; i++ ) { testChar = scheme.charAt( i ); if ( ! isAlphanum( testChar ) && SCHEME_CHARACTERS.indexOf( testChar ) == -1 ) return false; } return true; } /** * Determine whether a string is syntactically capable of representing * a valid IPv4 address or the domain name of a network host. A valid * IPv4 address consists of four decimal digit groups separated by a * '.'. A hostname consists of domain labels (each of which must * begin and end with an alphanumeric but may contain '-') separated * & by a '.'. See RFC 2396 Section 3.2.2. * * @param address The address string to check * @return true if the string is a syntactically valid IPv4 address or hostname */ public static boolean isWellFormedAddress( String address ) { char testChar; if ( address == null ) return false; address = address.trim(); int addrLength = address.length(); if ( addrLength == 0 || addrLength > 255 ) return false; if ( address.startsWith( "." ) || address.startsWith( "-" ) ) return false; // rightmost domain label starting with digit indicates IP address // since top level domain label can only start with an alpha // see RFC 2396 Section 3.2.2 int index = address.lastIndexOf( '.' ); if ( address.endsWith( "." ) ) index = address.substring( 0, index ).lastIndexOf( '.' ); if ( index + 1 < addrLength && isDigit( address.charAt( index + 1 ) ) ) { int numDots = 0; // make sure that 1) we see only digits and dot separators, 2) that // any dot separator is preceded and followed by a digit and // 3) that we find 3 dots for ( int i = 0 ; i < addrLength ; i++) { testChar = address.charAt( i ); if ( testChar == '.' ) { if ( ! isDigit( address.charAt( i - 1 ) ) || ( i + 1 < addrLength && ! isDigit( address.charAt( i + 1 ) ) ) ) return false; numDots++; } else if ( ! isDigit( testChar ) ) return false; } if ( numDots != 3 ) return false; } else { // domain labels can contain alphanumerics and '-" // but must start and end with an alphanumeric for ( int i = 0 ; i < addrLength ; i++ ) { testChar = address.charAt( i ); if ( testChar == '.' ) { if ( ! isAlphanum( address.charAt( i - 1 ) ) ) return false; if ( i + 1 < addrLength && ! isAlphanum( address.charAt( i + 1 ) ) ) return false; } else if ( ! isAlphanum( testChar ) && testChar != '-' ) return false; } } return true; } /** * Determine whether a char is a digit. * * @param ch the character to check * @return true if the char is betweeen '0' and '9', false otherwise */ private static boolean isDigit( char ch ) { return ch >= '0' && ch <= '9'; } /** * Determine whether a character is a hexadecimal character. * * @param ch the character to check * @return true if the char is betweeen '0' and '9', 'a' and 'f' * or 'A' and 'F', false otherwise */ private static boolean isHex( char ch ) { return ( isDigit( ch ) || ( ch >= 'a' && ch <= 'f' ) || ( ch >= 'A' && ch <= 'F' ) ); } /** * Determine whether a char is an alphabetic character: a-z or A-Z * * @param ch the character to check * @return true if the char is alphabetic, false otherwise */ private static boolean isAlpha( char ch ) { return ( ( ch >= 'a' && ch <= 'z' ) || ( ch >= 'A' && ch <= 'Z' ) ); } /** * Determine whether a char is an alphanumeric: 0-9, a-z or A-Z * * @param ch the character to check * @return true if the char is alphanumeric, false otherwise */ private static boolean isAlphanum( char ch ) { return ( isAlpha( ch ) || isDigit( ch ) ); } /** * Determine whether a character is a reserved character: * ';', '/', '?', ':', '@', '&', '=', '+', '$' or ',' * * @param ch the character to check * @return true if the string contains any reserved characters */ private static boolean isReservedCharacter( char ch ) { return RESERVED_CHARACTERS.indexOf( ch ) != -1; } /** * Determine whether a char is an unreserved character. * * @param ch the character to check * @return true if the char is unreserved, false otherwise */ private static boolean isUnreservedCharacter( char ch ) { return ( isAlphanum( ch ) || MARK_CHARACTERS.indexOf( ch ) != -1 ); } /** * Determine whether a given string contains only URI characters (also * called "uric" in RFC 2396). uric consist of all reserved * characters, unreserved characters and escaped characters. * * @param uric URI string * @return true if the string is comprised of uric, false otherwise */ private static boolean isURIString( String uric ) { if ( uric == null ) return false; int end = uric.length(); char testChar = '\0'; for ( int i = 0 ; i < end ; i++ ) { testChar = uric.charAt( i ); if ( testChar == '%' ) { if ( i + 2 >= end || ! isHex( uric.charAt( i + 1 ) ) || ! isHex( uric.charAt( i + 2 ) ) ) return false; else { i += 2; continue; } } if ( isReservedCharacter( testChar ) || isUnreservedCharacter( testChar ) ) continue; else return false; } return true; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -