1 11 package org.eclipse.search.internal.core.text; 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.Set ; 18 import java.util.regex.Matcher ; 19 import java.util.regex.Pattern ; 20 21 import org.eclipse.core.runtime.Assert; 22 import org.eclipse.core.runtime.IPath; 23 24 import org.eclipse.core.resources.IResource; 25 import org.eclipse.core.resources.IResourceProxy; 26 27 28 import org.eclipse.search.core.text.TextSearchScope; 29 30 public class FileNamePatternSearchScope extends TextSearchScope { 31 32 38 public static FileNamePatternSearchScope newSearchScope(String description, IResource[] resources, boolean includeDerived) { 39 return new FileNamePatternSearchScope(description, removeRedundantEntries(resources, includeDerived), includeDerived); 40 } 41 42 private static final boolean IS_CASE_SENSITIVE_FILESYSTEM = !new File ("Temp").equals(new File ("temp")); 44 private final String fDescription; 45 private final IResource[] fRootElements; 46 47 private final Set fFileNamePatterns; 48 private Matcher fFileNameMatcher; 49 50 private boolean fVisitDerived; 51 52 private FileNamePatternSearchScope(String description, IResource[] resources, boolean visitDerived) { 53 Assert.isNotNull(description); 54 fDescription= description; 55 fRootElements= resources; 56 fFileNamePatterns= new HashSet (3); 57 fFileNameMatcher= null; 58 fVisitDerived= visitDerived; 59 } 60 61 65 public String getDescription() { 66 return fDescription; 67 } 68 69 72 public IResource[] getRoots() { 73 return fRootElements; 74 } 75 76 79 public boolean contains(IResourceProxy proxy) { 80 if (!fVisitDerived && proxy.isDerived()) { 81 return false; } 83 84 if (proxy.getType() == IResource.FILE) { 85 return matchesFileName(proxy.getName()); 86 } 87 return true; 88 } 89 90 94 public void addFileNamePattern(String pattern) { 95 if (fFileNamePatterns.add(pattern)) { 96 fFileNameMatcher= null; } 98 } 99 100 public void setFileNamePattern(Pattern pattern) { 101 fFileNameMatcher= pattern.matcher(""); } 103 104 105 public Pattern getFileNamePattern() { 106 return getFileNameMatcher().pattern(); 107 } 108 109 114 public boolean isIncludeDerived() { 115 return fVisitDerived; 116 } 117 118 119 private Matcher getFileNameMatcher() { 120 if (fFileNameMatcher == null) { 121 Pattern pattern; 122 if (fFileNamePatterns.isEmpty()) { 123 pattern= Pattern.compile(".*"); } else { 125 String [] patternStrings= (String []) fFileNamePatterns.toArray(new String [fFileNamePatterns.size()]); 126 pattern= PatternConstructor.createPattern(patternStrings, IS_CASE_SENSITIVE_FILESYSTEM); 127 } 128 fFileNameMatcher= pattern.matcher(""); } 130 return fFileNameMatcher; 131 } 132 133 138 private boolean matchesFileName(String fileName) { 139 return getFileNameMatcher().reset(fileName).matches(); 140 } 141 142 146 public String getFileNamePatternDescription() { 147 String [] ext= (String []) fFileNamePatterns.toArray(new String [fFileNamePatterns.size()]); 148 Arrays.sort(ext); 149 StringBuffer buf= new StringBuffer (); 150 for (int i= 0; i < ext.length; i++) { 151 if (i > 0) { 152 buf.append(", "); } 154 buf.append(ext[i]); 155 } 156 return buf.toString(); 157 } 158 159 160 private static IResource[] removeRedundantEntries(IResource[] elements, boolean includeDerived) { 161 ArrayList res= new ArrayList (); 162 for (int i= 0; i < elements.length; i++) { 163 IResource curr= elements[i]; 164 addToList(res, curr, includeDerived); 165 } 166 return (IResource[])res.toArray(new IResource[res.size()]); 167 } 168 169 private static void addToList(ArrayList res, IResource curr, boolean includeDerived) { 170 if (!includeDerived && isDerived(curr)) { 171 return; 172 } 173 IPath currPath= curr.getFullPath(); 174 for (int k= res.size() - 1; k >= 0 ; k--) { 175 IResource other= (IResource) res.get(k); 176 IPath otherPath= other.getFullPath(); 177 if (otherPath.isPrefixOf(currPath)) { 178 return; 179 } 180 if (currPath.isPrefixOf(otherPath)) { 181 res.remove(k); 182 } 183 } 184 res.add(curr); 185 } 186 187 private static boolean isDerived(IResource curr) { 188 do { 189 if (curr.isDerived()) { 190 return true; 191 } 192 curr= curr.getParent(); 193 } while (curr != null); 194 return false; 195 } 196 197 198 } 199 | Popular Tags |