KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > completion > util > DefaultModelProvider


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xml.schema.completion.util;
20
21 import java.net.URI JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.List JavaDoc;
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 /**
39  * Helps in getting the model for code completion.
40  *
41  * @author Samaresh (Samaresh.Panda@Sun.Com)
42  */

43 public class DefaultModelProvider extends CompletionModelProvider {
44     
45     private CompletionContextImpl context;
46     
47     public DefaultModelProvider() {
48     }
49
50     /**
51      * Returns a list of CompletionModel. Default implementation looks for
52      * schemaLocation attribute in the document and if specified creates model
53      * for each schema mentioned in there.
54      */

55     public List JavaDoc<CompletionModel> getModels(CompletionContext context) {
56         this.context = (CompletionContextImpl)context;
57         List JavaDoc<URI JavaDoc> uris = context.getSchemas();
58         if(uris == null || uris.size() == 0)
59             return null;
60         List JavaDoc<CompletionModel> models = new ArrayList JavaDoc<CompletionModel>();
61         for(URI JavaDoc 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 JavaDoc 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                 //purely for unit testing purposes.
82
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 JavaDoc tns = sm.getSchema().getTargetNamespace();
94             List JavaDoc<String JavaDoc> 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 JavaDoc ex) {
101             //no model for exception
102
}
103         return model;
104     }
105     
106     /**
107      * Uses lookup to find all CatalogModelProvider. If found uses the first one,
108      * else returns null. This is purely to solve the problem of not being able to
109      * use TestCatalogModel from unit tests.
110      *
111      * During actual CC from IDE, this will return null.
112      */

113     private CatalogModelProvider getCatalogModelProvider() {
114         Lookup.Template templ = new Lookup.Template(CatalogModelProvider.class);
115         Lookup.Result result = Lookup.getDefault().lookup(templ);
116         Collection JavaDoc 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