1 19 package org.netbeans.modules.xml.schema.completion.util; 20 21 import java.net.URI ; 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.List ; 25 import org.netbeans.modules.xml.retriever.catalog.Utilities; 26 import org.netbeans.modules.xml.schema.completion.spi.CompletionContext; 27 import org.netbeans.modules.xml.schema.completion.spi.CompletionModelProvider; 28 import org.netbeans.modules.xml.schema.completion.spi.CompletionModelProvider.CompletionModel; 29 import org.netbeans.modules.xml.schema.completion.util.CatalogModelProvider; 30 import org.netbeans.modules.xml.schema.model.SchemaModel; 31 import org.netbeans.modules.xml.schema.model.SchemaModelFactory; 32 import org.netbeans.modules.xml.xam.ModelSource; 33 import org.netbeans.modules.xml.xam.locator.CatalogModel; 34 import org.netbeans.modules.xml.xam.locator.CatalogModelFactory; 35 import org.openide.filesystems.FileObject; 36 import org.openide.util.Lookup; 37 38 43 public class DefaultModelProvider extends CompletionModelProvider { 44 45 private CompletionContextImpl context; 46 47 public DefaultModelProvider() { 48 } 49 50 55 public List <CompletionModel> getModels(CompletionContext context) { 56 this.context = (CompletionContextImpl)context; 57 List <URI > uris = context.getSchemas(); 58 if(uris == null || uris.size() == 0) 59 return null; 60 List <CompletionModel> models = new ArrayList <CompletionModel>(); 61 for(URI uri : context.getSchemas()) { 62 CompletionModel model = getCompletionModel(uri); 63 if(model != null) 64 models.add(model); 65 } 66 67 return models; 68 } 69 70 private CompletionModel getCompletionModel(URI schemaURI) { 71 CompletionModel model = null; 72 try { 73 ModelSource modelSource = null; 74 CatalogModel catalogModel = null; 75 CatalogModelProvider catalogModelProvider = getCatalogModelProvider(); 76 if(catalogModelProvider == null) { 77 modelSource = Utilities.getModelSource(context.getPrimaryFile(), true); 78 CatalogModelFactory factory = CatalogModelFactory.getDefault(); 79 catalogModel = factory.getCatalogModel(modelSource); 80 } else { 81 modelSource = catalogModelProvider.getModelSource(context.getPrimaryFile(), true); 83 catalogModel = catalogModelProvider.getCatalogModel(); 84 } 85 ModelSource schemaModelSource; 86 schemaModelSource = catalogModel.getModelSource(schemaURI, modelSource); 87 SchemaModel sm = null; 88 if(schemaModelSource.getLookup().lookup(FileObject.class) == null) { 89 sm = SchemaModelFactory.getDefault().createFreshModel(schemaModelSource); 90 } else { 91 sm = SchemaModelFactory.getDefault().getModel(schemaModelSource); 92 } 93 String tns = sm.getSchema().getTargetNamespace(); 94 List <String > prefixes = CompletionUtil.getPrefixesAgainstTargetNamespace( 95 context, tns); 96 if(prefixes != null && prefixes.size() > 0) 97 model = new CompletionModelEx(context, prefixes.get(0), sm); 98 else 99 model = new CompletionModelEx(context, context.suggestPrefix(tns), sm); 100 } catch (Exception ex) { 101 } 103 return model; 104 } 105 106 113 private CatalogModelProvider getCatalogModelProvider() { 114 Lookup.Template templ = new Lookup.Template(CatalogModelProvider.class); 115 Lookup.Result result = Lookup.getDefault().lookup(templ); 116 Collection impls = result.allInstances(); 117 if(impls == null || impls.size() == 0) 118 return null; 119 120 return (CatalogModelProvider)impls.iterator().next(); 121 } 122 123 } 124 | Popular Tags |