KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > completion > WSDLCompletionModelProvider


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.wsdl.ui.completion;
20
21 import java.io.InputStream JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.netbeans.modules.xml.schema.completion.spi.CompletionContext;
26 import org.netbeans.modules.xml.schema.completion.spi.CompletionModelProvider;
27 import org.netbeans.modules.xml.schema.model.SchemaModel;
28 import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
29 import org.netbeans.modules.xml.wsdl.ui.extensibility.model.WSDLExtensibilityElements;
30 import org.netbeans.modules.xml.wsdl.ui.extensibility.model.WSDLExtensibilityElementsFactory;
31 import org.netbeans.modules.xml.wsdl.ui.extensibility.model.XMLSchemaFileInfo;
32 import org.netbeans.modules.xml.xam.ModelSource;
33 import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
34 import org.openide.util.lookup.Lookups;
35
36 /**
37  * CompletionModelProvider for WSDL document. The extensibility elements need to write their own completionmodelprovider.
38  *
39  * @author Shivanand (shivanand.kini@sun.com)
40  */

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

51     @Override JavaDoc
52     public List JavaDoc<CompletionModel> getModels(CompletionContext context) {
53         //check the ext is wsdl
54
if (!context.getPrimaryFile().getExt().equals("wsdl")) {
55             return null;
56         }
57         
58         SchemaModel wsdlSchemaModel = createWSDLSchemaModel();
59         if(wsdlSchemaModel == null)
60             return null;
61         CompletionModel cm = new WSDLCompletionModel(context, wsdlSchemaModel, "wsdl"); //NOI18N
62
List JavaDoc<CompletionModel> models = new ArrayList JavaDoc<CompletionModel>();
63         models.add(cm);
64         
65         try {
66             WSDLExtensibilityElements elements = WSDLExtensibilityElementsFactory.getInstance().getWSDLExtensibilityElements();
67             for (XMLSchemaFileInfo info : elements.getAllXMLSchemaFileInfos()) {
68                 CompletionModel model = createExtensibilityElementSchemaCompletionModel(info);
69                 if (model != null) {
70                     models.add(model);
71                 }
72             }
73         } catch (Exception JavaDoc e) {
74             e.printStackTrace();
75         }
76         return models;
77     }
78     
79     private CompletionModel createExtensibilityElementSchemaCompletionModel(XMLSchemaFileInfo info) {
80         if (info.getSchema() != null) {
81             return new ExtensibilityElementCompletionModel(info.getSchema().getModel(), info.getPrefix(), info.getSchema().getTargetNamespace());
82         }
83         return null;
84     }
85
86     private SchemaModel createWSDLSchemaModel() {
87         try {
88             InputStream JavaDoc in = getClass().getResourceAsStream("/org/netbeans/modules/xml/wsdl/ui/netbeans/module/resources/wsdl.xsd"); //NOI18N
89
javax.swing.text.Document JavaDoc d = AbstractDocumentModel.
90             getAccessProvider().loadSwingDocument(in);
91             ModelSource ms = new ModelSource(Lookups.singleton(d), false);
92             SchemaModel m = SchemaModelFactory.getDefault().createFreshModel(ms);
93             m.sync();
94             return m;
95         } catch (Exception JavaDoc ex) {
96             //just catch
97
}
98         return null;
99     }
100     
101     
102     class ExtensibilityElementCompletionModel extends CompletionModel {
103
104         SchemaModel schemaModel;
105         String JavaDoc targetNamespace;
106         String JavaDoc prefix;
107         
108         public ExtensibilityElementCompletionModel(SchemaModel schemaModel, String JavaDoc prefix, String JavaDoc targetNamespace) {
109             this.schemaModel = schemaModel;
110             this.prefix = prefix;
111             this.targetNamespace = targetNamespace;
112         }
113         
114         @Override JavaDoc
115         public SchemaModel getSchemaModel() {
116             return schemaModel;
117         }
118
119         @Override JavaDoc
120         public String JavaDoc getSuggestedPrefix() {
121             return prefix;
122         }
123
124         @Override JavaDoc
125         public String JavaDoc getTargetNamespace() {
126             
127             return targetNamespace;
128         }
129         
130     }
131         
132 }
133
Popular Tags