KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > extensibility > model > impl > ExtensibilityUtils


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
20 package org.netbeans.modules.xml.wsdl.ui.extensibility.model.impl;
21
22 import java.io.File JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25
26 import javax.xml.namespace.QName JavaDoc;
27
28 import org.netbeans.modules.xml.retriever.catalog.Utilities;
29 import org.netbeans.modules.xml.schema.model.Element;
30 import org.netbeans.modules.xml.schema.model.GlobalElement;
31 import org.netbeans.modules.xml.schema.model.LocalElement;
32 import org.netbeans.modules.xml.schema.model.Schema;
33 import org.netbeans.modules.xml.schema.model.SchemaModel;
34 import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
35 import org.netbeans.modules.xml.wsdl.model.Binding;
36 import org.netbeans.modules.xml.wsdl.model.BindingFault;
37 import org.netbeans.modules.xml.wsdl.model.BindingInput;
38 import org.netbeans.modules.xml.wsdl.model.BindingOperation;
39 import org.netbeans.modules.xml.wsdl.model.BindingOutput;
40 import org.netbeans.modules.xml.wsdl.model.Definitions;
41 import org.netbeans.modules.xml.wsdl.model.ExtensibilityElement;
42 import org.netbeans.modules.xml.wsdl.model.Message;
43 import org.netbeans.modules.xml.wsdl.model.Operation;
44 import org.netbeans.modules.xml.wsdl.model.Port;
45 import org.netbeans.modules.xml.wsdl.model.Service;
46 import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
47 import org.netbeans.modules.xml.wsdl.ui.extensibility.model.ModelSourceProvider;
48 import org.netbeans.modules.xml.wsdl.ui.extensibility.model.WSDLExtensibilityElement;
49 import org.netbeans.modules.xml.wsdl.ui.extensibility.model.WSDLExtensibilityElementInfo;
50 import org.netbeans.modules.xml.wsdl.ui.extensibility.model.WSDLExtensibilityElements;
51 import org.netbeans.modules.xml.wsdl.ui.extensibility.model.WSDLExtensibilityElementsFactory;
52 import org.netbeans.modules.xml.wsdl.ui.extensibility.model.XMLSchemaFileInfo;
53 import org.netbeans.modules.xml.wsdl.ui.schema.visitor.AbstractXSDVisitor;
54 import org.netbeans.modules.xml.xam.Model;
55 import org.netbeans.modules.xml.xam.ModelSource;
56 import org.openide.ErrorManager;
57 import org.openide.filesystems.FileObject;
58 import org.openide.filesystems.FileUtil;
59 import org.openide.loaders.DataObject;
60
61 /**
62  *
63  * @author radval
64  *
65  */

