You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by je...@apache.org on 2002/10/09 14:37:31 UTC

cvs commit: jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/util URIUtil.java

jericho     2002/10/09 05:37:31

  Modified:    httpclient/src/java/org/apache/commons/httpclient/util
                        URIUtil.java
  Log:
  - Add the getName and getQuery methods of an URI string
  - Make code robust
  
  Revision  Changes    Path
  1.3       +45 -4     jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/util/URIUtil.java
  
  Index: URIUtil.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/util/URIUtil.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- URIUtil.java	29 Sep 2002 09:02:36 -0000	1.2
  +++ URIUtil.java	9 Oct 2002 12:37:31 -0000	1.3
  @@ -84,12 +84,51 @@
       // ---------------------------------------------------------- URI utilities
   
       /**
  +     * Get the basename of an URI.
  +     *
  +     * @param uri a string regarded an URI
  +     * @return the basename string
  +     */
  +    public static String getName(String uri) {
  +        if (uri == null || uri.length() == 0) return uri;
  +        int at = uri.lastIndexOf("/");
  +        return uri.substring(at + 1);
  +    }
  +
  +
  +    /**
  +     * Get the query of an URI.
  +     *
  +     * @param uri a string regarded an URI
  +     * @return the query string
  +     */
  +    public static String getQuery(String uri) {
  +        if (uri == null || uri.length() == 0) return uri;
  +        // consider of net_path
  +        int at = uri.indexOf("//");
  +        int from = uri.indexOf("/", at >= 0 ?
  +                (uri.lastIndexOf("/", at - 1) >= 0 ? 0 : at + 2) : 0);
  +        // the authority part of URI ignored
  +        int to = uri.length();
  +        // reuse the at and from variables to consider the query
  +        at = uri.indexOf("?", from);
  +        from = (at > from) ? at + 1 : -1; 
  +        // check the fragment
  +        if (uri.lastIndexOf("#") > from)
  +            to = uri.lastIndexOf("#");
  +        // get the path and query.
  +        return (from >= 0) ? uri.substring(from, to) : "/";
  +    }
  +
  +
  +    /**
        * Get the path of an URI.
        *
        * @param uri a string regarded an URI
        * @return the path string
        */
       public static String getPath(String uri) {
  +        if (uri == null || uri.length() == 0) return uri;
           // consider of net_path
           int at = uri.indexOf("//");
           int from = uri.indexOf("/", at >= 0 ?
  @@ -115,6 +154,7 @@
        * @return the path and query string
        */
       public static String getPathQuery(String uri) {
  +        if (uri == null || uri.length() == 0) return uri;
           // consider of net_path
           int at = uri.indexOf("//");
           int from = uri.indexOf("/", at >= 0 ?
  @@ -137,6 +177,7 @@
        * @return the string from the path part
        */
       public static String getFromPath(String uri) {
  +        if (uri == null || uri.length() == 0) return uri;
           // consider of net_path
           int at = uri.indexOf("//");
           int from = uri.indexOf("/", at >= 0 ?
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: cvs commit: jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/util URIUtil.java

Posted by Ch...@dlr.de.
Hi,

To my knowledge, you will get an IndexOutOfBoundsException when the URI ends
with '/'.

If you want the code to be robust, you will need to check if 'at' is less or
equal to the string length.

-- 
:) Christoph Reck

jericho@apache.org wrote:
> jericho     2002/10/09 05:37:31
> 
>   Modified:    httpclient/src/java/org/apache/commons/httpclient/util
>                         URIUtil.java
>   Log:
>   - Add the getName and getQuery methods of an URI string
>   - Make code robust
>   
>   Revision  Changes    Path
>   1.3       +45 -4     jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/util/URIUtil.java
>   
>   Index: URIUtil.java
>   ===================================================================
>   RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/util/URIUtil.java,v
>   retrieving revision 1.2
>   retrieving revision 1.3
>   diff -u -r1.2 -r1.3
>   --- URIUtil.java	29 Sep 2002 09:02:36 -0000	1.2
>   +++ URIUtil.java	9 Oct 2002 12:37:31 -0000	1.3
>   @@ -84,12 +84,51 @@
>        // ---------------------------------------------------------- URI utilities
>    
>        /**
>   +     * Get the basename of an URI.
>   +     *
>   +     * @param uri a string regarded an URI
>   +     * @return the basename string
>   +     */
>   +    public static String getName(String uri) {
>   +        if (uri == null || uri.length() == 0) return uri;
>   +        int at = uri.lastIndexOf("/");
>   +        return uri.substring(at + 1);
>   +    }
[snip]

-- 
:) Christoph Reck


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>