Example usage for org.eclipse.jdt.internal.core.util Util relativePath

List of usage examples for org.eclipse.jdt.internal.core.util Util relativePath

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.core.util Util relativePath.

Prototype

public static String relativePath(IPath fullPath, int skipSegmentCount) 

Source Link

Document

Returns the toString() of the given full path minus the first given number of segments.

Usage

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

License:Open Source License

public boolean execute(IProgressMonitor progressMonitor) {

    if (this.isCancelled || progressMonitor != null && progressMonitor.isCanceled())
        return true;

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

    try {//  w  w w  .  j av a  2 s . c o m
        monitor.enterRead(); // ask permission to read
        String containerRelativePath = Util.relativePath(this.folderPath, this.containerPath.segmentCount());
        String[] paths = index.queryDocumentNames(containerRelativePath);
        // all file names belonging to the folder or its subfolders and that are not excluded (see http://bugs.eclipse.org/bugs/show_bug.cgi?id=32607)
        if (paths != null) {
            if (this.exclusionPatterns == null && this.inclusionPatterns == null) {
                for (int i = 0, max = paths.length; i < max; i++) {
                    this.manager.remove(paths[i], this.containerPath); // write lock will be acquired by the remove operation
                }
            } else {
                for (int i = 0, max = paths.length; i < max; i++) {
                    String documentPath = this.containerPath.toString() + '/' + paths[i];
                    if (!Util.isExcluded(new Path(documentPath), this.inclusionPatterns, this.exclusionPatterns,
                            false))
                        this.manager.remove(paths[i], this.containerPath); // write lock will be acquired by the remove operation
                }
            }
        }
    } catch (IOException e) {
        if (JobManager.VERBOSE) {
            Util.verbose("-> failed to remove " + this.folderPath //$NON-NLS-1$
                    + " from index because of the following exception:", System.err); //$NON-NLS-1$
            e.printStackTrace();
        }
        return false;
    } finally {
        monitor.exitRead(); // free read lock
    }
    return true;
}

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

License:Open Source License

/**
 * Add a path to current java search scope or all project fragment roots if null.
 * Use project resolved classpath to retrieve and store access restriction on each classpath entry.
 * Recurse if dependent projects are found.
 *
 * @param javaProject//  www  .j av  a  2s. c o m
 *         Project used to get resolved classpath entries
 * @param pathToAdd
 *         Path to add in case of single element or null if user want to add all project package fragment roots
 * @param includeMask
 *         Mask to apply on classpath entries
 * @param projectsToBeAdded
 *         Set to avoid infinite recursion
 * @param visitedProjects
 *         Set to avoid adding twice the same project
 * @param referringEntry
 *         Project raw entry in referring project classpath
 * @throws org.eclipse.jdt.core.JavaModelException
 *         May happen while getting java model info
 */
