blob: dc4006167e26640d4898bce91ef1f649a312afff [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
14import org.eclipse.core.runtime.CoreException;
15import org.eclipse.core.runtime.IProgressMonitor;
16import org.eclipse.core.runtime.OperationCanceledException;
17import org.eclipse.core.runtime.SubProgressMonitor;
18import org.eclipse.wst.common.core.search.document.SearchDocumentSet;
19import org.eclipse.wst.common.core.search.internal.Messages;
20import org.eclipse.wst.common.core.search.internal.SearchDocumentSetImpl;
21import org.eclipse.wst.common.core.search.pattern.SearchPattern;
22import org.eclipse.wst.common.core.search.scope.SearchScope;
23import org.eclipse.wst.common.core.search.util.CollectingSearchRequestor;
24
25/**
26 * A {@link SearchEngine} searches for the file references, component
27 * declarations and references, provided they have a quialified name and a
28 * component description. The search can be limited to a search scope. By
29 * default, whole workspace is searched.
30 *
31 * The search engine also provides a generic way of accessing the search
32 * function.
33 *
34 * {@link SearchRequestor} is expected to be passed in when performing searches
35 * and a client can use {@link CollectingSearchRequestor} to access the results
36 * of the search.
37 *
38 * This class may be instantiated; it is not intended to be subclassed.
39 */
40public class SearchEngine
41{
42
43 /**
44 * Searches for matches of a given search pattern using set participant and
45 * search scope. Search patterns can be created using factory methods and
46 * encapsulate the description of what is being searched (for example,
47 * search type declarations in a case sensitive way).
48 *
49 * @param pattern
50 * the pattern to search
51 * @param requestor
52 * the requestor to report the matches to
53 * @param monitor
54 * the progress monitor used to report progress
55 * @exception CoreException
56 * if the search failed.
57 */
58 public void search(SearchPattern pattern, SearchRequestor requestor,
59 SearchParticipant[] participants, SearchScope scope,
60 IProgressMonitor monitor) throws CoreException
61 {
62
63 if (monitor != null && monitor.isCanceled())
64 throw new OperationCanceledException();
65
66 /* initialize progress monitor */
67 if (monitor != null)
68 monitor.beginTask(Messages.engine_searching, 100);
69
70 try
71 {
72 // requestor.beginReporting();
73 SearchDocumentSet set = new SearchDocumentSetImpl();
74 SearchScope[] scopeArray = new SearchScope[participants.length];
75 for (int i = 0, l = participants == null ? 0 : participants.length; i < l; i++)
76 {
77 if (monitor != null && monitor.isCanceled())
78 throw new OperationCanceledException();
79
80 SearchParticipant participant = participants[i];
81 SubProgressMonitor subMonitor = monitor == null ? null
82 : new SubProgressMonitor(monitor, 1000);
83 if (subMonitor != null)
84 subMonitor.beginTask("", 1000); //$NON-NLS-1$
85 try
86 {
87 if (subMonitor != null)
88 subMonitor.subTask(Messages.bind(
89 Messages.engine_searching_locatingDocuments,
90 new String[]
91 { participant.getDescription() }));
92 participant.beginSearching(pattern);
93 // requestor.enterParticipant(participant);
94 // participant creates it's own search scope
95 SearchScope newScope = participant.selectDocumentLocations(pattern, scope, monitor);
96 scopeArray[i] = newScope;
97 // participant creates search documents based on it's search scope
98 participant.createSearchDocument(set, pattern, newScope, subMonitor);
99 }
100 catch(Exception e)
101 {
102 }
103 }
104 for (int i = 0, l = participants == null ? 0 : participants.length; i < l; i++)
105 {
106 if (monitor != null && monitor.isCanceled())
107 throw new OperationCanceledException();
108
109 SearchParticipant participant = participants[i];
110 SubProgressMonitor subMonitor = monitor == null ? null
111 : new SubProgressMonitor(monitor, 1000);
112 if (subMonitor != null && subMonitor.isCanceled())
113 throw new OperationCanceledException();
114 try
115 {
116 // locate index matches if any (note that all search matches
117 // could have been issued during index querying)
118 if (subMonitor != null)
119 subMonitor.subTask(Messages.bind(
120 Messages.engine_searching_matching,
121 new String[]
122 { participant.getDescription() }));
123 // a search document set should contain enough info to reduce the search scope even further
124 // before finding precize locations
125 participant.locateMatches(set, pattern, scopeArray[i], requestor, subMonitor);
126 }
127 finally
128 {
129 // requestor.exitParticipant(participant);
130 participant.doneSearching(pattern);
131 }
132 }
133 } finally
134 {
135 // requestor.endReporting();
136 if (monitor != null)
137 monitor.done();
138 }
139 }
140
141 public void search(SearchPattern pattern, SearchRequestor requestor,
142 SearchScope scope, IProgressMonitor monitor) throws CoreException
143 {
144 SearchParticipant[] participants = getApplicableParticipants(pattern);
145 search(pattern, requestor, participants, scope, monitor);
146 }
147
148 public SearchParticipant[] getApplicableParticipants(SearchPattern pattern)
149 {
150 return SearchPlugin.getDefault().loadSearchParticipants(pattern);
151 }
152
153}