Example usage for java.nio.file SimpleFileVisitor SimpleFileVisitor

List of usage examples for java.nio.file SimpleFileVisitor SimpleFileVisitor

Introduction

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

Prototype

protected SimpleFileVisitor() 

Source Link

Document

Initializes a new instance of this class.

Usage

From source file:io.liveoak.testtools.AbstractTestCase.java

@AfterClass
public static void tearDownMongo() throws IOException {
    if (mongoLauncher != null) {
        mongoLauncher.stopMongo();/*  w  ww  .  j  av a 2  s  .  c om*/

        // wait for it to stop
        long start = System.currentTimeMillis();
        while (mongoLauncher.serverRunning(mongoHost, mongoPort, (e) -> {
            if (System.currentTimeMillis() - start > 120000)
                throw new RuntimeException(e);
        })) {

            if (System.currentTimeMillis() - start > 120000) {
                throw new RuntimeException("mongod process still seems to be running (2m timeout)");
            }
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                throw new RuntimeException("Interrupted!");
            }
        }

        // now delete the data dir except log file
        Files.walkFileTree(new File(mongoLauncher.getDbPath()).toPath(), new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (!file.startsWith(mongoLauncher.getLogPath())) {
                    Files.delete(file);
                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                try {
                    Files.delete(dir);
                } catch (DirectoryNotEmptyException ignored) {
                }
                return FileVisitResult.CONTINUE;
            }
        });

        mongoLauncher = null;
    }
}

From source file:org.apdplat.superword.tools.PdfParser.java

public static void parseZip(String zipFile) {
    long start = System.currentTimeMillis();
    LOGGER.info("?ZIP" + zipFile);
    try (FileSystem fs = FileSystems.newFileSystem(Paths.get(zipFile), WordClassifier.class.getClassLoader())) {
        for (Path path : fs.getRootDirectories()) {
            LOGGER.info("?" + path);
            Files.walkFileTree(path, new SimpleFileVisitor<Path>() {

                @Override/*from   w w  w  .  j a v a 2  s.  co  m*/
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    LOGGER.info("?" + file);
                    // ?
                    Path temp = Paths.get("target/it-software-domain-temp.pdf");
                    Files.copy(file, temp, StandardCopyOption.REPLACE_EXISTING);
                    parseFile(temp.toFile().getAbsolutePath());
                    return FileVisitResult.CONTINUE;
                }

            });
        }
    } catch (Exception e) {
        LOGGER.error("?", e);
    }
    long cost = System.currentTimeMillis() - start;
    LOGGER.info("?" + cost + "");
}

From source file:gov.vha.isaac.rf2.filter.RF2Filter.java

@Override
public void execute() throws MojoExecutionException {
    if (!inputDirectory.exists() || !inputDirectory.isDirectory()) {
        throw new MojoExecutionException("Path doesn't exist or isn't a folder: " + inputDirectory);
    }//from  w w  w . j  av  a 2  s .c o m

    if (module == null) {
        throw new MojoExecutionException("You must provide a module or namespace for filtering");
    }

    moduleStrings_.add(module + "");

    outputDirectory.mkdirs();
    File temp = new File(outputDirectory, inputDirectory.getName());
    temp.mkdirs();

    Path source = inputDirectory.toPath();
    Path target = temp.toPath();
    try {
        getLog().info("Reading from " + inputDirectory.getAbsolutePath());
        getLog().info("Writing to " + outputDirectory.getCanonicalPath());

        summary_.append("This content was filtered by an RF2 filter tool.  The parameters were module: "
                + module + " software version: " + converterVersion);
        summary_.append("\r\n\r\n");

        getLog().info("Checking for nested child modules");

        //look in sct2_Relationship_ files, find anything where the 6th column (destinationId) is the 
        //starting module ID concept - and add that sourceId (5th column) to our list of modules to extract
        Files.walkFileTree(source, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (file.toFile().getName().startsWith("sct2_Relationship_")) {
                    //don't look for quotes, the data is bad, and has floating instances of '"' all by itself
                    CSVReader csvReader = new CSVReader(
                            new InputStreamReader(new FileInputStream(file.toFile())), '\t',
                            CSVParser.NULL_CHARACTER);
                    String[] line = csvReader.readNext();
                    if (!line[4].equals("sourceId") || !line[5].equals("destinationId")) {
                        csvReader.close();
                        throw new IOException("Unexpected error looking for nested modules");
                    }
                    line = csvReader.readNext();
                    while (line != null) {
                        if (line[5].equals(moduleStrings_.get(0))) {
                            moduleStrings_.add(line[4]);
                        }
                        line = csvReader.readNext();
                    }
                    csvReader.close();
                }
                return FileVisitResult.CONTINUE;
            }
        });

        log("Full module list (including detected nested modules: "
                + Arrays.toString(moduleStrings_.toArray(new String[moduleStrings_.size()])));

        Files.walkFileTree(source, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                Path targetdir = target.resolve(source.relativize(dir));
                try {
                    //this just creates the sub-directory in the target
                    Files.copy(dir, targetdir);
                } catch (FileAlreadyExistsException e) {
                    if (!Files.isDirectory(targetdir))
                        throw e;
                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                handleFile(file, target.resolve(source.relativize(file)));
                return FileVisitResult.CONTINUE;
            }
        });

        Files.write(new File(temp, "FilterInfo.txt").toPath(), summary_.toString().getBytes(),
                StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
    } catch (IOException e) {
        throw new MojoExecutionException("Failure", e);
    }

    getLog().info("Filter Complete");

}

