List<OMElement> portElements = TreeNodeBuilderUtil.evaluateXPathToElements(WSDLConstants.PORT_EXPR, serviceElement);
boolean portTypesFound = false;
for (OMElement portElement: portElements) {
String portName = portElement.getAttributeValue(new QName(WSDLConstants.PORT_NAME_ATTRIBUTE));
String portNamespace = TreeNodeBuilderUtil.evaluateXPathToValue(WSDLConstants.TARGET_NAMESPACE, portElement);
TreeNode portNode = new TreeNode(TreeNodeBuilderUtil.generateKeyName(WSDLConstants.SERVICE_PORT,
TreeNodeBuilderUtil.convertQNameToString(portName, portNamespace)));
serviceNode.addChild(portNode);
// finding the bindings for the port.
String bindingName = portElement.getAttributeValue(new QName(WSDLConstants.BINDING_ATTRIBUTE));
if (bindingName == null) {
continue;
}
String bindingNamespace = TreeNodeBuilderUtil.getNamespaceURI(bindingName, portElement);
String bindingLocalName = TreeNodeBuilderUtil.getLocalName(bindingName);
// so we cruising for the binding with that name.
String bindingExpression = "/wsdl:definitions/wsdl:binding[@name='" + bindingLocalName +
"' and ancestor::*[@targetNamespace='" + bindingNamespace + "']]";
// get the first bindingElement.
OMElement bindingElement = TreeNodeBuilderUtil.evaluateXPathToElement(bindingExpression, wsdlElement);
if (bindingElement != null) {
// until there is a bound binding elements, we doesn't mark the existence of port type
portTypesFound = true;
// now check for the soap binding
OMElement bindingProtocolElement = TreeNodeBuilderUtil.evaluateXPathToElement(WSDLConstants.SOAP_BINDING_EXPR, bindingElement);
if (bindingProtocolElement != null) {
// so it is soap 1.1
String endpoint = TreeNodeBuilderUtil.evaluateXPathToValue(WSDLConstants.SOAP_11_ENDPOINT_EXPR, portElement);
portNode.addChild(new TreeNode(WSDLConstants.SOAP_11_ENDPOINT, endpoint));
portNode.addChild(new TreeNode(WSDLConstants.SOAP_VERSION, WSDLConstants.SOAP_11));
} else {
bindingProtocolElement = TreeNodeBuilderUtil.evaluateXPathToElement(WSDLConstants.SOAP12_BINDING_EXPR, bindingElement);
if (bindingProtocolElement != null) {
// it is soap 1.2
String endpoint = TreeNodeBuilderUtil.evaluateXPathToValue(WSDLConstants.SOAP_12_ENDPOINT_EXPR, portElement);
portNode.addChild(new TreeNode(WSDLConstants.SOAP_12_ENDPOINT, endpoint));
portNode.addChild(new TreeNode(WSDLConstants.SOAP_VERSION, WSDLConstants.SOAP_12));
}
}
if (bindingProtocolElement != null) {
// We found this is SOAP (either 1.1. or 1.2), now we will get the transport and style info
String style = TreeNodeBuilderUtil.evaluateXPathToValue(WSDLConstants.BINDING_STYLE_EXPR, bindingProtocolElement);
portNode.addChild(new TreeNode(WSDLConstants.BINDING_STYLE, style));
String transport = bindingProtocolElement.getAttributeValue(new QName(WSDLConstants.BINDING_TRANSPORT_ATTRIBUTE));
if (transport.equals(WSDLConstants.HTTP_TRANSPORT_ATTRIBUTE_VALUE)) {
portNode.addChild(new TreeNode(WSDLConstants.BINDING_TRANSPORT,
WSDLConstants.HTTP_TRANSPORT_VALUE));
} else {
portNode.addChild(new TreeNode(WSDLConstants.BINDING_TRANSPORT,
WSDLConstants.NON_HTTP_TRANSPORT_VALUE));
}
}
if (bindingProtocolElement == null) {
// expecting this to be http
bindingProtocolElement = TreeNodeBuilderUtil.evaluateXPathToElement(WSDLConstants.HTTP_BINDING_EXPR, bindingElement);
if (bindingProtocolElement != null) {
String endpoint = TreeNodeBuilderUtil.evaluateXPathToValue(WSDLConstants.HTTP_ENDPOINT_EXPR, portElement);
portNode.addChild(new TreeNode(WSDLConstants.HTTP_ENDPOINT, endpoint));
portNode.addChild(new TreeNode(WSDLConstants.HTTP_BINDING, WSDLConstants.HTTP_BINDING_TRUE));
String verb = bindingProtocolElement.getAttributeValue(new QName(WSDLConstants.BINDING_VERB_ATTRIBUTE));
portNode.addChild(new TreeNode(WSDLConstants.BINDING_VERB, verb));
}
}
// Here we don't create a special node for bindingNode, so bindingNode = portNode
fillBinding(portNode, bindingElement);
} else {
// even if the binding element is not present, we probably can find the endpoint from the 'address'
// element
String endpoint = TreeNodeBuilderUtil.evaluateXPathToValue(WSDLConstants.SOAP_11_ENDPOINT_EXPR, portElement);
if (endpoint != null) {
portNode.addChild(new TreeNode(WSDLConstants.SOAP_11_ENDPOINT, endpoint));
portNode.addChild(new TreeNode(WSDLConstants.SOAP_VERSION, WSDLConstants.SOAP_11));
} else {
endpoint = TreeNodeBuilderUtil.evaluateXPathToValue(WSDLConstants.SOAP_12_ENDPOINT_EXPR, portElement);
if (endpoint != null) {
portNode.addChild(new TreeNode(WSDLConstants.SOAP_12_ENDPOINT, endpoint));
portNode.addChild(new TreeNode(WSDLConstants.SOAP_VERSION, WSDLConstants.SOAP_12));
}
}
if (endpoint == null) {
// then we can assume it is http
endpoint = TreeNodeBuilderUtil.evaluateXPathToValue(WSDLConstants.HTTP_ENDPOINT_EXPR, portElement);
if (endpoint != null) {
portNode.addChild(new TreeNode(WSDLConstants.HTTP_ENDPOINT, endpoint));
portNode.addChild(new TreeNode(WSDLConstants.HTTP_BINDING, WSDLConstants.HTTP_BINDING_TRUE));
}
}
}
portNode.addChild(new TreeNode(WSDLConstants.BINDING, TreeNodeBuilderUtil.convertQNameToString(bindingLocalName,
bindingNamespace)));
}
return portTypesFound;
}