1 11 package org.eclipse.search.internal.core.text; 12 13 import java.util.Arrays ; 14 import java.util.HashSet ; 15 import java.util.Iterator ; 16 import java.util.Set ; 17 import java.util.regex.Matcher ; 18 import java.util.regex.Pattern ; 19 20 import org.eclipse.core.resources.IFile; 21 import org.eclipse.core.resources.IResource; 22 import org.eclipse.core.resources.IResourceProxy; 23 import org.eclipse.core.runtime.IAdaptable; 24 25 import org.eclipse.ui.IWorkingSet; 26 27 import org.eclipse.search.internal.core.SearchScope; 28 import org.eclipse.search.internal.ui.SearchMessages; 29 30 33 public class TextSearchScope extends SearchScope { 34 35 private static class WorkspaceScope extends TextSearchScope { 36 37 private WorkspaceScope() { 38 super(SearchMessages.getString("WorkspaceScope")); } 40 41 public void add(IResource element) { 42 } 44 45 public boolean encloses(IResourceProxy proxy) { 46 if (proxy.getType() == IResource.FILE && skipFile(proxy)) 47 return false; 48 return true; 49 } 50 } 51 52 private Set fExtensions= new HashSet (3); 53 54 57 public static TextSearchScope newWorkspaceScope() { 58 return new WorkspaceScope(); 59 } 60 61 public TextSearchScope(String description) { 62 super(description); 63 } 64 65 public TextSearchScope(String description, IResource[] resources) { 66 super(description, resources); 67 } 68 69 public TextSearchScope(String description, IAdaptable[] elements) { 70 super(description, convertToResources(elements)); 71 72 } 73 74 public TextSearchScope(String description, IWorkingSet[] workingSets) { 75 super(description, convertToResources(getElements(workingSets))); 76 } 77 78 private static IResource[] convertToResources(IAdaptable[] elements) { 79 int length= elements.length; 80 Set resources= new HashSet (length); 81 for (int i= 0; i < length; i++) { 82 IResource resource= (IResource)elements[i].getAdapter(IResource.class); 83 if (resource != null) 84 resources.add(resource); 85 } 86 return (IResource[])resources.toArray(new IResource[resources.size()]); 87 } 88 89 private static IAdaptable[] getElements(IWorkingSet[] workingSets) { 90 int length= workingSets.length; 91 Set elements= new HashSet (length); 92 for (int i= 0; i < length; i++) { 93 elements.addAll(Arrays.asList(workingSets[i].getElements())); 94 } 95 return (IAdaptable[])elements.toArray(new IAdaptable[elements.size()]); 96 } 97 98 101 public void addExtension(String extension) { 102 Pattern pattern= Pattern.compile(asRegEx(extension), Pattern.CASE_INSENSITIVE); 103 fExtensions.add(pattern.matcher("")); } 105 106 109 private String asRegEx(String pattern) { 110 111 StringBuffer out= new StringBuffer (pattern.length()); 112 113 boolean escaped= false; 114 boolean quoting= false; 115 116 int i= 0; 117 while (i < pattern.length()) { 118 char ch= pattern.charAt(i++); 119 120 if (ch == '*' && !escaped) { 121 if (quoting) { 122 out.append("\\E"); quoting= false; 124 } 125 out.append(".*"); escaped= false; 127 continue; 128 } else if (ch == '?' && !escaped) { 129 if (quoting) { 130 out.append("\\E"); quoting= false; 132 } 133 out.append("."); escaped= false; 135 continue; 136 } else if (ch == '\\' && !escaped) { 137 escaped= true; 138 continue; 139 140 } else if (ch == '\\' && escaped) { 141 escaped= false; 142 if (quoting) { 143 out.append("\\E"); quoting= false; 145 } 146 out.append("\\\\"); continue; 148 } 149 150 if (!quoting) { 151 out.append("\\Q"); quoting= true; 153 } 154 if (escaped && ch != '*' && ch != '?' && ch != '\\') 155 out.append('\\'); 156 out.append(ch); 157 escaped= ch == '\\'; 158 159 } 160 if (quoting) 161 out.append("\\E"); 163 return out.toString(); 164 } 165 166 171 public void addExtensions(Set extensions) { 172 if (extensions == null) 173 return; 174 Iterator iter= extensions.iterator(); 175 while (iter.hasNext()) { 176 Object obj= iter.next(); 177 if (obj instanceof String ) 178 addExtension((String )obj); 179 } 180 } 181 182 185 public boolean encloses(IResourceProxy proxy) { 186 if (proxy.getType() == IResource.FILE && skipFile(proxy)) 187 return false; 188 return super.encloses(proxy); 189 } 190 191 boolean skipFile(IResourceProxy proxy) { 192 if (proxy != null) { 193 Iterator iter= fExtensions.iterator(); 194 while (iter.hasNext()) { 195 if (((Matcher )iter.next()).reset(proxy.getName()).matches()) 196 return false; 197 } 198 } 199 return true; 200 } 201 202 207 public boolean encloses(IResource element) { 208 if (element.getType() == IResource.FILE && skipFile((IFile)element)) 209 return false; 210 return super.encloses(element); 211 } 212 213 boolean skipFile(IFile file) { 214 if (file != null) { 215 Iterator iter= fExtensions.iterator(); 216 while (iter.hasNext()) { 217 if (((Matcher )iter.next()).reset(file.getName()).matches()) 218 return false; 219 } 220 } 221 return true; 222 } 223 } 224 | Popular Tags |