001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.core.search.matching;
011:
012: import java.io.IOException;
013:
014: import org.eclipse.core.runtime.*;
015: import org.eclipse.jdt.core.IJavaElement;
016: import org.eclipse.jdt.core.search.*;
017: import org.eclipse.jdt.internal.compiler.env.AccessRuleSet;
018: import org.eclipse.jdt.internal.core.index.*;
019: import org.eclipse.jdt.internal.core.search.*;
020:
021: /**
022: * Internal search pattern implementation
023: */
024: public abstract class InternalSearchPattern {
025:
026: /**
027: * The focus element (used for reference patterns)
028: */
029: IJavaElement focus;
030:
031: int kind;
032: boolean mustResolve = true;
033:
034: void acceptMatch(String relativePath, String containerPath,
035: char separator, SearchPattern pattern,
036: IndexQueryRequestor requestor,
037: SearchParticipant participant, IJavaSearchScope scope) {
038:
039: if (scope instanceof JavaSearchScope) {
040: JavaSearchScope javaSearchScope = (JavaSearchScope) scope;
041: // Get document path access restriction from java search scope
042: // Note that requestor has to verify if needed whether the document violates the access restriction or not
043: AccessRuleSet access = javaSearchScope.getAccessRuleSet(
044: relativePath, containerPath);
045: if (access != JavaSearchScope.NOT_ENCLOSED) { // scope encloses the document path
046: StringBuffer documentPath = new StringBuffer(
047: containerPath.length() + 1
048: + relativePath.length());
049: documentPath.append(containerPath);
050: documentPath.append(separator);
051: documentPath.append(relativePath);
052: if (!requestor.acceptIndexMatch(
053: documentPath.toString(), pattern, participant,
054: access))
055: throw new OperationCanceledException();
056: }
057: } else {
058: StringBuffer buffer = new StringBuffer(containerPath
059: .length()
060: + 1 + relativePath.length());
061: buffer.append(containerPath);
062: buffer.append(separator);
063: buffer.append(relativePath);
064: String documentPath = buffer.toString();
065: if (scope.encloses(documentPath))
066: if (!requestor.acceptIndexMatch(documentPath, pattern,
067: participant, null))
068: throw new OperationCanceledException();
069:
070: }
071: }
072:
073: SearchPattern currentPattern() {
074: return (SearchPattern) this ;
075: }
076:
077: /**
078: * Query a given index for matching entries. Assumes the sender has opened the index and will close when finished.
079: */
080: void findIndexMatches(Index index, IndexQueryRequestor requestor,
081: SearchParticipant participant, IJavaSearchScope scope,
082: IProgressMonitor monitor) throws IOException {
083: if (monitor != null && monitor.isCanceled())
084: throw new OperationCanceledException();
085: try {
086: index.startQuery();
087: SearchPattern pattern = currentPattern();
088: EntryResult[] entries = ((InternalSearchPattern) pattern)
089: .queryIn(index);
090: if (entries == null)
091: return;
092:
093: SearchPattern decodedResult = pattern.getBlankPattern();
094: String containerPath = index.containerPath;
095: char separator = index.separator;
096: for (int i = 0, l = entries.length; i < l; i++) {
097: if (monitor != null && monitor.isCanceled())
098: throw new OperationCanceledException();
099:
100: EntryResult entry = entries[i];
101: decodedResult.decodeIndexKey(entry.getWord());
102: if (pattern.matchesDecodedKey(decodedResult)) {
103: // TODO (kent) some clients may not need the document names
104: String[] names = entry.getDocumentNames(index);
105: for (int j = 0, n = names.length; j < n; j++)
106: acceptMatch(names[j], containerPath, separator,
107: decodedResult, requestor, participant,
108: scope);
109: }
110: }
111: } finally {
112: index.stopQuery();
113: }
114: }
115:
116: boolean isPolymorphicSearch() {
117: return false;
118: }
119:
120: EntryResult[] queryIn(Index index) throws IOException {
121: SearchPattern pattern = (SearchPattern) this;
122: return index.query(pattern.getIndexCategories(), pattern
123: .getIndexKey(), pattern.getMatchRule());
124: }
125:
126: }
|