1 11 package org.eclipse.team.internal.core; 12 13 import java.util.HashMap ; 14 import java.util.StringTokenizer ; 15 16 import org.eclipse.core.runtime.*; 17 import org.eclipse.core.runtime.content.IContentType; 18 import org.eclipse.osgi.util.NLS; 19 import org.eclipse.team.core.mapping.IStorageMerger; 20 21 public class StorageMergerRegistry { 22 23 private final static String ID_ATTRIBUTE = "id"; private final static String EXTENSIONS_ATTRIBUTE = "extensions"; private final static String CONTENT_TYPE_ID_ATTRIBUTE = "contentTypeId"; private static final String STORAGE_MERGER_EXTENSION_POINT = "storageMergers"; private static final Object STORAGE_MERGER = "storageMerger"; private static final String CONTENT_TYPE_BINDING= "contentTypeBinding"; private static final String STORAGE_MERGER_ID_ATTRIBUTE= "storageMergerId"; 31 private static boolean NORMALIZE_CASE= true; 32 33 private static StorageMergerRegistry instance; 34 35 private HashMap fIdMap; private HashMap fExtensionMap; private HashMap fContentTypeBindings; private boolean fRegistriesInitialized; 39 40 public static StorageMergerRegistry getInstance() { 41 if (instance == null) { 42 instance = new StorageMergerRegistry(); 43 } 44 return instance; 45 } 46 47 54 public IStorageMerger createStreamMerger(String type) { 55 initializeRegistry(); 56 StorageMergerDescriptor descriptor= (StorageMergerDescriptor) search(type); 57 if (descriptor != null) 58 return descriptor.createStreamMerger(); 59 return null; 60 } 61 62 69 public IStorageMerger createStreamMerger(IContentType type) { 70 initializeRegistry(); 71 StorageMergerDescriptor descriptor= (StorageMergerDescriptor) search(type); 72 if (descriptor != null) 73 return descriptor.createStreamMerger(); 74 return null; 75 } 76 77 private void initializeRegistry() { 78 if (!fRegistriesInitialized) { 79 registerExtensions(); 80 fRegistriesInitialized= true; 81 } 82 } 83 84 88 private void registerExtensions() { 89 IExtensionRegistry registry= Platform.getExtensionRegistry(); 90 91 IConfigurationElement[] elements= registry.getConfigurationElementsFor(TeamPlugin.ID, STORAGE_MERGER_EXTENSION_POINT); 93 for (int i= 0; i < elements.length; i++) { 94 IConfigurationElement element= elements[i]; 95 if (STORAGE_MERGER.equals(element.getName())) 96 register(element, new StorageMergerDescriptor(element)); 97 else if (CONTENT_TYPE_BINDING.equals(element.getName())) 98 createBinding(element, STORAGE_MERGER_ID_ATTRIBUTE); 99 } 100 } 101 102 private static String normalizeCase(String s) { 103 if (NORMALIZE_CASE && s != null) 104 return s.toUpperCase(); 105 return s; 106 } 107 108 void register(IConfigurationElement element, Object data) { 109 String id = element.getAttribute(ID_ATTRIBUTE); 110 if (id != null) { 111 if (fIdMap == null) 112 fIdMap = new HashMap (); 113 fIdMap.put(id, data); 114 } 115 116 String types = element.getAttribute(EXTENSIONS_ATTRIBUTE); 117 if (types != null) { 118 if (fExtensionMap == null) 119 fExtensionMap = new HashMap (); 120 StringTokenizer tokenizer = new StringTokenizer (types, ","); while (tokenizer.hasMoreElements()) { 122 String extension = tokenizer.nextToken().trim(); 123 fExtensionMap.put(normalizeCase(extension), data); 124 } 125 } 126 } 127 128 void createBinding(IConfigurationElement element, String idAttributeName) { 129 String type = element.getAttribute(CONTENT_TYPE_ID_ATTRIBUTE); 130 String id = element.getAttribute(idAttributeName); 131 if (id == null) 132 logErrorMessage(NLS.bind("Target attribute id '{0}' missing", idAttributeName)); if (type != null && id != null && fIdMap != null) { 134 Object o = fIdMap.get(id); 135 if (o != null) { 136 IContentType ct = Platform.getContentTypeManager().getContentType(type); 137 if (ct != null) { 138 if (fContentTypeBindings == null) 139 fContentTypeBindings = new HashMap (); 140 fContentTypeBindings.put(ct, o); 141 } else { 142 logErrorMessage(NLS.bind("Content type id '{0}' not found", type)); } 144 } else { 145 logErrorMessage(NLS.bind("Target '{0}' not found", id)); } 147 } 148 } 149 150 private void logErrorMessage(String string) { 151 TeamPlugin.log(IStatus.ERROR, string, null); 152 } 153 154 Object search(IContentType type) { 155 if (fContentTypeBindings != null) { 156 for (; type != null; type = type.getBaseType()) { 157 Object data = fContentTypeBindings.get(type); 158 if (data != null) 159 return data; 160 } 161 } 162 return null; 163 } 164 165 Object search(String extension) { 166 if (fExtensionMap != null) 167 return fExtensionMap.get(normalizeCase(extension)); 168 return null; 169 } 170 } 171 | Popular Tags |