Example usage for java.nio.file FileVisitResult CONTINUE

List of usage examples for java.nio.file FileVisitResult CONTINUE

Introduction

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

Prototype

FileVisitResult CONTINUE

To view the source code for java.nio.file FileVisitResult CONTINUE.

Click Source Link

Document

Continue.

Usage

From source file:com.gitpitch.services.DiskService.java

public void copyDirectory(Path source, Path dest) {

    log.debug("copyDirectory: source={}, dest={}", source, dest);

    try {//from w w w. ja  va 2 s.  c  om

        Files.walkFileTree(source, new SimpleFileVisitor<Path>() {

            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {

                Path relative = source.relativize(dir);
                Path visitPath = Paths.get(dest.toString(), relative.toString());
                ensure(visitPath);
                return FileVisitResult.CONTINUE;
            }

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

                Path copyTarget = Paths.get(dest.toString(), source.relativize(file).toString());
                if (!file.getFileName().toString().matches("\\..*")
                        && !copyTarget.getFileName().toString().matches("\\..*")) {
                    Files.copy(file, copyTarget);
                }
                return FileVisitResult.CONTINUE;
            }
        });

    } catch (Exception cex) {
        log.warn("copyDirectory: source={}, dest={}, ex={}", source, dest, cex);
    }
}

From source file:sadl.modellearner.rtiplus.SimplePDRTALearner.java

private Path initStepsDir(String dir) throws IOException {

    if (dir != null) {
        final Path p = Paths.get(dir, "steps");
        if (Files.exists(directory) && Files.isDirectory(directory)) {
            Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
                @Override//  www.  j  a  v a 2  s .co  m
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    Files.delete(file);
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult postVisitDirectory(Path d, IOException e) throws IOException {
                    if (e == null) {
                        Files.delete(d);
                        return FileVisitResult.CONTINUE;
                    } else {
                        throw e;
                    }
                }
            });
        }
        Files.createDirectories(directory);
        return p;
    } else {
        return null;
    }
}

From source file:com.nridge.connector.fs.con_fs.core.FileCrawler.java

/**
 * Invoked for a directory before entries in the directory are visited.
 * Unless overridden, this method returns {@link java.nio.file.FileVisitResult#CONTINUE}
 *
 * @param aDirectory Directory instance.
 * @param aFileAttributes File attribute instance.
 *///from w  ww .  j  a va 2 s.c o  m
@Override
public FileVisitResult preVisitDirectory(Path aDirectory, BasicFileAttributes aFileAttributes)
        throws IOException {
    Logger appLogger = mAppMgr.getLogger(this, "preVisitDirectory");

    if (mAppMgr.isAlive()) {
        String pathName = aDirectory.toAbsolutePath().toString();
        if (mCrawlFollow.isMatchedNormalized(pathName)) {
            appLogger.debug(String.format("Following Path: %s", pathName));
            return FileVisitResult.CONTINUE;
        } else {
            appLogger.debug(String.format("Skipping Path: %s", pathName));
            return FileVisitResult.SKIP_SUBTREE;
        }
    } else
        return FileVisitResult.TERMINATE;
}

From source file:com.marklogic.hub.RestAssetLoader.java

@Override
public FileVisitResult visitFileFailed(Path path, IOException exception) throws IOException {
    return FileVisitResult.CONTINUE;
}

From source file:io.anserini.index.IndexClueWeb09b.java