66
67 public class ExtensibilityUtils {
68     
69     private static ModelSourceProvider mProvider;
70     
71     public static Schema readSchema(DataObject dataObject) {
72         
73         Schema schema = null;
74         try {
75             FileObject fileObject = dataObject.getPrimaryFile();
76             ModelSource ms = null;
77             if(mProvider != null) {
78                 ms = mProvider.getModelSource(fileObject, false);
79             } else {
80                 ms = Utilities.getModelSource(fileObject, false);
81             }
82             SchemaModel schemaModel = SchemaModelFactory.getDefault().getModel(ms);
83             if (schemaModel.getState() != Model.State.NOT_WELL_FORMED) {
84                 schema = schemaModel.getSchema();
85             }
86         } catch(Exception JavaDoc ex) {
87             ex.printStackTrace();
88         }
89         return schema;
90     }
91     
92     public static XMLSchemaFileInfo createXMLSchemaFileInfo(DataObject dataObject) {
93         return new XMLSchemaFileInfoImpl(dataObject);
94     }
95     
96     public static String JavaDoc getExtensibilityElementType(WSDLComponent component) {
97         
98         
99         if (component instanceof Binding) {
100             return WSDLExtensibilityElements.ELEMENT_BINDING;
101         }
102         if (component instanceof BindingOperation) {
103             return WSDLExtensibilityElements.ELEMENT_BINDING_OPERATION;
104         }
105         if (component instanceof BindingFault) {
106             return WSDLExtensibilityElements.ELEMENT_BINDING_OPERATION_FAULT;
107         }
108         if (component instanceof BindingInput) {
109             return WSDLExtensibilityElements.ELEMENT_BINDING_OPERATION_INPUT;
110         }
111         if (component instanceof BindingOutput) {
112             return WSDLExtensibilityElements.ELEMENT_BINDING_OPERATION_OUTPUT;
113         }
114         if (component instanceof Definitions) {
115             return WSDLExtensibilityElements.ELEMENT_DEFINITIONS;
116         }
117         if (component instanceof Message) {
118             return WSDLExtensibilityElements.ELEMENT_MESSAGE;
119         }
120         if (component instanceof Operation) {
121             return WSDLExtensibilityElements.ELEMENT_PORTTYPE_OPERATION;
122         }
123         if (component instanceof Service) {
124             return WSDLExtensibilityElements.ELEMENT_SERVICE;
125         }
126         if (component instanceof Port) {
127             return WSDLExtensibilityElements.ELEMENT_SERVICE_PORT;
128         }
129         
130
131         return null;
132     }
133     
134     public static Element getElement(ExtensibilityElement component) {
135         List JavaDoc<QName JavaDoc> qnames = new ArrayList JavaDoc<QName JavaDoc>();
136         ExtensibilityElement tempComponent = component;
137         if (tempComponent != null) {
138             qnames.add(0, tempComponent.getQName());
139             while (ExtensibilityElement.class.isAssignableFrom(tempComponent.getParent().getClass())) {
140                 tempComponent = (ExtensibilityElement) tempComponent.getParent();
141                 qnames.add(0, tempComponent.getQName());
142             }
143         }
144         String JavaDoc extensibilityElementType = null;
145         if (component.getParent() != null) {
146             extensibilityElementType = getExtensibilityElementType(tempComponent.getParent());
147         }
148         
149         if (extensibilityElementType != null) {
150             try {
151                 WSDLExtensibilityElements elements = WSDLExtensibilityElementsFactory.getInstance().getWSDLExtensibilityElements();
152                 WSDLExtensibilityElement mExtensibilityElement = elements.getWSDLExtensibilityElement(extensibilityElementType);
153                 if (mExtensibilityElement != null && qnames.size() > 0) {
154                     WSDLExtensibilityElementInfo infos = mExtensibilityElement.getWSDLExtensibilityElementInfos(qnames.remove(0));
155                     if (infos != null) {
156                         Element element = infos.getElement();
157                         ElementFinderVisitor visitor = new ElementFinderVisitor(qnames);
158                         element.accept(visitor);
159                         return visitor.getElement();
160                     }
161                 }
162                 
163                 
164             } catch (Exception JavaDoc e) {
165                 ErrorManager.getDefault().notify(e);
166             }
167             
168             
169         }
170         return null;
171     }
172     
173     //This should be used only from JUnit test
174
public static void setModelSourceProvider(ModelSourceProvider provider) {
175         mProvider = provider;
176     }
177     
178     
179 }
180 class ElementFinderVisitor extends AbstractXSDVisitor {
181     
182     private List JavaDoc<QName JavaDoc> qnames;
183     private Element element;
184     
185     public ElementFinderVisitor(List JavaDoc<QName JavaDoc> qnames) {
186         this.qnames = qnames;
187     }
188     
189     @Override JavaDoc
190     public void visit(GlobalElement ge) {
191         if (qnames.size() > 0) {
192             if (new QName JavaDoc(ge.getModel().getSchema ().getTargetNamespace(), ge.getName()).equals(qnames.get(0))) {
193                 qnames.remove(0);
194                 if (qnames.size() == 0) {
195                     element = ge;
196                 }
197             }
198             if (element == null) {
199                 super.visit(ge);
200             }
201         }
202     }
203
204     @Override JavaDoc
205     public void visit(LocalElement le) {
206         if (qnames.size() > 0) {
207             if (new QName JavaDoc(le.getModel().getSchema ().getTargetNamespace(), le.getName()).equals(qnames.get(0))) {
208                 qnames.remove(0);
209                 if (qnames.size() == 0) {
210                     element = le;
211                 }
212             }
213             if (element == null) {
214                 super.visit(le);
215             }
216         }
217     }
218     
219     public Element getElement() {
220         return element;
221     }
222     
223     
224     
225     
226 }
227
Popular Tags