Java Code Examples for org.apache.commons.vfs2.provider.UriParser#extractScheme()

The following examples show how to use org.apache.commons.vfs2.provider.UriParser#extractScheme() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: S3NFileNameParser.java    From hop with Apache License 2.0 6 votes vote down vote up
public FileName parseUri( VfsComponentContext context, FileName base, String uri ) throws FileSystemException {
  StringBuilder name = new StringBuilder();

  String scheme = UriParser.extractScheme( uri, name );
  UriParser.canonicalizePath( name, 0, name.length(), this );

  // Normalize separators in the path
  UriParser.fixSeparators( name );

  // Normalise the path
  FileType fileType = UriParser.normalisePath( name );

  // Extract bucket name
  final String bucketName = UriParser.extractFirstElement( name );

  return new S3NFileName( scheme, bucketName, name.toString(), fileType );
}
 
Example 2
Source File: S3FileNameParser.java    From hop with Apache License 2.0 6 votes vote down vote up
public FileName parseUri( VfsComponentContext context, FileName base, String uri ) throws FileSystemException {
  StringBuilder name = new StringBuilder();

  String scheme = UriParser.extractScheme( uri, name );
  UriParser.canonicalizePath( name, 0, name.length(), this );

  // Normalize separators in the path
  UriParser.fixSeparators( name );

  // Normalise the path
  FileType fileType = UriParser.normalisePath( name );

  String fullPath = name.toString();
  // Extract bucket name
  final String bucketName = UriParser.extractFirstElement( name );

  return new S3FileName( scheme, bucketName, fullPath, fileType );
}
 
Example 3
Source File: S3AFileNameParser.java    From hop with Apache License 2.0 6 votes vote down vote up
public FileName parseUri( VfsComponentContext context, FileName base, String uri ) throws FileSystemException {
  StringBuilder name = new StringBuilder();

  String scheme = UriParser.extractScheme( uri, name );
  UriParser.canonicalizePath( name, 0, name.length(), this );

  // Normalize separators in the path
  UriParser.fixSeparators( name );

  // Normalise the path
  FileType fileType = UriParser.normalisePath( name );

  // Extract bucket name
  final String bucketName = UriParser.extractFirstElement( name );

  return new S3AFileName( scheme, bucketName, name.toString(), fileType );
}
 
Example 4
Source File: VFSFileSystem.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
@Override
public String getBasePath(final String path)
{
    if (UriParser.extractScheme(path) == null)
    {
        return super.getBasePath(path);
    }
    try
    {
        final FileSystemManager fsManager = VFS.getManager();
        final FileName name = fsManager.resolveURI(path);
        return name.getParent().getURI();
    }
    catch (final FileSystemException fse)
    {
        fse.printStackTrace();
        return null;
    }
}
 
Example 5
Source File: VFSFileSystem.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
@Override
public String getFileName(final String path)
{
    if (UriParser.extractScheme(path) == null)
    {
        return super.getFileName(path);
    }
    try
    {
        final FileSystemManager fsManager = VFS.getManager();
        final FileName name = fsManager.resolveURI(path);
        return name.getBaseName();
    }
    catch (final FileSystemException fse)
    {
        fse.printStackTrace();
        return null;
    }
}
 
Example 6
Source File: LocalFileNameParser.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
@Override
public FileName parseUri(final VfsComponentContext context, final FileName base, final String uri)
        throws FileSystemException {
    final StringBuilder name = new StringBuilder();

    // Extract the scheme
    String scheme = UriParser.extractScheme(getSchemes(context, base, uri), uri, name);
    if (scheme == null && base != null) {
        scheme = base.getScheme();
    }
    if (scheme == null) {
        scheme = "file";
    }

    // Remove encoding, and adjust the separators
    UriParser.canonicalizePath(name, 0, name.length(), this);

    UriParser.fixSeparators(name);

    // Extract the root prefix
    final String rootFile = extractRootPrefix(uri, name);

    // Normalise the path
    final FileType fileType = UriParser.normalisePath(name);

    final String path = name.toString();

    return createFileName(scheme, rootFile, path, fileType);
}
 
Example 7
Source File: TemporaryFileProvider.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Locates a file object, by absolute URI.
 *
 * @param baseFile The base FileObject.
 * @param uri The URI of the file to be located.
 * @param properties FileSystemOptions to use to locate or create the file.
 * @return The FileObject.
 * @throws FileSystemException if an error occurs.
 */
