diff options
Diffstat (limited to 'libjava/java/net/URI.java')
-rw-r--r-- | libjava/java/net/URI.java | 273 |
1 files changed, 253 insertions, 20 deletions
diff --git a/libjava/java/net/URI.java b/libjava/java/net/URI.java index 34d212f..a2f77a4 100644 --- a/libjava/java/net/URI.java +++ b/libjava/java/net/URI.java @@ -1,5 +1,5 @@ /* URI.java - An URI class - Copyright (C) 2002 Free Software Foundation, Inc. + Copyright (C) 2002, 2004 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -41,9 +41,13 @@ import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** - * @author Michael Koch <konqueror@gmx.de> + * @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 @@ -51,7 +55,45 @@ public final class URI { static final long serialVersionUID = -6052424284110960213L; - String string; + /** + * Regular expression for parsing URIs. + * + * Taken from RFC 2396, Appendix B. + * This expression doesn't parse IPv6 addresses. + */ + private static final String URI_REGEXP = + "^(([^:/?#]+):)?((//([^/?#]*))?([^?#]*)(\\?([^#]*))?)?(#(.*))?"; + + /** + * Index of scheme component in parsed URI. + */ + private static final int SCHEME_GROUP = 2; + + /** + * Index of scheme-specific-part in parsed URI. + */ + private static final int SCHEME_SPEC_PART_GROUP = 3; + + /** + * Index of authority component in parsed URI. + */ + private static final int AUTHORITY_GROUP = 5; + + /** + * Index of path component in parsed URI. + */ + private static final int PATH_GROUP = 6; + + /** + * Index of query component in parsed URI. + */ + private static final int QUERY_GROUP = 8; + + /** + * Index of fragment component in parsed URI. + */ + private static final int FRAGMENT_GROUP = 10; + private String scheme; private String schemeSpecificPart; private String authority; @@ -72,11 +114,117 @@ public final class URI { } + private static String getURIGroup (Matcher match, int group) { + String matched = match.group(group); + return matched.length() == 0 ? null : matched; + } + + /** + * Sets fields of this URI by parsing the given string. + * + * @param str The string to parse + * + * @exception URISyntaxException If the given string violates RFC 2396 + */ 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); + schemeSpecificPart = getURIGroup(matcher, SCHEME_SPEC_PART_GROUP); + authority = getURIGroup(matcher, AUTHORITY_GROUP); + path = getURIGroup(matcher, PATH_GROUP); + query = getURIGroup(matcher, QUERY_GROUP); + fragment = getURIGroup(matcher, FRAGMENT_GROUP); + } + else + throw new URISyntaxException(str, + "doesn't match URI regular expression"); + } + + /** + * Quote characters illegal in URIs in given string. + * + * Replace illegal characters by encoding their UTF-8 + * representation as "%" + hex code for each resulting + * UTF-8 character. + * + * @param str The string to quote + * + * @return The quoted string. + */ + private static String quote (String str) { + // FIXME: unimplemented. + return str; + } + + /** + * Quote characters illegal in URI authorities in given string. + * + * Replace illegal characters by encoding their UTF-8 + * representation as "%" + hex code for each resulting + * UTF-8 character. + * + * @param str The string to quote + * + * @return The quoted string. + */ + private static String quoteAuthority (String str) { + // FIXME: unimplemented. + return str; } - + + /** + * Quote characters illegal in URI hosts in given string. + * + * Replace illegal characters by encoding their UTF-8 + * representation as "%" + hex code for each resulting + * UTF-8 character. + * + * @param str The string to quote + * + * @return The quoted string. + */ + private static String quoteHost (String str) { + // FIXME: unimplemented. + return str; + } + + /** + * Quote characters illegal in URI paths in given string. + * + * Replace illegal characters by encoding their UTF-8 + * representation as "%" + hex code for each resulting + * UTF-8 character. + * + * @param str The string to quote + * + * @return The quoted string. + */ + private static String quotePath (String str) { + // FIXME: unimplemented. + return str; + } + + /** + * Quote characters illegal in URI user infos in given string. + * + * Replace illegal characters by encoding their UTF-8 + * representation as "%" + hex code for each resulting + * UTF-8 character. + * + * @param str The string to quote + * + * @return The quoted string. + */ + private static String quoteUserInfo (String str) { + // FIXME: unimplemented. + return str; + } + /** * Creates an URI from the given string * @@ -88,6 +236,7 @@ public final class URI public URI (String str) throws URISyntaxException { + parseURI(str); } /** @@ -107,6 +256,16 @@ public final class URI 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))); + + parseServerAuthority(); } /** @@ -116,7 +275,7 @@ public final class URI * @param authority The authority * @param path The apth * @param query The query - * @param fragment The fragmen + * @param fragment The fragment * * @exception URISyntaxException If the given string violates RFC 2396 */ @@ -124,6 +283,11 @@ public final class URI String fragment) throws URISyntaxException { + this((scheme == null ? "" : scheme + ":") + + (authority == null ? "" : "//" + quoteAuthority(authority)) + + (path == null ? "" : quotePath(path)) + + (query == null ? "" : "?" + quote(query)) + + (fragment == null ? "" : "#" + quote(fragment))); } /** @@ -139,6 +303,7 @@ public final class URI public URI (String scheme, String host, String path, String fragment) throws URISyntaxException { + this(scheme, null, host, -1, path, null, fragment); } /** @@ -153,6 +318,9 @@ public final class URI public URI (String scheme, String ssp, String fragment) throws URISyntaxException { + this((scheme == null ? "" : scheme + ":") + + (ssp == null ? "" : quote(ssp)) + + (fragment == null ? "" : "#" + quote(fragment))); } /** @@ -165,7 +333,15 @@ public final class URI */ public static URI create (String str) { - return null; + try + { + return new URI(str); + } + catch(URISyntaxException e) + { + throw (IllegalArgumentException) + new IllegalArgumentException().initCause(e); + } } /** @@ -193,13 +369,57 @@ public final class URI * * @param uri The URI to resolve against this URI * - * @return The resulting URI + * @return The resulting URI, or null when it couldn't be resolved + * for some reason. * * @exception NullPointerException If uri is null */ public URI resolve (URI uri) { - return null; + if (uri.isAbsolute()) + return uri; + if (uri.isOpaque()) + return uri; + + String scheme = uri.getScheme(); + String schemeSpecificPart = uri.getSchemeSpecificPart(); + String authority = uri.getAuthority(); + String path = uri.getPath(); + String query = uri.getQuery(); + String fragment = uri.getFragment(); + + try + { + if (fragment != null && + path != null && path.equals("") && + scheme == null && authority == null && query == null) + return new URI(this.scheme, this.schemeSpecificPart, fragment); + + if (authority == null) + { + authority = this.authority; + if (path == null) + path = ""; + if (!(path.startsWith("/"))) + { + StringBuffer basepath = new StringBuffer(this.path); + int i = this.path.lastIndexOf('/'); + + 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. + } + } + return new URI(this.scheme, authority, path, query, fragment); + } + catch (URISyntaxException e) + { + return null; + } } /** @@ -216,7 +436,7 @@ public final class URI public URI resolve (String str) throws IllegalArgumentException { - return null; + return resolve(create(str)); } /** @@ -243,7 +463,10 @@ public final class URI public URL toURL () throws IllegalArgumentException, MalformedURLException { - return null; + if (isAbsolute()) + return new URL(this.toString()); + + throw new IllegalArgumentException("not absolute"); } /** @@ -259,7 +482,7 @@ public final class URI */ public boolean isAbsolute () { - return false; + return (scheme != null); } /** @@ -267,7 +490,7 @@ public final class URI */ public boolean isOpaque () { - return false; + return ((scheme != null) && !(schemeSpecificPart.startsWith("/"))); } /** @@ -276,7 +499,7 @@ public final class URI */ public String getRawSchemeSpecificPart () { - return null; + return schemeSpecificPart; } /** @@ -284,7 +507,8 @@ public final class URI */ public String getSchemeSpecificPart () { - return null; + // FIXME: unimplemented. + return schemeSpecificPart; } /** @@ -300,7 +524,8 @@ public final class URI */ public String getAuthority () { - return null; + // FIXME: unimplemented. + return authority; } /** @@ -316,7 +541,8 @@ public final class URI */ public String getUserInfo () { - return null; + // FIXME: unimplemented. + return userInfo; } /** @@ -348,7 +574,8 @@ public final class URI */ public String getPath () { - return null; + // FIXME: unimplemented. + return path; } /** @@ -364,7 +591,8 @@ public final class URI */ public String getQuery () { - return null; + // FIXME: unimplemented. + return query; } /** @@ -380,7 +608,8 @@ public final class URI */ public String getFragment () { - return null; + // FIXME: unimplemented. + return fragment; } /** @@ -419,7 +648,11 @@ public final class URI */ public String toString () { - return ""; + return (getScheme() == null ? "" : getScheme() + ":") + + (getRawAuthority() == null ? "" : "//" + getRawAuthority()) + + (getRawPath() == null ? "" : getRawPath()) + + (getRawQuery() == null ? "" : "?" + getRawQuery()) + + (getRawFragment() == null ? "" : "#" + getRawFragment()); } /** |