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

The following examples show how to use org.apache.commons.vfs2.provider.UriParser#normalisePath() . 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: SmbFileNameParser.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 filename)
        throws FileSystemException {
    final StringBuilder name = new StringBuilder();

    // Extract the scheme and authority parts
    final Authority auth = extractToPath(context, filename, name);

    // extract domain
    String username = auth.getUserName();
    final String domain = extractDomain(username);
    if (domain != null) {
        username = username.substring(domain.length() + 1);
    }

    // Decode and adjust separators
    UriParser.canonicalizePath(name, 0, name.length(), this);
    UriParser.fixSeparators(name);

    // Extract the share
    final String share = UriParser.extractFirstElement(name);
    if (share == null || share.length() == 0) {
        throw new FileSystemException("vfs.provider.smb/missing-share-name.error", filename);
    }

    // Normalise the path. Do this after extracting the share name,
    // to deal with things like smb://hostname/share/..
    final FileType fileType = UriParser.normalisePath(name);
    final String path = name.toString();

    return new SmbFileName(auth.getScheme(), auth.getHostName(), auth.getPort(), username, auth.getPassword(),
            domain, share, path, fileType);
}
 
Example 5
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 6
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 7
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 8
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 );
}