@Override
public synchronized FileObject findFile(final FileObject baseFile, final String uri,
        final FileSystemOptions properties) throws FileSystemException {
    // Parse the name
    final StringBuilder buffer = new StringBuilder(uri);
    final String scheme = UriParser.extractScheme(getContext().getFileSystemManager().getSchemes(), uri, buffer);
    UriParser.fixSeparators(buffer);
    UriParser.normalisePath(buffer);
    final String path = buffer.toString();

    // Create the temp file system if it does not exist
    // FileSystem filesystem = findFileSystem( this, (Properties) null);
    FileSystem filesystem = findFileSystem(this, properties);
    if (filesystem == null) {
        if (rootFile == null) {
            rootFile = getContext().getTemporaryFileStore().allocateFile("tempfs");
        }
        final FileName rootName = getContext().parseURI(scheme + ":" + FileName.ROOT_PATH);
        // final FileName rootName =
        // new LocalFileName(scheme, scheme + ":", FileName.ROOT_PATH);
        filesystem = new LocalFileSystem(rootName, rootFile.getAbsolutePath(), properties);
        addFileSystem(this, filesystem);
    }

    // Find the file
    return filesystem.resolveFile(path);
}
 
Example 8
Source File: DefaultFileSystemManager.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Resolve the uri to a file name.
 *
 * @param uri The URI to resolve.
 * @return The FileName of the file.
 * @throws FileSystemException if an error occurs.
 */
@Override
public FileName resolveURI(final String uri) throws FileSystemException {
    UriParser.checkUriEncoding(uri);

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

    // Extract the scheme
    final String scheme = UriParser.extractScheme(getSchemes(), uri);
    if (scheme != null) {
        // An absolute URI - locate the provider
        final FileProvider provider = providers.get(scheme);
        if (provider != null) {
            return provider.parseUri(null, uri);
        }

        // Otherwise, assume a local file
    }

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

    if (scheme != null) {
        // An unknown scheme - hand it to the default provider
        FileSystemException.requireNonNull(defaultProvider, "vfs.impl/unknown-scheme.error", scheme, uri);
        return defaultProvider.parseUri(null, uri);
    }

    // Assume a relative name - use the supplied base file
    FileSystemException.requireNonNull(baseFile, "vfs.impl/find-rel-file.error", uri);

    return resolveName(baseFile.getName(), uri, NameScope.FILE_SYSTEM);
}
 
Example 9
Source File: VFSFileSystem.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
@Override
public URL getURL(final String basePath, final String file) throws MalformedURLException
{
    if ((basePath != null && UriParser.extractScheme(basePath) == null)
        || (basePath == null && UriParser.extractScheme(file) == null))
    {
        return super.getURL(basePath, file);
    }
    try
    {
        final FileSystemManager fsManager = VFS.getManager();

        FileName path;
        if (basePath != null && UriParser.extractScheme(file) == null)
        {
            final FileName base = fsManager.resolveURI(basePath);
            path = fsManager.resolveName(base, file);
        }
        else
        {
            path = fsManager.resolveURI(file);
        }

        final URLStreamHandler handler = new VFSURLStreamHandler(path);
        return new URL(null, path.getURI(), handler);
    }
    catch (final FileSystemException fse)
    {
        throw new ConfigurationRuntimeException("Could not parse basePath: " + basePath
            + " and fileName: " + file, fse);
    }
}
 
Example 10
Source File: VfsFileChooserControls.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private void updateLocation() {
  String pathText = wPath.getText();
  String scheme = pathText.isEmpty() ? HDFS_SCHEME : UriParser.extractScheme( pathText );
  if ( scheme != null ) {
    List<VFSScheme> availableVFSSchemes = getAvailableVFSSchemes();
    for ( int i = 0; i < availableVFSSchemes.size(); i++ ) {
      VFSScheme s = availableVFSSchemes.get( i );
      if ( scheme.equals( s.scheme ) ) {
        wLocation.select( i );
        selectedVFSScheme = s;
      }
    }
  }
}
 
