1 11 12 package org.eclipse.search.ui.text; 13 14 import java.io.File ; 15 import java.util.ArrayList ; 16 import java.util.Arrays ; 17 import java.util.regex.Matcher ; 18 import java.util.regex.Pattern ; 19 20 import org.eclipse.core.runtime.IAdaptable; 21 import org.eclipse.core.runtime.IPath; 22 import org.eclipse.core.runtime.content.IContentType; 23 24 import org.eclipse.core.resources.IResource; 25 import org.eclipse.core.resources.IResourceProxy; 26 import org.eclipse.core.resources.ResourcesPlugin; 27 28 import org.eclipse.ui.IWorkingSet; 29 30 import org.eclipse.search.core.text.TextSearchScope; 31 32 import org.eclipse.search.internal.core.text.PatternConstructor; 33 import org.eclipse.search.internal.ui.Messages; 34 import org.eclipse.search.internal.ui.SearchMessages; 35 import org.eclipse.search.internal.ui.WorkingSetComparator; 36 import org.eclipse.search.internal.ui.util.FileTypeEditor; 37 38 47 public final class FileTextSearchScope extends TextSearchScope { 48 49 private static final boolean IS_CASE_SENSITIVE_FILESYSTEM = !new File ("Temp").equals(new File ("temp")); 51 60 public static FileTextSearchScope newWorkspaceScope(String [] fileNamePatterns, boolean includeDerived) { 61 return new FileTextSearchScope(SearchMessages.WorkspaceScope, new IResource[] { ResourcesPlugin.getWorkspace().getRoot() }, null, fileNamePatterns, includeDerived); 62 } 63 64 74 public static FileTextSearchScope newSearchScope(IResource[] roots, String [] fileNamePatterns, boolean includeDerived) { 75 roots= removeRedundantEntries(roots, includeDerived); 76 77 String description; 78 if (roots.length == 0) { 79 description= SearchMessages.FileTextSearchScope_scope_empty; 80 } else if (roots.length == 1) { 81 String label= SearchMessages.FileTextSearchScope_scope_single; 82 description= Messages.format(label, roots[0].getName()); 83 } else if (roots.length == 2) { 84 String label= SearchMessages.FileTextSearchScope_scope_double; 85 description= Messages.format(label, new String [] { roots[0].getName(), roots[1].getName()}); 86 } else { 87 String label= SearchMessages.FileTextSearchScope_scope_multiple; 88 description= Messages.format(label, new String [] { roots[0].getName(), roots[1].getName()}); 89 } 90 return new FileTextSearchScope(description, roots, null, fileNamePatterns, includeDerived); 91 } 92 93 103 public static FileTextSearchScope newSearchScope(IWorkingSet[] workingSets, String [] fileNamePatterns, boolean includeDerived) { 104 String description; 105 Arrays.sort(workingSets, new WorkingSetComparator()); 106 if (workingSets.length == 0) { 107 description= SearchMessages.FileTextSearchScope_ws_scope_empty; 108 } else if (workingSets.length == 1) { 109 String label= SearchMessages.FileTextSearchScope_ws_scope_single; 110 description= Messages.format(label, workingSets[0].getLabel()); 111 } else if (workingSets.length == 2) { 112 String label= SearchMessages.FileTextSearchScope_ws_scope_double; 113 description= Messages.format(label, new String [] { workingSets[0].getLabel(), workingSets[1].getLabel()}); 114 } else { 115 String label= SearchMessages.FileTextSearchScope_ws_scope_multiple; 116 description= Messages.format(label, new String [] { workingSets[0].getLabel(), workingSets[1].getLabel()}); 117 } 118 FileTextSearchScope scope= new FileTextSearchScope(description, convertToResources(workingSets, includeDerived), workingSets, fileNamePatterns, includeDerived); 119 return scope; 120 } 121 122 private final String fDescription; 123 private final IResource[] fRootElements; 124 private final String [] fFileNamePatterns; 125 private final Matcher fPositiveFileNameMatcher; 126 private final Matcher fNegativeFileNameMatcher; 127 128 private boolean fVisitDerived; 129 private IWorkingSet[] fWorkingSets; 130 131 private FileTextSearchScope(String description, IResource[] resources, IWorkingSet[] workingSets, String [] fileNamePatterns, boolean visitDerived) { 132 fDescription= description; 133 fRootElements= resources; 134 fFileNamePatterns= fileNamePatterns; 135 fVisitDerived= visitDerived; 136 fWorkingSets= workingSets; 137 fPositiveFileNameMatcher= createMatcher(fileNamePatterns, false); 138 fNegativeFileNameMatcher= createMatcher(fileNamePatterns, true); 139 } 140 141 146 public String getDescription() { 147 return fDescription; 148 } 149 150 156 public String [] getFileNamePatterns() { 157 return fFileNamePatterns; 158 } 159 160 166 public IWorkingSet[] getWorkingSets() { 167 return fWorkingSets; 168 } 169 170 176 public IContentType[] getContentTypes() { 177 return null; } 179 180 185 public String getFilterDescription() { 186 String [] ext= fFileNamePatterns; 187 if (ext == null) { 188 return "*"; } 190 Arrays.sort(ext); 191 StringBuffer buf= new StringBuffer (); 192 for (int i= 0; i < ext.length; i++) { 193 if (i > 0) { 194 buf.append(", "); } 196 buf.append(ext[i]); 197 } 198 return buf.toString(); 199 } 200 201 206 public boolean includeDerived() { 207 return fVisitDerived; 208 } 209 210 213 public IResource[] getRoots() { 214 return fRootElements; 215 } 216 217 220 public boolean contains(IResourceProxy proxy) { 221 if (!fVisitDerived && proxy.isDerived()) { 222 return false; } 224 225 if (proxy.getType() == IResource.FILE) { 226 return matchesFileName(proxy.getName()); 227 } 228 return true; 229 } 230 231 private boolean matchesFileName(String fileName) { 232 if (fPositiveFileNameMatcher != null && !fPositiveFileNameMatcher.reset(fileName).matches()) { 233 return false; 234 } 235 if (fNegativeFileNameMatcher != null && fNegativeFileNameMatcher.reset(fileName).matches()) { 236 return false; 237 } 238 return true; 239 } 240 241 private Matcher createMatcher(String [] fileNamePatterns, boolean negativeMatcher) { 242 if (fileNamePatterns == null || fileNamePatterns.length == 0) { 243 return null; 244 } 245 ArrayList patterns= new ArrayList (); 246 for (int i= 0; i < fileNamePatterns.length; i++) { 247 String pattern= fFileNamePatterns[i]; 248 if (negativeMatcher == pattern.startsWith(FileTypeEditor.FILE_PATTERN_NEGATOR)) { 249 if (negativeMatcher) { 250 pattern= pattern.substring(FileTypeEditor.FILE_PATTERN_NEGATOR.length()).trim(); 251 } 252 if (pattern.length() > 0) { 253 patterns.add(pattern); 254 } 255 } 256 } 257 if (!patterns.isEmpty()) { 258 String [] patternArray= (String []) patterns.toArray(new String [patterns.size()]); 259 Pattern pattern= PatternConstructor.createPattern(patternArray, IS_CASE_SENSITIVE_FILESYSTEM); 260 return pattern.matcher(""); } 262 return null; 263 } 264 265 private static IResource[] removeRedundantEntries(IResource[] elements, boolean includeDerived) { 266 ArrayList res= new ArrayList (); 267 for (int i= 0; i < elements.length; i++) { 268 IResource curr= elements[i]; 269 addToList(res, curr, includeDerived); 270 } 271 return (IResource[])res.toArray(new IResource[res.size()]); 272 } 273 274 private static IResource[] convertToResources(IWorkingSet[] workingSets, boolean includeDerived) { 275 ArrayList res= new ArrayList (); 276 for (int i= 0; i < workingSets.length; i++) { 277 IWorkingSet workingSet= workingSets[i]; 278 if (workingSet.isAggregateWorkingSet() && workingSet.isEmpty()) { 279 return new IResource[] { ResourcesPlugin.getWorkspace().getRoot() }; 280 } 281 IAdaptable[] elements= workingSet.getElements(); 282 for (int k= 0; k < elements.length; k++) { 283 IResource curr= (IResource) elements[k].getAdapter(IResource.class); 284 if (curr != null) { 285 addToList(res, curr, includeDerived); 286 } 287 } 288 } 289 return (IResource[]) res.toArray(new IResource[res.size()]); 290 } 291 292 private static void addToList(ArrayList res, IResource curr, boolean includeDerived) { 293 if (!includeDerived && isDerived(curr)) { 294 return; 295 } 296 IPath currPath= curr.getFullPath(); 297 for (int k= res.size() - 1; k >= 0 ; k--) { 298 IResource other= (IResource) res.get(k); 299 IPath otherPath= other.getFullPath(); 300 if (otherPath.isPrefixOf(currPath)) { 301 return; 302 } 303 if (currPath.isPrefixOf(otherPath)) { 304 res.remove(k); 305 } 306 } 307 res.add(curr); 308 } 309 310 private static boolean isDerived(IResource curr) { 311 do { 312 if (curr.isDerived()) { 313 return true; 314 } 315 curr= curr.getParent(); 316 } while (curr != null); 317 return false; 318 } 319 } 320 | Popular Tags |