1 11 package org.eclipse.search.internal.ui.text; 12 13 import java.util.ArrayList ; 14 import java.util.regex.Pattern ; 15 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.IProgressMonitor; 18 import org.eclipse.core.runtime.IStatus; 19 20 import org.eclipse.core.resources.IFile; 21 import org.eclipse.core.resources.IResource; 22 23 import org.eclipse.search.ui.ISearchQuery; 24 import org.eclipse.search.ui.ISearchResult; 25 import org.eclipse.search.ui.text.AbstractTextSearchResult; 26 import org.eclipse.search.ui.text.FileTextSearchScope; 27 import org.eclipse.search.ui.text.Match; 28 29 import org.eclipse.search.core.text.TextSearchEngine; 30 import org.eclipse.search.core.text.TextSearchMatchAccess; 31 import org.eclipse.search.core.text.TextSearchRequestor; 32 33 import org.eclipse.search.internal.core.text.PatternConstructor; 34 import org.eclipse.search.internal.ui.Messages; 35 import org.eclipse.search.internal.ui.SearchMessages; 36 37 38 public class FileSearchQuery implements ISearchQuery { 39 40 private final static class TextSearchResultCollector extends TextSearchRequestor { 41 42 private final AbstractTextSearchResult fResult; 43 private final boolean fIsFileSearchOnly; 44 private final boolean fSearchInBinaries; 45 private ArrayList fCachedMatches; 46 47 private TextSearchResultCollector(AbstractTextSearchResult result, boolean isFileSearchOnly, boolean searchInBinaries) { 48 fResult= result; 49 fIsFileSearchOnly= isFileSearchOnly; 50 fSearchInBinaries= searchInBinaries; 51 52 } 53 54 public boolean acceptFile(IFile file) throws CoreException { 55 if (fIsFileSearchOnly) { 56 fResult.addMatch(new FileMatch(file, 0, 0)); 57 } 58 flushMatches(); 59 return true; 60 } 61 62 65 public boolean reportBinaryFile(IFile file) { 66 return fSearchInBinaries; 67 } 68 69 public boolean acceptPatternMatch(TextSearchMatchAccess matchRequestor) throws CoreException { 70 fCachedMatches.add(new FileMatch(matchRequestor.getFile(), matchRequestor.getMatchOffset(), matchRequestor.getMatchLength())); 71 return true; 72 } 73 74 public void beginReporting() { 75 fCachedMatches= new ArrayList (); 76 } 77 78 public void endReporting() { 79 flushMatches(); 80 fCachedMatches= null; 81 } 82 83 private void flushMatches() { 84 if (!fCachedMatches.isEmpty()) { 85 fResult.addMatches((Match[]) fCachedMatches.toArray(new Match[fCachedMatches.size()])); 86 fCachedMatches.clear(); 87 } 88 } 89 } 90 91 private final FileTextSearchScope fScope; 92 private final String fSearchText; 93 private final boolean fIsRegEx; 94 private final boolean fIsCaseSensitive; 95 96 private FileSearchResult fResult; 97 98 public FileSearchQuery(String searchText, boolean isRegEx, boolean isCaseSensitive, FileTextSearchScope scope) { 99 fSearchText= searchText; 100 fIsRegEx= isRegEx; 101 fIsCaseSensitive= isCaseSensitive; 102 fScope= scope; 103 } 104 105 public FileTextSearchScope getSearchScope() { 106 return fScope; 107 } 108 109 public boolean canRunInBackground() { 110 return true; 111 } 112 113 public IStatus run(final IProgressMonitor monitor) { 114 AbstractTextSearchResult textResult= (AbstractTextSearchResult) getSearchResult(); 115 textResult.removeAll(); 116 117 Pattern searchPattern= getSearchPattern(); 118 boolean isFileSearchOnly= searchPattern.pattern().length() == 0; 119 boolean searchInBinaries= !isScopeAllFileTypes(); 120 121 TextSearchResultCollector collector= new TextSearchResultCollector(textResult, isFileSearchOnly, searchInBinaries); 122 return TextSearchEngine.create().search(fScope, collector, searchPattern, monitor); 123 } 124 125 private boolean isScopeAllFileTypes() { 126 String [] fileNamePatterns= fScope.getFileNamePatterns(); 127 if (fileNamePatterns == null) 128 return true; 129 for (int i= 0; i < fileNamePatterns.length; i++) { 130 if ("*".equals(fileNamePatterns[i])) { return true; 132 } 133 } 134 return false; 135 } 136 137 138 public String getLabel() { 139 return SearchMessages.FileSearchQuery_label; 140 } 141 142 public String getSearchString() { 143 return fSearchText; 144 } 145 146 public String getResultLabel(int nMatches) { 147 String searchString= getSearchString(); 148 if (searchString.length() > 0) { 149 if (isScopeAllFileTypes()) { 151 if (nMatches == 1) { 153 Object [] args= { searchString, fScope.getDescription() }; 154 return Messages.format(SearchMessages.FileSearchQuery_singularLabel, args); 155 } 156 Object [] args= { searchString, new Integer (nMatches), fScope.getDescription() }; 157 return Messages.format(SearchMessages.FileSearchQuery_pluralPattern, args); 158 } 159 if (nMatches == 1) { 161 Object [] args= { searchString, fScope.getDescription(), fScope.getFilterDescription() }; 162 return Messages.format(SearchMessages.FileSearchQuery_singularPatternWithFileExt, args); 163 } 164 Object [] args= { searchString, new Integer (nMatches), fScope.getDescription(), fScope.getFilterDescription() }; 165 return Messages.format(SearchMessages.FileSearchQuery_pluralPatternWithFileExt, args); 166 } 167 if (nMatches == 1) { 169 Object [] args= { fScope.getFilterDescription(), fScope.getDescription() }; 170 return Messages.format(SearchMessages.FileSearchQuery_singularLabel_fileNameSearch, args); 171 } 172 Object [] args= { fScope.getFilterDescription(), new Integer (nMatches), fScope.getDescription() }; 173 return Messages.format(SearchMessages.FileSearchQuery_pluralPattern_fileNameSearch, args); 174 } 175 176 182 public IStatus searchInFile(final AbstractTextSearchResult result, final IProgressMonitor monitor, IFile file) { 183 FileTextSearchScope scope= FileTextSearchScope.newSearchScope(new IResource[] { file }, new String [] { "*" }, true); 185 Pattern searchPattern= getSearchPattern(); 186 boolean isFileSearchOnly= searchPattern.pattern().length() == 0; 187 TextSearchResultCollector collector= new TextSearchResultCollector(result, isFileSearchOnly, true); 188 189 return TextSearchEngine.create().search(scope, collector, searchPattern, monitor); 190 } 191 192 protected Pattern getSearchPattern() { 193 return PatternConstructor.createPattern(fSearchText, fIsCaseSensitive, fIsRegEx); 194 } 195 196 public boolean isRegexSearch() { 197 return fIsRegEx; 198 } 199 200 public boolean isCaseSensitive() { 201 return fIsCaseSensitive; 202 } 203 204 public boolean canRerun() { 205 return true; 206 } 207 208 public ISearchResult getSearchResult() { 209 if (fResult == null) { 210 fResult= new FileSearchResult(this); 211 new SearchResultUpdater(fResult); 212 } 213 return fResult; 214 } 215 } 216 | Popular Tags |