/** * 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, "/"); }
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; }
/** * 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()); }
/** * 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); }
@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; }
/** * Tests {@link PathUtils#removeLeadingSeparator(String, String)}. */ @Test public void testRemoveLeadingSeparator() { assert PathUtils.removeLeadingSeparator("/home/", "/").equals("home/"); assert PathUtils.removeLeadingSeparator("/home/maxence", "/").equals("home/maxence"); assert PathUtils.removeLeadingSeparator("home/", "/").equals("home/"); assert PathUtils.removeLeadingSeparator("/home/", "\\").equals("/home/"); assert PathUtils.removeLeadingSeparator("/", "/").equals(""); assert PathUtils.removeLeadingSeparator("C:\\home\\", "\\").equals("C:\\home\\"); assert PathUtils.removeLeadingSeparator("C:\\home\\", "/").equals("C:\\home\\"); assert PathUtils.removeLeadingSeparator("--home--", "--").equals("home--"); assert PathUtils.removeLeadingSeparator("--home--maxence", "--").equals("home--maxence"); assert PathUtils.removeLeadingSeparator("home--", "--").equals("home--"); assert PathUtils.removeLeadingSeparator("--home--", "/").equals("--home--"); assert PathUtils.removeLeadingSeparator("--", "--").equals(""); }
locationText = PathUtils.removeLeadingSeparator(locationText, "/");
@Override public void update(FileTableTab tab, int index) { if (JavaVersion.JAVA_1_5.isCurrentOrLower()) { /*setLockedAt(index, tab.isLocked()); setTitleAt(index, tab.getLocation().getName()); */ } else { setTabHeader(index, headersFactory.create(tab)); } String locationText = tab.getLocation().getPath(); // For OSes with 'root drives' (Windows, OS/2), remove the leading '/' character if(LocalFile.hasRootDrives()) locationText = PathUtils.removeLeadingSeparator(locationText, "/"); setToolTipTextAt(index, locationText); SwingUtilities.invokeLater(() -> validate()); }
filePath = PathUtils.removeLeadingSeparator(filePath, "/"); 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));