KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > view > treeeditor > newtype > ExtensibilityElementNewTypesFactory


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.view.treeeditor.newtype;
21
22 import java.io.Serializable JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Comparator JavaDoc;
27 import java.util.List JavaDoc;
28
29 import javax.xml.namespace.QName JavaDoc;
30
31 import org.netbeans.modules.xml.wsdl.model.Binding;
32 import org.netbeans.modules.xml.wsdl.model.BindingFault;
33 import org.netbeans.modules.xml.wsdl.model.BindingInput;
34 import org.netbeans.modules.xml.wsdl.model.BindingOperation;
35 import org.netbeans.modules.xml.wsdl.model.BindingOutput;
36 import org.netbeans.modules.xml.wsdl.model.ExtensibilityElement;
37 import org.netbeans.modules.xml.wsdl.model.Port;
38 import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
39 import org.netbeans.modules.xml.wsdl.ui.extensibility.model.WSDLExtensibilityElement;
40 import org.netbeans.modules.xml.wsdl.ui.extensibility.model.WSDLExtensibilityElementInfo;
41 import org.netbeans.modules.xml.wsdl.ui.extensibility.model.WSDLExtensibilityElements;
42 import org.netbeans.modules.xml.wsdl.ui.extensibility.model.WSDLExtensibilityElementsFactory;
43 import org.netbeans.modules.xml.wsdl.ui.netbeans.module.Utility;
44 import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
45 import org.openide.ErrorManager;
46 import org.openide.util.datatransfer.NewType;
47
48 /**
49  *
50  * @author skini
51  */

52 public class ExtensibilityElementNewTypesFactory implements NewTypesFactory {
53     
54     private String JavaDoc mNodeType;
55     private String JavaDoc[] mSpecialTargetNamespaces;
56     /** Creates a new instance of ExtensibilityElementNewTypesFactory */
57     public ExtensibilityElementNewTypesFactory(String JavaDoc nodeType) {
58         mNodeType = nodeType;
59     }
60     
61     public ExtensibilityElementNewTypesFactory(String JavaDoc nodeType, String JavaDoc[] specialTargetNS) {
62         mNodeType = nodeType;
63         mSpecialTargetNamespaces = specialTargetNS;
64     }
65     
66     public NewType[] getNewTypes(WSDLComponent component) {
67         ArrayList JavaDoc<ExtensibilityElementNewType> eeNewTypeList = new ArrayList JavaDoc<ExtensibilityElementNewType>();
68         try {
69             WSDLExtensibilityElements extensibilityElement
70                     = WSDLExtensibilityElementsFactory.getInstance().getWSDLExtensibilityElements();
71             
72             WSDLExtensibilityElement element = extensibilityElement.getWSDLExtensibilityElement(mNodeType);
73             String JavaDoc namespace = getExtensibilityElementNamespace(component);
74             if(element != null) {
75                 Collection JavaDoc<WSDLExtensibilityElementInfo> elementInfos = null;
76                 if (mSpecialTargetNamespaces != null) {
77                     elementInfos = new ArrayList JavaDoc<WSDLExtensibilityElementInfo>();
78                     for (String JavaDoc ns : mSpecialTargetNamespaces) {
79                         elementInfos.addAll(element.getWSDLExtensibilityElementInfos(ns));
80                     }
81                     
82                 } else {
83                     if (namespace != null) {
84                         elementInfos = element.getWSDLExtensibilityElementInfos(namespace);
85                     } else {
86                         elementInfos = element.getAllWSDLExtensibilityElementInfos();
87                     }
88                 }
89                 
90                 //remove already added ones.
91

92                 createExtensibilityElementNewTypes(component, elementInfos, eeNewTypeList);
93                
94             }
95         } catch(Exception JavaDoc ex) {
96             ErrorManager.getDefault().notify(ex);
97         }
98         return eeNewTypeList.toArray(new NewType[eeNewTypeList.size()]);
99     }
100     
101     private void createExtensibilityElementNewTypes(WSDLComponent component, Collection JavaDoc<WSDLExtensibilityElementInfo> infos, List JavaDoc<ExtensibilityElementNewType> list) {
102         
103         if (infos != null) {
104             for (WSDLExtensibilityElementInfo info : infos) {
105                 list.add(new ExtensibilityElementNewType(component, info));
106             }
107             Collections.sort(list, new ExtensibilityElementNewTypeComparator());
108         }
109     }
110     
111     private String JavaDoc getExtensibilityElementNamespace(WSDLComponent comp) {
112         Binding binding = null;
113         if (comp instanceof BindingOperation
114                 || comp instanceof BindingInput
115                 || comp instanceof BindingOutput
116                 || comp instanceof BindingFault)
117         {
118             WSDLComponent tempComp = comp;
119             while (!((tempComp = tempComp.getParent()) instanceof Binding)) {
120                 //do nothing
121
}
122             binding = (Binding) tempComp;
123         } else if (comp instanceof Port) {
124             NamedComponentReference<Binding> ref = ((Port)comp).getBinding();
125             if (ref != null && ref.get() != null) {
126                 binding = ref.get();
127             }
128         }
129         if (binding != null) {
130             List JavaDoc<ExtensibilityElement> eeList = binding.getExtensibilityElements();
131             if (eeList != null && !eeList.isEmpty()) {
132                 ExtensibilityElement element = eeList.get(0);
133                 QName JavaDoc qname = element.getQName();
134                 if (qname.getNamespaceURI() != null) {
135                     return qname.getNamespaceURI();
136                 } else if (qname.getPrefix() != null) {
137                     return Utility.getNamespaceURI(qname.getPrefix(), comp);
138                 }
139             }
140         }
141         
142         return null;
143     }
144     
145     
146     
147     static class ExtensibilityElementNewTypeComparator implements Comparator JavaDoc<ExtensibilityElementNewType>, Serializable JavaDoc {
148
149         private static final long serialVersionUID = 8682651956156135260L;
150
151         public int compare(ExtensibilityElementNewType o1, ExtensibilityElementNewType o2) {
152             return o1.getName().compareTo(o2.getName());
153         }
154         
155     }
156    
157 }
158
Popular Tags