void add(JavaProject javaProject, IPath pathToAdd, int includeMask, HashSet projectsToBeAdded,
        HashSet visitedProjects, IClasspathEntry referringEntry) throws JavaModelException {
    //        IProject project = javaProject.getProject();
    //        if (!project.isAccessible() || !visitedProjects.add(project)) return;

    IPath projectPath = javaProject.getFullPath();
    String projectPathString = projectPath.toString();
    addEnclosingProjectOrJar(projectPath);

    IClasspathEntry[] entries = javaProject.getResolvedClasspath();
    //        IJavaModel model = javaProject.getJavaModel();
    //        JavaModelManager.PerProjectInfo perProjectInfo = javaProject.getPerProjectInfo();
    for (int i = 0, length = entries.length; i < length; i++) {
        IClasspathEntry entry = entries[i];
        AccessRuleSet access = null;
        ClasspathEntry cpEntry = (ClasspathEntry) entry;
        if (referringEntry != null) {
            // Add only exported entries.
            // Source folder are implicitly exported.
            if (!entry.isExported() && entry.getEntryKind() != IClasspathEntry.CPE_SOURCE) {
                continue;
            }
            cpEntry = cpEntry.combineWith((ClasspathEntry) referringEntry);
            //            cpEntry = ((ClasspathEntry)referringEntry).combineWith(cpEntry);
        }
        access = cpEntry.getAccessRuleSet();
        switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_LIBRARY:
            IClasspathEntry rawEntry = null;
            //                    Map rootPathToRawEntries = perProjectInfo.rootPathToRawEntries;
            //                    if (rootPathToRawEntries != null) {
            //                        rawEntry = (IClasspathEntry)rootPathToRawEntries.get(entry.getPath());
            //                    }
            //                    if (rawEntry == null) break;
            rawKind: switch (cpEntry.getEntryKind()) {
            case IClasspathEntry.CPE_LIBRARY:
            case IClasspathEntry.CPE_VARIABLE:
                if ((includeMask & APPLICATION_LIBRARIES) != 0) {
                    IPath path = entry.getPath();
                    if (pathToAdd == null || pathToAdd.equals(path)) {
                        //                                    Object target = JavaModel.getTarget(path, false/*don't check existence*/);
                        //                                    if (target instanceof IFolder) // case of an external folder
                        //                                        path = ((IFolder)target).getFullPath();
                        String pathToString = path.getDevice() == null ? path.toString() : path.toOSString();
                        add(projectPath.toString(), "", pathToString, false/*not a package*/, access); //$NON-NLS-1$
                        addEnclosingProjectOrJar(entry.getPath());
                    }
                }
                break;
            case IClasspathEntry.CPE_CONTAINER:
                IClasspathContainer container = JavaCore.getClasspathContainer(rawEntry.getPath(), javaProject);
                if (container == null)
                    break;
                switch (container.getKind()) {
                case IClasspathContainer.K_APPLICATION:
                    if ((includeMask & APPLICATION_LIBRARIES) == 0)
                        break rawKind;
                    break;
                case IClasspathContainer.K_SYSTEM:
                case IClasspathContainer.K_DEFAULT_SYSTEM:
                    if ((includeMask & SYSTEM_LIBRARIES) == 0)
                        break rawKind;
                    break;
                default:
                    break rawKind;
                }
                IPath path = entry.getPath();
                if (pathToAdd == null || pathToAdd.equals(path)) {
                    Object target = JavaModel.getTarget(path, false/*don't check existence*/);
                    if (target instanceof IFolder) // case of an external folder
                        path = ((IFolder) target).getFullPath();
                    String pathToString = path.getDevice() == null ? path.toString() : path.toOSString();
                    add(projectPath.toString(), "", pathToString, false/*not a package*/, access); //$NON-NLS-1$
                    addEnclosingProjectOrJar(entry.getPath());
                }
                break;
            }
            break;
        case IClasspathEntry.CPE_PROJECT:
            //                    if ((includeMask & REFERENCED_PROJECTS) != 0) {
            //                        IPath path = entry.getPath();
            //                        if (pathToAdd == null || pathToAdd.equals(path)) {
            //                            JavaProject referencedProject = (JavaProject)model.getJavaProject(path.lastSegment());
            //                            if (!projectsToBeAdded
            //                                    .contains(referencedProject)) { // do not recurse if depending project was used to create the scope
            //                                add(referencedProject, null, includeMask, projectsToBeAdded, visitedProjects, cpEntry);
            //                            }
            //                        }
            //                    }
            break;
        case IClasspathEntry.CPE_SOURCE:
            if ((includeMask & SOURCES) != 0) {
                IPath path = entry.getPath();
                if (pathToAdd == null || pathToAdd.equals(path)) {
                    add(projectPath.toString(), Util.relativePath(path, 1/*remove project segment*/),
                            projectPathString, false/*not a package*/, access);
                }
            }
            break;
        }
    }
}

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

License:Open Source License

/**
 * Add an element to the java search scope.
 *
 * @param element//from  w  ww. ja  va2  s  . co  m
 *         The element we want to add to current java search scope
 * @throws org.eclipse.jdt.core.JavaModelException
 *         May happen if some Java Model info are not available
 */
