Search in sources :

Example 1 with DirectoryStream

use of org.uberfire.java.nio.file.DirectoryStream in project kie-wb-common by kiegroup.

the class DataModelerServiceHelper method resolvePackages.

public Set<String> resolvePackages(final KieModule project) {
    final Set<String> packages = new HashSet<String>();
    final Path rootPath = Paths.convert(project.getRootPath());
    final String[] subdirs = ModuleResourcePaths.MAIN_SRC_PATH.split("/");
    Path javaDir = rootPath;
    // ensure rout to java files exists
    for (String subdir : subdirs) {
        javaDir = javaDir.resolve(subdir);
        if (!ioService.exists(javaDir)) {
            javaDir = null;
            break;
        }
    }
    if (javaDir == null) {
        // uncommon case
        return packages;
    }
    final String javaDirURI = javaDir.toUri().toString();
    // path to java directory has been calculated, now visit the subdirectories to get the package names.
    final List<Path> childDirectories = new ArrayList<Path>();
    childDirectories.add(javaDir);
    Path subDir;
    while (childDirectories.size() > 0) {
        final DirectoryStream<Path> dirStream = ioService.newDirectoryStream(childDirectories.remove(0), new DirectoryStream.Filter<Path>() {

            @Override
            public boolean accept(final Path entry) throws IOException {
                return Files.isDirectory(entry);
            }
        });
        Iterator<Path> it = dirStream.iterator();
        while (it != null && it.hasNext()) {
            // visit this directory
            subDir = it.next();
            childDirectories.add(subDir);
            // store this package name
            packages.add(getPackagePart(javaDirURI, subDir));
        }
        dirStream.close();
    }
    return packages;
}
Also used : Path(org.uberfire.java.nio.file.Path) ArrayList(java.util.ArrayList) DirectoryStream(org.uberfire.java.nio.file.DirectoryStream) IOException(org.uberfire.java.nio.IOException) HashSet(java.util.HashSet)

Example 2 with DirectoryStream

use of org.uberfire.java.nio.file.DirectoryStream in project kie-wb-common by kiegroup.

the class FileUtils method scan.

private Collection<ScanResult> scan(IOService ioService, Path rootPath, final Collection<String> fileTypes, final boolean recursiveScan, final Map<Path, Path> scannedCache) throws IOException {
    final Collection<ScanResult> results = new ArrayList<ScanResult>();
    final List<Path> childDirectories = new ArrayList<Path>();
    if (rootPath != null) {
        if (Files.isDirectory(rootPath) && !scannedCache.containsKey(rootPath)) {
            scannedCache.put(rootPath, rootPath);
            final DirectoryStream<Path> foundFiles = ioService.newDirectoryStream(rootPath, new DirectoryStream.Filter<Path>() {

                @Override
                public boolean accept(final Path entry) throws IOException {
                    boolean include = false;
                    if (Files.isDirectory(entry) && recursiveScan) {
                        // use this check iteration to additionally remember child directories
                        childDirectories.add(entry);
                    } else {
                        if (fileTypes == null) {
                            include = true;
                        } else {
                            include = isFromType(entry, fileTypes);
                        }
                    }
                    return include;
                }
            });
            if (foundFiles != null) {
                for (Path acceptedFile : foundFiles) {
                    results.add(new ScanResult(acceptedFile));
                }
            }
            // finally
            if (recursiveScan) {
                for (Path child : childDirectories) {
                    results.addAll(scan(ioService, child, fileTypes, recursiveScan, scannedCache));
                }
            }
        }
    }
    return results;
}
Also used : Path(org.uberfire.java.nio.file.Path) DirectoryStream(org.uberfire.java.nio.file.DirectoryStream) IOException(org.uberfire.java.nio.IOException)

Aggregations

IOException (org.uberfire.java.nio.IOException)2 DirectoryStream (org.uberfire.java.nio.file.DirectoryStream)2 Path (org.uberfire.java.nio.file.Path)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1