csalter | c04ca3d | 2006-01-10 00:18:18 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2004, 2006 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 | |
| 12 | package org.eclipse.wst.common.core.search; |
| 13 | |
csalter | 404d183 | 2006-02-09 05:30:50 +0000 | [diff] [blame] | 14 | import java.util.Map; |
| 15 | |
csalter | c04ca3d | 2006-01-10 00:18:18 +0000 | [diff] [blame] | 16 | import org.eclipse.core.runtime.CoreException; |
| 17 | import org.eclipse.core.runtime.IProgressMonitor; |
| 18 | import org.eclipse.core.runtime.OperationCanceledException; |
| 19 | import org.eclipse.core.runtime.SubProgressMonitor; |
| 20 | import org.eclipse.wst.common.core.search.document.SearchDocumentSet; |
| 21 | import org.eclipse.wst.common.core.search.internal.Messages; |
| 22 | import org.eclipse.wst.common.core.search.internal.SearchDocumentSetImpl; |
| 23 | import org.eclipse.wst.common.core.search.pattern.SearchPattern; |
| 24 | import org.eclipse.wst.common.core.search.scope.SearchScope; |
csalter | c04ca3d | 2006-01-10 00:18:18 +0000 | [diff] [blame] | 25 | |
| 26 | /** |
csalter | 404d183 | 2006-02-09 05:30:50 +0000 | [diff] [blame] | 27 | * The {@link SearchEngine} class provides a generic way of searching for information |
| 28 | * without the need of knowing how or where that information is stored. The results |
| 29 | * returned by a search could be scattered in a number of files or stored in an index. |
| 30 | * Examples of the information you can search for include element declarations and |
| 31 | * references, references between files, and use of qualifiers. |
| 32 | * <p> |
| 33 | * The search can be limited to a specified search scope, or the entire workspace can |
| 34 | * be searched. Search matches are returned to the specified {@link SearchRequestor}. |
| 35 | * <p> |
csalter | c04ca3d | 2006-01-10 00:18:18 +0000 | [diff] [blame] | 36 | * This class may be instantiated; it is not intended to be subclassed. |
kchong | 443ced4 | 2006-01-11 16:57:16 +0000 | [diff] [blame] | 37 | * |
| 38 | * <p> |
| 39 | * <b>Note:</b> This class/interface is part of an interim API that is still under development and expected to |
| 40 | * change significantly before reaching stability. It is being made available at this early stage to solicit feedback |
| 41 | * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken |
| 42 | * (repeatedly) as the API evolves. |
| 43 | * </p> |
csalter | c04ca3d | 2006-01-10 00:18:18 +0000 | [diff] [blame] | 44 | */ |
csalter | 404d183 | 2006-02-09 05:30:50 +0000 | [diff] [blame] | 45 | public class SearchEngine implements ISearchOptions |
csalter | c04ca3d | 2006-01-10 00:18:18 +0000 | [diff] [blame] | 46 | { |
| 47 | |
| 48 | /** |
csalter | 404d183 | 2006-02-09 05:30:50 +0000 | [diff] [blame] | 49 | * Searches for matches of a given search pattern using a specified set of search |
| 50 | * participants and search scope. Search patterns can be created using factory |
| 51 | * methods and encapsulate the description of the information to be searched for |
| 52 | * (for example, element declarations of a specified type, in a case sensitive |
| 53 | * manner). |
csalter | c04ca3d | 2006-01-10 00:18:18 +0000 | [diff] [blame] | 54 | * @param pattern |
csalter | 404d183 | 2006-02-09 05:30:50 +0000 | [diff] [blame] | 55 | * The pattern describing the information to search for |
csalter | c04ca3d | 2006-01-10 00:18:18 +0000 | [diff] [blame] | 56 | * @param requestor |
csalter | 404d183 | 2006-02-09 05:30:50 +0000 | [diff] [blame] | 57 | * Callback object to notify with the results of the search (each match |
| 58 | * is reported to {@link SearchRequestor#acceptSearchMatch(SearchMatch)}) |
| 59 | * @param participants |
| 60 | * The search participants that will conduct the search |
| 61 | * @param scope |
| 62 | * Optional search scope to limit the source of search candidates; |
| 63 | * specify <code>null</code> to search the entire workspace |
| 64 | * @param searchOptions |
| 65 | * Optional map of options and values defining behavior of the search; |
| 66 | * some options and values are provided by {@link ISearchOptions} |
csalter | c04ca3d | 2006-01-10 00:18:18 +0000 | [diff] [blame] | 67 | * @param monitor |
csalter | 404d183 | 2006-02-09 05:30:50 +0000 | [diff] [blame] | 68 | * Optional progress monitor used to report work completed |
csalter | c04ca3d | 2006-01-10 00:18:18 +0000 | [diff] [blame] | 69 | * @exception CoreException |
csalter | 404d183 | 2006-02-09 05:30:50 +0000 | [diff] [blame] | 70 | * if the search fails |
csalter | c04ca3d | 2006-01-10 00:18:18 +0000 | [diff] [blame] | 71 | */ |
| 72 | public void search(SearchPattern pattern, SearchRequestor requestor, |
csalter | 404d183 | 2006-02-09 05:30:50 +0000 | [diff] [blame] | 73 | SearchParticipant[] participants, SearchScope scope, Map searchOptions, |
csalter | c04ca3d | 2006-01-10 00:18:18 +0000 | [diff] [blame] | 74 | IProgressMonitor monitor) throws CoreException |
| 75 | { |
| 76 | |
| 77 | if (monitor != null && monitor.isCanceled()) |
| 78 | throw new OperationCanceledException(); |
| 79 | |
| 80 | /* initialize progress monitor */ |
| 81 | if (monitor != null) |
| 82 | monitor.beginTask(Messages.engine_searching, 100); |
| 83 | |
csalter | 0de6b15 | 2006-06-06 05:17:51 +0000 | [diff] [blame] | 84 | SearchDocumentSet set = new SearchDocumentSetImpl(); |
csalter | c04ca3d | 2006-01-10 00:18:18 +0000 | [diff] [blame] | 85 | try |
| 86 | { |
| 87 | // requestor.beginReporting(); |
csalter | c04ca3d | 2006-01-10 00:18:18 +0000 | [diff] [blame] | 88 | SearchScope[] scopeArray = new SearchScope[participants.length]; |
| 89 | for (int i = 0, l = participants == null ? 0 : participants.length; i < l; i++) |
| 90 | { |
| 91 | if (monitor != null && monitor.isCanceled()) |
| 92 | throw new OperationCanceledException(); |
| 93 | |
| 94 | SearchParticipant participant = participants[i]; |
| 95 | SubProgressMonitor subMonitor = monitor == null ? null |
| 96 | : new SubProgressMonitor(monitor, 1000); |
| 97 | if (subMonitor != null) |
| 98 | subMonitor.beginTask("", 1000); //$NON-NLS-1$ |
| 99 | try |
| 100 | { |
| 101 | if (subMonitor != null) |
| 102 | subMonitor.subTask(Messages.bind( |
| 103 | Messages.engine_searching_locatingDocuments, |
| 104 | new String[] |
| 105 | { participant.getDescription() })); |
csalter | 404d183 | 2006-02-09 05:30:50 +0000 | [diff] [blame] | 106 | participant.beginSearching(pattern, searchOptions); |
csalter | c04ca3d | 2006-01-10 00:18:18 +0000 | [diff] [blame] | 107 | // requestor.enterParticipant(participant); |
| 108 | // participant creates it's own search scope |
csalter | 404d183 | 2006-02-09 05:30:50 +0000 | [diff] [blame] | 109 | SearchScope newScope = |
| 110 | participant.selectDocumentLocations(pattern, scope, searchOptions, monitor); |
csalter | c04ca3d | 2006-01-10 00:18:18 +0000 | [diff] [blame] | 111 | scopeArray[i] = newScope; |
| 112 | // participant creates search documents based on it's search scope |
csalter | 404d183 | 2006-02-09 05:30:50 +0000 | [diff] [blame] | 113 | participant.createSearchDocuments(set, pattern, newScope, searchOptions, subMonitor); |
csalter | c04ca3d | 2006-01-10 00:18:18 +0000 | [diff] [blame] | 114 | } |
| 115 | catch(Exception e) |
| 116 | { |
| 117 | } |
| 118 | } |
| 119 | for (int i = 0, l = participants == null ? 0 : participants.length; i < l; i++) |
| 120 | { |
| 121 | if (monitor != null && monitor.isCanceled()) |
| 122 | throw new OperationCanceledException(); |
| 123 | |
| 124 | SearchParticipant participant = participants[i]; |
| 125 | SubProgressMonitor subMonitor = monitor == null ? null |
| 126 | : new SubProgressMonitor(monitor, 1000); |
| 127 | if (subMonitor != null && subMonitor.isCanceled()) |
| 128 | throw new OperationCanceledException(); |
| 129 | try |
| 130 | { |
| 131 | // locate index matches if any (note that all search matches |
| 132 | // could have been issued during index querying) |
| 133 | if (subMonitor != null) |
| 134 | subMonitor.subTask(Messages.bind( |
| 135 | Messages.engine_searching_matching, |
| 136 | new String[] |
| 137 | { participant.getDescription() })); |
| 138 | // a search document set should contain enough info to reduce the search scope even further |
| 139 | // before finding precize locations |
kchong | 2f60653 | 2006-08-11 20:18:52 +0000 | [diff] [blame] | 140 | |
| 141 | // the scope could be null if the partcipant barfed and exeption in the first loop |
| 142 | if (scopeArray[i] != null) |
| 143 | { |
| 144 | participant.locateMatches(set, pattern, scopeArray[i], requestor, searchOptions, subMonitor); |
| 145 | } |
| 146 | } |
| 147 | catch (Exception e) |
| 148 | { |
| 149 | |
csalter | c04ca3d | 2006-01-10 00:18:18 +0000 | [diff] [blame] | 150 | } |
| 151 | finally |
| 152 | { |
| 153 | // requestor.exitParticipant(participant); |
csalter | 404d183 | 2006-02-09 05:30:50 +0000 | [diff] [blame] | 154 | participant.doneSearching(pattern, searchOptions); |
csalter | 0de6b15 | 2006-06-06 05:17:51 +0000 | [diff] [blame] | 155 | } |
csalter | c04ca3d | 2006-01-10 00:18:18 +0000 | [diff] [blame] | 156 | } |
| 157 | } finally |
| 158 | { |
csalter | 0de6b15 | 2006-06-06 05:17:51 +0000 | [diff] [blame] | 159 | set.dispose(); |
csalter | c04ca3d | 2006-01-10 00:18:18 +0000 | [diff] [blame] | 160 | // requestor.endReporting(); |
| 161 | if (monitor != null) |
| 162 | monitor.done(); |
| 163 | } |
| 164 | } |
| 165 | |
csalter | 404d183 | 2006-02-09 05:30:50 +0000 | [diff] [blame] | 166 | /** |
| 167 | * Searches for matches of a given search pattern. Search patterns can be created |
| 168 | * using factory methods and encapsulate the description of the information to be |
| 169 | * searched for (for example, element declarations of a specified type, in a case |
| 170 | * sensitive manner). |
| 171 | * @param pattern |
| 172 | * The pattern describing the information to search for |
| 173 | * @param requestor |
| 174 | * Callback object to notify with the results of the search (each match |
| 175 | * is reported to {@link SearchRequestor#acceptSearchMatch(SearchMatch)}) |
| 176 | * @param scope |
| 177 | * Optional search scope to limit the source of search candidates; |
| 178 | * specify <code>null</code> to search the entire workspace |
| 179 | * @param searchOptions |
| 180 | * Optional map of options and values defining behavior of the search; |
| 181 | * some options and values are provided by {@link ISearchOptions} |
| 182 | * @param monitor |
| 183 | * Optional progress monitor used to report work completed |
| 184 | * @exception CoreException |
| 185 | * if the search fails |
| 186 | */ |
csalter | c04ca3d | 2006-01-10 00:18:18 +0000 | [diff] [blame] | 187 | public void search(SearchPattern pattern, SearchRequestor requestor, |
csalter | 404d183 | 2006-02-09 05:30:50 +0000 | [diff] [blame] | 188 | SearchScope scope, Map searchOptions, IProgressMonitor monitor) |
| 189 | throws CoreException |
csalter | c04ca3d | 2006-01-10 00:18:18 +0000 | [diff] [blame] | 190 | { |
csalter | 404d183 | 2006-02-09 05:30:50 +0000 | [diff] [blame] | 191 | SearchParticipant[] participants = |
| 192 | getApplicableParticipants(pattern, searchOptions); |
csalter | 60974e1 | 2006-05-30 05:03:03 +0000 | [diff] [blame] | 193 | //System.out.println("participants = " + participants.length); |
csalter | 404d183 | 2006-02-09 05:30:50 +0000 | [diff] [blame] | 194 | search(pattern, requestor, participants, scope, searchOptions, monitor); |
csalter | c04ca3d | 2006-01-10 00:18:18 +0000 | [diff] [blame] | 195 | } |
| 196 | |
csalter | 404d183 | 2006-02-09 05:30:50 +0000 | [diff] [blame] | 197 | /** |
| 198 | * Queries the set of participants that support searches described by the |
| 199 | * specified search pattern and options. |
| 200 | * @param pattern |
| 201 | * The pattern describing the information to search for |
| 202 | * @param searchOptions |
| 203 | * Optional map of options and values defining behavior of the search; |
| 204 | * some options and values are provided by {@link ISearchOptions} |
| 205 | * @return Array of applicable search participants |
| 206 | */ |
| 207 | public SearchParticipant[] getApplicableParticipants(SearchPattern pattern, |
| 208 | Map searchOptions) |
csalter | c04ca3d | 2006-01-10 00:18:18 +0000 | [diff] [blame] | 209 | { |
csalter | 404d183 | 2006-02-09 05:30:50 +0000 | [diff] [blame] | 210 | return SearchPlugin.getDefault().loadSearchParticipants(pattern, searchOptions); |
csalter | c04ca3d | 2006-01-10 00:18:18 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | } |