六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 61|回复: 0

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

[复制链接]

升级  36%

4

主题

4

主题

4

主题

童生

Rank: 1

积分
18
 楼主| 发表于 2013-1-27 06:02:12 | 显示全部楼层 |阅读模式
一般来说,我们使用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);
    }
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表