KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.InputStream JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import org.netbeans.modules.xml.schema.completion.spi.CompletionContext;
25 import org.netbeans.modules.xml.schema.completion.spi.CompletionModelProvider;
26 import org.netbeans.modules.xml.schema.completion.spi.CompletionModelProvider.CompletionModel;
27 import org.netbeans.modules.xml.schema.model.SchemaModel;
28 import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
29 import org.netbeans.modules.xml.xam.ModelSource;
30 import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
31 import org.openide.util.lookup.Lookups;
32
33 /**
34  * Helps in getting the model for code completion.
35  *
36  * @author Samaresh (Samaresh.Panda@Sun.Com)
37  */

38 public class MetaSchemaModelProvider extends CompletionModelProvider {
39     
40     private CompletionContextImpl context;
41     
42     public MetaSchemaModelProvider() {
43     }
44
45     /**
46      * Returns a list of CompletionModel. Default implementation looks for
47      * schemaLocation attribute in the document and if specified creates model
48      * for each schema mentioned in there.
49      */

50     public List JavaDoc<CompletionModel> getModels(CompletionContext context) {
51         SchemaModel sm = createMetaSchemaModel();
52         if(sm == null)
53             return null;
54         CompletionModel cm = new CompletionModelEx((CompletionContextImpl)context, "xsd", sm); //NOI18N
55
List JavaDoc<CompletionModel> models = new ArrayList JavaDoc<CompletionModel>();
56         models.add(cm);
57         return models;
58     }
59     
60     private SchemaModel createMetaSchemaModel() {
61         try {
62             InputStream JavaDoc in = getClass().getResourceAsStream("XMLSchema.xsd"); //NOI18N
63
javax.swing.text.Document JavaDoc d = AbstractDocumentModel.
64                     getAccessProvider().loadSwingDocument(in);
65         ModelSource ms = new ModelSource(Lookups.singleton(d), false);
66             SchemaModel m = SchemaModelFactory.getDefault().createFreshModel(ms);
67             m.sync();
68             return m;
69         } catch (Exception JavaDoc ex) {
70             //just catch
71
}
72         return null;
73     }
74         
75 }
76
Popular Tags