1 11 package org.eclipse.search.internal.core; 12 13 import java.io.File ; 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.Set ; 19 import java.util.regex.Matcher ; 20 import java.util.regex.Pattern ; 21 22 import org.eclipse.core.runtime.IAdaptable; 23 import org.eclipse.core.runtime.IPath; 24 25 import org.eclipse.core.resources.IResource; 26 import org.eclipse.core.resources.ResourcesPlugin; 27 28 import org.eclipse.jface.util.Assert; 29 30 import org.eclipse.ui.IWorkingSet; 31 32 import org.eclipse.search.internal.core.text.PatternConstructor; 33 import org.eclipse.search.internal.ui.SearchMessages; 34 35 public class SearchScope { 36 37 41 public static SearchScope newWorkspaceScope() { 42 return new SearchScope(SearchMessages.WorkspaceScope, new IResource[] { ResourcesPlugin.getWorkspace().getRoot() }); 43 } 44 45 51 public static SearchScope newSearchScope(String description, IResource[] resources) { 52 return new SearchScope(description, removeRedundantEntries(resources)); 53 } 54 55 61 public static SearchScope newSearchScope(String description, IWorkingSet[] workingSets) { 62 return new SearchScope(description, convertToResources(workingSets)); 63 } 64 65 private static final boolean IS_CASE_SENSITIVE_FILESYSTEM = !new File ("Temp").equals(new File ("temp")); 67 private String fDescription; 68 private final IResource[] fRootElements; 69 70 private Set fFileNamePatterns= new HashSet (3); 71 private Matcher [] fFileNameMatchers= null; 72 73 private SearchScope(String description, IResource[] resources) { 74 Assert.isNotNull(description); 75 fDescription= description; 76 fRootElements= resources; 77 } 78 79 83 public String getDescription() { 84 return fDescription; 85 } 86 87 91 public IResource[] getRootElements() { 92 return fRootElements; 93 } 94 95 99 public void addFileNamePattern(String pattern) { 100 if (fFileNamePatterns.add(pattern)) { 101 fFileNameMatchers= null; } 103 } 104 105 private Matcher [] getFileNameMatchers() { 106 if (fFileNameMatchers == null) { 107 fFileNameMatchers= new Matcher [fFileNamePatterns.size()]; 108 int i= 0; 109 for (Iterator iter= fFileNamePatterns.iterator(); iter.hasNext();) { 110 String ext= (String ) iter.next(); 111 Pattern pattern= PatternConstructor.createPattern(ext, IS_CASE_SENSITIVE_FILESYSTEM, false); 112 fFileNameMatchers[i++]= pattern.matcher(""); } 114 } 115 return fFileNameMatchers; 116 } 117 118 123 public boolean matchesFileName(String fileName) { 124 Matcher [] matchers= getFileNameMatchers(); 125 for (int i= 0; i < matchers.length; i++) { 126 if (matchers[i].reset(fileName).matches()) { 127 return true; 128 } 129 } 130 return matchers.length == 0; 131 } 132 133 137 public String getFileNamePatternDescription() { 138 String [] ext= (String []) fFileNamePatterns.toArray(new String [fFileNamePatterns.size()]); 139 Arrays.sort(ext); 140 StringBuffer buf= new StringBuffer (); 141 for (int i= 0; i < ext.length; i++) { 142 if (i > 0) { 143 buf.append(", "); } 145 buf.append(ext[i]); 146 } 147 return buf.toString(); 148 } 149 150 151 private static IResource[] removeRedundantEntries(IResource[] elements) { 152 ArrayList res= new ArrayList (); 153 for (int i= 0; i < elements.length; i++) { 154 IResource curr= elements[i]; 155 addToList(res, curr); 156 } 157 return (IResource[])res.toArray(new IResource[res.size()]); 158 } 159 160 private static IResource[] convertToResources(IWorkingSet[] workingSets) { 161 ArrayList res= new ArrayList (); 162 for (int i= 0; i < workingSets.length; i++) { 163 IAdaptable[] elements= workingSets[i].getElements(); 164 for (int k= 0; k < elements.length; k++) { 165 IResource curr= (IResource) elements[k].getAdapter(IResource.class); 166 if (curr != null) { 167 addToList(res, curr); 168 } 169 } 170 } 171 return (IResource[]) res.toArray(new IResource[res.size()]); 172 } 173 174 private static void addToList(ArrayList res, IResource curr) { 175 IPath currPath= curr.getFullPath(); 176 for (int k= res.size() - 1; k >= 0 ; k--) { 177 IResource other= (IResource) res.get(k); 178 IPath otherPath= other.getFullPath(); 179 if (otherPath.isPrefixOf(currPath)) { 180 return; 181 } 182 if (currPath.isPrefixOf(otherPath)) { 183 res.remove(k); 184 } 185 } 186 res.add(curr); 187 } 188 } 189 | Popular Tags |