Example usage for org.eclipse.jdt.core.search SearchDocument getPath

List of usage examples for org.eclipse.jdt.core.search SearchDocument getPath

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.search SearchDocument getPath.

Prototype

public final String getPath() 

Source Link

Document

Returns the path to the original document to publicly mention in index or search results.

Usage

From source file:org.eclipse.jdt.internal.core.search.indexing.IndexManager.java

License:Open Source License

public void scheduleDocumentIndexing(final SearchDocument searchDocument, IPath container,
        final IPath indexLocation, final SearchParticipant searchParticipant) {
    request(new IndexRequest(container, this) {
        public boolean execute(IProgressMonitor progressMonitor) {
            if (this.isCancelled || progressMonitor != null && progressMonitor.isCanceled())
                return true;

            /* ensure no concurrent write access to index */
            Index index = getIndex(this.containerPath, indexLocation, 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 {
                monitor.enterWrite(); // ask permission to write
                indexDocument(searchDocument, searchParticipant, index, indexLocation);
            } finally {
                monitor.exitWrite(); // free write lock
            }/* w w  w. ja  v  a  2s  .c  om*/
            return true;
        }

        public String toString() {
            return "indexing " + searchDocument.getPath(); //$NON-NLS-1$
        }
    });
}

From source file:org.eclipse.jdt.internal.core.search.matching.MatchLocator.java

License:Open Source License

public static SearchDocument[] addWorkingCopies(SearchPattern pattern, SearchDocument[] indexMatches,
        org.eclipse.jdt.core.ICompilationUnit[] copies, SearchParticipant participant) {
    if (copies == null)
        return indexMatches;
    // working copies take precedence over corresponding compilation units
    HashMap workingCopyDocuments = workingCopiesThatCanSeeFocus(copies, pattern, participant);
    if (workingCopyDocuments.size() == 0)
        return indexMatches;
    SearchDocument[] matches = null;//from   w  w  w . j a va2s  .  c  om
    int length = indexMatches.length;
    for (int i = 0; i < length; i++) {
        SearchDocument searchDocument = indexMatches[i];
        if (searchDocument.getParticipant() == participant) {
            SearchDocument workingCopyDocument = (SearchDocument) workingCopyDocuments
                    .remove(searchDocument.getPath());
            if (workingCopyDocument != null) {
                if (matches == null) {
                    System.arraycopy(indexMatches, 0, matches = new SearchDocument[length], 0, length);
                }
                matches[i] = workingCopyDocument;
            }
        }
    }
    if (matches == null) { // no working copy
        matches = indexMatches;
    }
    int remainingWorkingCopiesSize = workingCopyDocuments.size();
    if (remainingWorkingCopiesSize != 0) {
        System.arraycopy(matches, 0, matches = new SearchDocument[length + remainingWorkingCopiesSize], 0,
                length);
        Iterator iterator = workingCopyDocuments.values().iterator();
        int index = length;
        while (iterator.hasNext()) {
            matches[index++] = (SearchDocument) iterator.next();
        }
    }
    return matches;
}

From source file:org.eclipse.jdt.internal.core.search.matching.MatchLocator.java

License:Open Source License

/**
 * Locate the matches in the given files and report them using the search requestor.
 *///from  w  w  w  .ja v  a2 s. c om
public void locateMatches(SearchDocument[] searchDocuments) throws CoreException {
    if (this.patternLocator == null)
        return;
    int docsLength = searchDocuments.length;
    int progressLength = docsLength;
    if (BasicSearchEngine.VERBOSE) {
        System.out.println("Locating matches in documents ["); //$NON-NLS-1$
        for (int i = 0; i < docsLength; i++)
            System.out.println("\t" + searchDocuments[i]); //$NON-NLS-1$
        System.out.println("]"); //$NON-NLS-1$
    }
    IJavaProject[] javaModelProjects = null;
    if (this.searchPackageDeclaration) {
        javaModelProjects = JavaModelManager.getJavaModelManager().getJavaModel().getJavaProjects();
        progressLength += javaModelProjects.length;
    }

    // init infos for progress increasing
    int n = progressLength < 1000 ? Math.min(Math.max(progressLength / 200 + 1, 2), 4)
            : 5 * (progressLength / 1000);
    this.progressStep = progressLength < n ? 1 : progressLength / n; // step should not be 0
    this.progressWorked = 0;

    // extract working copies
    ArrayList copies = new ArrayList();
    for (int i = 0; i < docsLength; i++) {
        SearchDocument document = searchDocuments[i];
        if (document instanceof WorkingCopyDocument) {
            copies.add(((WorkingCopyDocument) document).workingCopy);
        }
    }
    int copiesLength = copies.size();
    this.workingCopies = new org.eclipse.jdt.core.ICompilationUnit[copiesLength];
    copies.toArray(this.workingCopies);

    JavaModelManager manager = JavaModelManager.getJavaModelManager();
    this.bindings = new SimpleLookupTable();
    try {
        // optimize access to zip files during search operation
        manager.cacheZipFiles(this);

        // initialize handle factory (used as a cache of handles so as to optimize space)
        if (this.handleFactory == null)
            this.handleFactory = new HandleFactory();

        if (this.progressMonitor != null) {
            this.progressMonitor.beginTask("", searchDocuments.length); //$NON-NLS-1$
        }

        // initialize pattern for polymorphic search (i.e. method reference pattern)
        this.patternLocator.initializePolymorphicSearch(this);

        JavaProject previousJavaProject = null;
        PossibleMatchSet matchSet = new PossibleMatchSet();
        Util.sort(searchDocuments, new Util.Comparer() {
            public int compare(Object a, Object b) {
                return ((SearchDocument) a).getPath().compareTo(((SearchDocument) b).getPath());
            }
        });
        int displayed = 0; // progress worked displayed
        String previousPath = null;
        SearchParticipant searchParticipant = null;
        for (int i = 0; i < docsLength; i++) {
            if (this.progressMonitor != null && this.progressMonitor.isCanceled()) {
                throw new OperationCanceledException();
            }

            // skip duplicate paths
            SearchDocument searchDocument = searchDocuments[i];
            if (searchParticipant == null) {
                searchParticipant = searchDocument.getParticipant();
            }
            searchDocuments[i] = null; // free current document
            String pathString = searchDocument.getPath();
            if (i > 0 && pathString.equals(previousPath)) {
                if (this.progressMonitor != null) {
                    this.progressWorked++;
                    if ((this.progressWorked % this.progressStep) == 0)
                        this.progressMonitor.worked(this.progressStep);
                }
                displayed++;
                continue;
            }
            previousPath = pathString;

            Openable openable;
            org.eclipse.jdt.core.ICompilationUnit workingCopy = null;
            if (searchDocument instanceof WorkingCopyDocument) {
                workingCopy = ((WorkingCopyDocument) searchDocument).workingCopy;
                openable = (Openable) workingCopy;
            } else {
                openable = this.handleFactory.createOpenable(pathString, this.scope);
            }
            if (openable == null) {
                if (this.progressMonitor != null) {
                    this.progressWorked++;
                    if ((this.progressWorked % this.progressStep) == 0)
                        this.progressMonitor.worked(this.progressStep);
                }
                displayed++;
                continue; // match is outside classpath
            }

            // create new parser and lookup environment if this is a new project
            IResource resource = null;
            JavaProject javaProject = (JavaProject) openable.getJavaProject();
            resource = workingCopy != null ? workingCopy.getResource() : openable.getResource();
            if (resource == null)
                resource = javaProject.getProject(); // case of a file in an external jar or external folder
            if (!javaProject.equals(previousJavaProject)) {
                // locate matches in previous project
                if (previousJavaProject != null) {
                    try {
                        locateMatches(previousJavaProject, matchSet, i - displayed);
                        displayed = i;
                    } catch (JavaModelException e) {
                        // problem with classpath in this project -> skip it
                    }
                    matchSet.reset();
                }
                previousJavaProject = javaProject;
            }
            matchSet.add(new PossibleMatch(this, resource, openable, searchDocument, this.pattern.mustResolve));
        }

        // last project
        if (previousJavaProject != null) {
            try {
                locateMatches(previousJavaProject, matchSet, docsLength - displayed);
            } catch (JavaModelException e) {
                // problem with classpath in last project -> ignore
            }
        }

        if (this.searchPackageDeclaration) {
            locatePackageDeclarations(searchParticipant, javaModelProjects);
        }

    } finally {
        if (this.progressMonitor != null)
            this.progressMonitor.done();
        if (this.nameEnvironment != null)
            this.nameEnvironment.cleanup();
        manager.flushZipFiles(this);
        this.bindings = null;
    }
}

From source file:org.eclipse.jst.jsp.ui.internal.java.refactoring.BasicRefactorSearchRequestor.java

License:Open Source License

/**
 * @see org.eclipse.jdt.core.search.SearchRequestor#acceptSearchMatch(org.eclipse.jdt.core.search.SearchMatch)
 *///from  w  w w . jav a 2s  .c om
public void acceptSearchMatch(SearchMatch javaMatch) throws CoreException {

    String matchDocumentPath = javaMatch.getResource().getFullPath().toString();
    SearchDocument searchDoc = JSPSearchSupport.getInstance().getSearchDocument(matchDocumentPath);

    if (searchDoc != null && searchDoc instanceof JavaSearchDocumentDelegate) {

        String renameText = getRenameText((JavaSearchDocumentDelegate) searchDoc, javaMatch);

        //if rename text is null then don't create an edit for it
        if (renameText != null) {
            // add it for the correct document
            addJavaEdit(searchDoc.getPath(),
                    new ReplaceEdit(javaMatch.getOffset(), javaMatch.getLength(), renameText));
        }
    }
}

From source file:org.eclipse.jst.jsp.ui.internal.java.search.JSPSearchQuery.java

License:Open Source License

protected IStatus doQuery(IProgressMonitor monitor) {
    IStatus status = Status.OK_STATUS;/*from  ww  w  .j  av a 2  s. c om*/
    try {
        JSPSearchSupport support = JSPSearchSupport.getInstance();
        // index the file
        SearchDocument delegate = support.addJspFile(getFile());

        String scopePath = delegate.getPath();
        JSPSearchScope singleFileScope = new JSPSearchScope(
                new String[] { getFile().getFullPath().toString(), scopePath });

        // perform a searchs
        // by passing in this jsp search query, requstor can add matches
        support.searchRunnable(getJavaElement(), singleFileScope,
                new JSPSingleFileSearchRequestor(getInstance()), monitor);
    } catch (Exception e) {
        status = new Status(IStatus.ERROR, "org.eclipse.wst.sse.ui", IStatus.OK, "", null); //$NON-NLS-1$   //$NON-NLS-2$
    }
    return status;
}