static List<Path> discoverWarcFiles(Path p) {

    final List<Path> warcFiles = new ArrayList<>();

    FileVisitor<Path> fv = new SimpleFileVisitor<Path>() {
        @Override/*from w w w  .j a  va 2s  .c  om*/
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {

            Path name = file.getFileName();
            if (name != null && matcher.matches(name))
                warcFiles.add(file);
            return FileVisitResult.CONTINUE;
        }
    };

    try {
        Files.walkFileTree(p, fv);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return warcFiles;
}

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

@Override
public ResourceDTO handleZip(Resource zipResource) {

    final Path zipPath;
    try {//from   ww w .j  ava2  s.c  o 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:org.fao.geonet.kernel.harvest.harvester.localfilesystem.LocalFsHarvesterFileVisitor.java

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
    if (cancelMonitor.get()) {
        return FileVisitResult.TERMINATE;
    }/*from   w w  w .  j  a  va  2s  . c o m*/

    try {
        if (file != null && file.getFileName() != null && file.getFileName().toString() != null
                && (file.getFileName().toString().endsWith(".xml")
                        || MEFLib.isValidArchiveExtensionForMEF(file.getFileName().toString()))) {

            result.totalMetadata++;

            if (LOGGER.isDebugEnabled() && result.totalMetadata % 1000 == 0) {
                long elapsedTime = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - startTime);
                LOGGER.debug(result.totalMetadata + "records inserted in " + elapsedTime + " s ("
                        + result.totalMetadata / elapsedTime + " records/s).");
            }

            Path filePath = file.toAbsolutePath().normalize();
            if (MEFLib.isValidArchiveExtensionForMEF(file.getFileName().toString())) {
                processMef(file, filePath);
                return FileVisitResult.CONTINUE;
            }

            Element xml;
            try {
                LOGGER.debug("reading file: " + filePath);
                xml = Xml.loadFile(file);
            } catch (JDOMException e) { // JDOM problem
                LOGGER.debug("Error loading XML from file " + filePath + ", ignoring");
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.error(e);
                }
                result.badFormat++;
                return FileVisitResult.CONTINUE; // skip this one
            } catch (Throwable e) { // some other error
                LOGGER.debug("Error retrieving XML from file  " + filePath + ", ignoring");
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.error(e);
                }
                result.unretrievable++;
                return FileVisitResult.CONTINUE; // skip this one
            }

            // transform using importxslt if not none
            if (transformIt) {
                try {
                    xml = Xml.transform(xml, thisXslt);
                } catch (Exception e) {
                    LOGGER.debug("Cannot transform XML from file " + filePath + ", ignoring. Error was: "
                            + e.getMessage());
                    result.badFormat++;
                    return FileVisitResult.CONTINUE; // skip this one
                }
            }

            String schema = null;
            try {
                schema = dataMan.autodetectSchema(xml, null);
            } catch (Exception e) {
                result.unknownSchema++;
            }
            if (schema == null) {
                return FileVisitResult.CONTINUE;
            }

            try {
                params.getValidate().validate(dataMan, context, xml);
            } catch (Exception e) {
                LOGGER.debug("Cannot validate XML from file " + filePath + ", ignoring. Error was: "
                        + e.getMessage());
                result.doesNotValidate++;
                return FileVisitResult.CONTINUE; // skip this one
            }

            String uuid = getUuidFromFile(xml, filePath, schema);
            if (uuid == null || uuid.equals("")) {
                result.badFormat++;
                return FileVisitResult.CONTINUE;
            }

            String id = dataMan.getMetadataId(uuid);
            String changeDate = new ISODate(System.currentTimeMillis(), false).getDateAndTime();
            if (id == null) {
                // For new record change date will be the time of metadata xml date change or the date when
                // the record was harvested (if can't be obtained the metadata xml date change)
                String createDate;
                // or the last modified date of the file
                if (params.checkFileLastModifiedForUpdate) {
                    createDate = new ISODate(Files.getLastModifiedTime(file).toMillis(), false)
                            .getDateAndTime();
                } else {
                    try {
                        createDate = dataMan.extractDateModified(schema, xml);
                    } catch (Exception ex) {
                        LOGGER.error(
                                "LocalFilesystemHarvester - addMetadata - can't get metadata modified date for metadata uuid= "
                                        + uuid + " using current date for modified date");
                        createDate = new ISODate().toString();
                    }
                }

                LOGGER.debug("adding new metadata");
                id = addMetadata(xml, schema, uuid, createDate);
            } else {
                // Check last modified date of the file with the record change date
                // to check if an update is required
                if (params.checkFileLastModifiedForUpdate) {
                    Date fileDate = new Date(Files.getLastModifiedTime(file).toMillis());

                    final AbstractMetadata metadata = repo.findOne(id);
                    ISODate modified = new ISODate();
                    if (metadata != null && metadata.getDataInfo() != null) {
                        modified = metadata.getDataInfo().getChangeDate();
                    }

                    Date recordDate = modified.toDate();

                    changeDate = new ISODate(fileDate.getTime(), false).getDateAndTime();

                    LOGGER.debug(" File date is: " + filePath + "filePath / record date is: " + modified);

                    if (DateUtils.truncate(recordDate, Calendar.SECOND)
                            .before(DateUtils.truncate(fileDate, Calendar.SECOND))) {
                        LOGGER.debug("  Db record is older than file. Updating record with id: " + id);
                        updateMedata(xml, id, changeDate);
                    } else {
                        LOGGER.debug(
                                "  Db record is not older than last modified date of file. No need for update.");
                        result.unchangedMetadata++;
                    }
                } else {
                    id = dataMan.getMetadataId(uuid);
                    if (id == null) {
                        // For new record change date will be the time of metadata xml date change or the date when
                        // the record was harvested (if can't be obtained the metadata xml date change)
                        String createDate;
                        // or the last modified date of the file
                        if (params.checkFileLastModifiedForUpdate) {
                            createDate = new ISODate(Files.getLastModifiedTime(file).toMillis(), false)
                                    .getDateAndTime();
                        } else {
                            try {
                                createDate = dataMan.extractDateModified(schema, xml);
                            } catch (Exception ex) {
                                LOGGER.error(
                                        "LocalFilesystemHarvester - addMetadata - can't get metadata modified date for metadata uuid= "
                                                +

                                                uuid + ", using current date for modified date");
                                createDate = new ISODate().toString();
                            }
                        }

                        LOGGER.debug("adding new metadata");
                        id = harvester.addMetadata(xml, uuid, schema, localGroups, localCateg, createDate,
                                aligner, false);
                        listOfRecordsToIndex.add(Integer.valueOf(id));
                        result.addedMetadata++;
                    } else {
                        // Check last modified date of the file with the record change date
                        // to check if an update is required
                        if (params.checkFileLastModifiedForUpdate) {
                            Date fileDate = new Date(Files.getLastModifiedTime(file).toMillis());

                            final AbstractMetadata metadata = repo.findOne(id);
                            final ISODate modified;
                            if (metadata != null && metadata.getDataInfo() != null) {
                                modified = metadata.getDataInfo().getChangeDate();
                            } else {
                                modified = new ISODate();
                            }

                            Date recordDate = modified.toDate();

                            changeDate = new ISODate(fileDate.getTime(), false).getDateAndTime();

                            LOGGER.debug(
                                    " File date is: " + fileDate.toString() + " / record date is: " + modified);

                            if (DateUtils.truncate(recordDate, Calendar.SECOND)
                                    .before(DateUtils.truncate(fileDate, Calendar.SECOND))) {
                                LOGGER.debug("  Db record is older than file. Updating record with id: " + id);
                                harvester.updateMetadata(xml, id, localGroups, localCateg, changeDate, aligner);
                                listOfRecordsToIndex.add(Integer.valueOf(id));
                                result.updatedMetadata++;
                            } else {
                                LOGGER.debug(
                                        "  Db record is not older than last modified date of file. No need for update.");
                                result.unchangedMetadata++;
                            }
                        } else {
                            LOGGER.debug("  updating existing metadata, id is: " + id);

                            try {
                                changeDate = dataMan.extractDateModified(schema, xml);
                            } catch (Exception ex) {
                                LOGGER.error(
                                        "LocalFilesystemHarvester - updateMetadata - can't get metadata modified date for "
                                                + "metadata id= " + id
                                                + ", using current date for modified date");
                                changeDate = new ISODate().toString();
                            }

                            harvester.updateMetadata(xml, id, localGroups, localCateg, changeDate, aligner);
                            listOfRecordsToIndex.add(Integer.valueOf(id));
                            result.updatedMetadata++;
                        }
                    }

                    updateMedata(xml, id, changeDate);
                }
            }
            listOfRecords.add(Integer.valueOf(id));
        }
    } catch (Throwable e) {
        LOGGER.error("An error occurred while harvesting a local file:{}. Error is: " + e.getMessage());
    }
    return FileVisitResult.CONTINUE;
}