From source file:org.apdplat.superword.tools.WordClassifierForYouDao.java

public static void parseZip(String zipFile) {
    LOGGER.info("?ZIP" + zipFile);
    try (FileSystem fs = FileSystems.newFileSystem(Paths.get(zipFile),
            WordClassifierForYouDao.class.getClassLoader())) {
        for (Path path : fs.getRootDirectories()) {
            LOGGER.info("?" + path);
            Files.walkFileTree(path, new SimpleFileVisitor<Path>() {

                @Override// ww w. jav a 2s  . c om
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    LOGGER.info("?" + file);
                    // ?
                    Path temp = Paths.get("target/origin-html-temp.txt");
                    Files.copy(file, temp, StandardCopyOption.REPLACE_EXISTING);
                    parseFile(temp.toFile().getAbsolutePath());
                    return FileVisitResult.CONTINUE;
                }

            });
        }
    } catch (Exception e) {
        LOGGER.error("?", e);
    }
}

From source file:org.zaproxy.VerifyScripts.java

private static void readFiles() throws Exception {
    Optional<String> path = Arrays.stream(System.getProperty("java.class.path").split(File.pathSeparator))
            .filter(e -> e.endsWith("/scripts")).findFirst();
    assertThat(path).as("The scripts directory was not found on the classpath.").isPresent();

    List<Path> unexpectedFiles = new ArrayList<>();
    MutableInt depth = new MutableInt();

    files = new ArrayList<>();
    Files.walkFileTree(Paths.get(path.get()), new SimpleFileVisitor<Path>() {

        @Override//from ww  w  .j a  v  a2  s.  c  om
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            depth.increment();
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
            if (depth.intValue() != SCRIPT_TYPE_DIR_DEPTH) {
                unexpectedFiles.add(file);
                return FileVisitResult.CONTINUE;
            }

            if (!isExpectedNonScriptFile(file)) {
                files.add(file);
            }
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            depth.decrement();
            return FileVisitResult.CONTINUE;
        }
    });

    assertThat(unexpectedFiles).as("Files found not in a script type directory.").isEmpty();

    Collections.sort(files);
}

From source file:com.ibm.streamsx.topology.internal.context.remote.ZippedToolkitRemoteContext.java

