📄 uri.java
字号:
if (scheme == null && uri.getScheme() != null) return uri; if (scheme != null && !(scheme.equals(uri.getScheme()))) return uri; if (rawAuthority == null && uri.getRawAuthority() != null) return uri; if (rawAuthority != null && !(rawAuthority.equals(uri.getRawAuthority()))) return uri; if (!(uri.getRawPath().startsWith(rawPath))) return uri; try { return new URI(null, null, uri.getRawPath().substring(rawPath.length()), uri.getRawQuery(), uri.getRawFragment()); } catch (URISyntaxException e) { throw (Error) new InternalError("Relativized URI variant could not "+ "be constructed").initCause(e); } } /** * Creates an URL from an URI * * @throws MalformedURLException If a protocol handler for the URL could * not be found, or if some other error occurred while constructing the URL * @throws IllegalArgumentException If the URI is not absolute */ public URL toURL() throws IllegalArgumentException, MalformedURLException { if (isAbsolute()) return new URL(this.toString()); throw new IllegalArgumentException("not absolute"); } /** * Returns the scheme of the URI */ public String getScheme() { return scheme; } /** * Tells whether this URI is absolute or not */ public boolean isAbsolute() { return scheme != null; } /** * Tell whether this URI is opaque or not */ public boolean isOpaque() { return ((scheme != null) && ! (schemeSpecificPart.startsWith("/"))); } /** * Returns the raw scheme specific part of this URI. * The scheme-specific part is never undefined, though it may be empty */ public String getRawSchemeSpecificPart() { return rawSchemeSpecificPart; } /** * Returns the decoded scheme specific part of this URI. */ public String getSchemeSpecificPart() { return schemeSpecificPart; } /** * Returns the raw authority part of this URI */ public String getRawAuthority() { return rawAuthority; } /** * Returns the decoded authority part of this URI */ public String getAuthority() { return authority; } /** * Returns the raw user info part of this URI */ public String getRawUserInfo() { return rawUserInfo; } /** * Returns the decoded user info part of this URI */ public String getUserInfo() { return userInfo; } /** * Returns the hostname of the URI */ public String getHost() { return host; } /** * Returns the port number of the URI */ public int getPort() { return port; } /** * Returns the raw path part of this URI */ public String getRawPath() { return rawPath; } /** * Returns the path of the URI */ public String getPath() { return path; } /** * Returns the raw query part of this URI */ public String getRawQuery() { return rawQuery; } /** * Returns the query of the URI */ public String getQuery() { return query; } /** * Return the raw fragment part of this URI */ public String getRawFragment() { return rawFragment; } /** * Returns the fragment of the URI */ public String getFragment() { return fragment; } /** * <p> * Compares the URI with the given object for equality. If the * object is not a <code>URI</code>, then the method returns false. * Otherwise, the following criteria are observed: * </p> * <ul> * <li>The scheme of the URIs must either be null (undefined) in both cases, * or equal, ignorant of case.</li> * <li>The raw fragment of the URIs must either be null (undefined) in both * cases, or equal, ignorant of case.</li> * <li>Both URIs must be of the same type (opaque or hierarchial)</li> * <li><strong>For opaque URIs:</strong></li> * <ul> * <li>The raw scheme-specific parts must be equal.</li> * </ul> * <li>For hierarchical URIs:</li> * <ul> * <li>The raw paths must be equal, ignorant of case.</li> * <li>The raw queries are either both undefined or both equal, ignorant * of case.</li> * <li>The raw authority sections are either both undefined or:</li> * <li><strong>For registry-based authorities:</strong></li> * <ul><li>they are equal.</li></ul> * <li><strong>For server-based authorities:</strong></li> * <ul> * <li>the hosts are equal, ignoring case</li> * <li>the ports are equal</li> * <li>the user information components are equal</li> * </ul> * </ul> * </ul> * * @param obj the obj to compare the URI with. * @return <code>true</code> if the objects are equal, according to * the specification above. */ public boolean equals(Object obj) { if (!(obj instanceof URI)) return false; URI uriObj = (URI) obj; if (scheme == null) { if (uriObj.getScheme() != null) return false; } else if (!(scheme.equalsIgnoreCase(uriObj.getScheme()))) return false; if (rawFragment == null) { if (uriObj.getRawFragment() != null) return false; } else if (!(rawFragment.equalsIgnoreCase(uriObj.getRawFragment()))) return false; boolean opaqueThis = isOpaque(); boolean opaqueObj = uriObj.isOpaque(); if (opaqueThis && opaqueObj) return rawSchemeSpecificPart.equals(uriObj.getRawSchemeSpecificPart()); else if (!opaqueThis && !opaqueObj) { boolean common = rawPath.equalsIgnoreCase(uriObj.getRawPath()) && ((rawQuery == null && uriObj.getRawQuery() == null) || rawQuery.equalsIgnoreCase(uriObj.getRawQuery())); if (rawAuthority == null && uriObj.getRawAuthority() == null) return common; if (host == null) return common && rawAuthority.equalsIgnoreCase(uriObj.getRawAuthority()); return common && host.equalsIgnoreCase(uriObj.getHost()) && port == uriObj.getPort() && (rawUserInfo == null ? uriObj.getRawUserInfo() == null : rawUserInfo.equalsIgnoreCase(uriObj.getRawUserInfo())); } else return false; } /** * Computes the hashcode of the URI */ public int hashCode() { return (getScheme() == null ? 0 : 13 * getScheme().hashCode()) + 17 * getRawSchemeSpecificPart().hashCode() + (getRawFragment() == null ? 0 : 21 + getRawFragment().hashCode()); } /** * Compare the URI with another object that must also be a URI. * Undefined components are taken to be less than any other component. * The following criteria are observed: * </p> * <ul> * <li>Two URIs with different schemes are compared according to their * scheme, regardless of case.</li> * <li>A hierarchical URI is less than an opaque URI with the same * scheme.</li> * <li><strong>For opaque URIs:</strong></li> * <ul> * <li>URIs with differing scheme-specific parts are ordered according * to the ordering of the scheme-specific part.</li> * <li>URIs with the same scheme-specific part are ordered by the * raw fragment.</li> * </ul> * <li>For hierarchical URIs:</li> * <ul> * <li>URIs are ordered according to their raw authority sections, * if they are unequal.</li> * <li><strong>For registry-based authorities:</strong></li> * <ul><li>they are ordered according to the ordering of the authority * component.</li></ul> * <li><strong>For server-based authorities:</strong></li> * <ul> * <li>URIs are ordered according to the raw user information.</li> * <li>URIs with the same user information are ordered by the host, * ignoring case.</li> * <lI>URIs with the same host are ordered by the port.</li> * </ul> * <li>URIs with the same authority section are ordered by the raw path.</li> * <li>URIs with the same path are ordered by their raw query.</li> * <li>URIs with the same query are ordered by their raw fragments.</li> * </ul> * </ul> * * @param obj This object to compare this URI with * @return a negative integer, zero or a positive integer depending * on whether this URI is less than, equal to or greater * than that supplied, respectively. * @throws ClassCastException if the given object is not a URI */ public int compareTo(Object obj) throws ClassCastException { URI uri = (URI) obj; if (scheme == null && uri.getScheme() != null) return -1; if (scheme != null) { int sCompare = scheme.compareToIgnoreCase(uri.getScheme()); if (sCompare != 0) return sCompare; } boolean opaqueThis = isOpaque(); boolean opaqueObj = uri.isOpaque(); if (opaqueThis && !opaqueObj) return 1; if (!opaqueThis && opaqueObj) return -1; if (opaqueThis) { int ssCompare = rawSchemeSpecificPart.compareTo(uri.getRawSchemeSpecificPart()); if (ssCompare == 0) return compareFragments(uri); else return ssCompare; } if (rawAuthority == null && uri.getRawAuthority() != null) return -1; if (rawAuthority != null) { int aCompare = rawAuthority.compareTo(uri.getRawAuthority()); if (aCompare != 0) { if (host == null) return aCompare; if (rawUserInfo == null && uri.getRawUserInfo() != null) return -1; int uCompare = rawUserInfo.compareTo(uri.getRawUserInfo()); if (uCompare != 0) return uCompare; if (host == null && uri.getHost() != null) return -1; int hCompare = host.compareTo(uri.getHost()); if (hCompare != 0) return hCompare; return new Integer(port).compareTo(new Integer(uri.getPort())); } } if (rawPath == null && uri.getRawPath() != null) return -1; if (rawPath != null) { int pCompare = rawPath.compareTo(uri.getRawPath()); if (pCompare != 0) return pCompare; } if (rawQuery == null && uri.getRawQuery() != null) return -1; if (rawQuery != null) { int qCompare = rawQuery.compareTo(uri.getRawQuery()); if (qCompare != 0) return qCompare; } return compareFragments(uri); } /** * Compares the fragment of this URI with that of the supplied URI. * * @param uri the URI to compare with this one. * @return a negative integer, zero or a positive integer depending * on whether this uri's fragment is less than, equal to * or greater than the fragment of the uri supplied, respectively. */ private int compareFragments(URI uri) { if (rawFragment == null && uri.getRawFragment() != null) return -1; else if (rawFragment == null) return 0; else return rawFragment.compareTo(uri.getRawFragment()); } /** * Returns the URI as a String. If the URI was created using a constructor, * then this will be the same as the original input string. * * @return a string representation of the URI. */ public String toString() { return (scheme == null ? "" : scheme + ":") + rawSchemeSpecificPart + (rawFragment == null ? "" : "#" + rawFragment); } /** * Returns the URI as US-ASCII string. This is the same as the result * from <code>toString()</code> for URIs that don't contain any non-US-ASCII * characters. Otherwise, the non-US-ASCII characters are replaced * by their percent-encoded representations. * * @return a string representation of the URI, containing only US-ASCII * characters. */ public String toASCIIString() { String strRep = toString(); boolean inNonAsciiBlock = false; StringBuffer buffer = new StringBuffer(); StringBuffer encBuffer = null; for (int i = 0; i < strRep.length(); i++) { char c = strRep.charAt(i); if (c <= 127) { if (inNonAsciiBlock) { buffer.append(escapeCharacters(encBuffer.toString())); inNonAsciiBlock = false; } buffer.append(c); } else { if (!inNonAsciiBlock) { encBuffer = new StringBuffer(); inNonAsciiBlock = true; } encBuffer.append(c); } } return buffer.toString(); } /** * Converts the non-ASCII characters in the supplied string * to their equivalent percent-encoded representations. * That is, they are replaced by "%" followed by their hexadecimal value. * * @param str a string including non-ASCII characters. * @return the string with the non-ASCII characters converted to their * percent-encoded representations. */ private static String escapeCharacters(String str) { try { StringBuffer sb = new StringBuffer(); // this is far from optimal, but it works byte[] utf8 = str.getBytes("utf-8"); for (int j = 0; j < utf8.length; j++) { sb.append('%'); sb.append(HEX.charAt((utf8[j] & 0xff) / 16)); sb.append(HEX.charAt((utf8[j] & 0xff) % 16)); } return sb.toString(); } catch (java.io.UnsupportedEncodingException x) { throw (Error) new InternalError("Escaping error").initCause(x); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -