Java Code Examples for org.apache.commons.vfs2.FileName#getPath()

The following examples show how to use org.apache.commons.vfs2.FileName#getPath() . 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: HopVfs.java    From hop with Apache License 2.0 6 votes vote down vote up
public static String getFilename( FileObject fileObject ) {
  FileName fileName = fileObject.getName();
  String root = fileName.getRootURI();
  if ( !root.startsWith( "file:" ) ) {
    return fileName.getURI(); // nothing we can do about non-normal files.
  }
  if ( root.startsWith( "file:////" ) ) {
    return fileName.getURI(); // we'll see 4 forward slashes for a windows/smb network share
  }
  if ( root.endsWith( ":/" ) ) { // Windows
    root = root.substring( 8, 10 );
  } else { // *nix & OSX
    root = "";
  }
  String fileString = root + fileName.getPath();
  if ( !"/".equals( Const.FILE_SEPARATOR ) ) {
    fileString = Const.replace( fileString, "/", Const.FILE_SEPARATOR );
  }
  return fileString;
}
 
Example 2
Source File: ResourceFileProvider.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Locates a file object, by absolute URI.
 *
 * @param baseFile The base file.
 * @param uri The URI of the file to locate.
 * @param fileSystemOptions The FileSystem options.
 * @return the FileObject.
 * @throws FileSystemException if an error occurs.
 */
@Override
public FileObject findFile(final FileObject baseFile, final String uri, final FileSystemOptions fileSystemOptions)
        throws FileSystemException {
    final FileName fileName;
    if (baseFile != null) {
        fileName = parseUri(baseFile.getName(), uri);
    }
    else {
        fileName = parseUri(null, uri);
    }
    final String resourceName = fileName.getPath();

    ClassLoader classLoader = ResourceFileSystemConfigBuilder.getInstance().getClassLoader(fileSystemOptions);
    if (classLoader == null) {
        classLoader = getClass().getClassLoader();
    }
    FileSystemException.requireNonNull(classLoader, "vfs.provider.url/badly-formed-uri.error", uri);
    final URL url = classLoader.getResource(resourceName);

    FileSystemException.requireNonNull(url, "vfs.provider.url/badly-formed-uri.error", uri);

    return getContext().getFileSystemManager().resolveFile(url.toExternalForm());
}
 
Example 3
Source File: NamingTests.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Tests child file names.
 */
public void testChildName() throws Exception {
    final FileName baseName = getReadFolder().getName();
    final String basePath = baseName.getPath();
    final FileName name = getManager().resolveName(baseName, "some-child", NameScope.CHILD);

    // Test path is absolute
    assertTrue("is absolute", basePath.startsWith("/"));

    // Test base name
    assertEquals("base name", "some-child", name.getBaseName());

    // Test absolute path
    assertEquals("absolute path", basePath + "/some-child", name.getPath());

    // Test parent path
    assertEquals("parent absolute path", basePath, name.getParent().getPath());

    // Try using a compound name to find a child
    assertBadName(name, "a/b", NameScope.CHILD);

    // Check other invalid names
    checkDescendentNames(name, NameScope.CHILD);
}
 
Example 4
Source File: NamingTests.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Tests descendent name resolution.
 */
public void testDescendentName() throws Exception {
    final FileName baseName = getReadFolder().getName();

    // Test direct child
    String path = baseName.getPath() + "/some-child";
    assertSameName(path, baseName, "some-child", NameScope.DESCENDENT);

    // Test compound name
    path = path + "/grand-child";
    assertSameName(path, baseName, "some-child/grand-child", NameScope.DESCENDENT);

    // Test relative names
    assertSameName(path, baseName, "./some-child/grand-child", NameScope.DESCENDENT);
    assertSameName(path, baseName, "./nada/../some-child/grand-child", NameScope.DESCENDENT);
    assertSameName(path, baseName, "some-child/./grand-child", NameScope.DESCENDENT);

    // Test badly formed descendent names
    checkDescendentNames(baseName, NameScope.DESCENDENT);
}
 
Example 5
Source File: KettleVFS.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
public static String getFilename( FileObject fileObject ) {
  FileName fileName = fileObject.getName();
  String root = fileName.getRootURI();
  if ( !root.startsWith( "file:" ) ) {
    return fileName.getURI(); // nothing we can do about non-normal files.
  }
  if ( root.startsWith( "file:////" ) ) {
    return fileName.getURI(); // we'll see 4 forward slashes for a windows/smb network share
  }
  if ( root.endsWith( ":/" ) ) { // Windows
    root = root.substring( 8, 10 );
  } else { // *nix & OSX
    root = "";
  }
  String fileString = root + fileName.getPath();
  if ( !"/".equals( Const.FILE_SEPARATOR ) ) {
    fileString = Const.replace( fileString, "/", Const.FILE_SEPARATOR );
  }
  return fileString;
}
 
Example 6
Source File: AccessInputMeta.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
public static String getFilename( FileObject fileObject ) {
  FileName fileName = fileObject.getName();
  String root = fileName.getRootURI();
  if ( !root.startsWith( "file:" ) ) {
    return fileName.getURI();
  }
  if ( root.endsWith( ":/" ) ) {
    root = root.substring( 8, 10 );
  } else {
    root = root.substring( 7, root.length() - 1 );
  }
  String fileString = root + fileName.getPath();
  if ( !"/".equals( Const.FILE_SEPARATOR ) ) {
    fileString = Const.replace( fileString, "/", Const.FILE_SEPARATOR );
  }
  return fileString;
}
 