private static void addAllToZippedArchive(Map<Path, String> starts, Path zipFilePath) throws IOException {
    try (ZipArchiveOutputStream zos = new ZipArchiveOutputStream(zipFilePath.toFile())) {
        for (Path start : starts.keySet()) {
            final String rootEntryName = starts.get(start);
            Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    // Skip pyc files.
                    if (file.getFileName().toString().endsWith(".pyc"))
                        return FileVisitResult.CONTINUE;

                    String entryName = rootEntryName;
                    String relativePath = start.relativize(file).toString();
                    // If empty, file is the start file.
                    if (!relativePath.isEmpty()) {
                        entryName = entryName + "/" + relativePath;
                    }//from w  w w  . j a  v  a 2 s . c  om
                    // Zip uses forward slashes
                    entryName = entryName.replace(File.separatorChar, '/');

                    ZipArchiveEntry entry = new ZipArchiveEntry(file.toFile(), entryName);
                    if (Files.isExecutable(file))
                        entry.setUnixMode(0100770);
                    else
                        entry.setUnixMode(0100660);

                    zos.putArchiveEntry(entry);
                    Files.copy(file, zos);
                    zos.closeArchiveEntry();
                    return FileVisitResult.CONTINUE;
                }

                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                        throws IOException {
                    final String dirName = dir.getFileName().toString();
                    // Don't include pyc files or .toolkit 
                    if (dirName.equals("__pycache__"))
                        return FileVisitResult.SKIP_SUBTREE;

                    ZipArchiveEntry dirEntry = new ZipArchiveEntry(dir.toFile(), rootEntryName + "/"
                            + start.relativize(dir).toString().replace(File.separatorChar, '/') + "/");
                    zos.putArchiveEntry(dirEntry);
                    zos.closeArchiveEntry();
                    return FileVisitResult.CONTINUE;
                }
            });
        }
    }
}

From source file:com.sastix.cms.server.services.content.impl.ZipHandlerServiceImpl.java

@Override
public ResourceDTO handleZip(Resource zipResource) {

    final Path zipPath;
    try {//from   w  ww.  j  a  v a  2 s . co  m
        zipPath = hashedDirectoryService.getDataByURI(zipResource.getUri(), zipResource.getResourceTenantId());
    } catch (IOException | URISyntaxException e) {
        throw new ResourceAccessError(e.getMessage());
    }

    FileSystem zipfs = null;
    try {
        zipfs = FileSystems.newFileSystem(zipPath, null);
        final Path root = zipfs.getPath("/");
        final int maxDepth = 1;

        // Search for specific files.
        final Map<String, Path> map = Files
                .find(root, maxDepth, (path_,
                        attr) -> path_.getFileName() != null && (path_.toString().endsWith(METADATA_JSON_FILE)
                                || path_.toString().endsWith(METADATA_XML_FILE)))
                .collect(Collectors.toMap(p -> p.toAbsolutePath().toString().substring(1), p -> p));

        final String zipType;
        final String startPage;

        // Check if it is a cms file
        if (map.containsKey(METADATA_JSON_FILE)) {
            zipType = METADATA_JSON_FILE;
            LOG.info("Found CMS Metadata File " + map.get(zipType).toString());
            startPage = findStartPage(map.get(zipType));
            // Check if it is a Scrom file
        } else if (map.containsKey(METADATA_XML_FILE)) {
            zipType = METADATA_XML_FILE;
            LOG.info("Found CMS Metadata File " + map.get(zipType).toString());
            startPage = findScormStartPage(map.get(zipType));

        } else {
            throw new ResourceAccessError("Zip " + zipResource.getName() + " is not supported. "
                    + METADATA_JSON_FILE + " and " + METADATA_XML_FILE + " are missing");
        }

        LOG.trace(startPage);

        final List<ResourceDTO> resourceDTOs = new LinkedList<>();

        /* Path inside ZIP File */
        Files.walkFileTree(root, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {

                final String parentContext = zipResource.getUri().split("-")[0] + "-"
                        + zipResource.getResourceTenantId();
                final CreateResourceDTO createResourceDTO = new CreateResourceDTO();
                createResourceDTO.setResourceMediaType(tika.detect(file.toString()));
                createResourceDTO.setResourceAuthor(zipResource.getAuthor());
                createResourceDTO.setResourceExternalURI(file.toUri().toString());
                createResourceDTO.setResourceName(file.toString().substring(1));
                createResourceDTO.setResourceTenantId(zipResource.getResourceTenantId());

                final Resource resource = resourceService.insertChildResource(createResourceDTO, parentContext,
                        zipResource);

                distributedCacheService.cacheIt(resource.getUri(), resource.getResourceTenantId());

                if (file.toString().substring(1).equals(startPage)) {
                    resourceDTOs.add(0, crs.convertToDTO(resource));
                } else {
                    resourceDTOs.add(crs.convertToDTO(resource));
                }

                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                return FileVisitResult.CONTINUE;
            }
        });

        final ResourceDTO parentResourceDto = resourceDTOs.remove(0);
        parentResourceDto.setResourcesList(resourceDTOs);
        return parentResourceDto;

    } catch (IOException e) {
        throw new ResourceAccessError("Error while analyzing " + zipResource.toString());
    } finally {
        if (zipfs != null && zipfs.isOpen()) {
            try {
                LOG.info("Closing FileSystem");
                zipfs.close();
            } catch (IOException e) {
                LOG.error(e.getMessage());
                e.printStackTrace();
                throw new ResourceAccessError("Error while analyzing " + zipResource.toString());
            }
        }
    }
}

