Apache VFS(7): 文件管理器解析文件的方法

来源:互联网 发布:乐视网络电视盒 编辑:程序博客网 时间:2024/05/20 03:05

本系列文章导航

Apache VFS(1): 基本介绍

Apache VFS(2): 文件的监听和监控

Apache VFS(3): 文件过滤器和选择器

Apache VFS(4): 事件

Apache VFS(5): 使用它!

Apache VFS(6): 几个重要的概念性接口

Apache VFS(7): 文件管理器解析文件的方法

一般来说,我们使用Apache VFS时,直接从VFS对象获得的文件管理器是StandardFileSystemManager,StandardFileSystemManager从DefaultFileSystemManager继承而来。而解析文件在DefaultFileSystemManager中完成。
绝大部分时候,你会提供一个URI来定位你的文件系统,例如:ftp://yourftp/rootdir或者http://yourweb/rootdir或者file://c:/rootdir
,然后你将这个字符串作为参数传给StandardFileSystemManger, 这时候DefaultFileSystemManger的resolveFile方法负责处理URI的解析,并且最终返回一个FileObject文件对象。我们看一下这个方法:
这个方法有5种重载方式,分别接受不同的参数,但核心方法只有一个:

/**
     * Resolves a URI, realtive to a base file with specified FileSystem
     * configuration
     */
    public FileObject resolveFile(final FileObject baseFile, final String uri,
            final FileSystemOptions fileSystemOptions)
            throws FileSystemException
    {
        final FileObject realBaseFile;
        if (baseFile != null && VFS.isUriStyle()
                && baseFile.getName().getType() == FileType.FILE)
        {
            realBaseFile = baseFile.getParent();
        }
        else
        {
            realBaseFile = baseFile;
        }
        // TODO: use resolveName and use this name to resolve the fileObject

        UriParser.checkUriEncoding(uri);

        if (uri == null)
        {
            throw new IllegalArgumentException();
        }

        // Extract the scheme
        final String scheme = UriParser.extractScheme(uri);
        if (scheme != null)
        {
            // An absolute URI - locate the provider
            final FileProvider provider = (FileProvider) providers.get(scheme);
            if (provider != null)
            {
                return provider.findFile(realBaseFile, uri, fileSystemOptions);
            }
            // Otherwise, assume a local file
        }

        // Handle absolute file names
        if (localFileProvider != null
                && localFileProvider.isAbsoluteLocalName(uri))
        {
            return localFileProvider.findLocalFile(uri);
        }

        if (scheme != null)
        {
            // An unknown scheme - hand it to the default provider
            if (defaultProvider == null)
            {
                throw new FileSystemException("vfs.impl/unknown-scheme.error",
                        new Object[]
                        { scheme, uri });
            }
            return defaultProvider.findFile(realBaseFile, uri,
                    fileSystemOptions);
        }

        // Assume a relative name - use the supplied base file
        if (realBaseFile == null)
        {
            throw new FileSystemException("vfs.impl/find-rel-file.error", uri);
        }

        return realBaseFile.resolveFile(uri);
    }

 

 

文章来源:http://alartin.javaeye.com/blog/92436

原创粉丝点击