Search in sources :

Example 6 with IResourceVisitor

use of org.eclipse.core.resources.IResourceVisitor in project ow by vtst.

the class ClosureCompiler method getJavaScriptFilesOfOtherResource.

/**
   * Fallback function to get the JavaScript files of a resource which is not a project (i.e. a folder
   * or a single file).
   * @param resource  The resource to visit.
   * @return  The set of JavaScript files included in the resource.
   * @throws CoreException
   */
private static Set<IFile> getJavaScriptFilesOfOtherResource(IResource resource) throws CoreException {
    final Set<IFile> files = new HashSet<IFile>();
    IResourceVisitor visitor = new IResourceVisitor() {

        @Override
        public boolean visit(IResource resource) throws CoreException {
            if (resource instanceof IFile) {
                IFile file = (IFile) resource;
                if (ClosureCompiler.isJavaScriptFile(file))
                    files.add(file);
            }
            return true;
        }
    };
    resource.accept(visitor);
    return files;
}
Also used : IResourceVisitor(org.eclipse.core.resources.IResourceVisitor) IFile(org.eclipse.core.resources.IFile) IResource(org.eclipse.core.resources.IResource) HashSet(java.util.HashSet)

Example 7 with IResourceVisitor

use of org.eclipse.core.resources.IResourceVisitor in project ow by vtst.

the class ClosureCompiler method getJavaScriptFilesOfProject.

/**
   * Get the JavaScript files from a project, according to the class path entries of the project.
   * @param project
   * @return  The set of JavaScript files, may be empty but not null.
   * @throws CoreException
   */
// Inspired from org.eclipse.wst.jsdt.internal.core.builder.AbstractImageBuilder.addAllSourceFiles
public static Set<IFile> getJavaScriptFilesOfProject(IProject project) throws CoreException {
    final JavaProject javaProject = (JavaProject) JavaScriptCore.create(project);
    final Set<IFile> result = new HashSet<IFile>();
    boolean hasClasspathEntry = false;
    try {
        final IIncludePathEntry[] expandedClassPath = javaProject.getExpandedClasspath();
        for (IIncludePathEntry includePathEntry : expandedClassPath) {
            if (!(includePathEntry instanceof ClasspathEntry))
                continue;
            final ClasspathEntry entry = (ClasspathEntry) includePathEntry;
            int entryKind = entry.getEntryKind();
            if (entryKind != IIncludePathEntry.CPE_SOURCE && entryKind != IIncludePathEntry.CPE_LIBRARY)
                continue;
            IResource includeResource = ResourcesPlugin.getWorkspace().getRoot().findMember(entry.getPath());
            if (includeResource == null)
                continue;
            switch(entryKind) {
                case IIncludePathEntry.CPE_SOURCE:
                    hasClasspathEntry = true;
                    includeResource.accept(new IResourceVisitor() {

                        public boolean visit(IResource resource) throws CoreException {
                            if (resource instanceof IFile) {
                                IFile file = (IFile) resource;
                                if (ClosureCompiler.isJavaScriptFile(file) && !Util.isExcluded(file.getFullPath(), entry.fullInclusionPatternChars(), entry.fullExclusionPatternChars(), false)) {
                                    result.add(file);
                                }
                                return false;
                            } else if (resource instanceof IContainer) {
                                try {
                                    IContainer container = (IContainer) resource;
                                    if (container instanceof IFolder && isExcludedFromProject(javaProject, expandedClassPath, container.getFullPath())) {
                                        return false;
                                    }
                                    return (!Util.isExcluded(container.getFullPath(), entry.fullInclusionPatternChars(), entry.fullExclusionPatternChars(), false) || entry.fullInclusionPatternChars() != null);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    return false;
                                }
                            } else
                                return false;
                        }
                    });
                    break;
                case IIncludePathEntry.CPE_LIBRARY:
                    if (includeResource instanceof IFile && project.equals(includeResource.getProject())) {
                        hasClasspathEntry = true;
                        result.add((IFile) includeResource);
                    }
                    break;
            }
        }
        if (!hasClasspathEntry) {
            // If there was no class path entry, we add all JavaScript files from the project.
            project.accept(new IResourceVisitor() {

                public boolean visit(IResource resource) throws CoreException {
                    if (resource instanceof IFile) {
                        IFile file = (IFile) resource;
                        if (ClosureCompiler.isJavaScriptFile(file))
                            result.add(file);
                    }
                    return true;
                }
            });
        }
        return result;
    } catch (JavaScriptModelException e) {
        e.printStackTrace();
        return Collections.emptySet();
    }
}
Also used : JavaProject(org.eclipse.wst.jsdt.internal.core.JavaProject) IResourceVisitor(org.eclipse.core.resources.IResourceVisitor) IFile(org.eclipse.core.resources.IFile) JavaScriptModelException(org.eclipse.wst.jsdt.core.JavaScriptModelException) CoreException(org.eclipse.core.runtime.CoreException) JavaScriptModelException(org.eclipse.wst.jsdt.core.JavaScriptModelException) CoreException(org.eclipse.core.runtime.CoreException) IIncludePathEntry(org.eclipse.wst.jsdt.core.IIncludePathEntry) ClasspathEntry(org.eclipse.wst.jsdt.internal.core.ClasspathEntry) IContainer(org.eclipse.core.resources.IContainer) IResource(org.eclipse.core.resources.IResource) HashSet(java.util.HashSet) IFolder(org.eclipse.core.resources.IFolder)