From source file:cz.etnetera.reesmo.writer.storage.RestApiStorage.java

protected void addResultAttachment(final Result result, Object attachment) throws StorageException {
    File file = null;//from   ww w. java2 s .  co  m
    String path = null;
    String contentType = null;
    if (attachment instanceof File) {
        file = (File) attachment;
    } else if (attachment instanceof ExtendedFile) {
        ExtendedFile fileWithPath = (ExtendedFile) attachment;
        file = fileWithPath.getFile();
        path = fileWithPath.getPath();
        contentType = fileWithPath.getContentType();
    } else {
        throw new StorageException("Unsupported attachment type " + attachment.getClass());
    }

    if (path != null) {
        path = path.replaceAll("^/+", "").replaceAll("/+$", "");
    }

    if (file.isDirectory()) {
        Path root = file.toPath();
        final String rootPath = path == null ? file.getName() : path;
        try {
            Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    FileVisitResult res = super.visitFile(file, attrs);
                    String relativePath = rootPath + "/" + root.relativize(file).normalize().toString();
                    try {
                        addResultAttachment(result, ExtendedFile.withPath(file.toFile(), relativePath));
                    } catch (StorageException e) {
                        throw new IOException(e);
                    }
                    return res;
                }
            });
        } catch (IOException e) {
            throw new StorageException(e);
        }
        // directory is not stored, just paths
        return;
    }

    MultipartBody body = Unirest
            .post(getUrl(METHOD_RESULT_ATTACHMENT_CREATE).replace("{resultId}", result.getId()))
            .basicAuth(username, password).header("Accept", "application/json").field("file", file);
    if (path != null) {
        body.field("path", path);
    }
    if (contentType != null) {
        body.field("contentType", contentType);
    }

    HttpResponse<String> response;
    try {
        response = body.asString();
    } catch (UnirestException e) {
        throw new StorageException("Unable to store result attachment", e);
    }

    if (response.getStatus() != 200) {
        throw new StorageException("Wrong status code when storing result attachment " + response.getStatus());
    }

    ResultAttachment resultAttachment = null;
    try {
        resultAttachment = new ObjectMapper().readValue(response.getBody(), ResultAttachment.class);
    } catch (UnsupportedOperationException | IOException e) {
        throw new StorageException("Unable to parse result attachment from response", e);
    }

    getLogger().info("Result attachment stored " + resultAttachment.getPath() + " " + resultAttachment.getId());
}

From source file:io.syndesis.project.converter.DefaultProjectGeneratorTest.java

@After
public void tearDown() throws Exception {
    if (runtimeDir != null) {
        Files.walkFileTree(runtimeDir, new SimpleFileVisitor<Path>() {
            @Override//from   w w  w  .  jav a 2s.c  o m
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Files.delete(file);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                Files.delete(dir);
                return FileVisitResult.CONTINUE;
            }
        });
    }
}

From source file:com.expedia.tesla.compiler.Util.java

/**
 * Expand glob file patterns to path strings. Any path element that is not a glob pattern will be keep as it is.
 * //  ww w .j av a  2 s .  com
 * @param pathOrPatterns
 *       glob patterns.
 * 
 * @return
 *       The expanded paths.
 * 
 * @throws IOException
 *       On IO errors.
 */
public static Collection<String> expandWildcard(Collection<String> pathOrPatterns) throws IOException {
    final List<String> files = new ArrayList<String>();
    final List<PathMatcher> matchers = new ArrayList<PathMatcher>();
    for (String pattern : pathOrPatterns) {
        if (pattern.contains("*") || pattern.contains("?")) {
            PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern);
            matchers.add(matcher);
        } else {
            files.add(pattern);
        }
    }

    if (!matchers.isEmpty()) {
        Files.walkFileTree(new File(System.getProperty("user.dir")).toPath(), new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attr) {
                for (PathMatcher matcher : matchers) {
                    if (matcher.matches(file)) {
                        files.add(file.toString());
                    }
                }
                return FileVisitResult.CONTINUE;
            }
        });
    }

    return files;
}