1 11 package org.eclipse.compare.internal; 12 13 import com.ibm.icu.text.MessageFormat; 14 import java.util.StringTokenizer ; 15 16 import org.eclipse.core.resources.*; 17 import org.eclipse.core.resources.IWorkspace; 18 import org.eclipse.core.runtime.IStatus; 19 20 21 public class CompareFilter { 22 private static final char[][] NO_CHAR_CHAR= new char[0][]; 23 24 private char[][] fExtraResourceFileFilters; 25 private String [] fExtraResourceFolderFilters; 26 27 28 public CompareFilter() { 29 } 31 32 35 public boolean filter(String path0, boolean folder, boolean isArchive) { 36 if (!folder && fExtraResourceFileFilters != null) { 37 char[] name= path0.toCharArray(); 38 for (int i= 0, l= fExtraResourceFileFilters.length; i < l; i++) 39 if (match(fExtraResourceFileFilters[i], name, true)) 40 return true; 41 } 42 if (folder && fExtraResourceFolderFilters != null) { 43 for (int i= 0, l= fExtraResourceFolderFilters.length; i < l; i++) 44 if (fExtraResourceFolderFilters[i].equals(path0)) 45 return true; 46 } 47 return false; 48 } 49 50 public static String validateResourceFilters(String text) { 51 IWorkspace workspace= ResourcesPlugin.getWorkspace(); 52 String [] filters= getTokens(text, ","); for (int i= 0; i < filters.length; i++) { 54 String fileName= filters[i].replace('*', 'x'); 55 int resourceType= IResource.FILE; 56 int lastCharacter= fileName.length() - 1; 57 if (lastCharacter >= 0 && fileName.charAt(lastCharacter) == '/') { 58 fileName= fileName.substring(0, lastCharacter); 59 resourceType= IResource.FOLDER; 60 } 61 IStatus status= workspace.validateName(fileName, resourceType); 62 if (status.matches(IStatus.ERROR)) { 63 String format= Utilities.getString("ComparePreferencePage.filter.invalidsegment.error"); return MessageFormat.format(format, new String [] { status.getMessage() } ); 65 } 66 } 67 return null; 68 } 69 70 public void setFilters(String filterSequence) { 71 char[][] filters= filterSequence != null && filterSequence.length() > 0 72 ? splitAndTrimOn(',', filterSequence.toCharArray()) 73 : null; 74 if (filters == null) { 75 fExtraResourceFileFilters= null; 76 fExtraResourceFolderFilters= null; 77 } else { 78 int fileCount= 0, folderCount= 0; 79 for (int i= 0, l= filters.length; i < l; i++) { 80 char[] f= filters[i]; 81 if (f.length == 0) 82 continue; 83 if (f[f.length - 1] == '/') 84 folderCount++; 85 else 86 fileCount++; 87 } 88 fExtraResourceFileFilters= new char[fileCount][]; 89 fExtraResourceFolderFilters= new String [folderCount]; 90 for (int i= 0, l= filters.length; i < l; i++) { 91 char[] f= filters[i]; 92 if (f.length == 0) 93 continue; 94 if (f[f.length - 1] == '/') 95 fExtraResourceFolderFilters[--folderCount]= new String (subarray(f, 0, f.length - 1)); 96 else 97 fExtraResourceFileFilters[--fileCount]= f; 98 } 99 } 100 } 101 102 104 private static String [] getTokens(String text, String separator) { 105 StringTokenizer tok= new StringTokenizer (text, separator); 106 int nTokens= tok.countTokens(); 107 String [] res= new String [nTokens]; 108 for (int i= 0; i < res.length; i++) 109 res[i]= tok.nextToken().trim(); 110 return res; 111 } 112 113 157 private boolean match(char[] pattern, char[] name, boolean isCaseSensitive) { 158 if (name == null) 159 return false; if (pattern == null) 161 return true; return match(pattern, 0, pattern.length, name, 0, name.length, isCaseSensitive); 163 } 164 165 209 private boolean match(char[] pattern, int patternStart, int patternEnd, char[] name, int nameStart, int nameEnd, 210 boolean isCaseSensitive) { 211 if (name == null) 212 return false; if (pattern == null) 214 return true; int iPattern= patternStart; 216 int iName= nameStart; 217 if (patternEnd < 0) 218 patternEnd= pattern.length; 219 if (nameEnd < 0) 220 nameEnd= name.length; 221 222 char patternChar= 0; 223 while ((iPattern < patternEnd) && (patternChar= pattern[iPattern]) != '*') { 224 if (iName == nameEnd) 225 return false; 226 if (patternChar != (isCaseSensitive ? name[iName] : Character.toLowerCase(name[iName])) && patternChar != '?') { 227 return false; 228 } 229 iName++; 230 iPattern++; 231 } 232 233 int segmentStart; 234 if (patternChar == '*') { 235 segmentStart= ++iPattern; } else { 237 segmentStart= 0; } 239 int prefixStart= iName; 240 checkSegment : while (iName < nameEnd) { 241 if (iPattern == patternEnd) { 242 iPattern= segmentStart; iName= ++prefixStart; 244 continue checkSegment; 245 } 246 247 if ((patternChar= pattern[iPattern]) == '*') { 248 segmentStart= ++iPattern; if (segmentStart == patternEnd) { 250 return true; 251 } 252 prefixStart= iName; 253 continue checkSegment; 254 } 255 256 if ((isCaseSensitive ? name[iName] : Character.toLowerCase(name[iName])) != patternChar && patternChar != '?') { 257 iPattern= segmentStart; iName= ++prefixStart; 259 continue checkSegment; 260 } 261 iName++; 262 iPattern++; 263 } 264 return (segmentStart == patternEnd) || (iName == nameEnd && iPattern == patternEnd) 265 || (iPattern == patternEnd - 1 && pattern[iPattern] == '*'); 266 } 267 268 315 private char[][] splitAndTrimOn(char divider, char[] array) { 316 int length= array == null ? 0 : array.length; 317 if (length == 0) 318 return NO_CHAR_CHAR; 319 int wordCount= 1; 320 for (int i= 0; i < length; i++) 321 if (array[i] == divider) 322 wordCount++; 323 char[][] split= new char[wordCount][]; 324 int last= 0, currentWord= 0; 325 for (int i= 0; i < length; i++) { 326 if (array[i] == divider) { 327 int start= last, end= i - 1; 328 while (start < i && array[start] == ' ') 329 start++; 330 while (end > start && array[end] == ' ') 331 end--; 332 split[currentWord]= new char[end - start + 1]; 333 System.arraycopy(array, start, split[currentWord++], 0, end - start + 1); 334 last= i + 1; 335 } 336 } 337 int start= last, end= length - 1; 338 while (start < length && array[start] == ' ') 339 start++; 340 while (end > start && array[end] == ' ') 341 end--; 342 split[currentWord]= new char[end - start + 1]; 343 System.arraycopy(array, start, split[currentWord++], 0, end - start + 1); 344 return split; 345 } 346 347 384 private char[] subarray(char[] array, int start, int end) { 385 if (end == -1) 386 end= array.length; 387 if (start > end) 388 return null; 389 if (start < 0) 390 return null; 391 if (end > array.length) 392 return null; 393 char[] result= new char[end - start]; 394 System.arraycopy(array, start, result, 0, end - start); 395 return result; 396 } 397 } 398 | Popular Tags |