| 1 55 56 package org.jboss.axis.wsdl.symbolTable; 57 58 import org.jboss.axis.Constants; 59 import org.jboss.axis.enums.Style; 60 import org.jboss.axis.enums.Use; 61 import org.jboss.axis.utils.LinkedHashMap; 62 import org.jboss.axis.utils.Messages; 63 import org.jboss.axis.utils.URLHashSet; 64 import org.jboss.axis.utils.XMLUtils; 65 import org.jboss.logging.Logger; 66 import org.w3c.dom.Document ; 67 import org.w3c.dom.NamedNodeMap ; 68 import org.w3c.dom.Node ; 69 import org.w3c.dom.NodeList ; 70 import org.xml.sax.SAXException ; 71 72 import javax.wsdl.Binding; 73 import javax.wsdl.BindingFault; 74 import javax.wsdl.BindingInput; 75 import javax.wsdl.BindingOperation; 76 import javax.wsdl.BindingOutput; 77 import javax.wsdl.Definition; 78 import javax.wsdl.Fault; 79 import javax.wsdl.Import; 80 import javax.wsdl.Input; 81 import javax.wsdl.Message; 82 import javax.wsdl.Operation; 83 import javax.wsdl.Output; 84 import javax.wsdl.Part; 85 import javax.wsdl.Port; 86 import javax.wsdl.PortType; 87 import javax.wsdl.Service; 88 import javax.wsdl.WSDLException; 89 import javax.wsdl.extensions.UnknownExtensibilityElement; 90 import javax.wsdl.extensions.http.HTTPBinding; 91 import javax.wsdl.extensions.mime.MIMEContent; 92 import javax.wsdl.extensions.mime.MIMEMultipartRelated; 93 import javax.wsdl.extensions.mime.MIMEPart; 94 import javax.wsdl.extensions.soap.SOAPBinding; 95 import javax.wsdl.extensions.soap.SOAPBody; 96 import javax.wsdl.extensions.soap.SOAPFault; 97 import javax.wsdl.extensions.soap.SOAPHeader; 98 import javax.wsdl.extensions.soap.SOAPHeaderFault; 99 import javax.wsdl.factory.WSDLFactory; 100 import javax.wsdl.xml.WSDLReader; 101 import javax.xml.namespace.QName ; 102 import javax.xml.parsers.ParserConfigurationException ; 103 import javax.xml.rpc.holders.BooleanHolder ; 104 import javax.xml.rpc.holders.IntHolder ; 105 import java.io.File ; 106 import java.io.IOException ; 107 import java.net.MalformedURLException ; 108 import java.net.URL ; 109 import java.util.ArrayList ; 110 import java.util.Collection ; 111 import java.util.Collections ; 112 import java.util.HashMap ; 113 import java.util.HashSet ; 114 import java.util.Iterator ; 115 import java.util.List ; 116 import java.util.Map ; 117 import java.util.Vector ; 118 119 130 public class SymbolTable 131 { 132 private static final Logger log = Logger.getLogger(SymbolTable.class); 133 134 private boolean addImports; 136 137 146 private HashMap symbolTable = new HashMap(); 147 148 private final Map elementTypeEntries = new HashMap(); 150 private final Map elementIndex = Collections.unmodifiableMap(elementTypeEntries); 152 private final Map typeTypeEntries = new HashMap(); 154 private final Map typeIndex = Collections.unmodifiableMap(typeTypeEntries); 156 157 162 protected final Map node2ExtensionBase = new HashMap(); 164 private boolean verbose; 165 166 private BaseTypeMapping btm = null; 167 168 private boolean nowrap; 170 private boolean wrapped = false; 172 173 public static final String ANON_TOKEN = ">"; 174 175 private Definition def = null; 176 private String wsdlURI = null; 177 178 181 public SymbolTable(BaseTypeMapping btm, boolean addImports, 182 boolean verbose, boolean nowrap) 183 { 184 this.btm = btm; 185 this.addImports = addImports; 186 this.verbose = verbose; 187 this.nowrap = nowrap; 188 } 190 193 public HashMap getHashMap() 194 { 195 return symbolTable; 196 } 198 202 public Vector getSymbols(QName qname) 203 { 204 return (Vector )symbolTable.get(qname); 205 } 207 210 public SymTabEntry get(QName qname, Class cls) 211 { 212 Vector v = (Vector )symbolTable.get(qname); 213 if (v == null) 214 { 215 return null; 216 } 217 else 218 { 219 for (int i = 0; i < v.size(); ++i) 220 { 221 SymTabEntry entry = (SymTabEntry)v.elementAt(i); 222 if (cls.isInstance(entry)) 223 { 224 return entry; 225 } 226 } 227 return null; 228 } 229 } 231 232 238 public TypeEntry getTypeEntry(QName qname, boolean wantElementType) 239 { 240 if (wantElementType) 241 { 242 return getElement(qname); 243 } 244 else 245 return getType(qname); 246 } 248 252 public Type getType(QName qname) 253 { 254 return (Type)typeTypeEntries.get(qname); 255 } 257 261 public Element getElement(QName qname) 262 { 263 return (Element)elementTypeEntries.get(qname); 264 } 266 269 public MessageEntry getMessageEntry(QName qname) 270 { 271 return (MessageEntry)get(qname, MessageEntry.class); 272 } 274 277 public PortTypeEntry getPortTypeEntry(QName qname) 278 { 279 return (PortTypeEntry)get(qname, PortTypeEntry.class); 280 } 282 285 public BindingEntry getBindingEntry(QName qname) 286 { 287 return (BindingEntry)get(qname, BindingEntry.class); 288 } 290 293 public ServiceEntry getServiceEntry(QName qname) 294 { 295 return (ServiceEntry)get(qname, ServiceEntry.class); 296 } 298 304 public Vector getTypes() 305 { 306 Vector v = new Vector (); 307 v.addAll(elementTypeEntries.values()); 308 v.addAll(typeTypeEntries.values()); 309 return v; 310 } 312 318 public Map getElementIndex() 319 { 320 return elementIndex; 321 } 322 323 329 public Map getTypeIndex() 330 { 331 return typeIndex; 332 } 333 334 339 public int getTypeEntryCount() 340 { 341 return elementTypeEntries.size() + typeTypeEntries.size(); 342 } 343 344 348 public Definition getDefinition() 349 { 350 return def; 351 } 353 357 public String getWSDLURI() 358 { 359 return wsdlURI; 360 } 362 365 public boolean isWrapped() 366 { 367 return wrapped; 368 } 369 370 373 public void setWrapped(boolean wrapped) 374 { 375 this.wrapped = wrapped; 376 } 377 378 381 public void dump(java.io.PrintStream out) 382 { 383 out.println(); 384 out.println(Messages.getMessage("symbolTable00")); 385 out.println("-----------------------"); 386 Iterator it = symbolTable.values().iterator(); 387 while (it.hasNext()) 388 { 389 Vector v = (Vector )it.next(); 390 for (int i = 0; i < v.size(); ++i) 391 { 392 out.println(v.elementAt(i).getClass().getName()); 393 out.println(v.elementAt(i)); 394 } 395 } 396 out.println("-----------------------"); 397 } 399 400 405 406 public void populate(String uri) 407 throws IOException , WSDLException, 408 SAXException , ParserConfigurationException  409 { 410 populate(uri, null, null); 411 } 413 public void populate(String uri, String username, String password) 414 throws IOException , WSDLException, 415 SAXException , ParserConfigurationException  416 { 417 if (verbose) 418 System.out.println(Messages.getMessage("parsing00", uri)); 419 420 Document doc = XMLUtils.newDocument(uri, username, password); 421 this.wsdlURI = uri; 422 try 423 { 424 File f = new File (uri); 425 if (f.exists()) 426 { 427 uri = f.toURL().toString(); 428 } 429 } 430 catch (Exception e) 431 { 432 } 433 populate(uri, doc); 434 } 436 442 public void populate(String context, Document doc) 443 throws IOException , SAXException , WSDLException, 444 ParserConfigurationException  445 { 446 WSDLReader reader = WSDLFactory.newInstance().newWSDLReader(); 447 reader.setFeature("javax.wsdl.verbose", verbose); 448 this.def = reader.readWSDL(context, doc); 449 450 add(context, def, doc); 451 } 453 459 protected void add(String context, Definition def, Document doc) 460 throws IOException , SAXException , WSDLException, 461 ParserConfigurationException  462 { 463 URL contextURL = context == null ? null : getURL(null, context); 464 populate(contextURL, def, doc, null); 465 checkForUndefined(); 466 populateParameters(); 467 setReferences(def, doc); } 470 473 private void checkForUndefined(Definition def, String filename) throws IOException  474 { 475 if (def != null) 476 { 477 Iterator ib = def.getBindings().values().iterator(); 479 while (ib.hasNext()) 480 { 481 Binding binding = (Binding)ib.next(); 482 if (binding.isUndefined()) 483 { 484 if (filename == null) 485 { 486 throw new IOException (Messages.getMessage("emitFailtUndefinedBinding01", 487 binding.getQName().getLocalPart())); 488 } 489 else 490 { 491 throw new IOException (Messages.getMessage("emitFailtUndefinedBinding02", 492 binding.getQName().getLocalPart(), filename)); 493 } 494 } 495 } 496 497 Iterator ip = def.getPortTypes().values().iterator(); 499 while (ip.hasNext()) 500 { 501 PortType portType = (PortType)ip.next(); 502 if (portType.isUndefined()) 503 { 504 if (filename == null) 505 { 506 throw new IOException (Messages.getMessage("emitFailtUndefinedPort01", 507 portType.getQName().getLocalPart())); 508 } 509 else 510 { 511 throw new IOException (Messages.getMessage("emitFailtUndefinedPort02", 512 portType.getQName().getLocalPart(), filename)); 513 } 514 } 515 } 516 517 533 } 534 } 535 536 539 private void checkForUndefined() throws IOException  540 { 541 Iterator it = symbolTable.values().iterator(); 542 while (it.hasNext()) 543 { 544 Vector v = (Vector )it.next(); 545 for (int i = 0; i < v.size(); ++i) 546 { 547 SymTabEntry entry = (SymTabEntry)v.get(i); 548 549 if (entry instanceof UndefinedType) 551 { 552 QName qn = entry.getQName(); 553 554 if ((qn.getLocalPart().equals("dateTime") && 557 !qn.getNamespaceURI().equals(Constants.URI_2001_SCHEMA_XSD)) || 558 (qn.getLocalPart().equals("timeInstant") && 559 qn.getNamespaceURI().equals(Constants.URI_2001_SCHEMA_XSD))) 560 { 561 throw new IOException (Messages.getMessage("wrongNamespace00", 562 qn.getLocalPart(), 563 qn.getNamespaceURI())); 564 } 565 566 if (SchemaUtils.isSimpleSchemaType(entry.getQName())) 569 { 570 throw new IOException (Messages.getMessage("unsupportedSchemaType00", 571 qn.getLocalPart())); 572 } 573 574 throw new IOException (Messages.getMessage("undefined00", 576 entry.getQName().toString())); 577 } else if (entry instanceof UndefinedElement) 579 { 580 throw new IOException (Messages.getMessage("undefinedElem00", 581 entry.getQName().toString())); 582 } 583 } 584 } 585 } 587 594 private URLHashSet importedFiles = new URLHashSet(); 595 596 private void populate(URL context, Definition def, Document doc, 597 String filename) 598 throws IOException , ParserConfigurationException , 599 SAXException , WSDLException 600 { 601 if (doc != null) 602 { 603 populateTypes(context, doc); 604 605 if (addImports) 606 { 607 lookForImports(context, doc); 609 } 610 } 611 if (def != null) 612 { 613 checkForUndefined(def, filename); 614 if (addImports) 615 { 616 Map imports = def.getImports(); 618 Object [] importKeys = imports.keySet().toArray(); 619 for (int i = 0; i < importKeys.length; ++i) 620 { 621 Vector v = (Vector )imports.get(importKeys[i]); 622 for (int j = 0; j < v.size(); ++j) 623 { 624 Import imp = (Import)v.get(j); 625 if (!importedFiles.contains(imp.getLocationURI())) 626 { 627 importedFiles.add(imp.getLocationURI()); 628 URL url = getURL(context, imp.getLocationURI()); 629 populate(url, imp.getDefinition(), 630 XMLUtils.newDocument(url.toString()), 631 url.toString()); 632 } 633 } 634 } 635 } 636 populateMessages(def); 637 populatePortTypes(def); 638 populateBindings(def); 639 populateServices(def); 640 } 641 } 643 647 private static URL getURL(URL contextURL, String spec) throws IOException  648 { 649 String path = spec.replace('\\', '/'); 653 654 URL url = null; 656 try 657 { 658 url = new URL (contextURL, path); 660 661 if (contextURL != null && 664 url.getProtocol().equals("file") && 665 contextURL.getProtocol().equals("file")) 666 { 667 url = getFileURL(contextURL, path); 668 } 669 } 670 catch (MalformedURLException me) 671 { 672 url = getFileURL(contextURL, path); 674 } 675 676 return url; 680 } 682 private static URL getFileURL(URL contextURL, String path) 683 throws IOException  684 { 685 if (contextURL != null) 686 { 687 String contextFileName = contextURL.getFile(); 690 URL parent = new File (contextFileName).getParentFile().toURL(); 691 if (parent != null) 692 { 693 return new URL (parent, path); 694 } 695 } 696 return new URL ("file", "", path); 697 } 699 702 private void lookForImports(URL context, Node node) 703 throws IOException , ParserConfigurationException , 704 SAXException , WSDLException 705 { 706 NodeList children = node.getChildNodes(); 707 for (int i = 0; i < children.getLength(); i++) 708 { 709 Node child = children.item(i); 710 if ("import".equals(child.getLocalName())) 711 { 712 NamedNodeMap attributes = child.getAttributes(); 713 Node namespace = attributes.getNamedItem("namespace"); 714 if (namespace != null && 716 isKnownNamespace(namespace.getNodeValue())) 717 { 718 continue; 719 } 720 Node importFile = attributes.getNamedItem("schemaLocation"); 721 if (importFile != null) 722 { 723 URL url = getURL(context, 724 importFile.getNodeValue()); 725 if (!importedFiles.contains(url)) 726 { 727 importedFiles.add(url); 728 String filename = url.toString(); 729 populate(url, null, 730 XMLUtils.newDocument(filename), filename); 731 } 732 } 733 } 734 lookForImports(context, child); 735 } 736 } 738 744 public boolean isKnownNamespace(String namespace) 745 { 746 if (Constants.isSOAP_ENC(namespace)) 747 return true; 748 if (Constants.isSchemaXSD(namespace)) 749 return true; 750 if (Constants.isSchemaXSI(namespace)) 751 return true; 752 if (namespace.equals(Constants.NS_URI_XML)) 753 return true; 754 return false; 755 } 756 757 760 public void populateTypes(URL context, Document doc) 761 throws IOException , SAXException , WSDLException, 762 ParserConfigurationException  763 { 764 addTypes(context, doc, ABOVE_SCHEMA_LEVEL); 765 } 767 779 private static final int ABOVE_SCHEMA_LEVEL = -1; 780 private static final int SCHEMA_LEVEL = 0; 781 782 private void addTypes(URL context, Node node, int level) 783 throws IOException , ParserConfigurationException , 784 WSDLException, SAXException  785 { 786 if (node == null) 787 { 788 return; 789 } 790 QName nodeKind = Utils.getNodeQName(node); 792 793 if (nodeKind != null) 794 { 795 String localPart = nodeKind.getLocalPart(); 796 boolean isXSD = Constants.isSchemaXSD(nodeKind.getNamespaceURI()); 797 if ((isXSD && localPart.equals("complexType") || 798 localPart.equals("simpleType"))) 799 { 800 801 Node re = SchemaUtils.getRestrictionOrExtensionNode(node); 804 if (re != null && 805 Utils.getAttribute(re, "base") != null) 806 { 807 createTypeFromRef(re); 808 } 809 810 createTypeFromDef(node, false, false); 813 } 814 else if (isXSD && localPart.equals("element")) 815 { 816 createTypeFromRef(node); 818 |