Java Code Examples for org.openide.filesystems.FileUtil#normalizePath()

The following examples show how to use org.openide.filesystems.FileUtil#normalizePath() . 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: TransferFileTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testTransferFileRelations() {
    RemoteClientImplementation remoteClient = new RemoteClient(FileUtil.normalizePath("/a"), "/");
    TransferFile projectRoot = TransferFile.fromDirectory(remoteClient, null, new File("/a"));
    assertTrue(projectRoot.isRoot());
    assertTrue(projectRoot.isProjectRoot());
    assertTrue(projectRoot.getLocalChildren().toString(), projectRoot.getLocalChildren().isEmpty());

    TransferFile child1 = TransferFile.fromFile(remoteClient, projectRoot, new File("/a/1"));
    TransferFile child2 = TransferFile.fromFile(remoteClient, projectRoot, new File("/a/2"));
    for (TransferFile child : new TransferFile[] {child1, child2}) {
        assertNotNull(child.getParent());
        assertFalse(child.isRoot());
        assertFalse(child.isProjectRoot());
        assertSame(child.getParent().toString(), projectRoot, child.getParent());
    }
    assertFalse(projectRoot.getLocalChildren().toString(), projectRoot.getLocalChildren().isEmpty());
    assertSame(projectRoot.getLocalChildren().toString(), 2, projectRoot.getLocalChildren().size());
    assertTrue(projectRoot.getLocalChildren().toString(), projectRoot.getLocalChildren().contains(child1));
    assertTrue(projectRoot.getLocalChildren().toString(), projectRoot.getLocalChildren().contains(child2));
}
 
Example 2
Source File: GlobalVisibilityQueryImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize variables holding the user directory.
 */
private void initializeHome() {
    String homeRaw = System.getProperty("user.home");               //NOI18N
    if (homeRaw != null) {
        homePath = FileUtil.normalizePath(homeRaw);
        home = FileUtil.toFileObject(new File(homePath));
    }
    homeInitialized = true;
}
 
Example 3
Source File: TransferFileTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testLocalTransferFilePaths() {
    RemoteClientImplementation remoteClient = new RemoteClient(FileUtil.normalizePath("/a"), "/pub/project");
    TransferFile parent1 = TransferFile.fromDirectory(remoteClient, null, new File("/a/b"));
    TransferFile file1 = TransferFile.fromFile(remoteClient, parent1, new File("/a/b/c"));
    assertEquals("c", file1.getName());
    assertEquals("b/c", file1.getRemotePath());
    assertEquals("b", file1.getParent().getRemotePath());
    assertEquals(new File("/tmp/b/c").getAbsolutePath(), file1.resolveLocalFile(new File("/tmp")).getAbsolutePath());
    assertEquals(FileUtil.normalizePath("/a/b/c"), file1.getLocalAbsolutePath());
    assertEquals("/pub/project/b/c", file1.getRemoteAbsolutePath());

    TransferFile file2 = TransferFile.fromFile(new RemoteClient(FileUtil.normalizePath("/a/b"), "/"), null, new File("/a/b/c"));
    assertFalse(file1.equals(file2));

    TransferFile file3 = TransferFile.fromFile(new RemoteClient(FileUtil.normalizePath("/0/1/2"), "/"), null, new File("/0/1/2/b/c"));
    assertTrue(file1.equals(file3));

    TransferFile file4 = TransferFile.fromFile(new RemoteClient(FileUtil.normalizePath("/a"), "/"), null, new File("/a/b"));
    assertEquals("b", file4.getName());
    assertEquals("b", file4.getRemotePath());

    TransferFile file5 = TransferFile.fromFile(new RemoteClient(FileUtil.normalizePath("/a"), "/"), null, new File("/a"));
    assertEquals("a", file5.getName());
    assertTrue(file5.isProjectRoot());
    assertSame(TransferFile.REMOTE_PROJECT_ROOT, file5.getRemotePath());
    try {
        assertEquals(null, file5.getParent().getRemotePath());
        fail("Should not get here");
    } catch (IllegalStateException ex) {
        // expected
    }
}
 
Example 4
Source File: TransferFileTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testParentRemotePath() {
    RemoteClientImplementation remoteClient = new RemoteClient(FileUtil.normalizePath("/a"), "/");
    TransferFile projectRoot = TransferFile.fromDirectory(remoteClient, null, new File("/a"));
    TransferFile childWithParent = TransferFile.fromDirectory(remoteClient, projectRoot, new File("/a/b"));
    TransferFile childWithoutParent = TransferFile.fromFile(remoteClient, null, new File("/a/b"));

    assertEquals(projectRoot.getRemotePath(), childWithParent.getParentRemotePath());
    assertEquals(projectRoot.getRemotePath(), childWithoutParent.getParentRemotePath());

    TransferFile grandchildWithParent = TransferFile.fromFile(remoteClient, childWithParent, new File("/a/b/c"));
    TransferFile grandchildWithoutParent = TransferFile.fromFile(remoteClient, null, new File("/a/b/c"));

    assertEquals(childWithParent.getRemotePath(), grandchildWithParent.getParentRemotePath());
    assertEquals(childWithParent.getRemotePath(), grandchildWithoutParent.getParentRemotePath());
}
 
Example 5
Source File: VCSFileProxy.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Normalize a file path to a clean form.
 * <b>This method might block for a longer time and shouldn't be called in EDT.
 * 
 * @return a VCSFileProxy with a normalized file path
 * @see FileUtil#normalizePath(java.lang.String) 
 */
public VCSFileProxy normalizeFile() {
    if (proxy == null) {
        return new VCSFileProxy(FileUtil.normalizePath(path), null);
    } else {
        return proxy.normalize(this);
    }
}
 
Example 6
Source File: PathUtil.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public static String normalizePath(String path) throws IOException {
    return FileUtil.normalizePath(new File(path).toPath().toRealPath().toString());
}
 
Example 7
Source File: FXMLHyperlinkProvider.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static @CheckForNull
FileObject findFile(FileObject docFO, String path) {
    if (path == null || path.trim().isEmpty()) {
        return null;
    }
    
    Project prj = FileOwnerQuery.getOwner(docFO);
    if (prj == null) {
        return null;
    }
    
    Sources srcs = ProjectUtils.getSources(prj);
    SourceGroup[] grps = srcs.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
    if (grps.length == 0) {
        return null;
    }

    // XXX other source roots?
    final FileObject rootFolder = grps[0].getRootFolder();
    ClassPath cp = ClassPath.getClassPath(rootFolder, ClassPath.SOURCE);
    if (cp == null) {
        return null;
    }
    
    FileObject fo;
    String rootPath = FileUtil.normalizePath(rootFolder.getPath());
    String docPath = FileUtil.normalizePath(docFO.getParent().getPath());
    if (!docPath.startsWith(rootPath)) {
        // #228262 sanity check, for files which are outside of any source root
        return null;
    }

    // Java Controller
    String javaPath = path.trim().replace("\"", "").replace('.', '/') + ".java"; // NOI18N
    fo = cp.findResource(javaPath);
    if (fo == null) {
        javaPath = docPath.substring(rootPath.length()) + '/' + javaPath; // NOI18N
        fo = cp.findResource(javaPath);
    }
    
    // CSS file
    if (fo == null) {
        // try short path
        String cssPath = path.trim().replace("\"", "").replace("@", ""); // NOI18N
        fo = cp.findResource(cssPath);
        // try full path
        if (fo == null) {
            cssPath = docPath.substring(rootPath.length()) + '/' + cssPath; // NOI18N
            fo = cp.findResource(cssPath);
        }
    }
    return fo;
}