commit-classpath
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: uri patch


From: Mark Wielaard
Subject: Re: uri patch
Date: Thu, 11 Mar 2004 18:37:52 +0100

Hi,

On Thu, 2004-03-11 at 16:41, Dalibor Topic wrote:
> ChangeLog:
> 
>       2003-09-26 Ito Kazumitsu <address@hidden>
>          * java/net/URI.java:
>          (toURL): Implemented.
> 
>       2003-05-07 Ito Kazumitsu <address@hidden>
> 
>          * 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.
> 
>       2002-12-02 Dalibor Topic <address@hidden>
> 
>       * 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.
> 
> the patch probably doesn't conform to classpath's style, etc. feel free 
> to adapt before checking it in.

Thanks. I reformatted a bit and committed as attached.
This will help some people greatly thanks. Even though it needs some
more work. The class can use some more documentation and I was not
completely sure about all the (unimplemented!) private quoteXXX helper
methods. Are you sure you need so much different versions? And/Or can
the be implemented using URLEncoder?

Cheers,

Mark
Index: ChangeLog
===================================================================
RCS file: /cvsroot/classpath/classpath/ChangeLog,v
retrieving revision 1.1920
diff -u -r1.1920 ChangeLog
--- ChangeLog   11 Mar 2004 16:13:21 -0000      1.1920
+++ ChangeLog   11 Mar 2004 17:32:48 -0000
@@ -1,3 +1,45 @@
+2004-03-11  Ito Kazumitsu  <address@hidden>
+
+       * java/net/URI.java (toURL): Implemented.
+
+2003-03-11  Ito Kazumitsu  <address@hidden>
+
+       * 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.
+
+2002-03-11  Dalibor Topic  <address@hidden>
+
+       * 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.
+
 2004-03-11  Dalibor Topic  <address@hidden>
 
        Reported by: Adam Heath <address@hidden>
Index: java/net/URI.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/net/URI.java,v
retrieving revision 1.3
diff -u -r1.3 URI.java
--- java/net/URI.java   2 May 2003 14:19:37 -0000       1.3
+++ java/net/URI.java   11 Mar 2004 17:32:48 -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 <address@hidden>
+ * @author Ito Kazumitsu (address@hidden)
+ * @author Dalibor Topic (address@hidden)
+ * @author Michael Koch (address@hidden)
  * @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,11 +114,117 @@
   {
   }
 
+  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 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,13 +369,57 @@
    *
    * @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 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());
   }
 
   /**

Attachment: signature.asc
Description: This is a digitally signed message part


reply via email to

[Prev in Thread] Current Thread [Next in Thread]