KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > jaxws > api > WsdlWrapperHandler


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.jaxws.api;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24 import org.xml.sax.SAXException JavaDoc;
25 import org.xml.sax.helpers.DefaultHandler JavaDoc;
26
27 /** ConteHandler that gives information if wsdl wrapper need to be created
28  * This is the case when service element is missing
29  *
30  * @author mkuchtiak
31  */

32 public class WsdlWrapperHandler extends DefaultHandler JavaDoc{
33     
34     public static final String JavaDoc WSDL_SOAP_URI = "http://schemas.xmlsoap.org/wsdl/"; //NOI18N
35
public static final String JavaDoc SOAP_BINDING_PREFIX = "http://schemas.xmlsoap.org/wsdl/soap"; //NOI18N
36

37     private boolean isService, isPortType, isBinding;
38     private String JavaDoc tns;
39     private Map JavaDoc<String JavaDoc, String JavaDoc> prefixes;
40     private Map JavaDoc<String JavaDoc, BindingInfo> bindings;
41     private Map JavaDoc<String JavaDoc, String JavaDoc> ports;
42     private BindingInfo bindingInfo;
43     private boolean insideBinding, insideService;
44     
45     /** Creates a new instance of WsdlWrapperHandler */
46     public WsdlWrapperHandler() {
47         prefixes = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
48         bindings = new HashMap JavaDoc<String JavaDoc, BindingInfo>();
49         ports = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
50     }
51
52     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws SAXException JavaDoc {
53         if (!prefixes.containsKey(uri)) prefixes.put(uri,prefix);
54     }
55     
56     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qname, org.xml.sax.Attributes JavaDoc attributes) throws org.xml.sax.SAXException JavaDoc {
57         if("portType".equals(localName)) { // NOI18N
58
isPortType=true;
59         } else if("binding".equals(localName)) { // NOI18N
60
isBinding=true;
61             if (WSDL_SOAP_URI.equals(uri)) {
62                 String JavaDoc bindingName=attributes.getValue("name"); // NOI18N
63
insideBinding=true;
64                 if (bindingName!=null) {
65                     bindingInfo = new BindingInfo(bindingName);
66                     bindings.put(bindingName,bindingInfo);
67                 }
68             } else if (insideBinding && bindingInfo!=null && uri.startsWith(SOAP_BINDING_PREFIX)) {
69                 bindingInfo.setBindingType(attributes.getValue("transport")); //NOI18N
70
}
71         } else if("service".equals(localName)) { // NOI18N
72
isService=true;
73             insideService=true;
74         } else if("port".equals(localName) && insideService) { // NOI18N
75
String JavaDoc portName = attributes.getValue("name"); // NOI18N
76
if (portName!=null) ports.put(portName, attributes.getValue("binding")); // NOI18N
77
} else if("definitions".equals(localName)) { // NOI18N
78
tns=attributes.getValue("targetNamespace"); // NOI18N
79
}
80     }
81
82     public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
83         if("binding".equals(localName) && WSDL_SOAP_URI.equals(uri)) { // NOI18N
84
bindingInfo=null;
85             insideBinding=false;
86         } else if ("service".equals(localName)) {
87             insideService=false;
88         }
89     }
90     
91     public String JavaDoc getBindingTypeForPort(String JavaDoc name) {
92         String JavaDoc fullBindingName = ports.get(name);
93         if (fullBindingName!=null) {
94             String JavaDoc bindingName = getLocalPart(fullBindingName);
95             BindingInfo info = bindings.get(bindingName);
96             if (info!=null) return info.getBindingType();
97         }
98         return null;
99     }
100     
101     public boolean isServiceElement() {
102         return isService;
103     }
104     
105     public String JavaDoc getTargetNsPrefix() {
106         return (prefixes == null) ? null : prefixes.get(tns);
107     }
108
109     public void endDocument() throws SAXException JavaDoc {
110         // throw exception if service & binding & portType are missing
111
if (!isService && !isBinding && !isPortType) throw new SAXException JavaDoc("Missing wsdl elements (wsdl:service | wsdl:binding | wsdl:portType)"); //NOI18N
112
}
113     
114     private class BindingInfo {
115         private String JavaDoc bindingName;
116         private String JavaDoc bindingType;
117         
118         BindingInfo(String JavaDoc bindingName) {
119             this.bindingName=bindingName;
120         }
121         
122         void setBindingType(String JavaDoc bindingType) {
123             this.bindingType=bindingType;
124         }
125         
126         String JavaDoc getBindingType() {
127             return bindingType;
128         }
129     }
130     
131     private String JavaDoc getLocalPart(String JavaDoc fullName) {
132         int index = fullName.indexOf(":"); //NOI18N
133
return (index>=0?fullName.substring(index+1):fullName);
134     }
135 }
136
Popular Tags