1 19 20 package org.netbeans.modules.editor.structure; 21 import java.util.Collection ; 22 import java.util.Map ; 23 import java.util.WeakHashMap ; 24 import org.netbeans.api.editor.mimelookup.MimeLookup; 25 import org.netbeans.modules.editor.structure.spi.DocumentModelProvider; 26 import org.openide.ErrorManager; 27 import org.openide.util.Lookup; 28 29 40 public class DocumentModelProviderFactory { 41 42 static final String FOLDER_NAME = "DocumentModel"; 44 private Map <String , DocumentModelProvider> mime2provider; 45 46 private static DocumentModelProviderFactory defaultProvider = null; 47 48 public static DocumentModelProviderFactory getDefault() { 49 if(defaultProvider == null) { 50 defaultProvider = new DocumentModelProviderFactory(); 51 } 52 return defaultProvider; 53 } 54 55 private DocumentModelProviderFactory() { 56 mime2provider = new WeakHashMap <String , DocumentModelProvider>(); 57 } 58 59 60 public DocumentModelProvider getDocumentModelProvider(String mimeType) { 61 DocumentModelProvider provider = null; if(mimeType != null) { 63 provider = mime2provider.get(mimeType); 64 if (provider == null) { MimeLookup mimeLookup = MimeLookup.getMimeLookup(mimeType); 66 Collection <? extends DocumentModelProvider> providers = 67 mimeLookup.lookup(new Lookup.Template<DocumentModelProvider>(DocumentModelProvider.class)).allInstances(); 68 if(providers.size() > 1) 69 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "Only one DocumentModelProvider can be registered for one mimetype!"); 70 71 if(providers.size() == 0) 72 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, new IllegalStateException ("There isn't any DocumentModelProvider registered for " + mimeType + " mimetype!")); 73 74 provider = providers.size() > 0 ? (DocumentModelProvider)providers.iterator().next() : null; 75 mime2provider.put(mimeType, provider); 76 77 } else return provider; 78 } else 79 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, new NullPointerException ("mimeType cannot be null!")); 80 81 return provider; 82 } 83 84 85 } 86 | Popular Tags |