KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > search > matching > InternalSearchPattern


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core.search.matching;
12
13 import java.io.IOException JavaDoc;
14
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.jdt.core.IJavaElement;
17 import org.eclipse.jdt.core.search.*;
18 import org.eclipse.jdt.internal.compiler.env.AccessRuleSet;
19 import org.eclipse.jdt.internal.compiler.util.Util;
20 import org.eclipse.jdt.internal.core.index.*;
21 import org.eclipse.jdt.internal.core.search.*;
22
23 /**
24  * Internal search pattern implementation
25  */

26 public abstract class InternalSearchPattern {
27
28     /**
29      * The focus element (used for reference patterns)
30      */

31     IJavaElement focus;
32
33     int kind;
34     boolean mustResolve = true;
35     
36     void acceptMatch(String JavaDoc relativePath, String JavaDoc containerPath, SearchPattern pattern, IndexQueryRequestor requestor, SearchParticipant participant, IJavaSearchScope scope) {
37
38         if (scope instanceof JavaSearchScope) {
39             JavaSearchScope javaSearchScope = (JavaSearchScope) scope;
40             // Get document path access restriction from java search scope
41
// Note that requestor has to verify if needed whether the document violates the access restriction or not
42
AccessRuleSet access = javaSearchScope.getAccessRuleSet(relativePath, containerPath);
43             if (access != JavaSearchScope.NOT_ENCLOSED) { // scope encloses the document path
44
String JavaDoc documentPath = documentPath(containerPath, relativePath);
45                 if (!requestor.acceptIndexMatch(documentPath, pattern, participant, access))
46                     throw new OperationCanceledException();
47             }
48         } else {
49             String JavaDoc documentPath = documentPath(containerPath, relativePath);
50             if (scope.encloses(documentPath))
51                 if (!requestor.acceptIndexMatch(documentPath, pattern, participant, null))
52                     throw new OperationCanceledException();
53             
54         }
55     }
56     SearchPattern currentPattern() {
57         return (SearchPattern) this;
58     }
59     String JavaDoc documentPath(String JavaDoc containerPath, String JavaDoc relativePath) {
60         String JavaDoc separator = Util.isArchiveFileName(containerPath) ? IJavaSearchScope.JAR_FILE_ENTRY_SEPARATOR : "/"; //$NON-NLS-1$
61
StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(containerPath.length() + separator.length() + relativePath.length());
62         buffer.append(containerPath);
63         buffer.append(separator);
64         buffer.append(relativePath);
65         return buffer.toString();
66     }
67     /**
68      * Query a given index for matching entries. Assumes the sender has opened the index and will close when finished.
69      */

70     void findIndexMatches(Index index, IndexQueryRequestor requestor, SearchParticipant participant, IJavaSearchScope scope, IProgressMonitor monitor) throws IOException JavaDoc {
71         if (monitor != null && monitor.isCanceled()) throw new OperationCanceledException();
72         try {
73             index.startQuery();
74             SearchPattern pattern = currentPattern();
75             EntryResult[] entries = ((InternalSearchPattern)pattern).queryIn(index);
76             if (entries == null) return;
77         
78             SearchPattern decodedResult = pattern.getBlankPattern();
79             String JavaDoc containerPath = index.containerPath;
80             for (int i = 0, l = entries.length; i < l; i++) {
81                 if (monitor != null && monitor.isCanceled()) throw new OperationCanceledException();
82         
83                 EntryResult entry = entries[i];
84                 decodedResult.decodeIndexKey(entry.getWord());
85                 if (pattern.matchesDecodedKey(decodedResult)) {
86                     // TODO (kent) some clients may not need the document names
87
String JavaDoc[] names = entry.getDocumentNames(index);
88                     for (int j = 0, n = names.length; j < n; j++)
89                         acceptMatch(names[j], containerPath, decodedResult, requestor, participant, scope);
90                 }
91             }
92         } finally {
93             index.stopQuery();
94         }
95     }
96     boolean isPolymorphicSearch() {
97         return false;
98     }
99     EntryResult[] queryIn(Index index) throws IOException JavaDoc {
100         SearchPattern pattern = (SearchPattern) this;
101         return index.query(pattern.getIndexCategories(), pattern.getIndexKey(), pattern.getMatchRule());
102     }
103
104 }
105
Popular Tags