KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search > internal > core > text > MatchLocator


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.search.internal.core.text;
12
13 import java.util.regex.Matcher JavaDoc;
14 import java.util.regex.Pattern JavaDoc;
15 import java.util.regex.PatternSyntaxException JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.OperationCanceledException;
20
21 import org.eclipse.core.resources.IResourceProxy;
22
23 import org.eclipse.search.internal.ui.SearchMessages;
24
25 /**
26  * A class finding matches within a file.
27  * @since 3.0
28  */

29 public class MatchLocator {
30     
31     private Matcher JavaDoc fMatcher;
32     
33     public MatchLocator(Pattern JavaDoc pattern) {
34         fMatcher= pattern.matcher(""); //$NON-NLS-1$
35
}
36     
37     public MatchLocator(String JavaDoc pattern, boolean isCaseSensitive, boolean isRegexSearch) throws PatternSyntaxException JavaDoc {
38         this(PatternConstructor.createPattern(pattern, isCaseSensitive, isRegexSearch));
39     }
40     
41     public boolean isEmpty() {
42         return fMatcher.pattern().pattern().length() == 0;
43     }
44     
45     public void locateMatches(IProgressMonitor progressMonitor, CharSequence JavaDoc searchInput, ITextSearchResultCollector collector, IResourceProxy proxy) throws CoreException {
46         fMatcher.reset(searchInput);
47         int k= 0;
48         while (fMatcher.find()) {
49             int start= fMatcher.start();
50             int end= fMatcher.end();
51             if (end != start) { // don't report 0-length matches
52
collector.accept(proxy, start, end - start);
53             }
54             if (k++ == 20) {
55                 if (progressMonitor.isCanceled()) {
56                     throw new OperationCanceledException(SearchMessages.TextSearchVisitor_canceled);
57                 }
58                 k= 0;
59             }
60         }
61     }
62 }
63
Popular Tags