Example 8 with IResourceVisitor

use of org.eclipse.core.resources.IResourceVisitor in project OpenGrok by OpenGrok.

the class ResultsDialog method setResults.

public void setResults(List<Hit> results) {
    final Map<String, HitContainer> roots = new HashMap<String, HitContainer>();
    for (Hit hit : results) {
        String key = hit.getDirectory() + "/" + hit.getFilename();
        HitContainer container = roots.get(key);
        if (container == null) {
            container = new HitContainer(key);
            roots.put(key, container);
        }
        container.add(hit);
    }
    viewer.setHits(roots);
    if (resultsView != null) {
        resultsView.setHits(roots);
    }
    Display.getDefault().asyncExec(new Runnable() {

        @Override
        public void run() {
            if (!viewer.getControl().isDisposed()) {
                viewer.refresh();
            }
        }
    });
    if (Activator.getDefault().getPreferenceStore().getBoolean(EGrokPreferencePage.WORKSPACE_MATCHES)) {
        Runnable workspaceLocator = new Runnable() {

            @Override
            public void run() {
                IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
                final Map<HitContainer, HashMap<IProject, Integer>> potentialProjects = new HashMap<HitContainer, HashMap<IProject, Integer>>();
                final Map<IProject, ArrayList<String>> locationSegments = new HashMap<IProject, ArrayList<String>>();
                try {
                    workspaceRoot.accept(new IResourceVisitor() {

                        @Override
                        public boolean visit(IResource resource) throws CoreException {
                            if (resource instanceof IWorkspaceRoot) {
                                return true;
                            }
                            if (resource instanceof IProject) {
                                IProject project = (IProject) resource;
                                IPath location = project.getLocation();
                                for (String segment : location.segments()) {
                                    ArrayList<String> segments = locationSegments.get(project);
                                    if (segments == null) {
                                        segments = new ArrayList<String>();
                                        locationSegments.put(project, segments);
                                    }
                                    segments.add(segment);
                                }
                            }
                            return false;
                        }
                    });
                } catch (CoreException e) {
                    e.printStackTrace();
                }
                Map<HitContainer, ArrayList<String>> hitcontainerSegments = new HashMap<HitContainer, ArrayList<String>>();
                for (HitContainer hitcontainer : roots.values()) {
                    ArrayList<String> segments = new ArrayList<String>();
                    for (String segment : hitcontainer.getName().split("/")) {
                        segments.add(segment);
                    }
                    hitcontainerSegments.put(hitcontainer, segments);
                }
                for (IProject project : locationSegments.keySet()) {
                    ArrayList<String> segments = locationSegments.get(project);
                    int idx = 0;
                    for (String segment : segments) {
                        for (HitContainer container : hitcontainerSegments.keySet()) {
                            for (String containerPathSegment : hitcontainerSegments.get(container)) {
                                if (segment.equals(containerPathSegment)) {
                                    HashMap<IProject, Integer> matches = potentialProjects.get(container);
                                    if (matches == null) {
                                        matches = new HashMap<IProject, Integer>();
                                        potentialProjects.put(container, matches);
                                    }
                                    matches.put(project, idx);
                                }
                            }
                        }
                        idx++;
                    }
                }
                for (HitContainer container : potentialProjects.keySet()) {
                    String fullLocation = container.getName();
                    HashMap<IProject, Integer> matches = potentialProjects.get(container);
                    System.out.println(container.getName());
                    for (Entry<IProject, Integer> match : matches.entrySet()) {
                        IProject project = match.getKey();
                        Integer matchingLocation = match.getValue();
                        String matchingString = project.getLocation().segment(matchingLocation);
                        System.out.println("match: " + matchingString);
                        String local = fullLocation.substring(fullLocation.indexOf(matchingString) + matchingString.length());
                        System.out.println("local: " + local);
                        IResource member = project.findMember(local);
                        System.out.println("member: " + member);
                        if (member instanceof IFile) {
                            IFile file = (IFile) member;
                            container.setCorrespondingFile(file);
                        }
                    }
                }
                Display.getDefault().asyncExec(new Runnable() {

                    @Override
                    public void run() {
                        if (!viewer.getControl().isDisposed()) {
                            viewer.refresh();
                        }
                        if (resultsView != null) {
                            resultsView.refresh();
                        }
                    }
                });
            }
        };
        workspaceLocator.run();
    }
}
Also used : IResourceVisitor(org.eclipse.core.resources.IResourceVisitor) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IProject(org.eclipse.core.resources.IProject) Point(org.eclipse.swt.graphics.Point) HitContainer(org.opensolaris.opengrok.egrok.model.HitContainer) Hit(org.opensolaris.opengrok.egrok.model.Hit) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) CoreException(org.eclipse.core.runtime.CoreException) IResource(org.eclipse.core.resources.IResource)

