/** * Removes any redundant {@code "."}, {@code ".."} directories from the * path name. * * @return The normalized path string denoting the same file or * directory as this instance. */ public String getNormalizedPath() { return Paths.normalize(getPath(), separatorChar); }
private static int prefixLength(String p) { return Paths.prefixLength(p, SEPARATOR_CHAR, true); }
/** * Throws an {@code IOException} if and only if the path represented by * {@code a} contains the path represented by {@code b}, where a path is * said to contain another path if and only if it is equal or an ancestor * of the other path. * <p> * Note that this method uses the absolute path of both files as if by * calling {@link File#getAbsolutePath()}. * * @param a a file. * @param b another file. */ private static void checkContains(File a, File b) throws IOException { if (Paths.contains( a.getAbsolutePath(), b.getAbsolutePath(), File.separatorChar)) throw new IOException(b + " (contained in " + a + ")"); }
/** * Returns whether or not the given path is absolute. * <p> * A path is absolute if it doesn't need to be combined with other path * information in order to locate a file. * * @return Whether or not the given path is absolute. */ static boolean isAbsolute(URI uri) { return uri.isAbsolute() || Paths.isAbsolute( uri.getSchemeSpecificPart(), SEPARATOR_CHAR); }
@SuppressWarnings("RedundantStringConstructorCall") @Test public void testCutTrailingSeparators() { String path; path = ""; assertSame(path, cutTrailingSeparators(path, '/')); path = "d"; assertSame(path, cutTrailingSeparators(path, '/')); assertEquals("d", cutTrailingSeparators("d/", '/')); assertEquals("d", cutTrailingSeparators("d//", '/')); assertEquals("d", cutTrailingSeparators("d///", '/')); path = "/d"; assertSame(path, cutTrailingSeparators(path, '/')); assertEquals("/d", cutTrailingSeparators("/d/", '/')); assertEquals("/d", cutTrailingSeparators("/d//", '/')); assertEquals("/d", cutTrailingSeparators("/d///", '/')); path = new String("/"); // need new object! assertSame(path, cutTrailingSeparators(path, '/')); assertEquals("/", cutTrailingSeparators("//", '/')); assertEquals("/", cutTrailingSeparators("///", '/')); assertEquals("/", cutTrailingSeparators("////", '/')); }
private void assertSplit(final String path) { final File file = new File(path); final String parent = file.getParent(); final String member = file.getName(); final Splitter splitter = Paths.split(path, File.separatorChar, false); assertEquals(parent, splitter.getParentPath()); assertEquals(member, splitter.getMemberName()); }
/** * Similar to {@link #getAbsolutePath()}, but removes any redundant * {@code "."} and {@code ".."} directories from the path name. * The result is similar to {@link #getCanonicalPath()}, but symbolic * links are not resolved. * This may be useful if {@code getCanonicalPath()} throws an * IOException. * * @return The normalized absolute path string denoting the same file or * directory as this instance. * @see #getCanonicalPath() * @see #getNormalizedPath() */ public String getNormalizedAbsolutePath() { return Paths.normalize(getAbsolutePath(), separatorChar); }
static int pathPrefixLength(final URI uri) { final String ssp = uri.getSchemeSpecificPart(); final String a = uri.getAuthority(); final int al = null == a ? 0 : 2 + a.length(); final int pl = Paths.prefixLength(ssp, SEPARATOR_CHAR, true) - al; return pl >= 0 ? pl : Paths.prefixLength(uri.getPath(), SEPARATOR_CHAR, false); }
/** * Returns {@code true} if and only if the path represented * by {@code a} contains the path represented by {@code b}, * where a path is said to contain another path if and only * if it's equal or an ancestor of the other path. * <p> * Note: * <ul> * <li>This method uses the absolute path name of the given files. * <li>This method does <em>not</em> access the file system. * It just tests the paths. * </ul> * * @param a the file to test for containing {@code b}. * @param b the file to test for being contained by {@code a}. * @return {@code true} if and only if the path represented * by {@code a} contains the path represented by {@code b}. * @throws NullPointerException If any parameter is {@code null}. */ public static boolean contains(File a, File b) { return Paths.contains( a.getAbsolutePath(), b.getAbsolutePath(), separatorChar); }
/** * This convenience method simply returns the canonical form of this * abstract path or the normalized absolute form if resolving the * prior fails. * * @return The canonical or absolute path of this file as a * {@code String} instance. */ public String getCanOrAbsPath() { try { return getCanonicalPath(); } catch (IOException ex) { return Paths.normalize(getAbsolutePath(), separatorChar); } }
private static String stripPrefix(final String s) { return s.substring(Paths.prefixLength(s, SEPARATOR_CHAR, true)); }
/** * Returns {@code true} if and only if the path represented * by this instance is a direct or indirect parent of the path * represented by the given {@code file}. * <p> * Note: * <ul> * <li>This method uses the absolute path name of the given files. * <li>This method does <em>not</em> access the file system. * It just tests the paths. * </ul> * * @param file The file object for the path to test for being a direct or * indirect child of the path of this instance. * @return {@code true} if and only if the path represented * by this instance is a direct or indirect parent of the path * represented by the given {@code file}. */ public boolean isParentOf(final File file) { final String a = this.getAbsolutePath(); final String b = file.getAbsoluteFile().getParent(); return b != null ? Paths.contains(a, b, separatorChar) : false; }
private void assertNormalize( final String expected, final String path, final char separatorChar) { assertEquals(URI .create(path.replace(separatorChar, '/')) .normalize() .toString() .replace('/', separatorChar), expected); final String result = Paths.normalize(path, separatorChar); assertEquals(expected, result); assertTrue(!result.equals(path) || result == path); // mind contract! } }
^ (innerArchive == enclArchive && null == controller); assert null == enclArchive || Paths.contains( enclArchive.getPath(), file.getParentFile().getPath(), separatorChar)
return controller; final File file = this.file; final String path = Paths.normalize(file.getPath(), separatorChar); final FsScheme scheme = detector.getScheme(path);