Example usage for org.eclipse.jdt.internal.core.search.indexing IndexManager addSource

List of usage examples for org.eclipse.jdt.internal.core.search.indexing IndexManager addSource

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.core.search.indexing IndexManager addSource.

Prototype

public void addSource(IFile resource, IPath containerPath, SourceElementParser parser) 

Source Link

Document

Trigger addition of a resource to an index Note: the actual operation is performed in background

Usage

From source file:com.codenvy.ide.ext.java.server.internal.core.search.indexing.AddFolderToIndex.java

License:Open Source License

public boolean execute(IProgressMonitor progressMonitor) {

    if (this.isCancelled || progressMonitor != null && progressMonitor.isCanceled())
        return true;
    //      if (!this.project.isAccessible()) return true; // nothing to do
    //      IResource folder = this.project.getParent().findMember(this.folderPath);

    File folder = new File(folderPath.toOSString());
    if (!folder.exists())
        return true; // nothing to do, source folder was removed

    /* ensure no concurrent write access to index */
    Index index = this.manager.getIndex(this.containerPath, true, /*reuse index file*/ true /*create if none*/);
    if (index == null)
        return true;
    ReadWriteMonitor monitor = index.monitor;
    if (monitor == null)
        return true; // index got deleted since acquired

    try {/*from w w w.  j av a2  s. com*/
        monitor.enterRead(); // ask permission to read

        final IPath container = this.containerPath;
        final IndexManager indexManager = this.manager;
        final SourceElementParser parser = indexManager.getSourceElementParser(this.project,
                null/*requestor will be set by indexer*/);
        Path path = FileSystems.getDefault().getPath(folderPath.toOSString());
        if (this.exclusionPatterns == null && this.inclusionPatterns == null) {

            Files.walkFileTree(path, new FileVisitor<Path>() {
                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                        throws IOException {
                    return null;
                }

                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    if (Util.isJavaLikeFileName(file.toFile().getName()))
                        indexManager.addSource(file, container, parser);
                    return FileVisitResult.CONTINUE;
                }

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

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

            //                folder.accept(
            //               new IResourceProxyVisitor() {
            //                  public boolean visit(IResourceProxy proxy) /* throws CoreException */{
            //                     if (proxy.getType() == IResource.FILE) {
            //
            //                        return false;
            //                     }
            //                     return true;
            //                  }
            //               },
            //               IResource.NONE
            //            );
        } else {
            Files.walkFileTree(path, new FileVisitor<Path>() {
                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                        throws IOException {
                    if (AddFolderToIndex.this.exclusionPatterns != null
                            && AddFolderToIndex.this.inclusionPatterns == null) {
                        // if there are inclusion patterns then we must walk the children
                        if (Util.isExcluded(new org.eclipse.core.runtime.Path(dir.toFile().getPath()),
                                AddFolderToIndex.this.inclusionPatterns,
                                AddFolderToIndex.this.exclusionPatterns, true))
                            return FileVisitResult.SKIP_SUBTREE;
                    }
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    if (Util.isJavaLikeFileName(file.getFileName().toString())) {
                        //                                IResource resource = proxy.requestResource();
                        if (!Util.isExcluded(new org.eclipse.core.runtime.Path(file.toFile().getPath()),
                                AddFolderToIndex.this.inclusionPatterns,
                                AddFolderToIndex.this.exclusionPatterns, false))
                            indexManager.addSource(file, container, parser);
                    }
                    return null;
                }

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

                @Override
                public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                    return null;
                }
            });
            //                folder.accept(
            //               new IResourceProxyVisitor() {
            //                  public boolean visit(IResourceProxy proxy) /* throws CoreException */{
            //                     switch(proxy.getType()) {
            //                        case IResource.FILE :
            //
            //                           return false;
            //                        case IResource.FOLDER :
            //
            //                     }
            //                     return true;
            //                  }
            //               },
            //               IResource.NONE
            //            );
        }
    } catch (IOException e) {
        if (JobManager.VERBOSE) {
            Util.verbose(
                    "-> failed to add " + this.folderPath + " to index because of the following exception:", //$NON-NLS-1$//$NON-NLS-2$
                    System.err);
            e.printStackTrace();
        }
        return false;
    } finally {
        monitor.exitRead(); // free read lock
    }
    return true;
}