1 package org.objectweb.celtix.tools.processors.wsdl2.validators; 2 3 import java.util.ArrayList ; 4 import java.util.HashMap ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 import java.util.Map ; 8 9 import javax.wsdl.Binding; 10 import javax.wsdl.BindingOperation; 11 import javax.wsdl.Definition; 12 import javax.wsdl.Fault; 13 import javax.wsdl.Input; 14 import javax.wsdl.Message; 15 import javax.wsdl.Operation; 16 import javax.wsdl.Output; 17 import javax.wsdl.Part; 18 import javax.wsdl.Port; 19 import javax.wsdl.PortType; 20 import javax.wsdl.Service; 21 import javax.xml.namespace.QName ; 22 23 import org.w3c.dom.Document ; 24 import org.w3c.dom.Node ; 25 import org.w3c.dom.NodeList ; 26 27 import com.sun.org.apache.xerces.internal.xs.XSModel; 28 29 import org.objectweb.celtix.tools.common.WSDLConstants; 30 import org.objectweb.celtix.tools.utils.ErrNodeLocator; 31 32 public class WSDLElementReferenceValidator { 33 private java.util.List <XSModel> schemas; 34 private Definition def; 35 private Map <QName , List > msgPartsMap; 36 private SchemaWSDLValidator schemaWSDLValidator; 37 private Map <QName , QName > bindingMap; 38 private Map <QName , Map > portTypes; 39 private Document document; 40 private boolean isValid = true; 41 42 public WSDLElementReferenceValidator(Definition definition, SchemaWSDLValidator wsdlValidator) { 43 def = definition; 44 schemaWSDLValidator = wsdlValidator; 45 schemas = wsdlValidator.getXSModelList(); 46 msgPartsMap = wsdlValidator.getMsgPartsMap(); 47 portTypes = wsdlValidator.getPortTypesMap(); 48 bindingMap = wsdlValidator.getBindingMap(); 49 document = wsdlValidator.getSchemaValidatedDoc(); 50 51 } 52 53 public boolean isValid() { 54 this.validateMessages(); 55 this.validatePortType(); 56 this.vlidateBinding(); 57 this.validateService(); 58 return isValid; 59 60 } 61 62 private boolean validateMessages() { 63 64 String tns = def.getTargetNamespace(); 65 66 Map messageMap = def.getMessages(); 67 68 Document doc = schemaWSDLValidator.getSchemaValidatedDoc(); 69 70 NodeList nodeList = doc.getElementsByTagNameNS(WSDLConstants.QNAME_MESSAGE.getNamespaceURI(), 71 WSDLConstants.QNAME_MESSAGE.getLocalPart()); 72 73 for (int i = 0; i < nodeList.getLength(); i++) { 74 Node node = nodeList.item(i); 75 Node attNode = node.getAttributes().getNamedItem(WSDLConstants.ATTR_NAME); 76 QName qname = new QName (tns, attNode.getNodeValue()); 77 78 List <String > partsList = new ArrayList <String >(); 79 80 msgPartsMap.put(qname, partsList); 81 82 Message msg = (Message)messageMap.get(qname); 83 84 Map partsMap = msg.getParts(); 85 86 Iterator ite2 = partsMap.values().iterator(); 87 while (ite2.hasNext()) { 88 Part part = (Part)ite2.next(); 89 90 QName elementName = part.getElementName(); 91 QName typeName = part.getTypeName(); 92 if (elementName == null && typeName == null) { 93 Node errNode = ErrNodeLocator.getNode(doc, WSDLConstants.QNAME_MESSAGE, msg.getQName() 94 .getLocalPart(), part.getName()); 95 schemaWSDLValidator 96 .addError(errNode, 97 "The part does not have a type defined. Every part must " 98 + "specify a type from some type system. The type can " 99 + "be specified using the built in 'element' or 'type' attributes " 100 + "or may be specified using an extension attribute."); 101 102 isValid = false; 103 104 } 105 106 if (elementName != null && typeName != null) { 107 Node errNode = ErrNodeLocator.getNode(doc, WSDLConstants.QNAME_MESSAGE, msg.getQName() 108 .getLocalPart(), part.getName()); 109 schemaWSDLValidator.addError(errNode, 110 "The part has both an element and a type defined. Every " 111 + "part must only have an element or a type defined."); 112 isValid = false; 113 114 } 115 116 if (elementName != null && typeName == null) { 117 boolean valid = vlidatePartType(elementName.getNamespaceURI(), 118 elementName.getLocalPart(), true); 119 if (!valid) { 120 Node errNode = ErrNodeLocator.getNode(doc, WSDLConstants.QNAME_MESSAGE, msg 121 .getQName().getLocalPart(), part.getName()); 122 schemaWSDLValidator.addError(errNode, elementName + " refefrence can not find"); 123 124 isValid = false; 125 } 126 127 } 128 if (typeName != null && elementName == null) { 129 130 boolean valid = vlidatePartType(typeName.getNamespaceURI(), typeName.getLocalPart(), 131 false); 132 133 if (!valid) { 134 Node errNode = ErrNodeLocator.getNode(doc, WSDLConstants.QNAME_MESSAGE, msg 135 .getQName().getLocalPart(), part.getName()); 136 schemaWSDLValidator.addError(errNode, "reference can not find"); 137 isValid = false; 138 } 139 140 } 141 142 partsList.add(part.getName()); 143 144 } 145 146 } 147 return isValid; 148 149 } 150 151 private boolean vlidatePartType(String namespace, String name, boolean isElement) { 152 153 boolean partvalid = false; 154 155 if (namespace.equals(WSDLConstants.NS_XMLNS)) { 156 SchemaSymbolTable table = new SchemaSymbolTable(); 157 if (table.containsSymbol(name)) { 158 partvalid = true; 159 } 160 } else { 161 Iterator ite = schemas.iterator(); 162 while (ite.hasNext()) { 163 XSModel schema = (XSModel)ite.next(); 164 165 if (schema != null && isElement && schema.getElementDeclaration(name, namespace) != null) { 166 partvalid = true; 167 break; 168 169 } 170 if (schema != null && !isElement && schema.getTypeDefinition(name, namespace) != null) { 171 partvalid = true; 172 break; 173 } 174 } 175 } 176 return partvalid; 177 } 178 179 @SuppressWarnings ("unchecked") 180 private boolean vlidateBinding() { 181 182 NodeList nodelist = document.getElementsByTagNameNS(WSDLConstants.QNAME_BINDING.getNamespaceURI(), 183 WSDLConstants.QNAME_BINDING.getLocalPart()); 184 185 for (int i = 0; i < nodelist.getLength(); i++) { 186 Node node = nodelist.item(i); 187 Node n = node.getAttributes().getNamedItem(WSDLConstants.ATTR_NAME); 188 QName bindingName = new QName (def.getTargetNamespace(), n.getNodeValue()); 189 190 Binding binding = def.getBinding(bindingName); 191 192 QName typeName = binding.getPortType().getQName(); 193 194 if (!portTypes.containsKey(typeName)) { 195 Node errorNode = ErrNodeLocator.getNode(document, WSDLConstants.QNAME_DEFINITIONS, null, 196 bindingName.getLocalPart()); 197 schemaWSDLValidator.addError(errorNode, typeName + " is not defined"); 198 isValid = false; 199 } else { 200 201 Map <QName , Operation> operationMap = portTypes.get(typeName); 202 203 List <QName > operationList = new ArrayList <QName >(); 205 operationList.addAll(operationMap.keySet()); 206 207 Iterator ite = binding.getBindingOperations().iterator(); 209 while (ite.hasNext()) { 210 BindingOperation bop = (BindingOperation)ite.next(); 211 QName bopName = new QName (def.getTargetNamespace(), bop.getName()); 212 213 if (!operationList.contains(bopName)) { 214 215 Node errNode = ErrNodeLocator.getNode(document, WSDLConstants.QNAME_BINDING, 216 bindingName.getLocalPart(), bop.getName()); 217 schemaWSDLValidator.addError(errNode, "BindingOperation " + bop.getName() 218 + " is not defined"); 219 220 isValid = false; 221 222 } else { 223 Operation op = operationMap.get(bopName); 224 225 if (op.getInput() == null && bop.getBindingInput() != null) { 226 Node errNode = ErrNodeLocator.getNode(document, WSDLConstants.QNAME_BINDING, 227 bindingName.getLocalPart(), bop.getName()); 228 schemaWSDLValidator.addError(errNode, "BindingOperation " + bop.getName() 229 + " binding input is not defined"); 230 isValid = false; 231 } 232 233 if (op.getInput() != null && bop.getBindingInput() == null) { 234 Node errNode = ErrNodeLocator.getNode(document, WSDLConstants.QNAME_BINDING, 235 bindingName.getLocalPart(), bop.getName()); 236 schemaWSDLValidator.addError(errNode, "BindingOperation " + bop.getName() 237 + " binding input is not resolved"); 238 239 isValid = false; 240 } 241 242 if (op.getOutput() == null && bop.getBindingOutput() != null) { 243 Node errNode = ErrNodeLocator.getNode(document, WSDLConstants.QNAME_BINDING, 244 bindingName.getLocalPart(), bop.getName()); 245 schemaWSDLValidator.addError(errNode, "BindingOperation " + bop.getName() 246 + " binding output is not defined"); 247 isValid = false; 248 } 249 250 if (op.getOutput() != null && bop.getBindingOutput() == null) { 251 Node errNode = ErrNodeLocator.getNode(document, WSDLConstants.QNAME_BINDING, 252 bindingName.getLocalPart(), bop.getName()); 253 schemaWSDLValidator.addError(errNode, "BindingOperation " + bop.getName() 254 + " binding output is not resolved"); 255 256 isValid = false; 257 } 258 259 if (op.getFaults().size() != bop.getBindingFaults().size()) { 260 Node errNode = ErrNodeLocator.getNode(document, WSDLConstants.QNAME_BINDING, 261 bindingName.getLocalPart(), bop.getName()); 262 schemaWSDLValidator.addError(errNode, "BindingOperation " + bop.getName() 263 + " binding fault resolved error"); 264 isValid = false; 265 } 266 267 } 268 269 } 270 } 271 272 bindingMap.put(bindingName, typeName); 273 } 274 275 return isValid; 276 } 277 278 private boolean validateService() { 279 280 Map serviceMap = def.getServices(); 281 Iterator ite = serviceMap.values().iterator(); 282 while (ite.hasNext()) { 283 Service service = (Service)ite.next(); 284 Iterator portIte = service.getPorts().values().iterator(); 285 while (portIte.hasNext()) { 286 Port port = (Port)portIte.next(); 287 Binding binding = port.getBinding(); 288 if (!bindingMap.containsKey(binding.getQName())) { 289 Node errNode = ErrNodeLocator.getNode(document, WSDLConstants.QNAME_SERVICE, service 290 .getQName().getLocalPart(), port.getName()); 291 schemaWSDLValidator.addError(errNode, " port : " + port.getName() 292 + " reference binding is not defined"); 293 isValid = false; 294 } 295 } 296 } 297 298 return isValid; 299 } 300 301 private boolean validatePortType() { 302 303 String tns = def.getTargetNamespace(); 304 NodeList nodeList = document.getElementsByTagNameNS(WSDLConstants.QNAME_PORT_TYPE.getNamespaceURI(), 305 WSDLConstants.QNAME_PORT_TYPE.getLocalPart()); 306 for (int i = 0; i < nodeList.getLength(); i++) { 307 Node node = nodeList.item(i); 308 Node attNode = node.getAttributes().getNamedItem(WSDLConstants.ATTR_NAME); 309 QName qname = new QName (tns, attNode.getNodeValue()); 310 311 java.util.Map portTypeMap = def.getPortTypes(); 312 PortType portType = (PortType)portTypeMap.get(qname); 313 Map <QName , Operation> operationMap = new HashMap <QName , Operation>(); 314 315 portTypes.put(qname, operationMap); 316 317 for (Node n = node.getFirstChild(); n != null; n = n.getNextSibling()) { 319 if (n.getNodeType() == Node.ELEMENT_NODE 320 && n.getLocalName().equals(WSDLConstants.QNAME_OPERATION.getLocalPart())) { 321 Node opNameNode = n.getAttributes().getNamedItem(WSDLConstants.ATTR_NAME); 322 String opName = opNameNode.getNodeValue(); 323 List operations = portType.getOperations(); 324 Iterator ite2 = operations.iterator(); 325 while (ite2.hasNext()) { 326 Operation operation = (Operation)ite2.next(); 327 if (operation.getName().equals(opName)) { 328 329 operationMap.put(new QName (tns, opName), operation); 330 Input input = operation.getInput(); 331 if (input != null && input.getMessage() != null 332 && !msgPartsMap.containsKey(input.getMessage().getQName())) { 333 Node errNode = ErrNodeLocator.getNode(document, 334 WSDLConstants.QNAME_OPERATION, 335 operation.getName(), input.getName()); 336 schemaWSDLValidator.addError(errNode, " input : " + input.getName() 337 + " reference is not defined"); 338 isValid = false; 339 } 340 341 Output output = operation.getOutput(); 342 if (output != null && output.getMessage() != null 343 && !msgPartsMap.containsKey(output.getMessage().getQName())) { 344 Node errNode = ErrNodeLocator.getNode(document, 345 WSDLConstants.QNAME_OPERATION, 346 operation.getName(), output.getName()); 347 schemaWSDLValidator.addError(errNode, " output : " + output.getName() 348 + " reference is not defined"); 349 isValid = false; 350 } 351 352 Map faultMap = operation.getFaults(); 353 Iterator faultIte = faultMap.values().iterator(); 354 while (faultIte.hasNext()) { 355 Fault fault = (Fault)faultIte.next(); 356 if (fault != null && fault.getMessage() != null 357 && !msgPartsMap.containsKey(fault.getMessage().getQName())) { 358 Node errNode = getErrorNode(document, WSDLConstants.QNAME_OPERATION, 359 operation.getName(), fault.getName()); 360 addError(errNode, " fault : " + fault.getName() 361 + " reference is not defined"); 362 isValid = false; 363 } 364 365 } 366 367 } 368 } 369 } 370 } 371 } 372 373 return isValid; 374 } 375 376 private Node getErrorNode(Document doc, QName parentQN, String parentName, String childName) { 377 return getErrorNode(doc, parentQN, parentName, childName); 378 } 379 380 private void addError(Node node, String msg) { 381 schemaWSDLValidator.addError(node, msg); 382 383 } 384 385 } 386 | Popular Tags |