[jdom-interest] public static URL fileToURL (File file)

Jason Hunter jhunter at xquery.com
Tue Aug 31 15:06:06 PDT 2004


Hi Jared,

This fix went in on 2004/02/05 (revision 1.82) according to the CVS 
logs.  Beta 10 was revision 1.84, so the fix is in Beta 10.  Did you not 
see it there?  Did the fix not work for you?

-jh-

Jared Davis wrote:
> Hi,
> 
> We just hit a problem with special characters not being escaped correctly in
> SAXBuilder fileToURL. If the filename contains a # the build(file) method
> fails.  A search of the archives found a patch from Ben Preece on Dec 19
> 2003 that looks like it would fix the problem.   I'll copy his email below.
> Will this patch make it into the next release?  
> 
> Regards,
> 
> Jared Davis
> 
> -------------------------
> Ben Preece (ben.preece at qlogic.com) writes on 2003-12-19 (display as raw
> message)  
> Content-Type: text/plain;
> 	charset="iso-8859-1"
> From: "Ben Preece" <ben.preece at qlogic.com>
> Subject: [jdom-interest] SAXBuilder.fileToURL doesn't handle special
> characters
> Date: Fri, 19 Dec 2003 11:44:08 -0600
> 
> We've been using JDOM for our application, and like it pretty well.  Thanks
> for the hard work people have put into it.  It shows.
> 
> We noticed that the method fileToURL() in SAXBuilder doesn't try to handle
> special characters.  This causes the build(File) method to throw exceptions
> if the file name contains things like sharps ('#') or percents ('%').  The
> modified version below seems to handle it okay.  Please feel free to use it
> if you want.
> 
> Again, thanks for all the work you've done.
> 
> -Ben Preece
> 
> 
> 
>     public static URL fileToURL (File file)
>         throws MalformedURLException 
>     {
>         StringBuffer buffer = new StringBuffer();
>         String path = file.getAbsolutePath();
>         
>         // convert non-URL style file separators
>         if (File.separatorChar != '/') {
>             path = path.replace(File.separatorChar, '/');
>         }
>         
>         // make sure it starts at root
>         if (!path.startsWith("/")) {
>             buffer.append('/');
>         }
>         
>         // copy, converting URL special characters as we go
>         for (int i = 0; i < path.length(); i++) {
>             char c = path.charAt(i);
>             if (c == ' ')
>                 buffer.append ("%20");
>             else if (c == '#') 
>                 buffer.append ("%23");
>             else if (c == '%') 
>                 buffer.append ("%25");
>             else if (c == '&') 
>                 buffer.append ("%26");
>             else if (c == ';') 
>                 buffer.append ("%3B");
>             else if (c == '<') 
>                 buffer.append ("%3C");
>             else if (c == '=') 
>                 buffer.append ("%3D");
>             else if (c == '>') 
>                 buffer.append ("%3E");
>             else if (c == '?') 
>                 buffer.append ("%3F");
>             else if (c == '~') 
>                 buffer.append ("%7E");
>             else 
>                 buffer.append (c);
>         }
>         
>         // make sure directories end with slash
>         if (!path.endsWith("/") && file.isDirectory()) {
>             buffer.append('/');
>         }
>         
>         // return URL
>         return new URL("file", "", buffer.toString());
>     }
> 
> 
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com
> 


More information about the jdom-interest mailing list