1 11 package org.eclipse.core.internal.filebuffers; 12 13 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 20 import java.util.Set ; 21 import java.util.StringTokenizer ; 22 23 import org.eclipse.core.runtime.Assert; 24 import org.eclipse.core.runtime.CoreException; 25 import org.eclipse.core.runtime.IConfigurationElement; 26 import org.eclipse.core.runtime.IExtensionPoint; 27 import org.eclipse.core.runtime.ILog; 28 import org.eclipse.core.runtime.IPath; 29 import org.eclipse.core.runtime.IStatus; 30 import org.eclipse.core.runtime.Platform; 31 import org.eclipse.core.runtime.Status; 32 import org.eclipse.core.runtime.content.IContentType; 33 import org.eclipse.core.runtime.content.IContentTypeManager; 34 35 import org.eclipse.core.filebuffers.IAnnotationModelFactory; 36 import org.eclipse.core.filebuffers.IDocumentFactory; 37 import org.eclipse.core.filebuffers.IDocumentSetupParticipant; 38 import org.eclipse.core.filebuffers.LocationKind; 39 40 41 45 public class ExtensionsRegistry { 46 47 51 private static class ContentTypeAdapter { 52 53 54 private IContentType fContentType; 55 56 62 public ContentTypeAdapter(IContentType contentType) { 63 Assert.isNotNull(contentType); 64 fContentType= contentType; 65 } 66 67 72 public IContentType getContentType() { 73 return fContentType; 74 } 75 76 81 public String getId() { 82 return fContentType.getId(); 83 } 84 85 88 public boolean equals(Object obj) { 89 return obj instanceof ContentTypeAdapter && fContentType.getId().equals(((ContentTypeAdapter)obj).getId()); 90 } 91 92 95 public int hashCode() { 96 return fContentType.getId().hashCode(); 97 } 98 } 99 100 protected final static String WILDCARD= "*"; 102 103 private Map fFactoryDescriptors= new HashMap (); 104 105 private Map fFactories= new HashMap (); 106 107 private Map fSetupParticipantDescriptors= new HashMap (); 108 109 private Map fSetupParticipants= new HashMap (); 110 111 private Map fAnnotationModelFactoryDescriptors= new HashMap (); 112 113 private Map fAnnotationModelFactories= new HashMap (); 114 115 protected IContentTypeManager fContentTypeManager= Platform.getContentTypeManager(); 116 117 118 122 public ExtensionsRegistry() { 123 initialize("documentCreation", "contentTypeId", true, fFactoryDescriptors); initialize("documentCreation", "fileNames", false, fFactoryDescriptors); initialize("documentCreation", "extensions", false, fFactoryDescriptors); initialize("documentSetup", "contentTypeId", true, fSetupParticipantDescriptors); initialize("documentSetup", "fileNames", false, fSetupParticipantDescriptors); initialize("documentSetup", "extensions", false, fSetupParticipantDescriptors); initialize("annotationModelCreation", "contentTypeId", true, fAnnotationModelFactoryDescriptors); initialize("annotationModelCreation", "fileNames", false, fAnnotationModelFactoryDescriptors); initialize("annotationModelCreation", "extensions", false, fAnnotationModelFactoryDescriptors); 133 } 134 135 143 private void read(String attributeName, IConfigurationElement element, Map map) { 144 String value= element.getAttribute(attributeName); 145 if (value != null) { 146 StringTokenizer tokenizer= new StringTokenizer (value, ","); while (tokenizer.hasMoreTokens()) { 148 String token= tokenizer.nextToken().trim(); 149 150 Set s= (Set ) map.get(token); 151 if (s == null) { 152 s= new HashSet (); 153 map.put(token, s); 154 } 155 s.add(element); 156 } 157 } 158 } 159 160 168 private void readContentType(String attributeName, IConfigurationElement element, Map map) { 169 String value= element.getAttribute(attributeName); 170 if (value != null) { 171 IContentType contentType= fContentTypeManager.getContentType(value); 172 if (contentType == null) { 173 log(new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IStatus.OK, NLSUtility.format(FileBuffersMessages.ExtensionsRegistry_error_contentTypeDoesNotExist, value), null)); 174 return; 175 } 176 ContentTypeAdapter adapter= new ContentTypeAdapter(contentType); 177 Set s= (Set ) map.get(adapter); 178 if (s == null) { 179 s= new HashSet (); 180 map.put(adapter, s); 181 } 182 s.add(element); 183 } 184 } 185 186 190 private void log(IStatus status) { 191 ILog log= FileBuffersPlugin.getDefault().getLog(); 192 log.log(status); 193 } 194 195 205 private void initialize(String extensionPointName, String childElementName, boolean isContentTypeId, Map descriptors) { 206 207 IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(FileBuffersPlugin.PLUGIN_ID, extensionPointName); 208 if (extensionPoint == null) { 209 log(new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, 0, NLSUtility.format(FileBuffersMessages.ExtensionsRegistry_error_extensionPointNotFound, extensionPointName), null)); 210 return; 211 } 212 213 IConfigurationElement[] elements= extensionPoint.getConfigurationElements(); 214 for (int i= 0; i < elements.length; i++) { 215 if (isContentTypeId) 216 readContentType(childElementName, elements[i], descriptors); 217 else 218 read(childElementName, elements[i], descriptors); 219 } 220 } 221 222 232 private Object getExtension(IConfigurationElement entry, Map extensions, Class extensionType) { 233 Object extension= extensions.get(entry); 234 if (extension != null) 235 return extension; 236 237 try { 238 extension= entry.createExecutableExtension("class"); } catch (CoreException x) { 240 log(x.getStatus()); 241 } 242 243 if (extensionType.isInstance(extension)) { 244 extensions.put(entry, extension); 245 return extension; 246 } 247 248 return null; 249 } 250 251 257 private IConfigurationElement selectConfigurationElement(Set set) { 258 if (set != null && !set.isEmpty()) { 259 Iterator e= set.iterator(); 260 return (IConfigurationElement) e.next(); 261 } 262 return null; 263 } 264 265 271 protected IDocumentFactory getDocumentFactory(String nameOrExtension) { 272 Set set= (Set ) fFactoryDescriptors.get(nameOrExtension); 273 if (set != null) { 274 IConfigurationElement entry= selectConfigurationElement(set); 275 return (IDocumentFactory) getExtension(entry, fFactories, IDocumentFactory.class); 276 } 277 return null; 278 } 279 280 286 protected IDocumentFactory doGetDocumentFactory(IContentType[] contentTypes) { 287 Set set= null; 288 int i= 0; 289 while (i < contentTypes.length && set == null) { 290 set= (Set ) fFactoryDescriptors.get(new ContentTypeAdapter(contentTypes[i++])); 291 } 292 293 if (set != null) { 294 IConfigurationElement entry= selectConfigurationElement(set); 295 return (IDocumentFactory) getExtension(entry, fFactories, IDocumentFactory.class); 296 } 297 return null; 298 } 299 300 308 protected IDocumentFactory getDocumentFactory(IContentType[] contentTypes) { 309 IDocumentFactory factory= doGetDocumentFactory(contentTypes); 310 while (factory == null) { 311 contentTypes= computeBaseContentTypes(contentTypes); 312 if (contentTypes == null) 313 break; 314 factory= doGetDocumentFactory(contentTypes); 315 } 316 return factory; 317 } 318 319 325 protected List getDocumentSetupParticipants(String nameOrExtension) { 326 Set set= (Set ) fSetupParticipantDescriptors.get(nameOrExtension); 327 if (set == null) 328 return null; 329 330 List participants= new ArrayList (); 331 Iterator e= set.iterator(); 332 while (e.hasNext()) { 333 IConfigurationElement entry= (IConfigurationElement) e.next(); 334 Object participant= getExtension(entry, fSetupParticipants, IDocumentSetupParticipant.class); 335 if (participant != null) 336 participants.add(participant); 337 } 338 339 return participants; 340 } 341 342 348 private List doGetDocumentSetupParticipants(IContentType[] contentTypes) { 349 Set resultSet= new HashSet (); 350 int i= 0; 351 while (i < contentTypes.length) { 352 Set set= (Set ) fSetupParticipantDescriptors.get(new ContentTypeAdapter(contentTypes[i++])); 353 if (set != null) 354 resultSet.addAll(set); 355 } 356 357 List participants= new ArrayList (); 358 Iterator e= resultSet.iterator(); 359 while (e.hasNext()) { 360 IConfigurationElement entry= (IConfigurationElement) e.next(); 361 Object participant= getExtension(entry, fSetupParticipants, IDocumentSetupParticipant.class); 362 if (participant != null) 363 participants.add(participant); 364 } 365 366 return participants.isEmpty() ? null : participants; 367 } 368 369 377 protected List getDocumentSetupParticipants(IContentType[] contentTypes) { 378 List participants= doGetDocumentSetupParticipants(contentTypes); 379 while (participants == null) { 380 contentTypes= computeBaseContentTypes(contentTypes); 381 if (contentTypes == null) 382 break; 383 participants= doGetDocumentSetupParticipants(contentTypes); 384 } 385 return participants; 386 } 387 388 394 private IAnnotationModelFactory doGetAnnotationModelFactory(IContentType[] contentTypes) { 395 Set set= null; 396 int i= 0; 397 while (i < contentTypes.length && set == null) { 398 set= (Set ) fAnnotationModelFactoryDescriptors.get(new ContentTypeAdapter(contentTypes[i++])); 399 } 400 401 if (set != null) { 402 IConfigurationElement entry= selectConfigurationElement(set); 403 return (IAnnotationModelFactory) getExtension(entry, fAnnotationModelFactories, IAnnotationModelFactory.class); 404 } 405 return null; 406 } 407 408 416 protected IAnnotationModelFactory getAnnotationModelFactory(IContentType[] contentTypes) { 417 IAnnotationModelFactory factory= doGetAnnotationModelFactory(contentTypes); 418 while (factory == null) { 419 contentTypes= computeBaseContentTypes(contentTypes); 420 if (contentTypes == null) 421 break; 422 factory= doGetAnnotationModelFactory(contentTypes); 423 } 424 return factory; 425 } 426 427 433 protected IAnnotationModelFactory getAnnotationModelFactory(String extension) { 434 Set set= (Set ) fAnnotationModelFactoryDescriptors.get(extension); 435 if (set != null) { 436 IConfigurationElement entry= selectConfigurationElement(set); 437 return (IAnnotationModelFactory) getExtension(entry, fAnnotationModelFactories, IAnnotationModelFactory.class); 438 } 439 return null; 440 } 441 442 450 protected IContentType[] findContentTypes(IPath location, LocationKind locationKind) { 451 Assert.isLegal(locationKind != LocationKind.IFILE); 452 return fContentTypeManager.findContentTypesFor(location.lastSegment()); 453 } 454 455 463 private IContentType[] computeBaseContentTypes(IContentType[] contentTypes) { 464 List baseTypes= new ArrayList (); 465 for (int i= 0; i < contentTypes.length; i++) { 466 IContentType baseType= contentTypes[i].getBaseType(); 467 if (baseType != null) 468 baseTypes.add(baseType); 469 } 470 471 IContentType[] result= null; 472 int size= baseTypes.size(); 473 if (size > 0) { 474 result= new IContentType[size]; 475 baseTypes.toArray(result); 476 } 477 return result; 478 } 479 480 488 public IDocumentFactory getDocumentFactory(IPath location, LocationKind locationKind) { 489 IDocumentFactory factory= getDocumentFactory(findContentTypes(location, locationKind)); 490 if (factory == null) 491 factory= getDocumentFactory(location.lastSegment()); 492 if (factory == null) 493 factory= getDocumentFactory(location.getFileExtension()); 494 if (factory == null) 495 factory= getDocumentFactory(WILDCARD); 496 return factory; 497 } 498 499 507 public IDocumentSetupParticipant[] getDocumentSetupParticipants(IPath location, LocationKind locationKind) { 508 Set participants= new HashSet (); 509 510 List p= getDocumentSetupParticipants(findContentTypes(location, locationKind)); 511 if (p != null) 512 participants.addAll(p); 513 514 p= getDocumentSetupParticipants(location.lastSegment()); 515 if (p != null) 516 participants.addAll(p); 517 518 p= getDocumentSetupParticipants(location.getFileExtension()); 519 if (p != null) 520 participants.addAll(p); 521 522 p= getDocumentSetupParticipants(WILDCARD); 523 if (p != null) 524 participants.addAll(p); 525 526 IDocumentSetupParticipant[] result= new IDocumentSetupParticipant[participants.size()]; 527 participants.toArray(result); 528 return result; 529 } 530 531 539 public IAnnotationModelFactory getAnnotationModelFactory(IPath location, LocationKind locationKind) { 540 IAnnotationModelFactory factory= getAnnotationModelFactory(findContentTypes(location, locationKind)); 541 if (factory == null) 542 factory= getAnnotationModelFactory(location.lastSegment()); 543 if (factory == null) 544 factory= getAnnotationModelFactory(location.getFileExtension()); 545 if (factory == null) 546 factory= getAnnotationModelFactory(WILDCARD); 547 return factory; 548 } 549 550 } 551 | Popular Tags |