Example 11
Source File: DefaultFileSystemManager.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
/**
 * Resolves a URI, relative to a base file with specified FileSystem configuration.
 *
 * @param baseFile The base file.
 * @param uri The file name. May be a fully qualified or relative path or a url.
 * @param fileSystemOptions Options to pass to the file system.
 * @return A FileObject representing the target file.
 * @throws FileSystemException if an error occurs accessing the file.
 */
public FileObject resolveFile(final FileObject baseFile, final String uri,
        final FileSystemOptions fileSystemOptions) throws FileSystemException {
    final FileObject realBaseFile;
    if (baseFile != null && VFS.isUriStyle() && baseFile.getName().isFile()) {
        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(getSchemes(), uri);
    if (scheme != null) {
        // An absolute URI - locate the provider
        final FileProvider provider = 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
        FileSystemException.requireNonNull(defaultProvider, "vfs.impl/unknown-scheme.error", scheme, uri);
        return defaultProvider.findFile(realBaseFile, uri, fileSystemOptions);
    }

    // Assume a relative name - use the supplied base file
    FileSystemException.requireNonNull(realBaseFile, "vfs.impl/find-rel-file.error", uri);

    return realBaseFile.resolveFile(uri);
}
 
Example 12
Source File: DefaultFileSystemManager.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
/**
 * Resolves a name, relative to the root.
 *
 * @param base the base file name
 * @param name the name
 * @param scope the {@link NameScope}
 * @return The FileName of the file.
 * @throws FileSystemException if an error occurs.
 */
@Override
public FileName resolveName(final FileName base, final String name, final NameScope scope)
        throws FileSystemException {
    FileSystemException.requireNonNull(base, "Invalid base FileName.");
    FileSystemException.requireNonNull(name, "Invalid name FileName.");
    final FileName realBase;
    if (VFS.isUriStyle() && base.isFile()) {
        realBase = base.getParent();
    } else {
        realBase = base;
    }

    final StringBuilder buffer = new StringBuilder(name);

    // Adjust separators
    UriParser.fixSeparators(buffer);
    String scheme = UriParser.extractScheme(getSchemes(), buffer.toString());

    // Determine whether to prepend the base path
    if (name.length() == 0 || (scheme == null && buffer.charAt(0) != FileName.SEPARATOR_CHAR)) {
        // Supplied path is not absolute
        if (!VFS.isUriStyle()) {
            // when using URIs the parent already do have the trailing "/"
            buffer.insert(0, FileName.SEPARATOR_CHAR);
        }
        buffer.insert(0, realBase.getPath());
    }

    // Normalise the path
    final FileType fileType = UriParser.normalisePath(buffer);

    // Check the name is ok
    final String resolvedPath = buffer.toString();
    if (!AbstractFileName.checkName(realBase.getPath(), resolvedPath, scope)) {
        throw new FileSystemException("vfs.provider/invalid-descendent-name.error", name);
    }

    String fullPath;
    if (scheme != null) {
        fullPath = resolvedPath;
    } else {
        scheme = realBase.getScheme();
        fullPath = realBase.getRootURI() + resolvedPath;
    }
    final FileProvider provider = providers.get(scheme);
    if (provider != null) {
        // TODO: extend the file name parser to be able to parse
        // only a pathname and take the missing informations from
        // the base. Then we can get rid of the string operation.
        // // String fullPath = base.getRootURI() +
        // resolvedPath.substring(1);

        return provider.parseUri(realBase, fullPath);
    }

    // An unknown scheme - hand it to the default provider - if possible
    if (scheme != null && defaultProvider != null) {
        return defaultProvider.parseUri(realBase, fullPath);
    }

    // TODO: avoid fallback to this point
    // this happens if we have a virtual filesystem (no provider for scheme)
    return ((AbstractFileName) realBase).createName(resolvedPath, fileType);
}
 
Example 13
Source File: ConnectionFileNameParser.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public static AbstractFileName parseUri( String uri, FileNameParser fileNameParser ) throws FileSystemException {
  StringBuilder name = new StringBuilder();

  String scheme = UriParser.extractScheme( uri, name );
  UriParser.canonicalizePath( name, 0, name.length(), fileNameParser );

  UriParser.fixSeparators( name );

  FileType fileType = UriParser.normalisePath( name );

  // Extract the named connection name
  final String connection = UriParser.extractFirstElement( name );

  String path = name.toString();

  return new ConnectionFileName( scheme, connection, path, fileType );
}