1 11 package org.eclipse.team.core; 12 13 import java.io.*; 14 import java.util.*; 15 import java.util.Map.Entry; 16 17 import org.eclipse.core.resources.*; 18 import org.eclipse.core.runtime.*; 19 import org.eclipse.core.runtime.Preferences.PropertyChangeEvent; 20 import org.eclipse.core.runtime.content.IContentType; 21 import org.eclipse.team.core.mapping.IStorageMerger; 22 import org.eclipse.team.internal.core.*; 23 24 30 public final class Team { 31 32 private static class StringMappingWrapper implements IFileTypeInfo { 33 34 private final IStringMapping fMapping; 35 36 public StringMappingWrapper(IStringMapping mapping) { 37 fMapping= mapping; 38 } 39 40 public String getExtension() { 41 return fMapping.getString(); 42 } 43 44 public int getType() { 45 return fMapping.getType(); 46 } 47 48 } 49 50 private static final String PREF_TEAM_IGNORES = "ignore_files"; private static final String PREF_TEAM_SEPARATOR = "\n"; public static final Status OK_STATUS = new Status(IStatus.OK, TeamPlugin.ID, IStatus.OK, Messages.ok, null); 53 54 public static final int UNKNOWN = 0; 56 public static final int TEXT = 1; 57 public static final int BINARY = 2; 58 59 60 protected static SortedMap globalIgnore, pluginIgnore; 62 private static StringMatcher[] ignoreMatchers; 63 64 private final static FileContentManager fFileContentManager; 65 66 static { 67 fFileContentManager= new FileContentManager(); 68 } 69 70 71 86 public static int getType(IStorage storage) { 87 return fFileContentManager.getType(storage); 88 } 89 90 99 public static boolean isIgnoredHint(IResource resource) { 100 if (resource.isDerived()) return true; 101 return matchesEnabledIgnore(resource); 102 } 103 104 108 public static boolean isIgnoredHint(IFile file) { 109 if (file.isDerived()) return true; 110 return matchesEnabledIgnore(file); 111 } 112 113 private static boolean matchesEnabledIgnore(IResource resource) { 114 StringMatcher[] matchers = getStringMatchers(); 115 for (int i = 0; i < matchers.length; i++) { 116 if (matchers[i].match(resource.getName())) return true; 117 } 118 return false; 119 } 120 121 125 public static boolean isIgnored(IFile file) { 126 return matchesEnabledIgnore(file); 127 } 128 129 130 136 public static IFileTypeInfo[] getAllTypes() { 137 final IStringMapping [] mappings= fFileContentManager.getExtensionMappings(); 138 final IFileTypeInfo [] infos= new IFileTypeInfo[mappings.length]; 139 for (int i = 0; i < infos.length; i++) { 140 infos[i]= new StringMappingWrapper(mappings[i]); 141 } 142 return infos; 143 } 144 145 148 public synchronized static IIgnoreInfo[] getAllIgnores() { 149 initializeIgnores(); 153 IIgnoreInfo[] result = getIgnoreInfo(globalIgnore); 154 return result; 155 } 156 157 private static void initializeIgnores() { 158 if (globalIgnore == null) { 159 globalIgnore = new TreeMap(); 160 pluginIgnore = new TreeMap(); 161 ignoreMatchers = null; 162 try { 163 readIgnoreState(); 164 } catch (TeamException e) { 165 TeamPlugin.log(IStatus.ERROR, Messages.Team_Error_loading_ignore_state_from_disk_1, e); 166 } 167 initializePluginIgnores(pluginIgnore, globalIgnore); 168 } 169 } 170 171 private static IIgnoreInfo[] getIgnoreInfo(Map gIgnore) { 172 IIgnoreInfo[] result = new IIgnoreInfo[gIgnore.size()]; 173 Iterator e = gIgnore.entrySet().iterator(); 174 int i = 0; 175 while (e.hasNext() ) { 176 Map.Entry entry = (Entry) e.next(); 177 final String pattern = (String ) entry.getKey(); 178 final boolean enabled = ((Boolean )entry.getValue()).booleanValue(); 179 result[i++] = new IIgnoreInfo() { 180 private String p = pattern; 181 private boolean e1 = enabled; 182 public String getPattern() { 183 return p; 184 } 185 public boolean getEnabled() { 186 return e1; 187 } 188 }; 189 } 190 return result; 191 } 192 193 private synchronized static StringMatcher[] getStringMatchers() { 194 if (ignoreMatchers==null) { 195 IIgnoreInfo[] ignorePatterns = getAllIgnores(); 196 ArrayList matchers = new ArrayList(ignorePatterns.length); 197 for (int i = 0; i < ignorePatterns.length; i++) { 198 if (ignorePatterns[i].getEnabled()) { 199 matchers.add(new StringMatcher(ignorePatterns[i].getPattern(), true, false)); 200 } 201 } 202 ignoreMatchers = new StringMatcher[matchers.size()]; 203 ignoreMatchers = (StringMatcher[]) matchers.toArray(ignoreMatchers); 204 } 205 return ignoreMatchers; 206 } 207 208 209 223 public static void setAllTypes(String [] extensions, int[] types) { 224 fFileContentManager.addExtensionMappings(extensions, types); 225 } 226 227 228 229 232 public static void setAllIgnores(String [] patterns, boolean[] enabled) { 233 initializeIgnores(); 234 globalIgnore = new TreeMap(); 235 ignoreMatchers = null; 236 for (int i = 0; i < patterns.length; i++) { 237 globalIgnore.put(patterns[i], Boolean.valueOf(enabled[i])); 238 } 239 StringBuffer buf = new StringBuffer (); 241 Iterator e = globalIgnore.entrySet().iterator(); 242 while (e.hasNext()) { 243 Map.Entry entry = (Entry) e.next(); 244 String pattern = (String ) entry.getKey(); 245 Boolean value = (Boolean ) entry.getValue(); 246 boolean isCustom = (!pluginIgnore.containsKey(pattern)) || 247 !((Boolean )pluginIgnore.get(pattern)).equals(value); 248 if (isCustom) { 249 buf.append(pattern); 250 buf.append(PREF_TEAM_SEPARATOR); 251 boolean en = value.booleanValue(); 252 buf.append(en); 253 buf.append(PREF_TEAM_SEPARATOR); 254 } 255 256 } 257 TeamPlugin.getPlugin().getPluginPreferences().setValue(PREF_TEAM_IGNORES, buf.toString()); 258 } 259 260 261 262 263 268 private static void initializePluginIgnores(SortedMap pIgnore, SortedMap gIgnore) { 269 TeamPlugin plugin = TeamPlugin.getPlugin(); 270 if (plugin != null) { 271 IExtensionPoint extension = Platform.getExtensionRegistry().getExtensionPoint(TeamPlugin.ID, TeamPlugin.IGNORE_EXTENSION); 272 if (extension != null) { 273 IExtension[] extensions = extension.getExtensions(); 274 for (int i = 0; i < extensions.length; i++) { 275 IConfigurationElement [] configElements = extensions[i].getConfigurationElements(); 276 for (int j = 0; j < configElements.length; j++) { 277 String pattern = configElements[j].getAttribute("pattern"); if (pattern != null) { 279 String selected = configElements[j].getAttribute("enabled"); if (selected == null) { 281 selected = configElements[j].getAttribute("selected"); } 284 boolean enabled = selected != null && selected.equalsIgnoreCase("true"); if (gIgnore.containsKey(pattern)){ 287 pIgnore.put(pattern, gIgnore.get(pattern)); 289 } else { 290 pIgnore.put(pattern, Boolean.valueOf(enabled)); 292 gIgnore.put(pattern, Boolean.valueOf(enabled)); 293 } 294 } 295 } 296 } 297 } 298 } 299 } 300 301 306 private static void readIgnoreState() throws TeamException { 307 if (readBackwardCompatibleIgnoreState()) return; 308 Preferences pref = TeamPlugin.getPlugin().getPluginPreferences(); 309 if (!pref.contains(PREF_TEAM_IGNORES)) return; 310 pref.addPropertyChangeListener(new Preferences.IPropertyChangeListener() { 311 public void propertyChange(PropertyChangeEvent event) { 312 if(event.getProperty().equals(PREF_TEAM_IGNORES)) 315 globalIgnore = null; 316 } 317 }); 318 String prefIgnores = pref.getString(PREF_TEAM_IGNORES); 319 StringTokenizer tok = new StringTokenizer(prefIgnores, PREF_TEAM_SEPARATOR); 320 String pattern, enabled; 321 try { 322 while (true) { 323 pattern = tok.nextToken(); 324 if (pattern.length()==0) return; 325 enabled = tok.nextToken(); 326 globalIgnore.put(pattern, Boolean.valueOf(enabled)); 327 } 328 } catch (NoSuchElementException e) { 329 return; 330 } 331 } 332 333 336 private static boolean readBackwardCompatibleIgnoreState() throws TeamException { 337 String GLOBALIGNORE_FILE = ".globalIgnores"; IPath pluginStateLocation = TeamPlugin.getPlugin().getStateLocation().append(GLOBALIGNORE_FILE); 339 File f = pluginStateLocation.toFile(); 340 if (!f.exists()) return false; 341 try { 342 DataInputStream dis = new DataInputStream(new FileInputStream(f)); 343 try { 344 int ignoreCount = 0; 345 try { 346 ignoreCount = dis.readInt(); 347 } catch (EOFException e) { 348 return false; 351 } 352 for (int i = 0; i < ignoreCount; i++) { 353 String pattern = dis.readUTF(); 354 boolean enabled = dis.readBoolean(); 355 globalIgnore.put(pattern, Boolean.valueOf(enabled)); 356 } 357 } finally { 358 dis.close(); 359 } 360 f.delete(); 361 } catch (FileNotFoundException e) { 362 } catch (IOException ex) { 364 throw new TeamException(new Status(IStatus.ERROR, TeamPlugin.ID, 0, Messages.Team_readError, ex)); 365 } 366 return true; 367 } 368 373 public static void startup() { 374 ResourcesPlugin.getWorkspace().addResourceChangeListener(new TeamResourceChangeListener(), IResourceChangeEvent.POST_CHANGE); 376 } 377 378 383 public static void shutdown() { 384 TeamPlugin.getPlugin().savePluginPreferences(); 385 } 386 391 public static IProjectSetSerializer getProjectSetSerializer(String id) { 392 TeamPlugin plugin = TeamPlugin.getPlugin(); 393 if (plugin != null) { 394 IExtensionPoint extension = plugin.getDescriptor().getExtensionPoint(TeamPlugin.PROJECT_SET_EXTENSION); 395 if (extension != null) { 396 IExtension[] extensions = extension.getExtensions(); 397 for (int i = 0; i < extensions.length; i++) { 398 IConfigurationElement [] configElements = extensions[i].getConfigurationElements(); 399 for (int j = 0; j < configElements.length; j++) { 400 String extensionId = configElements[j].getAttribute("id"); if (extensionId != null && extensionId.equals(id)) { 402 try { 403 return (IProjectSetSerializer)configElements[j].createExecutableExtension("class"); } catch (CoreException e) { 405 TeamPlugin.log(e); 406 return null; 407 } 408 } 409 } 410 } 411 } 412 } 413 return null; 414 } 415 416 417 424 public static IIgnoreInfo[] getDefaultIgnores() { 425 SortedMap gIgnore = new TreeMap(); 426 SortedMap pIgnore = new TreeMap(); 427 initializePluginIgnores(pIgnore, gIgnore); 428 return getIgnoreInfo(gIgnore); 429 } 430 431 440 public static IFileTypeInfo[] getDefaultTypes() { 441 return asFileTypeInfo(getFileContentManager().getDefaultExtensionMappings()); 442 } 443 444 private static IFileTypeInfo [] asFileTypeInfo(IStringMapping [] mappings) { 445 final IFileTypeInfo [] infos= new IFileTypeInfo[mappings.length]; 446 for (int i = 0; i < infos.length; i++) { 447 infos[i]= new StringMappingWrapper(mappings[i]); 448 } 449 return infos; 450 } 451 452 462 public static IFileContentManager getFileContentManager() { 463 return fFileContentManager; 464 } 465 466 476 public IStorageMerger createStorageMerger(IContentType type) { 477 return StorageMergerRegistry.getInstance().createStreamMerger(type); 478 } 479 480 490 public IStorageMerger createStorageMerger(String extension) { 491 return StorageMergerRegistry.getInstance().createStreamMerger(extension); 492 } 493 } 494 | Popular Tags |