/** * Returns the depth of the specified entry path, based on the number of path delimiters ('/') it contains. * Top-level entries have a depth of 1. * * @param entryPath the path for which to calculate the depth * @return the depth of the given entry path */ public static int getDepth(String entryPath) { return PathUtils.getDepth(entryPath, "/"); }
/** * Extracts the filename from the given path and returns it, or <code>null</null> if the path does not contain * a filename. * * @param path the path from which to extract a filename * @param separator the path separator * @return the filename extracted from the given path, <code>null</code> if the path doesn't contain any */ public static String getFilenameFromPath(String path, String separator) { if(path.equals("") || path.equals("/")) return null; // Remove any trailing separator path = PathUtils.removeTrailingSeparator(path, separator); if(!separator.equals("/")) path = PathUtils.removeLeadingSeparator(path, "/"); // Extract filename int pos = path.lastIndexOf(separator); if(pos==-1) return null; return path.substring(pos+1); }
assert PathUtils.pathEquals("/home/", "/home/", "/"); assert PathUtils.pathEquals("/home", "/home", "/"); assert PathUtils.pathEquals("/home/", "/home/", "\\"); assert PathUtils.pathEquals("/home/", "/home", "/"); assert PathUtils.pathEquals("/home", "/home/", "/"); assert PathUtils.pathEquals("C:\\home\\", "C:\\home\\", "\\"); assert PathUtils.pathEquals("C:\\home", "C:\\home", "\\"); assert PathUtils.pathEquals("C:\\home\\", "C:\\home\\", "/"); assert PathUtils.pathEquals("C:\\home\\", "C:\\home", "\\"); assert PathUtils.pathEquals("C:\\home", "C:\\home\\", "\\"); assert PathUtils.pathEquals("--home--", "--home--", "--"); assert PathUtils.pathEquals("--home", "--home", "--"); assert PathUtils.pathEquals("--home--", "--home--", "/"); assert PathUtils.pathEquals("--home--", "--home", "--"); assert PathUtils.pathEquals("--home", "--home--", "--"); assert !(PathUtils.pathEquals("/", "/home", "/")); assert !(PathUtils.pathEquals("/home", "/home/", "\\")); assert !(PathUtils.pathEquals("/home/", "/home", "\\")); assert !(PathUtils.pathEquals("C:\\", "C:\\home", "\\")); assert !(PathUtils.pathEquals("C:\\home", "C:\\home\\", "/")); assert !(PathUtils.pathEquals("C:\\home\\", "C:\\home", "/")); assert !(PathUtils.pathEquals("--", "--home", "--")); assert !(PathUtils.pathEquals("--home", "--home--", "/")); assert !(PathUtils.pathEquals("--home--", "--home", "/"));
/** * Removes any leading slash from the given path and returns it. Does nothing if the path does not have a * leading path. * * @param path the path to normalize * @return the path without a leading slash */ private static String removeLeadingSlash(String path) { return PathUtils.removeLeadingSeparator(path, "/"); }
:PathUtils.removeLeadingFragments(entry.getPath(), "/", baseArchiveDepth); relDestPath = newName+(PathUtils.getDepth(relDestPath, "/")<=1?"":"/"+PathUtils.removeLeadingFragments(relDestPath, "/", 1));
/** * Tests {@link PathUtils#removeTrailingSeparator(String, String)}. */ @Test public void testRemoveTrailingSeparator() { assert PathUtils.removeTrailingSeparator("/home/", "/").equals("/home"); assert PathUtils.removeTrailingSeparator("/home/maxence", "/").equals("/home/maxence"); assert PathUtils.removeTrailingSeparator("/home/maxence/", "/").equals("/home/maxence"); assert PathUtils.removeTrailingSeparator("/home/", "\\").equals("/home/"); assert PathUtils.removeTrailingSeparator("/", "/").equals(""); assert PathUtils.removeTrailingSeparator("C:\\home", "\\").equals("C:\\home"); assert PathUtils.removeTrailingSeparator("C:\\home\\", "\\").equals("C:\\home"); assert PathUtils.removeTrailingSeparator("C:\\home\\maxence", "\\").equals("C:\\home\\maxence"); assert PathUtils.removeTrailingSeparator("C:\\home\\maxence", "\\").equals("C:\\home\\maxence"); assert PathUtils.removeTrailingSeparator("C:\\home\\", "/").equals("C:\\home\\"); assert PathUtils.removeTrailingSeparator("--home--", "--").equals("--home"); assert PathUtils.removeTrailingSeparator("--home--maxence", "--").equals("--home--maxence"); assert PathUtils.removeTrailingSeparator("--home--maxence--", "--").equals("--home--maxence"); assert PathUtils.removeTrailingSeparator("--home--", "/").equals("--home--"); assert PathUtils.removeTrailingSeparator("--", "--").equals(""); }
@Override public void run() { spinningDial.setAnimated(false); final String destPath = pathField.getText(); // Resolves destination folder (I/O bound) final PathUtils.ResolvedDestination resolvedDest = PathUtils.resolveDestination(destPath, mainFrame.getActivePanel().getCurrentFolder()); // Resolves destination folder (I/O bound) final boolean isValid = isValidDestination(resolvedDest, destPath); // Perform UI tasks in the AWT event thread SwingUtilities.invokeLater(() -> { if(interrupted) { dispose(); } else if(isValid) { dispose(); startJob(resolvedDest); } else { showErrorDialog(Translator.get("invalid_path", destPath), errorDialogTitle); // Re-enable the OK button and path field so that a new path can be entered okButton.setEnabled(true); pathField.setEnabled(true); } }); // Set the current thread to null synchronized(TransferDestinationDialog.this) { if(thread==this) // This thread may have been interrupted already thread = null; } }
/** * Tests {@link PathUtils#removeLeadingFragments(String, String, int)}. */ @Test public void testRemoveLeadingFragments() { assert "home/maxence/".equals(PathUtils.removeLeadingFragments("/home/maxence/", "/", 0)); assert "maxence/".equals(PathUtils.removeLeadingFragments("/home/maxence/", "/", 1)); assert "".equals(PathUtils.removeLeadingFragments("/home/maxence/", "/", 2)); assert "".equals(PathUtils.removeLeadingFragments("/home/maxence/", "/", 3)); assert "".equals(PathUtils.removeLeadingFragments("/home/maxence/", "\\", 1)); }
int h = PathUtils.getPathHashCode(path, separator);
private String getFilenameRepresentation(String filename, boolean local) { // Under for OSes with 'root drives' (Windows, OS/2), remove the leading '/' character if(local && LocalFile.hasRootDrives() && filename != null) return PathUtils.removeLeadingSeparator(filename, "/"); // Under other OSes, if the filename is empty return "/" else return filename == null ? "/" : filename; }
/** * Creates a new batch-rename dialog. * @param mainFrame the main frame * @param files a list of files to rename */ public BatchRenameDialog(MainFrame mainFrame, FileSet files) { super(mainFrame, ActionProperties.getActionLabel(BatchRenameAction.Descriptor.ACTION_ID), null); this.mainFrame = mainFrame; this.files = files; for (AbstractFile f : files) { this.blockNames.add(Boolean.FALSE); this.newNames.add(""); oldNames.put(PathUtils.removeTrailingSeparator(f.getAbsolutePath()), f); } initialize(); generateNewNames(); }
assertResult(PathUtils.resolveDestination(baseFolder.getURL().toString(true), baseRoot), baseFolder, expectedType); assertResult(PathUtils.resolveDestination(baseFolder.getURL().toString(true), null), baseFolder, expectedType); assertResult(PathUtils.resolveDestination(baseRoot.getURL().toString(true), baseRoot), baseRoot, expectedType); assertResult(PathUtils.resolveDestination(".", baseFolder), baseFolder, expectedType); assertResult(PathUtils.resolveDestination("."+baseFolder.getSeparator(), baseFolder), baseFolder, expectedType); assertResult(PathUtils.resolveDestination(baseFolder.getName(), baseParent), baseFolder, expectedType); assertResult(PathUtils.resolveDestination(baseFolder.getName()+separator, baseParent), baseFolder, expectedType); assertResult(PathUtils.resolveDestination("."+separator+baseFolder.getName(), baseParent), baseFolder, expectedType); assertResult(PathUtils.resolveDestination(existingArchive.getURL().toString(true)+separator, baseFolder), existingArchive, expectedType); assertResult(PathUtils.resolveDestination(existingArchiveFilename+separator, baseFolder), existingArchive, expectedType); assertResult(PathUtils.resolveDestination("."+separator+existingArchiveFilename+separator, baseFolder), existingArchive, expectedType); assertResult(PathUtils.resolveDestination(existingFile.getURL().toString(true), baseRoot), existingFile, expectedType); assertResult(PathUtils.resolveDestination(existingFilename, baseFolder), existingFile, expectedType); assertResult(PathUtils.resolveDestination("."+separator+existingFilename, baseFolder), existingFile, expectedType); assertResult(PathUtils.resolveDestination(existingArchive.getURL().toString(true), baseFolder), existingArchive, expectedType); assertResult(PathUtils.resolveDestination(existingArchiveFilename, baseFolder), existingArchive, expectedType); assertResult(PathUtils.resolveDestination("."+separator+existingArchiveFilename, baseFolder), existingArchive, expectedType); assertResult(PathUtils.resolveDestination(nonExistentFile.getURL().toString(true), baseRoot), nonExistentFile, expectedType); assertResult(PathUtils.resolveDestination(nonExistentFilename, baseFolder), nonExistentFile, expectedType); assertResult(PathUtils.resolveDestination("."+separator+nonExistentFilename, baseFolder), nonExistentFile, expectedType); assert PathUtils.resolveDestination(nonExistentFilename+separator+nonExistentFilename, baseFolder) == null;
filePath = PathUtils.removeLeadingSeparator(filePath, "/"); String currentPath = PathUtils.removeTrailingSeparator(pt.getCurrentPath(), pathSeparator); AbstractFile tempEntryFile = ((AbstractArchiveFile)currentFile).getArchiveEntryFile(PathUtils.removeLeadingSeparator(currentPath.substring(currentFile.getURL().getPath().length(), currentPath.length()), pathSeparator)); if (tempEntryFile.isArchive()) { currentFile = tempEntryFile; currentFile = ((AbstractArchiveFile)currentFile).getArchiveEntryFile(PathUtils.removeLeadingSeparator(currentPath.substring(currentFile.getURL().getPath().length(), currentPath.length()), pathSeparator));
/** * Removes the specified number of fragments from the beginning of the given path and returns the modified path, * free of a leading separator. Returns an empty string (<code>""</code>) if the path does not contain less or * exactly that many fragments. * * <p> * For instance, calling this method with * <ul> * <li><code>("/home/maxence/, "/", 0)</code> will return "home/maxence/"</li> * <li><code>("/home/maxence/, "/", 1)</code> will return "maxence/"</li> * <li><code>("/home/maxence/, "/", 2)</code> will return ""</li> * <li><code>("/home/maxence/, "/", 3)</code> will return ""</li> * </ul> * </p> * * @param path the path to modify * @param separator the path separator, usually "/" or "\\" * @param nbFragments number of path fragments to remove from the path * @return the modified path, free of any leading separator */ public static String removeLeadingFragments(String path, String separator, int nbFragments) { path = removeLeadingSeparator(path, separator); if(nbFragments==0) return path; int pos=-1; for(int i=0; i<nbFragments && (pos=path.indexOf(separator, pos+1))!=-1; i++); if(pos==-1 || pos==path.length()-1) return ""; return path.substring(pos+1, path.length()); }
/** * Tests {@link PathUtils#getDepth(String, String)}. */ @Test public void testGetDepth() { assert 0 == PathUtils.getDepth("/", "/"); assert 0 == PathUtils.getDepth("", "/"); assert 1 == PathUtils.getDepth("/home", "/"); assert 1 == PathUtils.getDepth("/home/", "/"); assert 2 == PathUtils.getDepth("/home/maxence", "/"); assert 2 == PathUtils.getDepth("/home/maxence/", "/"); assert 1 == PathUtils.getDepth("/home/maxence", "\\"); assert 1 == PathUtils.getDepth("C:", "\\"); assert 1 == PathUtils.getDepth("C:\\", "\\"); assert 2 == PathUtils.getDepth("C:\\home", "\\"); assert 2 == PathUtils.getDepth("C:\\home\\", "\\"); assert 3 == PathUtils.getDepth("C:\\home\\maxence", "\\"); assert 3 == PathUtils.getDepth("C:\\home\\maxence\\", "\\"); }
} while (haveRemaining); String parentPath = PathUtils.removeTrailingSeparator(fileURL .getPath()) + SEPARATOR;
PathUtils.ResolvedDestination resolvedDest = PathUtils.resolveDestination(enteredPath, mainFrame.getActivePanel().getCurrentFolder());
/** * Returns <code>true</code> if the given object is an <code>ArchiveEntry</code> whose path is equal to this one, * according to {@link PathUtils#pathEquals(String, String, String)} (trailing slash-insensitive comparison). * * @param o the object to test * @return <code>true</code> if the given object is an <code>ArchiveEntry</code> whose path is equal to this one * @see PathUtils#pathEquals(String, String, String) */ public boolean equals(Object o) { if(!(o instanceof ArchiveEntry)) return false; return PathUtils.pathEquals(getPath(), ((ArchiveEntry)o).getPath(), "/"); }
@Override FileURL getServerURL() throws MalformedURLException { updateValues(); FileURL url = FileURL.getFileURL(FileProtocols.VSPHERE + "://" + lastVsphere + "/" + lastGuest + "/" + PathUtils.removeLeadingSeparator(lastDir)); url.setCredentials(new Credentials(lastUsername, new String(passwordField.getPassword()))); url.setProperty(VSphereFile.GUEST_CREDENTIALS, lastGuestUsername + ":" + new String(guestPasswordField.getPassword())); return url; }
depth = PathUtils.getDepth(volumePath, volume.getSeparator()); if(depth>bestDepth) { bestDepth = depth;