public void add(IJavaElement element) throws JavaModelException {
    IPath containerPath = null;
    String containerPathToString = null;
    PackageFragmentRoot root = null;
    int includeMask = SOURCES | APPLICATION_LIBRARIES | SYSTEM_LIBRARIES;
    switch (element.getElementType()) {
    case IJavaElement.JAVA_MODEL:
        // a workspace sope should be used
        break;
    case IJavaElement.JAVA_PROJECT:
        add((JavaProject) element, null, includeMask, new HashSet(2), new HashSet(2), null);
        break;
    case IJavaElement.PACKAGE_FRAGMENT_ROOT:
        root = (PackageFragmentRoot) element;
        IPath rootPath = root.internalPath();
        containerPath = root.getKind() == IPackageFragmentRoot.K_SOURCE ? root.getParent().getPath() : rootPath;
        containerPathToString = containerPath.getDevice() == null ? containerPath.toString()
                : containerPath.toOSString();
        IResource rootResource = root.resource();
        String projectPath = root.getJavaProject().getPath().toString();
        if (rootResource != null && rootResource.isAccessible()) {
            String relativePath = Util.relativePath(rootResource.getFullPath(), containerPath.segmentCount());
            add(projectPath, relativePath, containerPathToString, false/*not a package*/, null);
        } else {
            add(projectPath, "", containerPathToString, false/*not a package*/, null); //$NON-NLS-1$
        }
        break;
    case IJavaElement.PACKAGE_FRAGMENT:
        root = (PackageFragmentRoot) element.getParent();
        projectPath = root.getJavaProject().getPath().toString();
        if (root.isArchive()) {
            String relativePath = Util.concatWith(((PackageFragment) element).names, '/');
            containerPath = root.getPath();
            containerPathToString = containerPath.getDevice() == null ? containerPath.toString()
                    : containerPath.toOSString();
            add(projectPath, relativePath, containerPathToString, true/*package*/, null);
        } else {
            IResource resource = ((JavaElement) element).resource();
            if (resource != null) {
                if (resource.isAccessible()) {
                    containerPath = root.getKind() == IPackageFragmentRoot.K_SOURCE ? root.getParent().getPath()
                            : root.internalPath();
                } else {
                    // for working copies, get resource container full path
                    containerPath = resource.getParent().getFullPath();
                }
                containerPathToString = containerPath.getDevice() == null ? containerPath.toString()
                        : containerPath.toOSString();
                String relativePath = Util.relativePath(resource.getFullPath(), containerPath.segmentCount());
                add(projectPath, relativePath, containerPathToString, true/*package*/, null);
            }
        }
        break;
    default:
        // remember sub-cu (or sub-class file) java elements
        if (element instanceof IMember) {
            if (this.elements == null) {
                this.elements = new ArrayList();
            }
            this.elements.add(element);
        }
        root = (PackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
        projectPath = root.getJavaProject().getPath().toString();
        String relativePath;
        if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
            containerPath = root.getParent().getPath();
            relativePath = Util.relativePath(getPath(element, false/*full path*/), 1/*remove project segment*/);
        } else {
            containerPath = root.internalPath();
            relativePath = getPath(element, true/*relative path*/).toString();
        }
        containerPathToString = containerPath.getDevice() == null ? containerPath.toString()
                : containerPath.toOSString();
        add(projectPath, relativePath, containerPathToString, false/*not a package*/, null);
    }

    if (root != null)
        addEnclosingProjectOrJar(
                root.getKind() == IPackageFragmentRoot.K_SOURCE ? root.getParent().getPath() : root.getPath());
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.LocalVariablePattern.java

License:Open Source License

public void findIndexMatches(Index index, IndexQueryRequestor requestor, SearchParticipant participant,
        IJavaSearchScope scope, IProgressMonitor progressMonitor) {
    IPackageFragmentRoot root = (IPackageFragmentRoot) this.localVariable
            .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
    String documentPath;/*from   w  w  w  .j av a  2 s.co  m*/
    String relativePath;
    if (root.isArchive()) {
        IType type = (IType) this.localVariable.getAncestor(IJavaElement.TYPE);
        relativePath = (type.getFullyQualifiedName('$')).replace('.', '/')
                + SuffixConstants.SUFFIX_STRING_class;
        documentPath = root.getPath() + IJavaSearchScope.JAR_FILE_ENTRY_SEPARATOR + relativePath;
    } else {
        IPath path = this.localVariable.getPath();
        documentPath = path.toString();
        relativePath = Util.relativePath(path, 1/*remove project segment*/);
    }

    if (scope instanceof JavaSearchScope) {
        JavaSearchScope javaSearchScope = (JavaSearchScope) scope;
        // Get document path access restriction from java search scope
        // Note that requestor has to verify if needed whether the document violates the access restriction or not
        AccessRuleSet access = javaSearchScope.getAccessRuleSet(relativePath, index.containerPath);
        if (access != JavaSearchScope.NOT_ENCLOSED) { // scope encloses the path
            if (!requestor.acceptIndexMatch(documentPath, this, participant, access))
                throw new OperationCanceledException();
        }
    } else if (scope.encloses(documentPath)) {
        if (!requestor.acceptIndexMatch(documentPath, this, participant, null))
            throw new OperationCanceledException();
    }
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.TypeParameterPattern.java

License:Open Source License

public void findIndexMatches(Index index, IndexQueryRequestor requestor, SearchParticipant participant,
        IJavaSearchScope scope, IProgressMonitor progressMonitor) {
    IPackageFragmentRoot root = (IPackageFragmentRoot) this.typeParameter
            .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
    String documentPath;/* w ww  . ja v  a 2  s  . c  o m*/
    String relativePath;
    if (root.isArchive()) {
        IType type = (IType) this.typeParameter.getAncestor(IJavaElement.TYPE);
        relativePath = (type.getFullyQualifiedName('$')).replace('.', '/')
                + SuffixConstants.SUFFIX_STRING_class;
        documentPath = root.getPath() + IJavaSearchScope.JAR_FILE_ENTRY_SEPARATOR + relativePath;
    } else {
        IPath path = this.typeParameter.getPath();
        documentPath = path.toString();
        relativePath = Util.relativePath(path, 1/*remove project segment*/);
    }

    if (scope instanceof JavaSearchScope) {
        JavaSearchScope javaSearchScope = (JavaSearchScope) scope;
        // Get document path access restriction from java search scope
        // Note that requestor has to verify if needed whether the document violates the access restriction or not
        AccessRuleSet access = javaSearchScope.getAccessRuleSet(relativePath, index.containerPath);
        if (access != JavaSearchScope.NOT_ENCLOSED) { // scope encloses the path
            if (!requestor.acceptIndexMatch(documentPath, this, participant, access))
                throw new OperationCanceledException();
        }
    } else if (scope.encloses(documentPath)) {
        if (!requestor.acceptIndexMatch(documentPath, this, participant, null))
            throw new OperationCanceledException();
    }
}