KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > wsdl > config > PortInformationHandler


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.websvc.wsdl.config;
21
22 import java.util.*;
23 import org.netbeans.modules.websvc.jaxrpc.PortInformation;
24 import org.netbeans.modules.websvc.jaxrpc.Utilities;
25 import org.xml.sax.Attributes JavaDoc;
26 import org.xml.sax.SAXException JavaDoc;
27 import org.xml.sax.SAXParseException JavaDoc;
28 import org.xml.sax.helpers.DefaultHandler JavaDoc;
29
30 /**
31  *
32  * @author Peter Williams
33  */

34 public class PortInformationHandler extends DefaultHandler JavaDoc implements PortInformation {
35
36     private static final String JavaDoc W3C_WSDL_SCHEMA = "http://schemas.xmlsoap.org/wsdl";
37     private static final String JavaDoc W3C_WSDL_SCHEMA_SLASH = "http://schemas.xmlsoap.org/wsdl/";
38     private static final String JavaDoc W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
39     
40     /* Currently, binding types are determined by the URI used for the binding subnode of
41      * the wsdl:binding element. E.g. soap:binding, http:binding, etc.
42      *
43      * Use the following constants to interpret the result of PortInfo.getBindingType()
44      */

45     private static final String JavaDoc SOAP_BINDING = "http://schemas.xmlsoap.org/wsdl/soap";
46     private static final String JavaDoc SOAP_BINDING_SLASH = "http://schemas.xmlsoap.org/wsdl/soap/";
47     
48     // Data collection results
49
private Map/*serviceName, ServiceInfo*/ serviceMap;
50     private String JavaDoc targetNamespace;
51     
52     // Intermediate storage
53
private List/*PortInfo*/ entirePortList;
54     private List/*PortInfo*/ bindingPortList;
55     private List/*String*/ wsdlImports;
56     private String JavaDoc currentServiceName;
57     private PortInfo bindingPort;
58     private Set wscompileFeatures;
59     private boolean serviceNameConflict;
60     
61     public PortInformationHandler() {
62         serviceMap = new LinkedHashMap(5);
63         entirePortList = new ArrayList();
64         bindingPortList = new ArrayList();
65         wsdlImports = new ArrayList();
66         initWscompileFeatures();
67     }
68     
69     public PortInformationHandler(String JavaDoc targetNamespace, Map serviceMap, List entirePortList, List bindingPortList, List wsdlImports) {
70         this.targetNamespace=targetNamespace;
71         this.serviceMap=serviceMap;
72         this.entirePortList=entirePortList;
73         this.bindingPortList=bindingPortList;
74         this.wsdlImports=wsdlImports;
75         initWscompileFeatures();
76     }
77     
78     private void initWscompileFeatures() {
79         wscompileFeatures = new HashSet(5);
80         wscompileFeatures.add("wsi");wscompileFeatures.add("strict");
81     }
82     
83     public void startElement(String JavaDoc uri, String JavaDoc localname, String JavaDoc qname, Attributes JavaDoc attributes) throws SAXException JavaDoc {
84         if(W3C_WSDL_SCHEMA.equals(uri) || W3C_WSDL_SCHEMA_SLASH.equals(uri)) {
85             if("portType".equals(localname)) { // NOI18N
86
PortInfo key = new PortInfo();
87                 key.setPortType(attributes.getValue("name")); // NOI18N
88

89                 PortInfo pi = (PortInfo)getPortInfoByKey(key);
90                 if(pi == null) {
91                     entirePortList.add(key);
92                 }
93             } else if("binding".equals(localname)) { // NOI18N
94
PortInfo key = new PortInfo();
95                 key.setBinding(attributes.getValue("name")); // NOI18N
96
key.setPortType(getLocalPart(attributes.getValue("type"))); // NOI18N
97

98                 PortInfo pi = (PortInfo)getPortInfoByKey(key);
99                 if(pi == null) {
100                     entirePortList.add(key);
101                 } else {
102                     if(pi.getBinding() == null) {
103                         pi.setBinding(key.getBinding());
104                     }
105                     if(pi.getPortType() == null) {
106                         pi.setPortType(key.getPortType());
107                     }
108                 }
109                 bindingPort = pi;
110                 bindingPortList.add(bindingPort);
111             } else if("port".equals(localname)) { // NOI18N
112
PortInfo key = new PortInfo();
113                 key.setPort(attributes.getValue("name")); // NOI18N
114
key.setBinding(getLocalPart(attributes.getValue("binding"))); // NOI18N
115

116                 PortInfo pi = (PortInfo)getPortInfoByKey(key);
117                 if(pi == null) {
118                     entirePortList.add(key);
119                     pi = key;
120                 } else {
121                     if(pi.getPort() == null) {
122                         pi.setPort(key.getPort());
123                     }
124                     if(pi.getBinding() == null) {
125                         pi.setBinding(key.getBinding());
126                     }
127                 }
128                 
129                 assert currentServiceName != null;
130                 
131                 ServiceInfo serviceInfo = (ServiceInfo) serviceMap.get(currentServiceName);
132                 if(serviceInfo == null) {
133                     serviceInfo = new ServiceInfo(currentServiceName);
134                     serviceMap.put(currentServiceName, serviceInfo);
135                 }
136                 
137                 List servicePorts = serviceInfo.getPorts();
138                 servicePorts.add(pi);
139             } else if("service".equals(localname)) { // NOI18N
140
currentServiceName = attributes.getValue("name"); // NOI18N
141
if (currentServiceName != null) {
142                     currentServiceName = Utilities.removeSpacesFromServiceName(currentServiceName);
143                 }
144             } else if("definitions".equals(localname)) { // NOI18N
145
targetNamespace = attributes.getValue("targetNamespace"); //NOI18N
146
} else if ("import".equals(localname)) { // NOI18N
147
String JavaDoc location = attributes.getValue("location"); // NOI18N
148
if (location!=null) wsdlImports.add(location);
149             }
150         } else if(bindingPort != null && "binding".equals(localname)) {
151             bindingPort.setBindingType(normalizeUri(uri));
152             if(SOAP_BINDING.equals(uri) || SOAP_BINDING_SLASH.equals(uri) ){
153                 String JavaDoc style = attributes.getValue("style"); //NOI18N
154
if (style!=null && ("rpc".equals(style)|| style.endsWith(":rpc"))) { //NOI18N
155
wscompileFeatures.remove("strict"); //NOI18N
156
wscompileFeatures.add("rpcliteral"); //NOI18N
157
}
158             }
159         } else if(W3C_XML_SCHEMA.equals(uri)) {
160             if ("element".equals(localname)) {
161                 String JavaDoc elementType = attributes.getValue("type"); //NOI18N
162
if (elementType!=null && ("anyType".equals(elementType) || elementType.endsWith(":anyType"))) { //NOI18N
163
wscompileFeatures.add("nodatabinding"); //NOI18N
164
}
165             } else if ("import".equals(localname)) {
166                 if (attributes.getValue("schemaLocation")==null && attributes.getValue("namespace")!=null) { //NOI18N
167
wscompileFeatures.add("searchschema"); //NOI18N
168
}
169             }
170         }
171     }
172     
173     public Map getServices() {
174         return serviceMap;
175     }
176     
177     public void endElement(String JavaDoc uri, String JavaDoc localname, String JavaDoc qname) throws SAXException JavaDoc {
178         if(W3C_WSDL_SCHEMA.equals(uri) || W3C_WSDL_SCHEMA_SLASH.equals(uri)) {
179             if("binding".equals(localname)) {
180                 bindingPort = null;
181             } else if("service".equals(localname)) { //NOI18N
182
if (currentServiceName!=null) {
183                     PortInformation.ServiceInfo si = getServiceInfo(currentServiceName);
184                     if (si!=null) {
185                         List ports = si.getPorts();
186                         for (Iterator it = ports.iterator();it.hasNext();) {
187                             PortInformation.PortInfo pi = ( PortInformation.PortInfo)it.next();
188                             if (currentServiceName.equals(pi.getPortType())) {
189                                 serviceNameConflict=true;
190                                 break;
191                             }
192                         }
193                         
194                     }
195                 }
196                 currentServiceName = null;
197             }
198         }
199     }
200     
201     public boolean isServiceNameConflict() {
202         return serviceNameConflict;
203     }
204     
205     public String JavaDoc [] getServiceNames() {
206         Set keys = serviceMap.keySet();
207         return (String JavaDoc []) keys.toArray(new String JavaDoc[keys.size()]);
208     }
209     
210     public PortInformation.ServiceInfo getServiceInfo(String JavaDoc serviceName) {
211         // !PW FIXME this is another place where case sensitivity of service names
212
// is biting me. websvc/registry forces first character of service name
213
// to uppercase (done inside JAXRPC/wscompile, so difficult to resolve).
214
// That is likely where the key will come from. However, we have stored
215
// the true case sensitive service name here. So to resolve matching
216
// problems, for now, search on key, if not found, iterate service map,
217
// doing case insensitive comparisons to find a match.
218
// return (ServiceInfo) serviceMap.get(serviceName);
219
ServiceInfo result = (ServiceInfo) serviceMap.get(serviceName);
220         if(result == null) {
221             Iterator iter = serviceMap.values().iterator();
222             while(iter.hasNext()) {
223                 ServiceInfo si = (ServiceInfo) iter.next();
224                 if(serviceName.equalsIgnoreCase(si.getServiceName())) {
225                     result = si;
226                     break;
227                 }
228             }
229         }
230         return result;
231     }
232     
233     public List getBindings() {
234         return bindingPortList;
235     }
236     
237     public List getImportedSchemas() {
238         return wsdlImports;
239     }
240     
241     public String JavaDoc getTargetNamespace() {
242         return targetNamespace;
243     }
244     
245     public List getEntirePortList() {
246         return entirePortList;
247     }
248     
249     public Set getWscompileFeatures() {
250         return wscompileFeatures;
251     }
252     
253     /** If there is a trailing backslash on the uri, remove it.
254      */

255     private String JavaDoc normalizeUri(String JavaDoc uri) {
256         String JavaDoc result = uri;
257         
258         if(uri.charAt(uri.length()-1) == '/') {
259             result = uri.substring(0, uri.length()-1);
260         }
261         
262         return result;
263     }
264     
265     private PortInformation.PortInfo getPortInfoByKey(PortInfo key) {
266         PortInfo result = null;
267         Iterator iter = entirePortList.iterator();
268         
269         while(iter.hasNext()) {
270             PortInfo pi = (PortInfo) iter.next();
271             
272             if(compareField(key.getPort(), pi.getPort())) {
273                 result = pi;
274                 break;
275             } else if(compareField(key.getBinding(), pi.getBinding())) {
276                 result = pi;
277                 break;
278             } else if(compareField(key.getPortType(), pi.getPortType())) {
279                 result = pi;
280                 break;
281             }
282         }
283         
284         return result;
285     }
286     
287     private boolean compareField(final String JavaDoc key, final String JavaDoc match) {
288         boolean result = false;
289         
290         if(match != null && match.equals(key)) {
291             result = true;
292         }
293         
294         return result;
295     }
296     
297     private String JavaDoc getLocalPart(String JavaDoc uri) {
298         String JavaDoc result = uri;
299         int index = uri.lastIndexOf(':');
300         if(index != -1) {
301             result = uri.substring(index+1);
302         }
303         return result;
304     }
305     
306     // public void fatalError(SAXParseException exception) throws SAXException {
307
// super.fatalError(exception);
308
// }
309
//
310
// public void error(SAXParseException exception) throws SAXException {
311
// super.error(exception);
312
// }
313
//
314
// public void warning(SAXParseException exception) throws SAXException {
315
// super.warning(exception);
316
// }
317

318     public static final class PortInfo implements PortInformation.PortInfo {
319         private String JavaDoc portName;
320         private String JavaDoc bindingName;
321         private String JavaDoc bindingType;
322         private String JavaDoc portTypeName;
323         
324         public PortInfo() {
325         }
326         
327         public String JavaDoc getPortType() {
328             return portTypeName;
329         }
330         
331         void setPortType(String JavaDoc pt) {
332             portTypeName = pt;
333         }
334         
335         public String JavaDoc getBinding() {
336             return bindingName;
337         }
338         
339         void setBinding(String JavaDoc b) {
340             bindingName = b;
341         }
342         
343         public String JavaDoc getBindingType() {
344             return bindingType;
345         }
346         
347         void setBindingType(String JavaDoc bt) {
348             bindingType = bt;
349         }
350         
351         public String JavaDoc getPort() {
352             return portName;
353         }
354         
355         void setPort(String JavaDoc p) {
356             portName = p;
357         }
358         
359         public String JavaDoc toString() {
360             return "(" + portName + ", " + bindingName + ", " + portTypeName + ")"; // NOI18N
361
}
362     }
363     
364     public static final class ServiceInfo implements PortInformation.ServiceInfo {
365         
366         private String JavaDoc serviceName;
367         private List portList;
368         
369         public ServiceInfo(String JavaDoc name, List ports) {
370             init(name, ports);
371         }
372         
373         public ServiceInfo(String JavaDoc name) {
374             init(name, new ArrayList());
375         }
376         
377         private void init(String JavaDoc name, List ports) {
378             this.serviceName = name;
379             this.portList = ports;
380         }
381         
382         public String JavaDoc getServiceName() {
383             return serviceName;
384         }
385         
386         public List/*PortInfo*/ getPorts() {
387             return portList;
388         }
389         
390     }
391 }
392
Popular Tags