📄 uri.java
字号:
if ( '\\' != testChar ) throw new MalformedURIException( "Path contains invalid character: " + testChar ); } index++; } _path = uriSpec.substring( start, index ); // query - starts with ? and up to fragment or end if ( testChar == '?' ) { index++; start = index; while ( index < end ) { testChar = uriSpec.charAt( index ); if ( testChar == '#' ) break; if ( testChar == '%' ) { if ( index + 2 >= end || ! isHex( uriSpec.charAt( index + 1 ) ) || ! isHex( uriSpec.charAt( index + 2 ) ) ) throw new MalformedURIException( "Query string contains invalid escape sequence!" ); } else if ( ! isReservedCharacter( testChar ) && ! isUnreservedCharacter( testChar ) ) throw new MalformedURIException( "Query string contains invalid character:" + testChar ); index++; } _queryString = uriSpec.substring( start, index ); } // fragment - starts with # if ( testChar == '#' ) { index++; start = index; while ( index < end ) { testChar = uriSpec.charAt( index ); if ( testChar == '%' ) { if ( index + 2 >= end || ! isHex( uriSpec.charAt( index + 1 ) ) || !isHex( uriSpec.charAt( index + 2 ) ) ) throw new MalformedURIException( "Fragment contains invalid escape sequence!" ); } else if ( ! isReservedCharacter( testChar ) && ! isUnreservedCharacter( testChar ) ) throw new MalformedURIException( "Fragment contains invalid character:" + testChar ); index++; } _fragment = uriSpec.substring( start, index ); } } /** * Get the scheme for this URI. * * @return the scheme for this URI */ public String getScheme() { return _scheme; } /** * Get the scheme-specific part for this URI (everything following the * scheme and the first colon). See RFC 2396 Section 5.2 for spec. * * @return the scheme-specific part for this URI */ public String getSchemeSpecificPart() { StringBuffer schemespec = new StringBuffer(); if ( _userinfo != null || _host != null || _port != -1 ) { schemespec.append( "//" ); if ( _userinfo != null) { schemespec.append( _userinfo ); schemespec.append( '@' ); } if ( _host != null ) schemespec.append( _host ); if ( _port != -1 ) { schemespec.append( ':' ); schemespec.append( _port ); } } if ( _path != null ) schemespec.append( _path ); if ( _queryString != null ) { schemespec.append( '?' ); schemespec.append( _queryString ); } if ( _fragment != null ) { schemespec.append( '#' ); schemespec.append( _fragment ); } return schemespec.toString(); } /** * Get the userinfo for this URI. * * @return the userinfo for this URI (null if not specified). */ public String getUserinfo() { return _userinfo; } /** * Get the host for this URI. * * @return the host for this URI (null if not specified). */ public String getHost() { return _host; } /** * Get the port for this URI. * * @return the port for this URI (-1 if not specified). */ public int getPort() { return _port; } /** * Get the path for this URI (optionally with the query string and * fragment). * * @param includeQueryString if true (and query string is not null), * then a "?" followed by the query string will be appended * @param includeFragment if true (and fragment is not null), * then a "#" followed by the fragment will be appended * @return the path for this URI possibly including the query string and fragment */ public String getPath( boolean includeQueryString, boolean includeFragment ) { StringBuffer pathString = new StringBuffer( _path ); if ( includeQueryString && _queryString != null ) { pathString.append( '?' ); pathString.append( _queryString ); } if ( includeFragment && _fragment != null ) { pathString.append( '#' ); pathString.append( _fragment ); } return pathString.toString(); } /** * Get the path for this URI. Note that the value returned is the path * only and does not include the query string or fragment. * * @return the path for this URI. */ public String getPath() { return _path; } /** * Get the query string for this URI. * * @return the query string for this URI. Null is returned if there * was no "?" in the URI spec, empty string if there was a "?" but no * query string following it. */ public String getQueryString() { return _queryString; } /** * Get the fragment for this URI. * * @return the fragment for this URI. Null is returned if there * was no "#" in the URI spec, empty string if there was a * "#" but no fragment following it. */ public String getFragment() { return _fragment; } /** * Set the scheme for this URI. The scheme is converted to lowercase * before it is set. * * @param scheme the scheme for this URI (cannot be null) * @throws MalformedURIException scheme is not a conformant scheme name */ public void setScheme( String scheme ) throws MalformedURIException { if ( scheme == null ) throw new MalformedURIException( "Argument scheme is null" ); if ( ! isConformantSchemeName( scheme ) ) throw new MalformedURIException( "The scheme is not conformant." ); _scheme = scheme.toLowerCase(); } /** * Set the userinfo for this URI. If a non-null value is passed in and * the host value is null, then an exception is thrown. * * @param userinfo the userinfo for this URI * @throws MalformedURIException userinfo contains invalid characters */ public void setUserinfo( String userinfo ) throws MalformedURIException { if ( userinfo == null ) _userinfo = null; else { if ( _host == null) throw new MalformedURIException( "Userinfo cannot be set when host is null!" ); // userinfo can contain alphanumerics, mark characters, escaped // and ';',':','&','=','+','$',',' int index = 0; int end = userinfo.length(); char testChar = '\0'; while ( index < end ) { testChar = userinfo.charAt( index ); if ( testChar == '%' ) { if ( index + 2 >= end || ! isHex( userinfo.charAt( index + 1 ) ) || ! isHex( userinfo.charAt( index + 2 ) ) ) throw new MalformedURIException( "Userinfo contains invalid escape sequence!" ); } else if ( ! isUnreservedCharacter( testChar ) && USERINFO_CHARACTERS.indexOf( testChar ) == -1 ) throw new MalformedURIException( "Userinfo contains invalid character:" + testChar ); index++; } } _userinfo = userinfo; } /** * Set the host for this URI. If null is passed in, the userinfo * field is also set to null and the port is set to -1. * * @param host the host for this URI * @throws MalformedURIException host is not a valid IP address or DNS hostname. */ public void setHost( String host ) throws MalformedURIException { if ( host == null || host.trim().length() == 0 ) { _host = host; _userinfo = null; _port = -1; } else if ( ! isWellFormedAddress( host ) ) throw new MalformedURIException( "Host is not a well formed address!" ); _host = host; } /** * Set the port for this URI. -1 is used to indicate that the port is * not specified, otherwise valid port numbers are between 0 and 65535. * If a valid port number is passed in and the host field is null, * an exception is thrown. * * @param port the port number for this URI * @throws MalformedURIException port is not -1 and not a valid port number */ public void setPort( int port ) throws MalformedURIException { if ( port >= 0 && port <= 65535 ) { if ( _host == null ) throw new MalformedURIException( "Port cannot be set when host is null!" ); } else if ( port != -1 ) throw new MalformedURIException( "Invalid port number!" ); _port = port; } /** * Set the path for this URI. If the supplied path is null, then the * query string and fragment are set to null as well. If the supplied * path includes a query string and/or fragment, these fields will be * parsed and set as well. Note that, for URIs following the "generic * URI" syntax, the path specified should start with a slash. * For URIs that do not follow the generic URI syntax, this method * sets the scheme-specific part. * * @param path the path for this URI (may be null) * @throws MalformedURIException path contains invalid characters */ public void setPath( String path ) throws MalformedURIException { if ( path == null ) { _path = null; _queryString = null; _fragment = null; } else initializePath( path ); } /** * Append to the end of the path of this URI. If the current path does * not end in a slash and the path to be appended does not begin with * a slash, a slash will be appended to the current path before the * new segment is added. Also, if the current path ends in a slash * and the new segment begins with a slash, the extra slash will be * removed before the new segment is appended. * * @param addToPath the new segment to be added to the current path * @exception MalformedURIException addToPath contains syntax errors */ public void appendPath( String addToPath ) throws MalformedURIException { if ( addToPath == null || addToPath.trim().length() == 0 ) return; if ( ! isURIString( addToPath ) ) throw new MalformedURIException( "Path contains invalid character!" ); if ( _path == null || _path.trim().length() == 0 ) { if ( addToPath.startsWith( "/" ) ) _path = addToPath; else _path = "/" + addToPath; } else if ( _path.endsWith( "/" ) ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -