Example usage for java.nio.file Path relativize

List of usage examples for java.nio.file Path relativize

Introduction

In this page you can find the example usage for java.nio.file Path relativize.

Prototype

Path relativize(Path other);

Source Link

Document

Constructs a relative path between this path and a given path.

Usage

From source file:de.ks.flatadocdb.defaults.DefaultIdGenerator.java

protected String getRelativePath(Path repository, Path targetPath) {
    return StringUtils.replace(repository.relativize(targetPath).toString(), "\\", "/");
}

From source file:org.graphwalker.java.source.SourceFile.java

public SourceFile(Path inputPath, Path basePath, Path outputPath) {
    this.inputPath = inputPath;
    this.relativePath = basePath.relativize(inputPath);
    if (null != this.relativePath.getParent()) {
        this.packageName = this.relativePath.getParent().toString().replace(File.separator, ".").replaceAll(" ",
                "_");
    } else {//w ww .ja va  2s .  c om
        this.packageName = "";
    }
    this.outputPath = outputPath.resolve(this.relativePath).resolveSibling(getFileName() + ".java");
}

From source file:com.cloudbees.clickstack.util.Files2.java

/**
 * Copy content for {@code srcDir} to {@code destDir}
 *
 * @param srcDir//from ww  w  .j  av  a 2s.  c  o  m
 * @param destDir
 * @throws RuntimeIOException
 */
public static void copyDirectoryContent(@Nonnull final Path srcDir, @Nonnull final Path destDir)
        throws RuntimeIOException {
    logger.trace("Copy from {} to {}", srcDir, destDir);

    FileVisitor<Path> copyDirVisitor = new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            Path targetPath = destDir.resolve(srcDir.relativize(dir));
            if (!Files.exists(targetPath)) {
                Files.createDirectory(targetPath);
            }
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            Files.copy(file, destDir.resolve(srcDir.relativize(file)), StandardCopyOption.REPLACE_EXISTING);
            return FileVisitResult.CONTINUE;
        }
    };
    try {
        Files.walkFileTree(srcDir, copyDirVisitor);
    } catch (IOException e) {
        throw new RuntimeIOException("Exception copying content of dir " + srcDir + " to " + destDir, e);
    }
}

From source file:org.olat.core.util.ZipUtil.java

/**
 * Add the content of a directory to a zip stream.
 * //  ww w  . ja  v a2s . co  m
 * @param path
 * @param dirName
 * @param zout
 */
public static void addDirectoryToZip(final Path path, final String baseDirName, final ZipOutputStream zout) {
    try {
        Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (!attrs.isDirectory()) {
                    Path relativeFile = path.relativize(file);
                    String names = baseDirName + "/" + relativeFile.toString();
                    zout.putNextEntry(new ZipEntry(names));

                    try (InputStream in = Files.newInputStream(file)) {
                        FileUtils.copy(in, zout);
                    } catch (Exception e) {
                        log.error("", e);
                    }

                    zout.closeEntry();
                }
                return FileVisitResult.CONTINUE;
            }
        });
    } catch (IOException e) {
        log.error("", e);
    }
}

From source file:ffx.xray.RealSpaceFile.java

/**
 * read in a Real Space density file based on the molecular assembly
 * filename, using a weight of 1.0//from   w  w  w.  j av  a 2  s .c om
 *
 * @param assembly {@link ffx.potential.MolecularAssembly} from which a
 * filename will be determined
 * @param weight the weight of the data
 */
public RealSpaceFile(MolecularAssembly assembly, double weight) {
    String name = removeExtension(assembly.getFile().getPath());

    File tmp = new File(name + ".map");
    if (tmp.exists()) {
        logger.info(" Data file: " + tmp.getName());
        realspacefilter = new CCP4MapFilter();
    } else {
        logger.severe(" No input data was found.");
        realspacefilter = null;
    }
    String filenameHolder; // Compiler complains if I set this.filename directly.
    try {
        Path filepath = Paths.get(tmp.getCanonicalPath());
        Path pwdPath = Paths.get(new File("").getCanonicalPath());
        filenameHolder = pwdPath.relativize(filepath).toString();
    } catch (IOException ex) {
        logger.warning(
                " Relative path to provided data file could not be resolved: using map file name instead.");
        filenameHolder = tmp.getName();
    }
    this.filename = filenameHolder;
    this.weight = weight;
}

