/** * 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; }
/** * 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; }
/** * 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; }
/** * 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; }
/** * 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 the preceding slash is removed as requested */ @Test public void testRemovePrecedingSlashWithNoPrecedingSlashEqualToInput() { log.info("testRemovePrecedingSlash"); final String noPrecedingSlash = "test/something"; final String result = PathUtil.optionallyRemovePrecedingSlash(noPrecedingSlash); Assert.assertEquals( "Call to remove preceding slash on input with no preceding slash should return equal by value to input", noPrecedingSlash, result); }
/** * {@inheritDoc} * * @see org.jboss.shrinkwrap.impl.base.exporter.StreamExporterTestBase#getContentsFromExportedFile(java.io.File, * org.jboss.shrinkwrap.api.ArchivePath) */ @Override protected InputStream getContentsFromExportedFile(final File file, final ArchivePath path) throws IOException { // Precondition checks assert file != null : "file must be specified"; assert path != null : "path must be specified"; // Get as Zip File final ZipFile zipFile = new ZipFile(file); final ZipEntry entry = zipFile.getEntry(PathUtil.optionallyRemovePrecedingSlash(path.get())); if (entry == null) { return null; } final byte[] actualContents = IOUtil.asByteArray(zipFile.getInputStream(entry)); return new ByteArrayInputStream(actualContents); }
/** * {@inheritDoc} * * @see org.jboss.shrinkwrap.impl.base.exporter.StreamExporterTestBase#getContentsFromExportedFile(java.io.File, * org.jboss.shrinkwrap.api.ArchivePath) */ @Override protected InputStream getContentsFromExportedFile(final File file, final ArchivePath path) throws IOException { // Precondition checks assert file != null : "file must be specified"; assert path != null : "path must be specified"; // Get as Zip File final ZipFile zipFile = new ZipFile(file); final ZipEntry entry = zipFile.getEntry(PathUtil.optionallyRemovePrecedingSlash(path.get())); if (entry == null) { return null; } final byte[] actualContents = IOUtil.asByteArray(zipFile.getInputStream(entry)); return new ByteArrayInputStream(actualContents); }
/** * Obtains the entry from the specified ZIP file at the specified Path, ensuring it exists along the way * * @param expectedZip * @param path * @return * @throws IllegalArgumentException * @throws IOException */ private ZipEntry getEntryFromZip(final ZipFile expectedZip, final ArchivePath path) throws IllegalArgumentException, IOException { final String entryPath = PathUtil.optionallyRemovePrecedingSlash(path.get()); final ZipEntry entry = expectedZip.getEntry(entryPath); Assert.assertNotNull("Expected path not found in ZIP: " + path, entry); return entry; } }
/** * Obtains the entry from the specified ZIP file at the specified Path, ensuring it exists along the way * * @param expectedZip * @param path * @return * @throws IllegalArgumentException * @throws IOException */ private ZipEntry getEntryFromZip(final ZipFile expectedZip, final ArchivePath path) throws IllegalArgumentException, IOException { final String entryPath = PathUtil.optionallyRemovePrecedingSlash(path.get()); final ZipEntry entry = expectedZip.getEntry(entryPath); Assert.assertNotNull("Expected path not found in ZIP: " + path, entry); return entry; } }
final String pathName = PathUtil.optionallyRemovePrecedingSlash(currentPath.get());
final String pathName = PathUtil.optionallyRemovePrecedingSlash(currentPath.get());
/** * 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; }