/** * Composes an absolute context from a given base and actual context relative to the base, returning the result. ie. * base of "base" and context of "context" will result in form "/base/context". */ public static String composeAbsoluteContext(final String base, final String context) { // Precondition checks assertSpecified(base); assertSpecified(context); // Compose final String relative = PathUtil.adjustToAbsoluteDirectoryContext(base); final String reformedContext = PathUtil.optionallyRemovePrecedingSlash(context); final String actual = relative + reformedContext; // Return return actual; }
/** * Creates a new Path using the specified base and specified relative context. * * @param basePath * @param context */ public BasicPath(final String basePath, String context) { this(PathUtil.composeAbsoluteContext(basePath, context)); }
/** * {@inheritDoc} * * @see org.jboss.shrinkwrap.api.ArchivePath#getParent() */ @Override public ArchivePath getParent() { return PathUtil.getParent(this); }
/** * Adds, if not already present, the absolute slash following the specified path, and returns the adjusted result. * * @param path * @return */ public static String optionallyAppendSlash(final String path) { // Precondition check assertSpecified(path); // If the last character is not a slash if (!isLastCharSlash(path)) { // Append return path + ArchivePath.SEPARATOR; } // Return as-is return path; }
/** * Adjusts the specified path to absolute form: * * 1) Adds, if not present, a preceding slash 2) Adds, if not present, a trailing slash * * Null arguments are returned as-is * * @param path */ public static String adjustToAbsoluteDirectoryContext(String path) { // Return nulls if (path == null) { return path; } // Add prefic slash final String prefixedPath = optionallyPrependSlash(path); // Add end of context slash final String addedPostfix = optionallyAppendSlash(prefixedPath); // Return return addedPostfix; }
/** * Adjusts the specified path to relative form: * * 1) Removes, if present, a preceding slash 2) Adds, if not present, a trailing slash * * Null arguments are returned as-is * * @param path */ public static String adjustToRelativeDirectoryContext(final String path) { // Return nulls if (path == null) { return path; } // Strip absolute form final String removedPrefix = optionallyRemovePrecedingSlash(path); // Add end of context slash final String addedPostfix = optionallyAppendSlash(removedPrefix); // Return return addedPostfix; }
/** * Obtains an {@link InputStream} to an entry of specified name from the specified TAR.GZ file, or null if not * found. We have to iterate through all entries for a matching name, as the instream does not support random * access. * * @param expectedZip * @param path * @return * @throws IllegalArgumentException * @throws IOException */ private InputStream getEntryFromTarFile(final File archive, final ArchivePath path) throws IllegalArgumentException, IOException { String entryPath = PathUtil.optionallyRemovePrecedingSlash(path.get()); final TarInputStream in = this.getTarInputStreamFromFile(archive); TarEntry currentEntry = null; while ((currentEntry = in.getNextEntry()) != null) { final String entryName = currentEntry.getName(); if (currentEntry.isDirectory()) { entryPath = PathUtil.optionallyAppendSlash(entryPath); } else { entryPath = PathUtil.optionallyRemoveFollowingSlash(entryPath); } if (entryName.equals(entryPath)) { return in; } } // Not found return null; }
private Set<String> adjust(String... paths) { Set<String> adjusted = new HashSet<String>(); for(String path : paths) { adjusted.add(PathUtil.optionallyPrependSlash(path)); } return adjusted; }
final String adjustedContext = PathUtil.optionallyRemoveFollowingSlash(context); final String adjustedOther = PathUtil.optionallyRemoveFollowingSlash(other.context); if (!adjustedContext.equals(adjustedOther)) { return false;
/** * Ensures that the preceding slash is removed as requested */ @Test public void testRemovePrecedingSlash() { log.info("testRemovePrecedingSlash"); final String precedingSlash = "/test/something"; final String expected = precedingSlash.substring(1); final String result = PathUtil.optionallyRemovePrecedingSlash(precedingSlash); Assert.assertEquals("Call to remove preceding slash should return everything in input except the first slash", expected, result); }
/** * Ensures that a slash may be appended to a given String */ @Test public void testAppendSlash() { log.info("testRemovePrecedingSlash"); final String noFollowingSlash = "test/something"; final String expected = noFollowingSlash + ArchivePath.SEPARATOR; final String result = PathUtil.optionallyAppendSlash(noFollowingSlash); Assert.assertEquals("Call to append a slash failed", expected, result); }
/** * Removes, if present, the absolute slash preceding the specified path, and returns the adjusted result. * * @param path * @return */ public static String optionallyRemovePrecedingSlash(final String path) { // Precondition check assertSpecified(path); // Is there's a first character of slash if (isFirstCharSlash(path)) { // Return everything but first char return path.substring(1); } // Return as-is return path; }
/** * Ensures that a relative path may be converted to full absolute directory form */ @Test public void testAdjustToAbsoluteDirectoryContext() { log.info("testRemovePrecedingSlash"); final String relativeWithoutTrailingSlash = "test/something"; final String expected = ArchivePath.SEPARATOR + relativeWithoutTrailingSlash + ArchivePath.SEPARATOR; final String result = PathUtil.adjustToAbsoluteDirectoryContext(relativeWithoutTrailingSlash); Assert.assertEquals("Adjusting to absolute form should prepend preceding slash and append a trailing one", expected, result); }
/** * Returns whether or not the first character in the specified String is a slash */ private static boolean isFirstCharSlash(final String path) { assertSpecified(path); if (path.length() == 0) { return false; } return path.charAt(0) == ArchivePath.SEPARATOR; }
/** * Adds, if not already present, the absolute slash preceding the specified path, and returns the adjusted result. * If the argument is null, adjusts to an empty String before processing. * * @param path * @return */ public static String optionallyPrependSlash(final String path) { // Adjust null String resolved = path; if (resolved == null) { resolved = EMPTY; } // If the first character is not a slash if (!isFirstCharSlash(resolved)) { // Prepend the slash return ArchivePath.SEPARATOR + resolved; } // Return as-is return resolved; }
/** * Ensures that an absolute path may be converted to full relative directory form */ @Test public void testAdjustToRelativeDirectoryContext() { log.info("testRemovePrecedingSlash"); final String absoulteWithoutTrailingSlash = "/test/something"; final String expected = absoulteWithoutTrailingSlash.substring(1) + ArchivePath.SEPARATOR; final String result = PathUtil.adjustToRelativeDirectoryContext(absoulteWithoutTrailingSlash); Assert.assertEquals("Adjusting to relative form should strip preceding slash and append a trailing one", expected, result); }
/** * Adjusts the specified path to relative form: * * 1) Removes, if present, a preceding slash 2) Adds, if not present, a trailing slash * * Null arguments are returned as-is * * @param path */ public static String adjustToRelativeDirectoryContext(final String path) { // Return nulls if (path == null) { return path; } // Strip absolute form final String removedPrefix = optionallyRemovePrecedingSlash(path); // Add end of context slash final String addedPostfix = optionallyAppendSlash(removedPrefix); // Return return addedPostfix; }
private Set<String> adjust(String... paths) { Set<String> adjusted = new HashSet<String>(); for(String path : paths) { adjusted.add(PathUtil.optionallyPrependSlash(path)); } return adjusted; }
/** * Adjusts the specified path to absolute form: * * 1) Adds, if not present, a preceding slash 2) Adds, if not present, a trailing slash * * Null arguments are returned as-is * * @param path */ public static String adjustToAbsoluteDirectoryContext(String path) { // Return nulls if (path == null) { return path; } // Add prefic slash final String prefixedPath = optionallyPrependSlash(path); // Add end of context slash final String addedPostfix = optionallyAppendSlash(prefixedPath); // Return return addedPostfix; }
final String adjustedContext = PathUtil.optionallyRemoveFollowingSlash(context); final String adjustedOther = PathUtil.optionallyRemoveFollowingSlash(other.context); if (!adjustedContext.equals(adjustedOther)) { return false;