1 19 20 package org.netbeans.modules.websvc.jaxws.api; 21 22 import java.util.HashMap ; 23 import java.util.Map ; 24 import org.xml.sax.SAXException ; 25 import org.xml.sax.helpers.DefaultHandler ; 26 27 32 public class WsdlWrapperHandler extends DefaultHandler { 33 34 public static final String WSDL_SOAP_URI = "http://schemas.xmlsoap.org/wsdl/"; public static final String SOAP_BINDING_PREFIX = "http://schemas.xmlsoap.org/wsdl/soap"; 37 private boolean isService, isPortType, isBinding; 38 private String tns; 39 private Map <String , String > prefixes; 40 private Map <String , BindingInfo> bindings; 41 private Map <String , String > ports; 42 private BindingInfo bindingInfo; 43 private boolean insideBinding, insideService; 44 45 46 public WsdlWrapperHandler() { 47 prefixes = new HashMap <String , String >(); 48 bindings = new HashMap <String , BindingInfo>(); 49 ports = new HashMap <String , String >(); 50 } 51 52 public void startPrefixMapping(String prefix, String uri) throws SAXException { 53 if (!prefixes.containsKey(uri)) prefixes.put(uri,prefix); 54 } 55 56 public void startElement(String uri, String localName, String qname, org.xml.sax.Attributes attributes) throws org.xml.sax.SAXException { 57 if("portType".equals(localName)) { isPortType=true; 59 } else if("binding".equals(localName)) { isBinding=true; 61 if (WSDL_SOAP_URI.equals(uri)) { 62 String bindingName=attributes.getValue("name"); 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")); } 71 } else if("service".equals(localName)) { isService=true; 73 insideService=true; 74 } else if("port".equals(localName) && insideService) { String portName = attributes.getValue("name"); if (portName!=null) ports.put(portName, attributes.getValue("binding")); } else if("definitions".equals(localName)) { tns=attributes.getValue("targetNamespace"); } 80 } 81 82 public void endElement(String uri, String localName, String qName) throws SAXException { 83 if("binding".equals(localName) && WSDL_SOAP_URI.equals(uri)) { bindingInfo=null; 85 insideBinding=false; 86 } else if ("service".equals(localName)) { 87 insideService=false; 88 } 89 } 90 91 public String getBindingTypeForPort(String name) { 92 String fullBindingName = ports.get(name); 93 if (fullBindingName!=null) { 94 String 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 getTargetNsPrefix() { 106 return (prefixes == null) ? null : prefixes.get(tns); 107 } 108 109 public void endDocument() throws SAXException { 110 if (!isService && !isBinding && !isPortType) throw new SAXException ("Missing wsdl elements (wsdl:service | wsdl:binding | wsdl:portType)"); } 113 114 private class BindingInfo { 115 private String bindingName; 116 private String bindingType; 117 118 BindingInfo(String bindingName) { 119 this.bindingName=bindingName; 120 } 121 122 void setBindingType(String bindingType) { 123 this.bindingType=bindingType; 124 } 125 126 String getBindingType() { 127 return bindingType; 128 } 129 } 130 131 private String getLocalPart(String fullName) { 132 int index = fullName.indexOf(":"); return (index>=0?fullName.substring(index+1):fullName); 134 } 135 } 136 | Popular Tags |