From source file:fr.ortolang.diffusion.client.cmd.CopyCommand.java

private void copy(Path localPath, String workspace, String remotePath) {
    try {//from www . j a  v a 2s  .  com
        Files.walkFileTree(localPath, new FileVisitor<Path>() {

            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                switch (mode) {
                case "objects":
                    String remoteDir = remotePath + localPath.getParent().relativize(dir).toString();
                    System.out.println("Copying dir " + dir + " to " + workspace + ":" + remoteDir);
                    try {
                        client.writeCollection(workspace, remoteDir, "");
                    } catch (OrtolangClientException | OrtolangClientAccountException e) {
                        e.printStackTrace();
                        errors.append("-> Unable to copy dir ").append(dir).append(" to ").append(remoteDir)
                                .append("\r\n");
                        return FileVisitResult.TERMINATE;
                    }
                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                switch (mode) {
                case "objects":
                    String remoteFile = remotePath + localPath.getParent().relativize(file).toString();
                    System.out.println("Copying file " + file + " to " + workspace + ":" + remoteFile);
                    try {
                        client.writeDataObject(workspace, remoteFile, "", file.toFile(), null);
                    } catch (OrtolangClientException | OrtolangClientAccountException e) {
                        e.printStackTrace();
                        errors.append("-> Unable to copy file ").append(file).append(" to ").append(remoteFile)
                                .append("\r\n");
                        return FileVisitResult.TERMINATE;
                    }
                    break;
                case "metadata":
                    String remoteDir = remotePath
                            + localPath.getParent().relativize(file).getParent().toString();
                    System.out.println("Creating metadata file " + file + " to " + workspace + ":" + remoteDir);
                    String name = file.getFileName().toString();
                    try {
                        client.writeMetaData(workspace, remoteDir, name, null, file.toFile());
                    } catch (OrtolangClientException | OrtolangClientAccountException e) {
                        e.printStackTrace();
                        errors.append("-> Unable to copy file ").append(file).append(" to ").append(remoteDir)
                                .append("\r\n");
                        return FileVisitResult.TERMINATE;
                    }
                    break;
                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                return FileVisitResult.CONTINUE;
            }

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

        });
    } catch (IOException e) {
        System.out.println("Unable to walk file tree: " + e.getMessage());
    }
}

From source file:fr.ortolang.diffusion.client.cmd.CheckBagCommand.java

private void checkSnapshotMetadata(Path root) {
    Path metadata = Paths.get(root.toString(), "metadata");
    try {//from w  w w .j  a va 2  s .  co m
        Files.walkFileTree(metadata, new FileVisitor<Path>() {

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

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Path target = Paths.get(root.toString(), "objects",
                        metadata.relativize(file.getParent()).toString());
                if (!Files.exists(target)) {
                    errors.append("-> unexisting target for metadata: ").append(file).append("\r\n");
                    if (fix) {
                        try {
                            Files.delete(file);
                            fixed.append("-> deleted metadata: ").append(file).append("\r\n");
                        } catch (IOException e) {
                            errors.append("-> unable to fix: ").append(e.getMessage()).append("\r\n");
                        }
                    }
                } else if (file.endsWith("ortolang-item-json")) {
                    checkOrtolangItemJson(file);
                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                return FileVisitResult.CONTINUE;
            }

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

        });
    } catch (IOException e) {
        System.out.println("Unable to walk file tree: " + e.getMessage());
    }
}

From source file:net.sourceforge.pmd.cache.AbstractAnalysisCache.java

private URL[] getClassPathEntries() {
    final String classpath = System.getProperty("java.class.path");
    final String[] classpathEntries = classpath.split(File.pathSeparator);
    final List<URL> entries = new ArrayList<>();

    final SimpleFileVisitor<Path> fileVisitor = new SimpleFileVisitor<Path>() {
        @Override//from w w  w  . ja  va  2s. c  o m
        public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
            if (!attrs.isSymbolicLink()) { // Broken link that can't be followed
                entries.add(file.toUri().toURL());
            }
            return FileVisitResult.CONTINUE;
        }
    };
    final SimpleFileVisitor<Path> jarFileVisitor = new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
            String extension = FilenameUtils.getExtension(file.toString());
            if ("jar".equalsIgnoreCase(extension)) {
                fileVisitor.visitFile(file, attrs);
            }
            return FileVisitResult.CONTINUE;
        }
    };

    try {
        for (final String entry : classpathEntries) {
            final File f = new File(entry);
            if (isClassPathWildcard(entry)) {
                Files.walkFileTree(new File(entry.substring(0, entry.length() - 1)).toPath(),
                        EnumSet.of(FileVisitOption.FOLLOW_LINKS), 1, jarFileVisitor);
            } else if (f.isFile()) {
                entries.add(f.toURI().toURL());
            } else {
                Files.walkFileTree(f.toPath(), EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE,
                        fileVisitor);
            }
        }
    } catch (final IOException e) {
        LOG.log(Level.SEVERE, "Incremental analysis can't check execution classpath contents", e);
        throw new RuntimeException(e);
    }

    return entries.toArray(new URL[0]);
}