This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

FYI: Patch: java.net.URI


Hi list,


I just commited the attached patch to merge java.net.URI from classpath.
Its not complete yet and has many FIXMEs but it works already good 
enough for most things. (e.g. I use it in my javax.print package).


Michael


2004-03-12  Ito Kazumitsu  <ito.kazumitsu@hitachi-cable.co.jp>

	* java/net/URI.java (toURL): Implemented.

2004-03-12  Ito Kazumitsu  <ito.kazumitsu@hitachi-cable.co.jp>

	* java/net/URI.java
	(URI_REGEXP) updated to contain scheme specific part.
	(SCHEME_SPEC_PART_GROUP) new constant.
	(AUTHORITY_GROUP, PATH_GROUP, QUERY_GROUP, FRAGMENT_GROUP)
	updated to make room for SCHEME_SPEC_PART_GROUP.
	(parseURI) parse scheme specific part.
	(resolve, isAbsolute, isOpaque, getRawSchemeSpecificPart,
	getSchemeSpecificPart, getAuthority, getUserInfo, getPath,
	getQuery, getFragment) implemented.

2004-03-12  Dalibor Topic  <robilad@kaffe.org>

	* libraries/javalib/java/net/URI.java
	partially implemented using java.util.regex.
	(URI_REGEXP) new constant. Used to parse URIs.
	(SCHEME_GROUP) new constant representing index of scheme group
	in parsed URI.
	(AUTHORITY_GROUP) new constant representing index of authority
	group in parsed URI.
	(PATH_GROUP) new constant representing index of path group in
	parsed URI.
	(QUERY_GROUP) new constant representing index of query group in
	parsed URI.
	(FRAGMENT_GROUP) new constant representing index of fragment
	group in parsed URI.
	(getURIGroup) new static utility method.
	(parseURI) implemented.
	(quote) stub for new static utility method.
	(quoteAuthority) stub for new static utility method.
	(quoteHost) stub for new static utility method.
	(quotePath) stub for new static utility method.
	(quoteUserInfo) stub for new static utility method.
	(URI) implemented.
	(create) don't throw URISyntaxException. Implemented.
	(toString) implemented.
Index: java/net/URI.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/URI.java,v
retrieving revision 1.2
diff -u -b -B -r1.2 URI.java
--- java/net/URI.java	5 May 2003 05:10:47 -0000	1.2
+++ java/net/URI.java	12 Mar 2004 08:04:36 -0000
@@ -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.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 @@
 {
   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,9 +114,115 @@
   {
   }
 
+  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;
   }
   
   /**
@@ -88,6 +236,7 @@
   public URI (String str)
     throws URISyntaxException
   {
+    parseURI(str);
   }
  
   /**
@@ -107,6 +256,16 @@
 	     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 @@
    * @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 @@
 	     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 URI (String scheme, String host, String path, String fragment)
     throws URISyntaxException
   {
+    this(scheme, null, host, -1, path, null, fragment);
   }
 
   /**
@@ -153,6 +318,9 @@
   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 static URI create (String str)
   {
-    return null;
+    try
+      {
+	return new URI(str);
+      }
+    catch(URISyntaxException e)
+      {
+	throw (IllegalArgumentException)
+	  new IllegalArgumentException().initCause(e);
+      }
   }
 
   /**
@@ -193,14 +369,58 @@
    *
    * @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)
   { 
+    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;
   }
+  }
 
   /**
    * Resolves the given URI string against this URI
@@ -216,7 +436,7 @@
   public URI resolve (String str)
     throws IllegalArgumentException
   {
-    return null;
+    return resolve(create(str));
   }
 
   /**
@@ -243,7 +463,10 @@
   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 boolean isAbsolute ()
   {
-    return false;
+    return (scheme != null);
   }
 
   /**
@@ -267,7 +490,7 @@
    */
   public boolean isOpaque ()
   {
-    return false;
+    return ((scheme != null) && !(schemeSpecificPart.startsWith("/")));
   }
 
   /**
@@ -276,7 +499,7 @@
    */
   public String getRawSchemeSpecificPart ()
   {
-    return null;
+    return schemeSpecificPart;
   }
 
   /**
@@ -284,7 +507,8 @@
    */
   public String getSchemeSpecificPart ()
   {
-    return null;
+    // FIXME: unimplemented.
+    return schemeSpecificPart;
   }
 
   /**
@@ -300,7 +524,8 @@
    */
   public String getAuthority ()
   {
-    return null;
+    // FIXME: unimplemented.
+    return authority;
   }
 
   /**
@@ -316,7 +541,8 @@
    */
   public String getUserInfo ()
   {
-    return null;
+    // FIXME: unimplemented.
+    return userInfo;
   }
 
   /**
@@ -348,7 +574,8 @@
    */
   public String getPath ()
   {
-    return null;
+    // FIXME: unimplemented.
+    return path;
   }
 
   /**
@@ -364,7 +591,8 @@
    */
   public String getQuery ()
   {
-    return null;
+    // FIXME: unimplemented.
+    return query;
   }
 
   /**
@@ -380,7 +608,8 @@
    */
   public String getFragment ()
   {
-    return null;
+    // FIXME: unimplemented.
+    return fragment;
   }
 
   /**
@@ -419,7 +648,11 @@
    */
   public String toString ()
   {
-    return "";
+    return (getScheme() == null ? "" : getScheme() + ":")
+      + (getRawAuthority() == null ? "" : "//" + getRawAuthority())
+      + (getRawPath() == null ? "" : getRawPath())
+      + (getRawQuery() == null ? "" : "?" + getRawQuery())
+      + (getRawFragment() == null ? "" : "#" + getRawFragment());
   }
 
   /**

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]