W3C home > Mailing lists > Public > uri@w3.org > May 2005

Re: Converting filenames to file: URIs

From: Julian Reschke <julian.reschke@gmx.de>
Date: Thu, 12 May 2005 15:30:04 +0200
Message-ID: <42835A5C.9030404@gmx.de>
To: Graham Klyne <gk@ninebynine.org>
CC: uri@w3.org

Graham Klyne wrote:
> 
> I've recently been tinkering with a Java program that uses URIs, finding 
> that it doesn't work on Windows because of its filename->URI conversion 
> logic.  This has prompted me to map out a generic function to map a 
> filename to a file: URI.
> 
> Examples:
> Unix:
>   Filename /foo/bar maps to file:///foo/bar
>   i.e. prepend "file://"
> Windows:
>   Filename C:\foo\bar maps to file:///C:foo/bar
>   i.e. prepend "file:///" and map \ to /.
> 
> This has prompted me to try and create a generic, operating-system 
> independent function to turn (almost all) filename strings into 
> corresponding file: URI string.  My first attempt is this, which 
> attempts to cover Windows and Unix:
> 
> [[
>     public static String uriFromFilename(
>         String filename)
>         {
>         StringBuffer mapfilename = new StringBuffer( filename ) ;
>         for ( int i = 0 ; i < mapfilename.length() ; i++ )
>             {
>             if ( mapfilename.charAt(i) == '\\' )
>                 mapfilename.setCharAt(i, '/') ;
>             }
>         if (filename.charAt(0) == '/')
>             {
>             return "file://"+mapfilename.toString() ;
>             }
>         else
>             {
>             return "file:///"+mapfilename.toString() ;
>             }
>         }
> ]]
> 
> What's missing here?  How does this stand up on a Mac platform?

It misses non-ASCII characters, and ASCII characters that need to be 
escaped in URIs.

Best regards, Julian
Received on Thursday, 12 May 2005 13:30:16 UTC

This archive was generated by hypermail 2.3.1 : Tuesday, 6 January 2015 21:25:09 UTC