blob: 3fed117411e8c48cf11bf260ee8cc16704c15b77 [file] [log] [blame]
csalterc04ca3d2006-01-10 00:18:18 +00001/*******************************************************************************
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
12package org.eclipse.wst.common.core.search;
13
csalter404d1832006-02-09 05:30:50 +000014import java.util.Map;
15
csalterc04ca3d2006-01-10 00:18:18 +000016import org.eclipse.core.runtime.CoreException;
17import org.eclipse.core.runtime.IProgressMonitor;
18import org.eclipse.core.runtime.OperationCanceledException;
19import org.eclipse.core.runtime.SubProgressMonitor;
20import org.eclipse.wst.common.core.search.document.SearchDocumentSet;
21import org.eclipse.wst.common.core.search.internal.Messages;
22import org.eclipse.wst.common.core.search.internal.SearchDocumentSetImpl;
23import org.eclipse.wst.common.core.search.pattern.SearchPattern;
24import org.eclipse.wst.common.core.search.scope.SearchScope;
csalterc04ca3d2006-01-10 00:18:18 +000025
26/**
csalter404d1832006-02-09 05:30:50 +000027 * 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>
csalterc04ca3d2006-01-10 00:18:18 +000036 * This class may be instantiated; it is not intended to be subclassed.
kchong443ced42006-01-11 16:57:16 +000037 *
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>
csalterc04ca3d2006-01-10 00:18:18 +000044 */
csalter404d1832006-02-09 05:30:50 +000045public class SearchEngine implements ISearchOptions
csalterc04ca3d2006-01-10 00:18:18 +000046{
47
48 /**
csalter404d1832006-02-09 05:30:50 +000049 * 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).
csalterc04ca3d2006-01-10 00:18:18 +000054 * @param pattern
csalter404d1832006-02-09 05:30:50 +000055 * The pattern describing the information to search for
csalterc04ca3d2006-01-10 00:18:18 +000056 * @param requestor
csalter404d1832006-02-09 05:30:50 +000057 * 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}
csalterc04ca3d2006-01-10 00:18:18 +000067 * @param monitor
csalter404d1832006-02-09 05:30:50 +000068 * Optional progress monitor used to report work completed
csalterc04ca3d2006-01-10 00:18:18 +000069 * @exception CoreException
csalter404d1832006-02-09 05:30:50 +000070 * if the search fails
csalterc04ca3d2006-01-10 00:18:18 +000071 */
72 public void search(SearchPattern pattern, SearchRequestor requestor,
csalter404d1832006-02-09 05:30:50 +000073 SearchParticipant[] participants, SearchScope scope, Map searchOptions,
csalterc04ca3d2006-01-10 00:18:18 +000074 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
csalter0de6b152006-06-06 05:17:51 +000084 SearchDocumentSet set = new SearchDocumentSetImpl();
csalterc04ca3d2006-01-10 00:18:18 +000085 try
86 {
87 // requestor.beginReporting();
csalterc04ca3d2006-01-10 00:18:18 +000088 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() }));
csalter404d1832006-02-09 05:30:50 +0000106 participant.beginSearching(pattern, searchOptions);
csalterc04ca3d2006-01-10 00:18:18 +0000107 // requestor.enterParticipant(participant);
108 // participant creates it's own search scope
csalter404d1832006-02-09 05:30:50 +0000109 SearchScope newScope =
110 participant.selectDocumentLocations(pattern, scope, searchOptions, monitor);
csalterc04ca3d2006-01-10 00:18:18 +0000111 scopeArray[i] = newScope;
112 // participant creates search documents based on it's search scope
csalter404d1832006-02-09 05:30:50 +0000113 participant.createSearchDocuments(set, pattern, newScope, searchOptions, subMonitor);
csalterc04ca3d2006-01-10 00:18:18 +0000114 }
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
kchong2f606532006-08-11 20:18:52 +0000140
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
csalterc04ca3d2006-01-10 00:18:18 +0000150 }
151 finally
152 {
153 // requestor.exitParticipant(participant);
csalter404d1832006-02-09 05:30:50 +0000154 participant.doneSearching(pattern, searchOptions);
csalter0de6b152006-06-06 05:17:51 +0000155 }
csalterc04ca3d2006-01-10 00:18:18 +0000156 }
157 } finally
158 {
csalter0de6b152006-06-06 05:17:51 +0000159 set.dispose();
csalterc04ca3d2006-01-10 00:18:18 +0000160 // requestor.endReporting();
161 if (monitor != null)
162 monitor.done();
163 }
164 }
165
csalter404d1832006-02-09 05:30:50 +0000166 /**
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 */
csalterc04ca3d2006-01-10 00:18:18 +0000187 public void search(SearchPattern pattern, SearchRequestor requestor,
csalter404d1832006-02-09 05:30:50 +0000188 SearchScope scope, Map searchOptions, IProgressMonitor monitor)
189 throws CoreException
csalterc04ca3d2006-01-10 00:18:18 +0000190 {
csalter404d1832006-02-09 05:30:50 +0000191 SearchParticipant[] participants =
192 getApplicableParticipants(pattern, searchOptions);
csalter60974e12006-05-30 05:03:03 +0000193 //System.out.println("participants = " + participants.length);
csalter404d1832006-02-09 05:30:50 +0000194 search(pattern, requestor, participants, scope, searchOptions, monitor);
csalterc04ca3d2006-01-10 00:18:18 +0000195 }
196
csalter404d1832006-02-09 05:30:50 +0000197 /**
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)
csalterc04ca3d2006-01-10 00:18:18 +0000209 {
csalter404d1832006-02-09 05:30:50 +0000210 return SearchPlugin.getDefault().loadSearchParticipants(pattern, searchOptions);
csalterc04ca3d2006-01-10 00:18:18 +0000211 }
212
213}