1 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 ; 26 import org.xml.sax.SAXException ; 27 import org.xml.sax.SAXParseException ; 28 import org.xml.sax.helpers.DefaultHandler ; 29 30 34 public class PortInformationHandler extends DefaultHandler implements PortInformation { 35 36 private static final String W3C_WSDL_SCHEMA = "http://schemas.xmlsoap.org/wsdl"; 37 private static final String W3C_WSDL_SCHEMA_SLASH = "http://schemas.xmlsoap.org/wsdl/"; 38 private static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema"; 39 40 45 private static final String SOAP_BINDING = "http://schemas.xmlsoap.org/wsdl/soap"; 46 private static final String SOAP_BINDING_SLASH = "http://schemas.xmlsoap.org/wsdl/soap/"; 47 48 private Map serviceMap; 50 private String targetNamespace; 51 52 private List entirePortList; 54 private List bindingPortList; 55 private List wsdlImports; 56 private String 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 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 uri, String localname, String qname, Attributes attributes) throws SAXException { 84 if(W3C_WSDL_SCHEMA.equals(uri) || W3C_WSDL_SCHEMA_SLASH.equals(uri)) { 85 if("portType".equals(localname)) { PortInfo key = new PortInfo(); 87 key.setPortType(attributes.getValue("name")); 89 PortInfo pi = (PortInfo)getPortInfoByKey(key); 90 if(pi == null) { 91 entirePortList.add(key); 92 } 93 } else if("binding".equals(localname)) { PortInfo key = new PortInfo(); 95 key.setBinding(attributes.getValue("name")); key.setPortType(getLocalPart(attributes.getValue("type"))); 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)) { PortInfo key = new PortInfo(); 113 key.setPort(attributes.getValue("name")); key.setBinding(getLocalPart(attributes.getValue("binding"))); 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)) { currentServiceName = attributes.getValue("name"); if (currentServiceName != null) { 142 currentServiceName = Utilities.removeSpacesFromServiceName(currentServiceName); 143 } 144 } else if("definitions".equals(localname)) { targetNamespace = attributes.getValue("targetNamespace"); } else if ("import".equals(localname)) { String location = attributes.getValue("location"); 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 style = attributes.getValue("style"); if (style!=null && ("rpc".equals(style)|| style.endsWith(":rpc"))) { wscompileFeatures.remove("strict"); wscompileFeatures.add("rpcliteral"); } 158 } 159 } else if(W3C_XML_SCHEMA.equals(uri)) { 160 if ("element".equals(localname)) { 161 String elementType = attributes.getValue("type"); if (elementType!=null && ("anyType".equals(elementType) || elementType.endsWith(":anyType"))) { wscompileFeatures.add("nodatabinding"); } 165 } else if ("import".equals(localname)) { 166 if (attributes.getValue("schemaLocation")==null && attributes.getValue("namespace")!=null) { wscompileFeatures.add("searchschema"); } 169 } 170 } 171 } 172 173 public Map getServices() { 174 return serviceMap; 175 } 176 177 public void endElement(String uri, String localname, String qname) throws SAXException { 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)) { 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 [] getServiceNames() { 206 Set keys = serviceMap.keySet(); 207 return (String []) keys.toArray(new String [keys.size()]); 208 } 209 210 public PortInformation.ServiceInfo getServiceInfo(String serviceName) { 211 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 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 255 private String normalizeUri(String uri) { 256 String 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 key, final String 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 getLocalPart(String uri) { 298 String result = uri; 299 int index = uri.lastIndexOf(':'); 300 if(index != -1) { 301 result = uri.substring(index+1); 302 } 303 return result; 304 } 305 306 318 public static final class PortInfo implements PortInformation.PortInfo { 319 private String portName; 320 private String bindingName; 321 private String bindingType; 322 private String portTypeName; 323 324 public PortInfo() { 325 } 326 327 public String getPortType() { 328 return portTypeName; 329 } 330 331 void setPortType(String pt) { 332 portTypeName = pt; 333 } 334 335 public String getBinding() { 336 return bindingName; 337 } 338 339 void setBinding(String b) { 340 bindingName = b; 341 } 342 343 public String getBindingType() { 344 return bindingType; 345 } 346 347 void setBindingType(String bt) { 348 bindingType = bt; 349 } 350 351 public String getPort() { 352 return portName; 353 } 354 355 void setPort(String p) { 356 portName = p; 357 } 358 359 public String toString() { 360 return "(" + portName + ", " + bindingName + ", " + portTypeName + ")"; } 362 } 363 364 public static final class ServiceInfo implements PortInformation.ServiceInfo { 365 366 private String serviceName; 367 private List portList; 368 369 public ServiceInfo(String name, List ports) { 370 init(name, ports); 371 } 372 373 public ServiceInfo(String name) { 374 init(name, new ArrayList()); 375 } 376 377 private void init(String name, List ports) { 378 this.serviceName = name; 379 this.portList = ports; 380 } 381 382 public String getServiceName() { 383 return serviceName; 384 } 385 386 public List getPorts() { 387 return portList; 388 } 389 390 } 391 } 392 | Popular Tags |