From source file:org.apache.geode.management.internal.cli.commands.ExportLogsIntegrationTest.java

@Test
public void withFiles_savedToLocatorSpecifiedRelativeDir() throws Exception {
    String[] extensions = { "zip" };
    Path workingDirPath = getWorkingDirectory().toPath();
    Path subdirPath = workingDirPath.resolve("some").resolve("test").resolve("directory");
    Path relativeDir = workingDirPath.relativize(subdirPath);
    // Expects locator to produce file in own working directory when connected via JMX
    gfsh.executeCommand("export logs --dir=" + relativeDir.toString());
    assertThat(FileUtils.listFiles(getWorkingDirectory(), extensions, false)).isEmpty();
    assertThat(FileUtils.listFiles(getWorkingDirectory(), extensions, true)).isNotEmpty();
    assertThat(FileUtils.listFiles(subdirPath.toFile(), extensions, false)).isNotEmpty();
}

From source file:org.beryx.vbundle.chart.html.HtmlChartContentPane.java

public String getContent() {
    if (optFile.isSelected()) {
        String filePath = txtFile.getText().replace('\\', '/');
        Path path = Paths.get(filePath);
        Path pathUserDir = Paths.get(System.getProperty("user.dir").replace('\\', '/'));
        if (path.startsWith(pathUserDir)) {
            path = pathUserDir.relativize(path);
        }/* w ww .  j av a  2 s  .  c o  m*/
        return "file('" + path.toString().replace('\\', '/') + "')";
    } else if (optUrl.isSelected()) {
        return "url('" + txtUrl.getText() + "')";
    } else {
        return "\"" + StringEscapeUtils.escapeJava(txtInline.getText()) + "\"";
    }
}

From source file:ffx.realspace.RealSpaceFile.java

/**
 * Read in a Real Space density file based on the molecular assembly
 * filename, using a weight of 1.0.//from  w w w. j ava2s .  c  o  m
 *
 * @param assembly {@link ffx.potential.MolecularAssembly} from which a
 * filename will be determined
 * @param weight the weight of the data
 */
public RealSpaceFile(MolecularAssembly assembly, double weight) {
    String name = removeExtension(assembly.getFile().getPath());

    File tmp = new File(name + ".map");
    if (tmp.exists()) {
        logger.info(" Data file: " + tmp.getName());
        realSpaceFileFilter = new CCP4MapFilter();
    } else {
        logger.severe(" No input data was found.");
        realSpaceFileFilter = null;
    }

    String filenameHolder;
    try {
        Path filePath = Paths.get(tmp.getCanonicalPath());
        Path pwdPath = Paths.get(new File("").getCanonicalPath());
        filenameHolder = pwdPath.relativize(filePath).toString();
    } catch (IOException e) {
        String message = " Relative path to provided data file could not be resolved: using map file name instead.";
        logger.log(Level.WARNING, message, e);
        filenameHolder = tmp.getName();
    }
    this.filename = filenameHolder;
    this.weight = weight;
}

From source file:com.evolveum.midpoint.model.impl.ExtensionSchemaRestService.java

private String computeName(String midpointHome, SchemaDescription description) {
    String path = description.getPath();
    if (path == null) {
        return null;
    }/*from  www.j av a 2 s .c  o  m*/

    File file = new File(path);
    if (!file.exists()) {
        return null;
    }

    File home = new File(midpointHome, "/schema");
    java.nio.file.Path homePath = home.toPath();

    java.nio.file.Path filePath = file.toPath();

    java.nio.file.Path relative = homePath.relativize(filePath);

    return relative.toString();
}

From source file:org.pieShare.pieShareApp.service.fileService.FileServiceBase.java

@Override
public Path relitivizeFilePath(File file) {
    Path pathBase = configuration.getWorkingDir().getAbsoluteFile().toPath();
    Path pathAbsolute = file.getAbsoluteFile().toPath();
    return pathBase.relativize(pathAbsolute);
}