[ 
https://issues.apache.org/jira/browse/VFS-443?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13497328#comment-13497328
 ] 

Gary Gregory commented on VFS-443:
----------------------------------

@Joerge's 
https://issues.apache.org/jira/browse/VFS-443?focusedCommentId=13496963&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-13496963

Well, this is quite a mess. 

Is {{LocalFileName.getURI()}} the only place at fault here?

{code:java}
    /**
     * Returns the absolute URI of the file.
     * @return The absolute URI of the file.
     */
    @Override
    public String getURI()
    {
        String uri = super.getURI();

        if (uri != null && uri.length() > 0)
        {
            try
            {
                // VFS-325: Handle URI special characters in filename
                // Decode the base uri and re-encode with URI special characters
                uri = UriParser.decode(uri);

                uri = UriParser.encode(uri, RESERVED_URI_CHARS);
            }
            catch (FileSystemException e)
            {
                // Default to base uri value
            }
        }

        return uri;
    }
{code}

Then we have:

{code:java}
    // URI Characters that are possible in local filenames, but must be escaped
    // for proper URI handling.
    //
    // How reserved URI chars were selected:
    //
    //  URIs can contain :, /, ?, #, @
    //      See http://download.oracle.com/javase/6/docs/api/java/net/URI.html
    //          http://tools.ietf.org/html/rfc3986#section-2.2
    //
    //  Since : and / occur before the path, only chars after path are escaped 
(i.e., # and ?)
    //  ? is a reserved filesystem character for Windows and Unix, so can't be 
part of a filename.
    //  Therefore only # is a reserved char in a URI as part of the path that 
can be in the filename.
    private static final char RESERVED_URI_CHARS[] = {'#'};
{code}

Adding {{' '}} (a space char) to the array causes the two tests above to pass. 

But this is a hack, and seems to go against the method as coded.

Any thoughts on how to best fix this?

                
> Need an easy way to convert from a FileObject to a File
> -------------------------------------------------------
>
>                 Key: VFS-443
>                 URL: https://issues.apache.org/jira/browse/VFS-443
>             Project: Commons VFS
>          Issue Type: Bug
>    Affects Versions: 2.0
>            Reporter: Nicholas Allen
>            Assignee: Gary Gregory
>             Fix For: 2.1
>
>
> I've seen the reasons why Apache does not want to provide an easy way to 
> convert from a FileObject to a java.io.File and those reasons make sense - 
> however, I think that some things are being overlooked and there are still 
> valid reasons for needing to convert from a FileObject to a File.
> Firstly, I would like to always use Apache VFS for everything I do - even if 
> I know it's only on the local file system. The reasons for this are:
> 1. it makes the code more flexible (it might start of being local file system 
> and then as specs change it could become a requirement to work over http or 
> inside zip files for example). 
> 2. The API is nicer to use than the java.io.File and it's easier to write 
> cross platform code using it (file separator is always "/" etc).
> So if I work with Apache VFS for local file system use I would like to be 
> able to get back to a java.io.File in case I need to interface with same 
> other library. I would like a method that converted to a File or null if not 
> possible. This would allow me to take an alternate action (eg copy file to 
> local temp file if it's not already a local file). There's no need to copy 
> the file if it is already local.
> The simplest fix for this is to just make the getLocalFile() method in 
> LocalFile public. Once the user knows it's a LocalFile object it makes sense 
> to call this method to obtain the java.io.File. So I could write a method 
> like this:
> {code:title=FileUtilities.java}
> /**
>     * If the supplied {@link FileObject} represents a local file then this 
> returns that, otherwise
>     * returns null.
>     */
>    public File getLocalFile(final FileObject fileObject)
>    {
>       if (fileObject instanceof LocalFile)
>       {
>          final LocalFile localFile = (LocalFile)fileObject;
>          return localFile.getLocalFile();
>       }
>       return null;
>    }
> {code}

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Reply via email to