diff options
Diffstat (limited to 'libjava/java/net/URI.java')
-rw-r--r-- | libjava/java/net/URI.java | 317 |
1 files changed, 161 insertions, 156 deletions
diff --git a/libjava/java/net/URI.java b/libjava/java/net/URI.java index e1d833a..b456f71 100644 --- a/libjava/java/net/URI.java +++ b/libjava/java/net/URI.java @@ -7,7 +7,7 @@ GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. - + GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU @@ -44,14 +44,14 @@ import java.io.Serializable; import java.util.regex.Matcher; import java.util.regex.Pattern; + /** * @author Ito Kazumitsu (ito.kazumitsu@hitachi-cable.co.jp) * @author Dalibor Topic (robilad@kaffe.org) * @author Michael Koch (konqueror@gmx.de) * @since 1.4 */ -public final class URI - implements Comparable, Serializable +public final class URI implements Comparable, Serializable { static final long serialVersionUID = -6052424284110960213L; @@ -70,11 +70,14 @@ public final class URI private static final String RFC2396_DIGIT = "0123456789"; private static final String RFC2396_LOWALPHA = "abcdefghijklmnopqrstuvwxyz"; private static final String RFC2396_UPALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - private static final String RFC2396_ALPHA = RFC2396_LOWALPHA + RFC2396_UPALPHA; + private static final String RFC2396_ALPHA = + RFC2396_LOWALPHA + RFC2396_UPALPHA; private static final String RFC2396_ALPHANUM = RFC2396_DIGIT + RFC2396_ALPHA; private static final String RFC2396_MARK = "-_.!~*'()"; - private static final String RFC2396_UNRESERVED = RFC2396_ALPHANUM + RFC2396_MARK; - private static final String RFC2396_REG_NAME = RFC2396_UNRESERVED + "$,;:@&=+"; + private static final String RFC2396_UNRESERVED = + RFC2396_ALPHANUM + RFC2396_MARK; + private static final String RFC2396_REG_NAME = + RFC2396_UNRESERVED + "$,;:@&=+"; private static final String RFC2396_PCHAR = RFC2396_UNRESERVED + ":@&=+$,"; private static final String RFC2396_SEGMENT = RFC2396_PCHAR + ";"; private static final String RFC2396_PATH_SEGMENTS = RFC2396_SEGMENT + "/"; @@ -108,7 +111,6 @@ public final class URI * Index of fragment component in parsed URI. */ private static final int FRAGMENT_GROUP = 10; - private String scheme; private String rawSchemeSpecificPart; private String schemeSpecificPart; @@ -126,17 +128,17 @@ public final class URI private String rawFragment; private String fragment; - private void readObject (ObjectInputStream is) + private void readObject(ObjectInputStream is) throws ClassNotFoundException, IOException { } - private void writeObject (ObjectOutputStream is) - throws IOException + private void writeObject(ObjectOutputStream is) throws IOException { } - private static String getURIGroup (Matcher match, int group) { + private static String getURIGroup(Matcher match, int group) + { String matched = match.group(group); return matched.length() == 0 ? null : matched; } @@ -148,23 +150,22 @@ public final class URI * * @exception URISyntaxException If the given string violates RFC 2396 */ - private void parseURI (String str) - throws URISyntaxException + private void parseURI(String str) throws URISyntaxException { Pattern pattern = Pattern.compile(URI_REGEXP); Matcher matcher = pattern.matcher(str); if (matcher.matches()) { scheme = getURIGroup(matcher, SCHEME_GROUP); - rawSchemeSpecificPart = getURIGroup(matcher, SCHEME_SPEC_PART_GROUP); + rawSchemeSpecificPart = getURIGroup(matcher, SCHEME_SPEC_PART_GROUP); rawAuthority = getURIGroup(matcher, AUTHORITY_GROUP); rawPath = getURIGroup(matcher, PATH_GROUP); rawQuery = getURIGroup(matcher, QUERY_GROUP); rawFragment = getURIGroup(matcher, FRAGMENT_GROUP); } else - throw new URISyntaxException(str, - "doesn't match URI regular expression"); + throw new URISyntaxException(str, "doesn't match URI regular expression"); + // We must eagerly unquote the parts, because this is the only time // we may throw an exception. schemeSpecificPart = unquote(rawSchemeSpecificPart); @@ -184,35 +185,39 @@ public final class URI * @exception URISyntaxException If the given string contains invalid * escape sequences. */ - private static String unquote (String str) - throws URISyntaxException + private static String unquote(String str) throws URISyntaxException { if (str == null) return null; byte[] buf = new byte[str.length()]; int pos = 0; - for (int i = 0; i < str.length(); i++) { - char c = str.charAt(i); - if (c > 127) + for (int i = 0; i < str.length(); i++) + { + char c = str.charAt(i); + if (c > 127) throw new URISyntaxException(str, "Invalid character"); - if (c == '%') { - if (i + 2 >= str.length()) - throw new URISyntaxException(str, "Invalid quoted character"); - String hex = "0123456789ABCDEF"; - int hi = hex.indexOf(str.charAt(++i)); - int lo = hex.indexOf(str.charAt(++i)); - if (lo < 0 || hi < 0) - throw new URISyntaxException(str, "Invalid quoted character"); - buf[pos++] = (byte)(hi * 16 + lo); - } else { - buf[pos++] = (byte)c; + if (c == '%') + { + if (i + 2 >= str.length()) + throw new URISyntaxException(str, "Invalid quoted character"); + String hex = "0123456789ABCDEF"; + int hi = hex.indexOf(str.charAt(++i)); + int lo = hex.indexOf(str.charAt(++i)); + if (lo < 0 || hi < 0) + throw new URISyntaxException(str, "Invalid quoted character"); + buf[pos++] = (byte) (hi * 16 + lo); + } + else + buf[pos++] = (byte) c; + } + try + { + return new String(buf, 0, pos, "utf-8"); + } + catch (java.io.UnsupportedEncodingException x2) + { + throw (Error) new InternalError().initCause(x2); } - } - try { - return new String(buf, 0, pos, "utf-8"); - } catch (java.io.UnsupportedEncodingException x2) { - throw (Error)new InternalError().initCause(x2); - } } /** @@ -223,10 +228,11 @@ public final class URI * UTF-8 character. * * @param str The string to quote - * + * * @return The quoted string. */ - private static String quote (String str) { + private static String quote(String str) + { // FIXME: unimplemented. return str; } @@ -239,10 +245,11 @@ public final class URI * UTF-8 character. * * @param str The string to quote - * + * * @return The quoted string. */ - private static String quoteAuthority (String str) { + private static String quoteAuthority(String str) + { // Technically, we should be using RFC2396_AUTHORITY, but // it contains no additional characters. return quote(str, RFC2396_REG_NAME); @@ -257,37 +264,39 @@ public final class URI * * @param str The string to quote * @param legalCharacters The set of legal characters - * + * * @return The quoted string. */ - private static String quote (String str, String legalCharacters) + private static String quote(String str, String legalCharacters) { StringBuffer sb = new StringBuffer(str.length()); - for (int i = 0; i < str.length(); i++) { - char c = str.charAt(i); - if (legalCharacters.indexOf(c) == -1) { - String hex = "0123456789ABCDEF"; - if (c <= 127) { - sb.append('%') - .append(hex.charAt(c / 16)) - .append(hex.charAt(c % 16)); - } else { - try { - // this is far from optimal, but it works - byte[] utf8 = str.substring(i, i + 1).getBytes("utf-8"); - for (int j = 0; j < utf8.length; j++) { - sb.append('%') - .append(hex.charAt((utf8[j] & 0xff) / 16)) - .append(hex.charAt((utf8[j] & 0xff) % 16)); - } - } catch (java.io.UnsupportedEncodingException x) { - throw (Error)new InternalError().initCause(x); + for (int i = 0; i < str.length(); i++) + { + char c = str.charAt(i); + if (legalCharacters.indexOf(c) == -1) + { + String hex = "0123456789ABCDEF"; + if (c <= 127) + sb.append('%').append(hex.charAt(c / 16)).append(hex.charAt(c % 16)); + else + { + try + { + // this is far from optimal, but it works + byte[] utf8 = str.substring(i, i + 1).getBytes("utf-8"); + for (int j = 0; j < utf8.length; j++) + sb.append('%').append(hex.charAt((utf8[j] & 0xff) / 16)) + .append(hex.charAt((utf8[j] & 0xff) % 16)); + } + catch (java.io.UnsupportedEncodingException x) + { + throw (Error) new InternalError().initCause(x); + } + } } - } - } else { - sb.append(c); + else + sb.append(c); } - } return sb.toString(); } @@ -299,10 +308,11 @@ public final class URI * UTF-8 character. * * @param str The string to quote - * + * * @return The quoted string. */ - private static String quoteHost (String str) { + private static String quoteHost(String str) + { // FIXME: unimplemented. return str; } @@ -315,10 +325,11 @@ public final class URI * UTF-8 character. * * @param str The string to quote - * + * * @return The quoted string. */ - private static String quotePath (String str) { + private static String quotePath(String str) + { // Technically, we should be using RFC2396_PATH, but // it contains no additional characters. return quote(str, RFC2396_PATH_SEGMENTS); @@ -332,10 +343,11 @@ public final class URI * UTF-8 character. * * @param str The string to quote - * + * * @return The quoted string. */ - private static String quoteUserInfo (String str) { + private static String quoteUserInfo(String str) + { // FIXME: unimplemented. return str; } @@ -348,12 +360,11 @@ public final class URI * @exception URISyntaxException If the given string violates RFC 2396 * @exception NullPointerException If str is null */ - public URI (String str) - throws URISyntaxException + public URI(String str) throws URISyntaxException { parseURI(str); } - + /** * Create an URI from the given components * @@ -367,19 +378,19 @@ public final class URI * * @exception URISyntaxException If the given string violates RFC 2396 */ - public URI (String scheme, String userInfo, String host, int port, - String path, String query, String fragment) + public URI(String scheme, String userInfo, String host, int port, + String path, String query, String fragment) throws URISyntaxException { - this((scheme == null ? "" : scheme + ":" ) - + (userInfo == null && host == null && port == -1 ? "" : "//") - + (userInfo == null ? "" : quoteUserInfo(userInfo) + "@") - + (host == null ? "" : quoteHost(host)) - + (port == -1 ? "" : ":" + String.valueOf(port)) - + (path == null ? "" : quotePath(path)) - + (query == null ? "" : "?" + quote(query)) - + (fragment == null ? "" : "#" + quote(fragment))); - + this((scheme == null ? "" : scheme + ":") + + (userInfo == null && host == null && port == -1 ? "" : "//") + + (userInfo == null ? "" : quoteUserInfo(userInfo) + "@") + + (host == null ? "" : quoteHost(host)) + + (port == -1 ? "" : ":" + String.valueOf(port)) + + (path == null ? "" : quotePath(path)) + + (query == null ? "" : "?" + quote(query)) + + (fragment == null ? "" : "#" + quote(fragment))); + parseServerAuthority(); } @@ -394,15 +405,14 @@ public final class URI * * @exception URISyntaxException If the given string violates RFC 2396 */ - public URI (String scheme, String authority, String path, String query, - String fragment) - throws URISyntaxException + public URI(String scheme, String authority, String path, String query, + String fragment) throws URISyntaxException { this((scheme == null ? "" : scheme + ":") - + (authority == null ? "" : "//" + quoteAuthority(authority)) - + (path == null ? "" : quotePath(path)) - + (query == null ? "" : "?" + quote(query)) - + (fragment == null ? "" : "#" + quote(fragment))); + + (authority == null ? "" : "//" + quoteAuthority(authority)) + + (path == null ? "" : quotePath(path)) + + (query == null ? "" : "?" + quote(query)) + + (fragment == null ? "" : "#" + quote(fragment))); } /** @@ -415,7 +425,7 @@ public final class URI * * @exception URISyntaxException If the given string violates RFC 2396 */ - public URI (String scheme, String host, String path, String fragment) + public URI(String scheme, String host, String path, String fragment) throws URISyntaxException { this(scheme, null, host, -1, path, null, fragment); @@ -430,12 +440,12 @@ public final class URI * * @exception URISyntaxException If the given string violates RFC 2396 */ - public URI (String scheme, String ssp, String fragment) + public URI(String scheme, String ssp, String fragment) throws URISyntaxException { this((scheme == null ? "" : scheme + ":") - + (ssp == null ? "" : quote(ssp)) - + (fragment == null ? "" : "#" + quote(fragment))); + + (ssp == null ? "" : quote(ssp)) + + (fragment == null ? "" : "#" + quote(fragment))); } /** @@ -446,16 +456,16 @@ public final class URI * @exception IllegalArgumentException If the given string violates RFC 2396 * @exception NullPointerException If str is null */ - public static URI create (String str) + public static URI create(String str) { try { return new URI(str); } - catch(URISyntaxException e) + catch (URISyntaxException e) { - throw (IllegalArgumentException) - new IllegalArgumentException().initCause(e); + throw (IllegalArgumentException) new IllegalArgumentException() + .initCause(e); } } @@ -465,8 +475,7 @@ public final class URI * * @exception URISyntaxException If the given string violates RFC 2396 */ - public URI parseServerAuthority () - throws URISyntaxException + public URI parseServerAuthority() throws URISyntaxException { return null; } @@ -474,7 +483,7 @@ public final class URI /** * Returns a normalizes versions of the URI */ - public URI normalize () + public URI normalize() { return null; } @@ -489,8 +498,8 @@ public final class URI * * @exception NullPointerException If uri is null */ - public URI resolve (URI uri) - { + public URI resolve(URI uri) + { if (uri.isAbsolute()) return uri; if (uri.isOpaque()) @@ -505,35 +514,34 @@ public final class URI try { - if (fragment != null && - path != null && path.equals("") && - scheme == null && authority == null && query == null) + if (fragment != null && path != null && path.equals("") + && scheme == null && authority == null && query == null) return new URI(this.scheme, this.schemeSpecificPart, fragment); - if (authority == null) + if (authority == null) { - authority = this.authority; - if (path == null) + authority = this.authority; + if (path == null) path = ""; - if (!(path.startsWith("/"))) + if (! (path.startsWith("/"))) { - StringBuffer basepath = new StringBuffer(this.path); - int i = this.path.lastIndexOf('/'); + StringBuffer basepath = new StringBuffer(this.path); + int i = this.path.lastIndexOf('/'); - if (i >= 0) - basepath.delete(i+1, basepath.length()); + if (i >= 0) + basepath.delete(i + 1, basepath.length()); - basepath.append(path); - path = basepath.toString(); - // FIXME We must normalize the path here. - // Normalization process omitted. + basepath.append(path); + path = basepath.toString(); + // FIXME We must normalize the path here. + // Normalization process omitted. } } - return new URI(this.scheme, authority, path, query, fragment); + return new URI(this.scheme, authority, path, query, fragment); } catch (URISyntaxException e) { - return null; + return null; } } @@ -548,8 +556,7 @@ public final class URI * violates RFC 2396 * @exception NullPointerException If uri is null */ - public URI resolve (String str) - throws IllegalArgumentException + public URI resolve(String str) throws IllegalArgumentException { return resolve(create(str)); } @@ -563,7 +570,7 @@ public final class URI * * @exception NullPointerException If uri is null */ - public URI relativize (URI uri) + public URI relativize(URI uri) { return null; } @@ -575,8 +582,7 @@ public final class URI * not be found, or if some other error occurred while constructing the URL * @exception IllegalArgumentException If the URI is not absolute */ - public URL toURL () - throws IllegalArgumentException, MalformedURLException + public URL toURL() throws IllegalArgumentException, MalformedURLException { if (isAbsolute()) return new URL(this.toString()); @@ -587,7 +593,7 @@ public final class URI /** * Returns the scheme of the URI */ - public String getScheme () + public String getScheme() { return scheme; } @@ -595,7 +601,7 @@ public final class URI /** * Tells whether this URI is absolute or not */ - public boolean isAbsolute () + public boolean isAbsolute() { return (scheme != null); } @@ -603,16 +609,16 @@ public final class URI /** * Tell whether this URI is opaque or not */ - public boolean isOpaque () + public boolean isOpaque() { - return ((scheme != null) && !(schemeSpecificPart.startsWith("/"))); + 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 () + public String getRawSchemeSpecificPart() { return rawSchemeSpecificPart; } @@ -620,7 +626,7 @@ public final class URI /** * Returns the decoded scheme specific part of this URI. */ - public String getSchemeSpecificPart () + public String getSchemeSpecificPart() { return schemeSpecificPart; } @@ -628,7 +634,7 @@ public final class URI /** * Returns the rae authority part of this URI */ - public String getRawAuthority () + public String getRawAuthority() { return rawAuthority; } @@ -636,7 +642,7 @@ public final class URI /** * Returns the decoded authority part of this URI */ - public String getAuthority () + public String getAuthority() { return authority; } @@ -644,7 +650,7 @@ public final class URI /** * Returns the raw user info part of this URI */ - public String getRawUserInfo () + public String getRawUserInfo() { return rawUserInfo; } @@ -652,7 +658,7 @@ public final class URI /** * Returns the decoded user info part of this URI */ - public String getUserInfo () + public String getUserInfo() { return userInfo; } @@ -660,7 +666,7 @@ public final class URI /** * Returns the hostname of the URI */ - public String getHost () + public String getHost() { return host; } @@ -668,7 +674,7 @@ public final class URI /** * Returns the port number of the URI */ - public int getPort () + public int getPort() { return port; } @@ -676,7 +682,7 @@ public final class URI /** * Returns the raw path part of this URI */ - public String getRawPath () + public String getRawPath() { return rawPath; } @@ -684,7 +690,7 @@ public final class URI /** * Returns the path of the URI */ - public String getPath () + public String getPath() { return path; } @@ -692,7 +698,7 @@ public final class URI /** * Returns the raw query part of this URI */ - public String getRawQuery () + public String getRawQuery() { return rawQuery; } @@ -700,7 +706,7 @@ public final class URI /** * Returns the query of the URI */ - public String getQuery () + public String getQuery() { return query; } @@ -708,7 +714,7 @@ public final class URI /** * Return the raw fragment part of this URI */ - public String getRawFragment () + public String getRawFragment() { return rawFragment; } @@ -716,7 +722,7 @@ public final class URI /** * Returns the fragment of the URI */ - public String getFragment () + public String getFragment() { return fragment; } @@ -734,7 +740,7 @@ public final class URI /** * Computes the hascode of the URI */ - public int hashCode () + public int hashCode() { return 0; } @@ -746,8 +752,7 @@ public final class URI * * @exception ClassCastException If given object ist not an URI */ - public int compareTo (Object obj) - throws ClassCastException + public int compareTo(Object obj) throws ClassCastException { return 0; } @@ -755,19 +760,19 @@ public final class URI /** * Returns the URI as string */ - public String toString () + public String toString() { return (getScheme() == null ? "" : getScheme() + ":") - + (getRawAuthority() == null ? "" : "//" + getRawAuthority()) - + (getRawPath() == null ? "" : getRawPath()) - + (getRawQuery() == null ? "" : "?" + getRawQuery()) - + (getRawFragment() == null ? "" : "#" + getRawFragment()); + + (getRawAuthority() == null ? "" : "//" + getRawAuthority()) + + (getRawPath() == null ? "" : getRawPath()) + + (getRawQuery() == null ? "" : "?" + getRawQuery()) + + (getRawFragment() == null ? "" : "#" + getRawFragment()); } /** * Returns the URI as US-ASCII string */ - public String toASCIIString () + public String toASCIIString() { return ""; } |