1 22 package org.jboss.virtual.plugins.vfs.helpers; 23 24 import java.util.Arrays ; 25 import java.util.Comparator ; 26 import java.util.ArrayList ; 27 import java.util.Collections ; 28 29 import org.jboss.virtual.VirtualFile; 30 import org.jboss.virtual.VirtualFileFilter; 31 32 46 public class ExtensibleFilter implements VirtualFileFilter 47 { 48 51 private static final Comparator <String > reverseComparator = new Comparator <String >() 52 { 53 public int compare(String o1, String o2) 54 { 55 int idx1 = o1.length(); 56 int idx2 = o2.length(); 57 int comp = 0; 58 59 while (comp == 0 && idx1 > 0 && idx2 > 0) 60 comp = o1.charAt(--idx1) - o2.charAt(--idx2); 61 62 return (comp == 0) ? (idx1 - idx2) : comp; 63 } 64 }; 65 66 67 private static final String [] DEFAULT_PREFIXES = 68 {"#", "%", ",", ".", "_$"}; 69 70 71 private static final String [] DEFAULT_SUFFIXES = 72 {"#", "$", "%", "~", ",v", ".BAK", ".bak", ".old", ".orig", ".tmp", ".rej", ".sh" }; 73 74 75 private static final String [] DEFAULT_MATCHES = 76 {".make.state", ".nse_depinfo", "CVS", "CVS.admin", "RCS", "RCSLOG", 77 "SCCS", "TAGS", "core", "tags"}; 78 79 80 private ArrayList <String > suffixes; 81 82 83 private ArrayList <String > prefixes; 84 85 86 private ArrayList <String > matches; 87 88 89 public ExtensibleFilter() 90 { 91 this(DEFAULT_MATCHES, DEFAULT_PREFIXES, DEFAULT_SUFFIXES); 92 } 93 94 99 public ExtensibleFilter(String [] matches, String [] prefixes, String [] suffixes) 100 { 101 if( matches == null ) 102 matches = DEFAULT_MATCHES; 103 Arrays.sort(matches); 104 this.matches = new ArrayList <String >(Arrays.asList(matches)); 105 106 if( prefixes == null ) 107 prefixes = DEFAULT_PREFIXES; 108 Arrays.sort(prefixes); 109 this.prefixes = new ArrayList <String >(Arrays.asList(prefixes)); 110 111 if( suffixes == null ) 112 suffixes = DEFAULT_SUFFIXES; 113 Arrays.sort(suffixes, reverseComparator); 114 this.suffixes = new ArrayList <String >(Arrays.asList(suffixes)); 115 } 116 117 public void addPrefix(String prefix) 118 { 119 this.prefixes.add(prefix); 120 Collections.sort(this.prefixes); 121 } 122 public void addPrefixes(String [] prefixes) 123 { 124 this.prefixes.addAll(Arrays.asList(prefixes)); 125 Collections.sort(this.prefixes); 126 } 127 128 public void delPrefix(String prefix) 129 { 130 this.prefixes.remove(prefix); 131 } 132 public void delPrefixes(String [] prefixes) 133 { 134 this.prefixes.removeAll(Arrays.asList(prefixes)); 135 Collections.sort(this.prefixes); 136 } 137 138 public void addSuffix(String suffix) 139 { 140 this.suffixes.add(suffix); 141 Collections.sort(this.suffixes, reverseComparator); 142 } 143 public void addSuffixes(String [] suffixes) 144 { 145 this.suffixes.addAll(Arrays.asList(suffixes)); 146 Collections.sort(this.suffixes, reverseComparator); 147 } 148 149 public void delSuffix(String suffix) 150 { 151 this.suffixes.remove(suffix); 152 } 153 public void delSuffixes(String [] suffixes) 154 { 155 this.suffixes.removeAll(Arrays.asList(suffixes)); 156 Collections.sort(this.suffixes, reverseComparator); 157 } 158 159 public String [] getSuffixes() 160 { 161 String [] tmp = new String [suffixes.size()]; 162 suffixes.toArray(tmp); 163 return tmp; 164 } 165 public void setSuffixes(String [] suffixes) 166 { 167 Arrays.sort(suffixes, reverseComparator); 168 this.suffixes.clear(); 169 this.suffixes.addAll(Arrays.asList(suffixes)); 170 } 171 172 public String [] getPrefixes() 173 { 174 String [] tmp = new String [prefixes.size()]; 175 prefixes.toArray(tmp); 176 return tmp; 177 } 178 public void setPrefixes(String [] prefixes) 179 { 180 Arrays.sort(prefixes); 181 this.prefixes.clear(); 182 this.prefixes.addAll(Arrays.asList(prefixes)); 183 } 184 185 public String [] getMatches() 186 { 187 String [] tmp = new String [matches.size()]; 188 matches.toArray(tmp); 189 return tmp; 190 } 191 public void setMatches(String [] matches) 192 { 193 Arrays.sort(matches); 194 this.matches.clear(); 195 this.matches.addAll(Arrays.asList(matches)); 196 } 197 198 207 public boolean accepts(VirtualFile file) 208 { 209 String name = file.getName(); 210 int index = Collections.binarySearch(matches, name); 212 if (index >= 0) 213 return false; 214 215 index = Collections.binarySearch(prefixes, name); 217 if (index >= 0) 218 return false; 219 if (index < -1) 220 { 221 int firstLessIndex = -2 - index; 223 String prefix = (String ) prefixes.get(firstLessIndex); 224 if( name.startsWith(prefix) ) 226 return false; 227 } 228 229 index = Collections.binarySearch(suffixes, name, reverseComparator); 231 if (index >= 0) 232 return false; 233 if (index < -1) 234 { 235 int firstLessIndex = -2 - index; 237 String suffix = (String ) suffixes.get(firstLessIndex); 238 if( name.endsWith(suffix) ) 240 return false; 241 } 242 243 return true; 245 } 246 } 247 | Popular Tags |