1 11 package org.eclipse.ui.texteditor; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.HashSet ; 16 import java.util.Hashtable ; 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.osgi.framework.Bundle; 24 25 import org.eclipse.core.resources.IFile; 26 import org.eclipse.core.runtime.CoreException; 27 import org.eclipse.core.runtime.IConfigurationElement; 28 import org.eclipse.core.runtime.IExtensionPoint; 29 import org.eclipse.core.runtime.ILog; 30 import org.eclipse.core.runtime.IStatus; 31 import org.eclipse.core.runtime.Platform; 32 import org.eclipse.core.runtime.Status; 33 34 import org.eclipse.ui.editors.text.EditorsUI; 35 36 import org.eclipse.ui.IEditorInput; 37 import org.eclipse.ui.PlatformUI; 38 import org.eclipse.ui.internal.editors.text.NLSUtility; 39 40 41 51 public class DocumentProviderRegistry { 52 53 54 private static DocumentProviderRegistry fgRegistry; 55 56 61 public static DocumentProviderRegistry getDefault() { 62 if (fgRegistry == null) 63 fgRegistry= new DocumentProviderRegistry(); 64 return fgRegistry; 65 } 66 67 68 69 private Map fExtensionMapping= new HashMap (); 70 71 private Map fInputTypeMapping= new HashMap (); 72 73 private Map fInstances= new HashMap (); 74 75 76 80 private DocumentProviderRegistry() { 81 initialize(); 82 } 83 84 93 private void read(Map map, IConfigurationElement element, String attributeName) { 94 String value= element.getAttribute(attributeName); 95 if (value != null) { 96 StringTokenizer tokenizer= new StringTokenizer (value, ","); while (tokenizer.hasMoreTokens()) { 98 String token= tokenizer.nextToken().trim(); 99 100 Set s= (Set ) map.get(token); 101 if (s == null) { 102 s= new HashSet (); 103 map.put(token, s); 104 } 105 s.add(element); 106 } 107 } 108 } 109 110 115 private void initialize() { 116 117 IExtensionPoint extensionPoint; 118 extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(EditorsUI.PLUGIN_ID, "documentProviders"); 120 if (extensionPoint == null) { 121 String msg= NLSUtility.format(TextEditorMessages.DocumentProviderRegistry_error_extension_point_not_found, PlatformUI.PLUGIN_ID); 122 Bundle bundle = Platform.getBundle(PlatformUI.PLUGIN_ID); 123 ILog log= Platform.getLog(bundle); 124 log.log(new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, IStatus.OK, msg, null)); 125 return; 126 } 127 128 IConfigurationElement[] elements= extensionPoint.getConfigurationElements(); 129 for (int i= 0; i < elements.length; i++) { 130 read(fExtensionMapping, elements[i], "extensions"); read(fInputTypeMapping, elements[i], "inputTypes"); } 133 } 134 135 143 private IDocumentProvider getDocumentProvider(IConfigurationElement entry) { 144 IDocumentProvider provider= (IDocumentProvider) fInstances.get(entry); 145 if (provider == null) { 146 try { 147 provider= (IDocumentProvider) entry.createExecutableExtension("class"); fInstances.put(entry, provider); 149 } catch (CoreException x) { 150 } 151 } 152 return provider; 153 } 154 155 161 private IConfigurationElement selectConfigurationElement(Set set) { 162 if (set != null && !set.isEmpty()) { 163 Iterator e= set.iterator(); 164 return (IConfigurationElement) e.next(); 165 } 166 return null; 167 } 168 169 175 public IDocumentProvider getDocumentProvider(String extension) { 176 177 Set set= (Set ) fExtensionMapping.get(extension); 178 if (set != null) { 179 IConfigurationElement entry= selectConfigurationElement(set); 180 return getDocumentProvider(entry); 181 } 182 return null; 183 } 184 185 192 private List computeClassList(Class type) { 193 194 List result= new ArrayList (); 195 196 Class c= type; 197 while (c != null) { 198 result.add(c); 199 c= c.getSuperclass(); 200 } 201 202 return result; 203 } 204 205 213 private List computeInterfaceList(List classes) { 214 215 List result= new ArrayList (4); 216 Hashtable visited= new Hashtable (4); 217 218 Iterator e= classes.iterator(); 219 while (e.hasNext()) { 220 Class c= (Class ) e.next(); 221 computeInterfaceList(c.getInterfaces(), result, visited); 222 } 223 224 return result; 225 } 226 227 235 private void computeInterfaceList(Class [] interfaces, List result, Hashtable visited) { 236 237 List toBeVisited= new ArrayList (interfaces.length); 238 239 for (int i= 0; i < interfaces.length; i++) { 240 Class iface= interfaces[i]; 241 if (visited.get(iface) == null) { 242 visited.put(iface, iface); 243 result.add(iface); 244 toBeVisited.add(iface); 245 } 246 } 247 248 Iterator e= toBeVisited.iterator(); 249 while(e.hasNext()) { 250 Class iface= (Class ) e.next(); 251 computeInterfaceList(iface.getInterfaces(), result, visited); 252 } 253 } 254 255 262 private Object getFirstInputTypeMapping(List classes) { 263 Iterator e= classes.iterator(); 264 while (e.hasNext()) { 265 Class c= (Class ) e.next(); 266 Object mapping= fInputTypeMapping.get(c.getName()); 267 if (mapping != null) 268 return mapping; 269 } 270 return null; 271 } 272 273 281 private Object findInputTypeMapping(Class type) { 282 283 if (type == null) 284 return null; 285 286 Object mapping= fInputTypeMapping.get(type.getName()); 287 if (mapping != null) 288 return mapping; 289 290 List classList= computeClassList(type); 291 mapping= getFirstInputTypeMapping(classList); 292 if (mapping != null) 293 return mapping; 294 295 return getFirstInputTypeMapping(computeInterfaceList(classList)); 296 } 297 298 304 public IDocumentProvider getDocumentProvider(IEditorInput editorInput) { 305 306 IDocumentProvider provider= null; 307 308 IFile file= (IFile) editorInput.getAdapter(IFile.class); 309 if (file != null) 310 provider= getDocumentProvider(file.getFileExtension()); 311 312 if (provider == null) { 313 Set set= (Set ) findInputTypeMapping(editorInput.getClass()); 314 if (set != null) { 315 IConfigurationElement entry= selectConfigurationElement(set); 316 provider= getDocumentProvider(entry); 317 } 318 } 319 320 return provider; 321 } 322 } 323 | Popular Tags |