Example usage for java.nio.file Path resolve

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

Introduction

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

Prototype

default Path resolve(String other) 

Source Link

Document

Converts a given path string to a Path and resolves it against this Path in exactly the manner specified by the #resolve(Path) resolve method.

Usage

From source file:com.github.blindpirate.gogradle.util.IOUtils.java

public static Path ensureDirExistAndWritable(Path base, String relativePath) {
    return ensureDirExistAndWritable(base.resolve(Paths.get(relativePath)));
}

From source file:fr.pilato.elasticsearch.crawler.fs.framework.FsCrawlerUtil.java

/**
 * Reads a mapping from dir/version/type.json file
 *
 * @param dir Directory containing mapping files per major version
 * @param version Elasticsearch major version number (only major digit is kept so for 2.3.4 it will be 2)
 * @param type The expected type (will be expanded to type.json)
 * @return the mapping// w  ww .  j  a  v  a 2  s  .c  o  m
 * @throws IOException If the mapping can not be read
 */
private static String readJsonVersionedFile(Path dir, String version, String type) throws IOException {
    Path file = dir.resolve(version).resolve(type + ".json");
    return new String(Files.readAllBytes(file), "UTF-8");
}

From source file:codes.writeonce.maven.plugins.soy.CompileMojo.java

private static void updateDigest(Path root, MessageDigest sourceDigest, Path soyFilePath) throws IOException {
    sourceDigest.update(soyFilePath.toString().getBytes(TEXT_DIGEST_CHARSET));
    final Path soyFile = root.resolve(soyFilePath);
    sourceDigest.update(Longs.toByteArray(Files.size(soyFile)));
    sourceDigest.update(Longs.toByteArray(Files.getLastModifiedTime(soyFile).toMillis()));
}

From source file:com.spotify.docker.client.CompressedDirectory.java

/**
 * This method creates a gzip tarball of the specified directory. File permissions will be
 * retained. The file will be created in a temporary directory using the {@link
 * Files#createTempFile(String, String, FileAttribute[])} method. The returned object is
 * auto-closeable, and upon closing it, the archive file will be deleted.
 *
 * @param directory the directory to compress
 * @return a Path object representing the compressed directory
 * @throws IOException if the compressed directory could not be created.
 *//*from   w  w  w .j  ava 2 s  .c o m*/
public static CompressedDirectory create(final Path directory) throws IOException {
    final Path file = Files.createTempFile("docker-client-", ".tar.gz");

    final Path dockerIgnorePath = directory.resolve(".dockerignore");
    final ImmutableSet<PathMatcher> ignoreMatchers = parseDockerIgnore(dockerIgnorePath);

    try (final OutputStream fileOut = Files.newOutputStream(file);
            final GzipCompressorOutputStream gzipOut = new GzipCompressorOutputStream(fileOut);
            final TarArchiveOutputStream tarOut = new TarArchiveOutputStream(gzipOut)) {
        tarOut.setLongFileMode(LONGFILE_POSIX);
        tarOut.setBigNumberMode(BIGNUMBER_POSIX);
        Files.walkFileTree(directory, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE,
                new Visitor(directory, ignoreMatchers, tarOut));

    } catch (Throwable t) {
        // If an error occurs, delete temporary file before rethrowing exception.
        try {
            Files.delete(file);
        } catch (IOException e) {
            // So we don't lose track of the reason the file was deleted... might be important
            t.addSuppressed(e);
        }

        throw t;
    }

    return new CompressedDirectory(file);
}

From source file:fr.pilato.elasticsearch.crawler.fs.framework.FsCrawlerUtil.java

/**
 * Copy default resources files which are available as project resources under
 * fr.pilato.elasticsearch.crawler.fs._default package to a given configuration path
 * under a _default sub directory./* w  w  w  .  j a  v  a  2s  .  c  om*/
 * @param configPath The config path which is by default .fscrawler
 * @throws IOException If copying does not work
 */
public static void copyDefaultResources(Path configPath) throws IOException, URISyntaxException {
    Path targetResourceDir = configPath.resolve("_default");

    for (String filename : MAPPING_RESOURCES) {
        Path target = targetResourceDir.resolve(filename);
        if (Files.exists(target)) {
            logger.debug("Mapping [{}] already exists", filename);
        } else {
            logger.debug("Copying [{}]...", filename);
            copyResourceFile(CLASSPATH_RESOURCES_ROOT + filename, target);
        }
    }
}

From source file:de.bps.onyx.plugin.OnyxModule.java