Example 7
Source File: NamingTests.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Name resolution tests that are common for CHILD or DESCENDENT scope.
 */
private void checkDescendentNames(final FileName name, final NameScope scope) throws Exception {
    // Make some assumptions about the name
    assertFalse(name.getPath().equals("/"));
    assertFalse(name.getPath().endsWith("/a"));
    assertFalse(name.getPath().endsWith("/a/b"));

    // Test names with the same prefix
    final String path = name.getPath() + "/a";
    assertSameName(path, name, path, scope);
    assertSameName(path, name, "../" + name.getBaseName() + "/a", scope);

    // Test an empty name
    assertBadName(name, "", scope);

    // Test . name
    assertBadName(name, ".", scope);
    assertBadName(name, "./", scope);

    // Test ancestor names
    assertBadName(name, "..", scope);
    assertBadName(name, "../a", scope);
    assertBadName(name, "../" + name.getBaseName() + "a", scope);
    assertBadName(name, "a/..", scope);

    // Test absolute names
    assertBadName(name, "/", scope);
    assertBadName(name, "/a", scope);
    assertBadName(name, "/a/b", scope);
    assertBadName(name, name.getPath(), scope);
    assertBadName(name, name.getPath() + "a", scope);
}
 
Example 8
Source File: FileNameWrapper.java    From otroslogviewer with Apache License 2.0 4 votes vote down vote up
public FileNameWrapper(FileName fileName) {
  super(fileName.getScheme(), fileName.getPath(), fileName.getType());
  this.fileName = fileName;
}
 
Example 9
Source File: AbstractFileName.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
/**
 * Converts a file name to a relative name, relative to this file name.
 *
 * @param name The FileName.
 * @return The relative path to the file.
 * @throws FileSystemException if an error occurs.
 */
@Override
public String getRelativeName(final FileName name) throws FileSystemException {
    final String path = name.getPath();

    // Calculate the common prefix
    final int basePathLen = getPath().length();
    final int pathLen = path.length();

    // Deal with root
    if (basePathLen == 1 && pathLen == 1) {
        return ".";
    } else if (basePathLen == 1) {
        return path.substring(1);
    }

    final int maxlen = Math.min(basePathLen, pathLen);
    int pos = 0;
    for (; pos < maxlen && getPath().charAt(pos) == path.charAt(pos); pos++) {
    }

    if (pos == basePathLen && pos == pathLen) {
        // Same names
        return ".";
    } else if (pos == basePathLen && pos < pathLen && path.charAt(pos) == SEPARATOR_CHAR) {
        // A descendent of the base path
        return path.substring(pos + 1);
    }

    // Strip the common prefix off the path
    final StringBuilder buffer = new StringBuilder();
    if (pathLen > 1 && (pos < pathLen || getPath().charAt(pos) != SEPARATOR_CHAR)) {
        // Not a direct ancestor, need to back up
        pos = getPath().lastIndexOf(SEPARATOR_CHAR, pos);
        buffer.append(path.substring(pos));
    }

    // Prepend a '../' for each element in the base path past the common
    // prefix
    buffer.insert(0, "..");
    pos = getPath().indexOf(SEPARATOR_CHAR, pos + 1);
    while (pos != -1) {
        buffer.insert(0, "../");
        pos = getPath().indexOf(SEPARATOR_CHAR, pos + 1);
    }

    return buffer.toString();
}
 
Example 10
Source File: NamingTests.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
/**
 * Tests relative name resolution, relative to the base folder.
 */
public void testNameResolution() throws Exception {
    final FileName baseName = getReadFolder().getName();
    final String parentPath = baseName.getParent().getPath();
    final String path = baseName.getPath();
    final String childPath = path + "/some-child";

    // Test empty relative path
    assertSameName(path, baseName, "");

    // Test . relative path
    assertSameName(path, baseName, ".");

    // Test ./ relative path
    assertSameName(path, baseName, "./");

    // Test .// relative path
    assertSameName(path, baseName, ".//");

    // Test .///.///. relative path
    assertSameName(path, baseName, ".///.///.");
    assertSameName(path, baseName, "./\\/.\\//.");

    // Test <elem>/.. relative path
    assertSameName(path, baseName, "a/..");

    // Test .. relative path
    assertSameName(parentPath, baseName, "..");

    // Test ../ relative path
    assertSameName(parentPath, baseName, "../");

    // Test ..//./ relative path
    assertSameName(parentPath, baseName, "..//./");
    assertSameName(parentPath, baseName, "..//.\\");

    // Test <elem>/../.. relative path
    assertSameName(parentPath, baseName, "a/../..");

    // Test <elem> relative path
    assertSameName(childPath, baseName, "some-child");

    // Test ./<elem> relative path
    assertSameName(childPath, baseName, "./some-child");

    // Test ./<elem>/ relative path
    assertSameName(childPath, baseName, "./some-child/");

    // Test <elem>/././././ relative path
    assertSameName(childPath, baseName, "./some-child/././././");

    // Test <elem>/../<elem> relative path
    assertSameName(childPath, baseName, "a/../some-child");

    // Test <elem>/<elem>/../../<elem> relative path
    assertSameName(childPath, baseName, "a/b/../../some-child");
}