1 19 20 26 package org.netbeans.modules.xml.wsdl.ui.actions; 27 28 import java.util.Collection ; 29 import java.util.HashSet ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Set ; 33 34 import javax.xml.namespace.QName ; 35 36 import org.netbeans.api.project.FileOwnerQuery; 37 import org.netbeans.api.project.Project; 38 import org.netbeans.api.project.ProjectInformation; 39 import org.netbeans.api.project.ProjectUtils; 40 import org.netbeans.modules.xml.wsdl.model.Binding; 41 import org.netbeans.modules.xml.wsdl.model.BindingFault; 42 import org.netbeans.modules.xml.wsdl.model.BindingOperation; 43 import org.netbeans.modules.xml.wsdl.model.Definitions; 44 import org.netbeans.modules.xml.wsdl.model.ExtensibilityElement; 45 import org.netbeans.modules.xml.wsdl.model.Fault; 46 import org.netbeans.modules.xml.wsdl.model.Input; 47 import org.netbeans.modules.xml.wsdl.model.Message; 48 import org.netbeans.modules.xml.wsdl.model.Operation; 49 import org.netbeans.modules.xml.wsdl.model.Output; 50 import org.netbeans.modules.xml.wsdl.model.Part; 51 import org.netbeans.modules.xml.wsdl.model.Port; 52 import org.netbeans.modules.xml.wsdl.model.PortType; 53 import org.netbeans.modules.xml.wsdl.model.Service; 54 import org.netbeans.modules.xml.wsdl.model.WSDLComponent; 55 import org.netbeans.modules.xml.wsdl.model.WSDLModel; 56 import org.netbeans.modules.xml.wsdl.ui.netbeans.module.Utility; 57 import org.netbeans.modules.xml.wsdl.ui.netbeans.module.WSDLDataObject; 58 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent; 59 import org.openide.util.NbBundle; 60 import org.w3c.dom.Attr ; 61 import org.w3c.dom.NamedNodeMap ; 62 63 64 70 public class NameGenerator { 71 72 private static NameGenerator mInstance; 73 74 private static final String MESSAGE_NAME_PREFIX = NbBundle.getMessage(NameGenerator.class, "NameGenerator_MESSAGE_PREFIX") ; 75 76 private static final String MESSAGE_PART_NAME_PREFIX = NbBundle.getMessage(NameGenerator.class, "NameGenerator_PART_PREFIX") ; 77 78 private static final String PORTTYPE_NAME_PREFIX = NbBundle.getMessage(NameGenerator.class, "NameGenerator_PORTTYPE_PREFIX") ; 79 80 private static final String OPERATION_NAME_PREFIX = NbBundle.getMessage(NameGenerator.class, "NameGenerator_OPERATION_PREFIX") ; 81 82 private static final String OPERATION_INPUT_NAME_PREFIX = NbBundle.getMessage(NameGenerator.class, "NameGenerator_INPUT") ; 83 84 private static final String OPERATION_OUTPUT_NAME_PREFIX = NbBundle.getMessage(NameGenerator.class, "NameGenerator_OUTPUT") ; 85 86 private static final String OPERATION_FAULT_NAME_PREFIX = NbBundle.getMessage(NameGenerator.class, "NameGenerator_FAULT_PREFIX") ; 87 88 private static final String BINDING_NAME_PREFIX = NbBundle.getMessage(NameGenerator.class, "NameGenerator_BINDING_PREFIX") ; 89 90 private static final String BINDING_OPERATION_FAULT_NAME_PREFIX = NbBundle.getMessage(NameGenerator.class, "NameGenerator_BINDING_OPERATION_FAULT_PREFIX") ; 91 92 private static final String SERVICE_NAME_PREFIX = NbBundle.getMessage(NameGenerator.class, "NameGenerator_SERVICE_PREFIX") ; 93 94 private static final String PORT_NAME_PREFIX = NbBundle.getMessage(NameGenerator.class, "NameGenerator_PORT_PREFIX") ; 95 96 private static int counterStart = 1; 97 98 private NameGenerator() { 99 100 } 101 102 public static NameGenerator getInstance() { 103 if(mInstance == null) { 104 mInstance = new NameGenerator(); 105 } 106 107 return mInstance; 108 } 109 110 public String generateUniqueMessageName(WSDLModel document) { 111 int counter = counterStart; 112 String messageName = MESSAGE_NAME_PREFIX + counter; 113 while(isMessageExists(messageName, document)) { 114 counter++; 115 messageName = MESSAGE_NAME_PREFIX + counter; 116 } 117 118 return messageName; 119 } 120 121 public String generateUniqueInputMessageName(String operationName, WSDLModel document) { 122 int counter = counterStart; 123 String messageName = operationName + "Request"; 124 while(isMessageExists(messageName, document)) { 125 messageName = messageName + counter; 126 counter++; 127 } 128 129 return messageName; 130 } 131 132 public String generateUniqueOutputMessageName(String operationName, WSDLModel document) { 133 int counter = counterStart; 134 String messageName = operationName + "Reply"; 135 while(isMessageExists(messageName, document)) { 136 messageName = messageName + counter; 137 counter++; 138 } 139 140 return messageName; 141 } 142 143 public String generateUniqueFaultMessageName(String operationName, WSDLModel document) { 144 int counter = counterStart; 145 String messageName = operationName + "Fault"; 146 while(isMessageExists(messageName, document)) { 147 messageName = messageName + counter; 148 counter++; 149 } 150 151 return messageName; 152 } 153 154 public boolean isMessageExists(String name, WSDLModel document) { 155 boolean exists = false; 156 Collection messages = document.getDefinitions().getMessages(); 157 158 Iterator it = messages.iterator(); 159 while(it.hasNext()) { 160 Message message = (Message) it.next(); 161 if(name.equals(message.getName())) { 162 exists = true; 163 break; 164 } 165 } 166 167 return exists; 168 } 169 170 public String generateUniqueMessagePartName(Message message) { 171 int counter = counterStart; 172 String messagePartName = MESSAGE_PART_NAME_PREFIX + counter; 173 174 while(isMessagePartExists(messagePartName, message)) { 175 counter++; 176 messagePartName = MESSAGE_PART_NAME_PREFIX + counter; 177 } 178 179 return messagePartName; 180 } 181 182 public boolean isMessagePartExists(String name, Message message) { 183 boolean exists = false; 184 Collection parts = message.getParts(); 185 186 Iterator it = parts.iterator(); 187 while(it.hasNext()) { 188 Part part = (Part) it.next(); 189 if(name.equals(part.getName())) { 190 exists = true; 191 break; 192 } 193 } 194 195 return exists; 196 } 197 198 public String generateUniquePortTypeName(WSDLModel document) { 199 int counter = counterStart; 200 String messageName = PORTTYPE_NAME_PREFIX + counter; 201 202 while(isPortTypeExists(messageName, document)) { 203 counter++; 204 messageName = PORTTYPE_NAME_PREFIX + counter; 205 } 206 207 return messageName; 208 } 209 210 public String generateUniquePortTypeName(WSDLModel document, String portTypePrefix) { 211 int counter = counterStart; 212 String portTypeName = portTypePrefix; 213 214 while(isPortTypeExists(portTypeName, document)) { 215 portTypeName = portTypePrefix + counter; 216 counter++; 217 } 218 219 return portTypeName; 220 } 221 222 public boolean isPortTypeExists(String name, WSDLModel document) { 223 boolean exists = false; 224 Collection portTypes = document.getDefinitions().getPortTypes(); 225 226 Iterator it = portTypes.iterator(); 227 while(it.hasNext()) { 228 PortType portType = (PortType) it.next(); 229 if(name.equals(portType.getName())) { 230 exists = true; 231 break; 232 } 233 } 234 235 return exists; 236 } 237 238 public String generateUniqueOperationName(PortType portType) { 239 int counter = counterStart; 240 String messageName = OPERATION_NAME_PREFIX + counter; 241 while(isOperationExists(messageName, portType)) { 242 counter++; 243 messageName = OPERATION_NAME_PREFIX + counter; 244 } 245 246 return messageName; 247 } 248 249 public boolean isOperationExists(String name, PortType portType) { 250 boolean exists = false; 251 Collection operations = portType.getOperations(); 252 253 Iterator it = operations.iterator(); 254 while(it.hasNext()) { 255 Operation operation = (Operation) it.next(); 256 if(name.equals(operation.getName())) { 257 exists = true; 258 break; 259 } 260 } 261 262 return exists; 263 } 264 265 public String generateUniqueOperationInputName(Operation operation) { 266 Set inputNames = getAllInputs(operation); 267 int counter = counterStart; 268 String operationInputName = OPERATION_INPUT_NAME_PREFIX + counter; 269 while(inputNames.contains(operationInputName)) { 270 counter++; 271 operationInputName = OPERATION_INPUT_NAME_PREFIX + counter; 272 } 273 return operationInputName; 274 } 275 276 public Set <String > getAllInputs(Operation operation) { 277 Set <String > hashSet = new HashSet <String >(); 278 PortType portType = (PortType) operation.getParent(); 279 Collection operations = portType.getOperations(); 280 if (operations != null) { 281 Iterator operIter = operations.iterator(); 282 while (operIter.hasNext()) { 283 Operation oper = (Operation) operIter.next(); 284 if (oper.getInput() != null) { 285 hashSet.add(oper.getInput().getName()); 286 } 287 } 288 } 289 return hashSet; 290 } 291 292 public boolean isOperationInputExists(Operation operation) { 293 boolean exists = false; 294 295 296 Input operationInput = operation.getInput(); 297 298 if(operationInput != null) { 299 exists = true; 300 } 301 return exists; 302 } 303 304 public String generateUniqueOperationOutputName(Operation operation) { 305 Set outputNames = getAllOutputs(operation); 306 int counter = counterStart; 307 String operationOutputName = OPERATION_OUTPUT_NAME_PREFIX + counter; 308 while(outputNames.contains(operationOutputName)) { 309 counter++; 310 operationOutputName = OPERATION_OUTPUT_NAME_PREFIX + counter; 311 } 312 return operationOutputName; 313 } 314 315 public Set <String > getAllOutputs(Operation operation) { 316 Set <String > hashSet = new HashSet <String >(); 317 PortType portType = (PortType) operation.getParent(); 318 Collection operations = portType.getOperations(); 319 if (operations != null) { 320 Iterator operIter = operations.iterator(); 321 while (operIter.hasNext()) { 322 Operation oper = (Operation) operIter.next(); 323 if (oper.getOutput() != null) { 324 hashSet.add(oper.getOutput().getName()); 325 } 326 } 327 } 328 return hashSet; 329 } 330 331 public boolean isOperationOutputExists(Operation operation) { 332 boolean exists = false; 333 Output operationOutput = operation.getOutput(); 334 335 if(operationOutput != null) { 336 exists = true; 337 } 338 return exists; 339 } 340 341 public String generateUniqueOperationFaultName(Operation operation) { 342 int counter = counterStart; 343 String messageName = OPERATION_FAULT_NAME_PREFIX + counter; 344 while(isOperationFaultExists(messageName, operation)) { 345 counter++; 346 messageName = OPERATION_FAULT_NAME_PREFIX + counter; 347 } 348 349 return messageName; 350 } 351 352 private boolean isOperationFaultExists(String name, Operation operation) { 353 boolean exists = false; 354 Collection faults = operation.getFaults(); 355 356 Iterator it = faults.iterator(); 357 while(it.hasNext()) { 358 Fault fault = (Fault) it.next(); 359 if(name.equals(fault.getName())) { 360 exists = true; 361 break; 362 } 363 } 364 365 return exists; 366 } 367 368 public String generateUniqueBindingName(WSDLModel document) { 369 int counter = counterStart; 370 String messageName = BINDING_NAME_PREFIX + counter; 371 while(isBindingExists(messageName, document)) { 372 counter++; 373 messageName = BINDING_NAME_PREFIX + counter; 374 } 375 376 return messageName; 377 } 378 379 public String generateUniqueBindingName(WSDLModel document, String appendage) { 380 int counter = counterStart; 381 String prefix = BINDING_NAME_PREFIX + "_" + appendage; 382 String messageName = prefix + counter; 383 while(isBindingExists(messageName, document)) { 384 counter++; 385 messageName = BINDING_NAME_PREFIX + counter; 386 } 387 388 return messageName; 389 } 390 391 public String generateUniqueBindingName(String prefix, WSDLModel document) { 392 if (prefix == null) { 393 return generateUniqueBindingName(document); 394 } 395 int counter = counterStart; 396 String messageName = prefix + counter; 397 while(isBindingExists(messageName, document)) { 398 counter++; 399 messageName = BINDING_NAME_PREFIX + counter; 400 } 401 402 return messageName; 403 } 404 405 public boolean isBindingExists(String name, WSDLModel document) { 406 boolean exists = false; 407 Collection bindings = document.getDefinitions().getBindings(); 408 409 Iterator it = bindings.iterator(); 410 while(it.hasNext()) { 411 Binding binding = (Binding) it.next(); 412 if(name.equals(binding.getName())) { 413 exists = true; 414 break; 415 } 416 } 417 418 return exists; 419 } 420 421 422 public String generateUniqueBindingOperationFaultName(BindingOperation bo) { 423 int counter = counterStart; 424 String faultName = BINDING_OPERATION_FAULT_NAME_PREFIX + counter; 425 while(isBindingOperationFaultExists(faultName, bo)) { 426 counter++; 427 faultName = BINDING_OPERATION_FAULT_NAME_PREFIX + counter; 428 } 429 430 return faultName; 431 } 432 433 public boolean isBindingOperationFaultExists(String name, BindingOperation bo) { 434 boolean exists = false; 435 Collection faults = bo.getBindingFaults(); 436 437 Iterator it = faults.iterator(); 438 while(it.hasNext()) { 439 BindingFault bindingFault = (BindingFault) it.next(); 440 if(name.equals(bindingFault.getName())) { 441 exists = true; 442 break; 443 } 444 } 445 446 return exists; 447 } 448 449 public String generateUniqueServiceName(WSDLModel document) { 450 int counter = counterStart; 451 String messageName = SERVICE_NAME_PREFIX + counter; 452 while(isServiceExists(messageName, document)) { 453 counter++; 454 messageName = SERVICE_NAME_PREFIX + counter; 455 } 456 457 return messageName; 458 } 459 460 public boolean isServiceExists(String name, WSDLModel document) { 461 boolean exists = false; 462 Collection services = document.getDefinitions().getServices(); 463 464 Iterator it = services.iterator(); 465 while(it.hasNext()) { 466 Service service = (Service) it.next(); 467 if(name.equals(service.getName())) { 468 exists = true; 469 break; 470 } 471 } 472 473 return exists; 474 } 475 476 public String generateUniqueServicePortName(Service service) { 477 int counter = counterStart; 478 String messageName = PORT_NAME_PREFIX + counter; 479 while(isServicePortExists(messageName, service)) { 480 counter++; 481 messageName = PORT_NAME_PREFIX + counter; 482 } 483 484 return messageName; 485 } 486 487 public String generateUniqueServicePortName(String prefix, Service service) { 488 int counter = counterStart; 489 String messageName = prefix + counter; 490 while(isServicePortExists(messageName, service)) { 491 counter++; 492 messageName = prefix + counter; 493 } 494 495 return messageName; 496 } 497 498 public boolean isServicePortExists(String name, Service service) { 499 boolean exists = false; 500 Collection ports = service.getPorts(); 501 502 Iterator it = ports.iterator(); 503 while(it.hasNext()) { 504 Port port = (Port) it.next(); 505 if(name.equals(port.getName())) { 506 exists = true; 507 break; 508 } 509 } 510 511 return exists; 512 } 513 514 public String generateNamespacePrefix(String optionalPrefixNameString, WSDLComponent element) { 515 return generateNamespacePrefix(optionalPrefixNameString, element.getModel()); 516 } 517 518 528 public String generateNamespacePrefix(String prefix, WSDLModel model) { 529 return generateNamespacePrefix(prefix, model, 0); 530 } 531 532 542 public String generateNamespacePrefix(String prefix, WSDLModel model, 543 int counter) { 544 if (prefix == null) { 545 prefix = NbBundle.getMessage(NameGenerator.class, 546 "NameGenerator_DEFAULT_PREFIX"); 547 } 548 String generated = prefix + counter; 549 while (isPrefixExist(generated, model)) { 550 counter++; 551 generated = prefix + counter; 552 } 553 return generated; 554 } 555 556 public boolean isPrefixExist(String prefix, WSDLModel model) { 557 return Utility.getNamespaceURI(prefix, model) != null ? true : false; 558 } 559 560 public boolean isAttributeExists(String attrQName, WSDLComponent element) { 561 if(attrQName == null) { 562 return false; 563 } 564 565 boolean result = false; 566 567 568 NamedNodeMap attrs = ((AbstractDocumentComponent) element).getPeer().getAttributes(); 570 572 if(attrs != null) { 573 for (int i = 0; i < attrs.getLength();i++) { 574 Attr attr = (Attr ) attrs.item(i); 575 if(attrQName.equals(attr.getName())) { return true; 577 } 578 579 } 580 } 581 582 589 590 591 return result; 592 593 } 594 595 public static String createNewTargetNamespace (WSDLModel model, WSDLDataObject dObj) { 596 Definitions definitions = model.getDefinitions(); 597 598 String targetNamespaceStr = null; 599 600 StringBuffer targetNamespace = new StringBuffer ("http://localhost/"); 601 602 Project project = FileOwnerQuery.getOwner(dObj.getPrimaryFile()); 603 if(project != null) { 604 ProjectInformation pi = ProjectUtils.getInformation(project); 605 if(pi != null) { 606 targetNamespace.append(pi.getDisplayName()); 607 targetNamespace.append("/"); 608 } 609 } 610 611 targetNamespace.append(dObj.getName()); 612 targetNamespaceStr = targetNamespace.toString(); 613 614 615 return targetNamespaceStr; 616 } 617 618 619 public static String generateUniqueValueForKeyAttribute(WSDLComponent component, String attributeName, QName qname, String prefix) { 620 WSDLComponent parent = component.getParent(); 621 HashSet <String > set = new HashSet <String >(); 622 List <ExtensibilityElement> list = parent.getExtensibilityElements(); 623 if (list != null) { 624 for (ExtensibilityElement element : list) { 625 if (element.getQName().equals(qname) && element != component) { 626 if (element.getAttribute(attributeName) != null) { 627 set.add(element.getAttribute(attributeName)); 628 } 629 } 630 } 631 } 632 633 int counter = counterStart; 634 String messageName = prefix + counter; 635 while(set.contains(messageName)) { 636 counter++; 637 messageName = prefix + counter; 638 } 639 640 return messageName; 641 } 642 643 public static String generateUniquePartnerLinkType(String partnerLinkTypeNamePrefix, QName partnerLinkTypeElementQName, WSDLModel document) { 644 int counter = counterStart; 645 String partnerLinkTypeName = null; 646 if (partnerLinkTypeNamePrefix == null) 647 partnerLinkTypeNamePrefix = "new"; 648 649 String partnerLinkType = partnerLinkTypeNamePrefix + counter; 650 partnerLinkTypeName = partnerLinkType; 651 while(isParterLinkTypeExists(partnerLinkTypeName, partnerLinkTypeElementQName, document)) { 652 counter++; 653 partnerLinkTypeName = partnerLinkType + counter; 654 } 655 656 return partnerLinkTypeName; 657 } 658 659 private static boolean isParterLinkTypeExists(String partnerLinkTypeName, QName partnerLinkTypeElementQName, WSDLModel document) { 660 return findExtensibilityElementByQName(partnerLinkTypeName, partnerLinkTypeElementQName, document.getDefinitions()) != null; 661 } 662 663 private static WSDLComponent findExtensibilityElementByQName(String name, QName elementQName, WSDLComponent component) { 664 if(name == null || elementQName == null || elementQName.getNamespaceURI() == null || elementQName.getLocalPart() == null || component == null) { 665 return null; 666 } 667 668 List <ExtensibilityElement> elements = component.getExtensibilityElements(); 669 Iterator <ExtensibilityElement> it = elements.iterator(); 670 671 while(it.hasNext()) { 672 ExtensibilityElement ee = it.next(); 673 QName eeQName = ee.getQName(); 674 String eeNamespace = eeQName.getNamespaceURI(); 675 String eeLocalPart = eeQName.getLocalPart(); 676 677 if(elementQName.getNamespaceURI().equals(eeNamespace) && elementQName.getLocalPart().equals(eeLocalPart)) { 678 String eeName = ee.getAttribute("name"); 679 if(eeName != null && eeName.equals(name)) { 680 return ee; 681 } 682 } 683 } 684 685 return null; 686 } 687 } 688 | Popular Tags |