Example 9 with IResourceVisitor

use of org.eclipse.core.resources.IResourceVisitor in project tdi-studio-se by Talend.

the class CodeGenInit method removeLinkedResources.

private void removeLinkedResources() throws CoreException {
    //$NON-NLS-1$
    info(Messages.getString("CodeGenInit.removeLink"));
    //$NON-NLS-1$
    IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(".JETEmitters");
    project.accept(new IResourceVisitor() {

        @Override
        public boolean visit(IResource resource) throws CoreException {
            if (resource.isLinked()) {
                resource.delete(true, new NullProgressMonitor());
            }
            return true;
        }
    }, IResource.DEPTH_ONE, false);
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IResourceVisitor(org.eclipse.core.resources.IResourceVisitor) CoreException(org.eclipse.core.runtime.CoreException) IProject(org.eclipse.core.resources.IProject) IResource(org.eclipse.core.resources.IResource)

Example 10 with IResourceVisitor

use of org.eclipse.core.resources.IResourceVisitor in project sling by apache.

the class ImportRepositoryContentAction method recordNotIgnoredResources.

private void recordNotIgnoredResources() throws CoreException {
    final ResourceChangeCommandFactory rccf = new ResourceChangeCommandFactory(serializationManager, Activator.getDefault().getPreferences().getIgnoredFileNamesForSync());
    IResource importStartingPoint = contentSyncRootDir.findMember(repositoryImportRoot);
    if (importStartingPoint == null) {
        return;
    }
    importStartingPoint.accept(new IResourceVisitor() {

        @Override
        public boolean visit(IResource resource) throws CoreException {
            try {
                ResourceAndInfo rai = rccf.buildResourceAndInfo(resource, repository);
                if (rai == null) {
                    // can be a prerequisite
                    return true;
                }
                String repositoryPath = rai.getResource().getPath();
                FilterResult filterResult = filter.filter(repositoryPath);
                if (ignoredResources.isIgnored(repositoryPath)) {
                    return false;
                }
                if (filterResult == FilterResult.ALLOW) {
                    currentResources.add(resource);
                    return true;
                }
                return false;
            } catch (IOException e) {
                throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed reading current project's resources", e));
            }
        }
    });
    logger.trace("Found {0} not ignored local resources", currentResources.size());
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IResourceVisitor(org.eclipse.core.resources.IResourceVisitor) CoreException(org.eclipse.core.runtime.CoreException) ResourceAndInfo(org.apache.sling.ide.eclipse.core.internal.ResourceAndInfo) ResourceChangeCommandFactory(org.apache.sling.ide.eclipse.core.internal.ResourceChangeCommandFactory) FilterResult(org.apache.sling.ide.filter.FilterResult) IOException(java.io.IOException) IResource(org.eclipse.core.resources.IResource)

Aggregations

IResource (org.eclipse.core.resources.IResource)13 IResourceVisitor (org.eclipse.core.resources.IResourceVisitor)13 IFile (org.eclipse.core.resources.IFile)9 CoreException (org.eclipse.core.runtime.CoreException)9 IProject (org.eclipse.core.resources.IProject)8 HashMap (java.util.HashMap)3 IFolder (org.eclipse.core.resources.IFolder)3 Status (org.eclipse.core.runtime.Status)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 ResourceChangeCommandFactory (org.apache.sling.ide.eclipse.core.internal.ResourceChangeCommandFactory)2 IPath (org.eclipse.core.runtime.IPath)2 IStatus (org.eclipse.core.runtime.IStatus)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)2 Pair (org.eclipse.xtext.xbase.lib.Pair)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1