James -

I've just been reviewing the RFC 1738 (URL spec, mostly in context of some formal 
comments that I putting together about the 2.2 spec). From that RFC:

   A file URL takes the form:

       file://<host>/<path>

   where <host> is the fully qualified domain name of the system on
   which the <path> is accessible, and <path> is a hierarchical
   directory path of the form <directory>/<directory>/.../<name>.

   As a special case, <host> can be the string "localhost" or the empty
   string; this is interpreted as `the machine from which the URL is
   being interpreted'.

   The file URL scheme is unusual in that it does not specify an
   Internet protocol or access method for such files; as such, its
   utility in network protocols between hosts is limited.

And from the BNF at the end of the RFC:

   fileurl        = "file://" [ host | "localhost" ] "/" fpath

So, according to the spec, both of

   file://localhost/PATH
   file:///PATH

a equal. The BNF further implies that URL are absolute: There is no way to specify a 
resource otherwise.

Now, RFC 1630 (URI spec) does allow for relative URIs:

Partial (relative) form

   Within a object whose URI is well defined, the URI of another object
   may be given in abbreviated form, where parts of the two URIs are the
   same. This allows objects within a group to refer to each other
   without requiring the space for a complete reference, and it
   incidentally allows the group of objects to be moved without changing
   any references.  It must be emphasized that when a reference is
   passed in anything other than a well controlled context, the full
   form must always be used.

   The rules for the use of a partial name relative to the URI of the
   context are:

      If the scheme parts are different, the whole absolute URI must
      be given.  Otherwise, the scheme is omitted, and:

      If the partial URI starts with a non-zero number of consecutive
      slashes, then everything from the context URI up to (but not
      including) the first occurrence of exactly the same number of
      consecutive slashes which has no greater number of consecutive
      slashes anywhere to the right of it is taken to be the same and
      so prepended to the partial URL to form the full URL. Otherwise:

      The last part of the path of the context URI (anything following
      the rightmost slash) is removed, and the given partial URI
      appended in its place, and then:

      Within the result, all occurrences of "xxx/../" or "/." are
      recursively removed, where xxx, ".." and "." are complete path
      elements.

So, if you've got a "URI in context", you can use relative URIs. Some examples (from 
the spec):

   In the context of URI

                        magic://a/b/c//d/e/f

   the partial URIs would expand as follows:

     g                  magic://a/b/c//d/e/g
     /g                 magic://a/g
     //g                magic://g
     ../g               magic://a/b/c//d/g
     g:h                g:h

   and in the context of the URI

                        magic://a/b/c//d/e/

   the results would be exactly the same.

This is nice, but it really only gives you a way to translate relative URIs into 
absolute URLs, based on the URI context.

So basically, what I'm saying is that there is currently no way to specify a "relative 
file url", mostly because for the URL to be relative, it has to have some sort of 
context. Basically, I think it comes down to is using ".." in an absolute URL and 
generating that URL:

    String url;
    String file = "../foo.jar";
    String base = getCodeBase();

    if ( base.endsWith( "/" ) ) {
        url = base + file;
    } else {
        url = base + "/" + file;    // this might always be valid;
    }

This is why I always use getResource(). Perhaps there should be a File() constructor 
that takes a URL, or developers should consistantly use getResource() and 
URL.getFile().

- Paul Philion

James Todd wrote:
>
> [i'm going to cross-post my reply to the jsp and servlet
> interest lists ... as i've been burned on this very item
> in the past and am curious as to folks opinions].
>
> hi joe -
>
> !great information!
>
> for a good part of the day i've been trying to resolve
> a "portable file url specified docBase" issue. for what
> it is worth and this may be a bit dated but i've used
> the following format since jdk 1.0.x days with measurable
> success:
>
>         file://localhost/PATH
>
>         where PATH can look like "foo/bar" for unix
>                 and "C|/foo/bar" (or something like this)
>                 on win
>
> i've run into modo various jdk version/platform issues
> when using file urls which are specified with anything
> less then the above. that said, i'm really not sure how
> to specify relative file urls with the above scheme in
> a portable manner as the "third" slash designates the
> the root file system ... or so i believe.
>
>         eg: file:../foo.jar
>
> i'd like to hear thoughts on this item as it is really
> a bit of a pain in the a$$ when it comes to specifying
> a reliable (ie jdk 1.1.x and jdk 1.2.x on win/solaris/*
> for relative and absolute paths) file based docBase.
>
> any ideas? i am optimistic (and stubborn) so i believe
> there is a solution here.

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to