public static ResourceEvaluation isOnyxTest(File file, String filename) {
    ResourceEvaluation eval = new ResourceEvaluation();
    BufferedReader reader = null;
    try {/* ww w  .ja v  a 2 s  . c  o m*/
        ImsManifestFileFilter visitor = new ImsManifestFileFilter();
        Path fPath = PathUtils.visit(file, filename, visitor);
        if (visitor.isValid()) {
            Path qtiPath = fPath.resolve("imsmanifest.xml");
            reader = Files.newBufferedReader(qtiPath, StandardCharsets.UTF_8);
            while (reader.ready()) {
                String l = reader.readLine();
                if (l.indexOf("imsqti_xmlv2p1") != -1 || l.indexOf("imsqti_test_xmlv2p1") != -1
                        || l.indexOf("imsqti_assessment_xmlv2p1") != -1) {
                    eval.setValid(true);
                    break;
                }
            }
        } else {
            eval.setValid(false);
        }
    } catch (NoSuchFileException nsfe) {
        eval.setValid(false);
    } catch (IOException | IllegalArgumentException e) {
        log.error("", e);
        eval.setValid(false);
    } finally {
        IOUtils.closeQuietly(reader);
    }
    return eval;
}

From source file:fr.pilato.elasticsearch.crawler.fs.util.FsCrawlerUtil.java

/**
 * Reads a mapping from dir/version/type.json file
 *
 * @param dir Directory containing mapping files per major version
 * @param version Elasticsearch major version number (only major digit is kept so for 2.3.4 it will be 2)
 * @param type The expected type (will be expanded to type.json)
 * @return the mapping//from w  w w .java 2  s  . co  m
 * @throws URISyntaxException
 * @throws IOException
 */
public static String readMapping(Path dir, String version, String type) throws URISyntaxException, IOException {
    Path file = dir.resolve(version).resolve(type + ".json");
    return new String(Files.readAllBytes(file), "UTF-8");
}

From source file:fr.pilato.elasticsearch.crawler.fs.util.FsCrawlerUtil.java

/**
 * Copy default resources files which are available as project resources under
 * fr.pilato.elasticsearch.crawler.fs._default package to a given configuration path
 * under a _default sub directory./*  w  w w .  j a v  a 2 s . c o m*/
 * @param configPath The config path which is by default .fscrawler
 * @throws IOException If copying does not work
 */
public static void copyDefaultResources(Path configPath) throws IOException, URISyntaxException {
    Path targetResourceDir = configPath.resolve("_default");

    for (String filename : MAPPING_RESOURCES) {
        Path target = targetResourceDir.resolve(filename);
        if (Files.exists(target)) {
            logger.debug("Mapping [{}] already exsits", filename);
        } else {
            logger.debug("Copying [{}]...", filename);
            copyResourceFile(CLASSPATH_RESOURCES_ROOT + filename, target);
        }
    }
}

From source file:fr.pilato.elasticsearch.crawler.fs.framework.FsCrawlerUtil.java

/**
 * Reads a mapping from config/_default/version/type.json file
 *
 * @param config Root dir where we can find the configuration (default to ~/.fscrawler)
 * @param version Elasticsearch major version number (only major digit is kept so for 2.3.4 it will be 2)
 * @param type The expected type (will be expanded to type.json)
 * @return the mapping/*from w  ww  .  j av a  2 s.c o  m*/
 * @throws URISyntaxException If URI is malformed
 * @throws IOException If the mapping can not be read
 */
public static String readDefaultJsonVersionedFile(Path config, String version, String type)
        throws URISyntaxException, IOException {
    Path defaultConfigDir = config.resolve("_default");
    try {
        return readJsonVersionedFile(defaultConfigDir, version, type);
    } catch (NoSuchFileException e) {
        throw new IllegalArgumentException(
                "Mapping file " + type + ".json does not exist for elasticsearch version " + version + " in ["
                        + defaultConfigDir + "] dir");
    }
}

From source file:net.minecrell.quartz.launch.mappings.MappingsLoader.java

public static Mappings load(Logger logger) throws IOException {
    URI source;//from w  w w. j  av  a2s.  co  m
    try {
        source = requireNonNull(Mapping.class.getProtectionDomain().getCodeSource(),
                "Unable to find class source").getLocation().toURI();
    } catch (URISyntaxException e) {
        throw new IOException("Failed to find class source", e);
    }

    Path location = Paths.get(source);
    logger.debug("Mappings location: {}", location);

    List<ClassNode> mappingClasses = new ArrayList<>();

    // Load the classes from the source
    if (Files.isDirectory(location)) {
        // We're probably in development environment or something similar
        // Search for the class files
        Files.walkFileTree(location.resolve(PACKAGE), new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (file.getFileName().toString().endsWith(".class")) {
                    try (InputStream in = Files.newInputStream(file)) {
                        ClassNode classNode = MappingsParser.loadClassStructure(in);
                        mappingClasses.add(classNode);
                    }
                }

                return FileVisitResult.CONTINUE;
            }
        });
    } else {
        // Go through the JAR file
        try (ZipFile zip = new ZipFile(location.toFile())) {
            Enumeration<? extends ZipEntry> entries = zip.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                String name = StringUtils.removeStart(entry.getName(), MAPPINGS_DIR);
                if (entry.isDirectory() || !name.endsWith(".class") || !name.startsWith(PACKAGE_PREFIX)) {
                    continue;
                }

                // Ok, we found something
                try (InputStream in = zip.getInputStream(entry)) {
                    ClassNode classNode = MappingsParser.loadClassStructure(in);
                    mappingClasses.add(classNode);
                }
            }
        }
    }

    return new Mappings(mappingClasses);
}