Search in sources :

Example 1 with ClasspathEntry

use of org.eclipse.wst.jsdt.internal.core.ClasspathEntry 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)

Aggregations

HashSet (java.util.HashSet)1 IContainer (org.eclipse.core.resources.IContainer)1 IFile (org.eclipse.core.resources.IFile)1 IFolder (org.eclipse.core.resources.IFolder)1 IResource (org.eclipse.core.resources.IResource)1 IResourceVisitor (org.eclipse.core.resources.IResourceVisitor)1 CoreException (org.eclipse.core.runtime.CoreException)1 IIncludePathEntry (org.eclipse.wst.jsdt.core.IIncludePathEntry)1 JavaScriptModelException (org.eclipse.wst.jsdt.core.JavaScriptModelException)1 ClasspathEntry (org.eclipse.wst.jsdt.internal.core.ClasspathEntry)1 JavaProject (org.eclipse.wst.jsdt.internal.core.JavaProject)1