1 57 58 package org.apache.wsif.util; 59 60 import java.io.IOException ; 61 import java.io.Reader ; 62 import java.io.StringReader ; 63 import java.io.Writer ; 64 import java.lang.reflect.Method ; 65 import java.net.MalformedURLException ; 66 import java.net.URL ; 67 import java.security.AccessController ; 68 import java.security.PrivilegedAction ; 69 import java.util.ArrayList ; 70 import java.util.HashMap ; 71 import java.util.Iterator ; 72 import java.util.List ; 73 import java.util.Map ; 74 import java.util.Set ; 75 import java.util.StringTokenizer ; 76 77 import javax.wsdl.Binding; 78 import javax.wsdl.BindingOperation; 79 import javax.wsdl.Definition; 80 import javax.wsdl.Import; 81 import javax.wsdl.Message; 82 import javax.wsdl.Operation; 83 import javax.wsdl.Part; 84 import javax.wsdl.PortType; 85 import javax.wsdl.Service; 86 import javax.wsdl.WSDLException; 87 import javax.wsdl.extensions.ExtensibilityElement; 88 import javax.wsdl.extensions.ExtensionRegistry; 89 import javax.wsdl.factory.WSDLFactory; 90 import javax.wsdl.xml.WSDLReader; 91 import javax.wsdl.xml.WSDLWriter; 92 import javax.xml.namespace.QName ; 93 94 import org.apache.wsif.WSIFConstants; 95 import org.apache.wsif.WSIFException; 96 import org.apache.wsif.WSIFService; 97 import org.apache.wsif.base.WSIFServiceImpl; 98 import org.apache.wsif.compiler.schema.tools.Schema2Java; 99 import org.apache.wsif.format.WSIFFormatHandler; 100 import org.apache.wsif.logging.MessageLogger; 101 import org.apache.wsif.logging.Trc; 102 import org.apache.wsif.schema.ComplexType; 103 import org.apache.wsif.schema.ElementType; 104 import org.apache.wsif.schema.Parser; 105 import org.apache.wsif.schema.SequenceElement; 106 import org.apache.wsif.wsdl.WSIFWSDLLocatorImpl; 107 import org.apache.wsif.wsdl.extensions.format.TypeMapping; 108 import org.w3c.dom.Document ; 109 import org.w3c.dom.Element ; 110 import org.xml.sax.InputSource ; 111 112 import com.ibm.wsdl.Constants; 113 import com.ibm.wsdl.PartImpl; 114 115 127 public class WSIFUtils { 128 private static Class initContextClass; 129 private static final String SLASH = "/"; 130 private static final String DOT = "."; 131 private static final String FORMAT_HANDLER = "FormatHandler"; 132 private static final String ELEMENT_FORMAT_HANDLER = "ElementFormatHandler"; 133 private static final String PHYSICALREP = "physicalrep/"; 134 private static final String FORMATBINDING = "formatbinding/"; 135 private static final String XMLSEPARATORS = 136 "\u002D\u002E\u003A\u00B7\u0387\u06DD\u06DE\u30FB"; 137 private static final String XMLSEPARATORS_NODOT = 138 "\u002D\u003A\u00B7\u0387\u06DD\u06DE"; 139 private static final String UNDERSCORE = "_"; 140 private static final String WWW = "www"; 141 private static final String lookupPrefix = "java:comp/env/"; 142 private static final String emptyString = ""; 143 144 private static Boolean providersInitialized = new Boolean (false); 145 private static Boolean simpleTypesMapCreated = new Boolean (false); 146 private static HashMap simpleTypesMap = new HashMap (); 147 private static HashMap keywordMap = null; 148 149 155 public static boolean isJNDIAvailable() { 156 Trc.entry(null); 157 initContextClass = 158 (Class ) AccessController.doPrivileged(new PrivilegedAction () { 159 public Object run() { 160 try { 161 return Class.forName( 162 "javax.naming.InitialContext", 163 true, 164 Thread.currentThread().getContextClassLoader()); 165 } catch (Throwable ignored) { 166 Trc.ignoredException(ignored); 167 } 168 return null; 169 } 170 }); 171 172 boolean b = true; 173 if (initContextClass == null) 174 b = false; 175 Trc.exit(b); 176 return b; 177 } 178 179 183 public static WSIFService lookupFactoryFromJNDI( 184 String serviceNS, 185 String serviceName, 186 String portTypeNS, 187 String portTypeName) 188 throws WSIFException { 189 Trc.entry(null, serviceNS, serviceName, portTypeNS, portTypeName); 190 if (serviceNS == null) 191 throw new IllegalArgumentException ("service namespace can not be null"); 192 if (serviceName == null) 193 throw new IllegalArgumentException ("service name can not be null"); 194 if (portTypeNS == null) 195 throw new IllegalArgumentException ("port type namespace can not be null"); 196 if (portTypeName == null) 197 throw new IllegalArgumentException ("port type name can not be null"); 198 199 WSIFService ws = null; 200 try { 201 if (initContextClass == null) { 202 initContextClass = 203 Class.forName( 204 "javax.naming.InitialContext", 205 true, 206 WSIFUtils.class.getClassLoader()); 207 } 208 Object ic = initContextClass.newInstance(); 209 Class [] lookupSig = new Class [] { String .class }; 210 Object [] lookupArgs = 211 new String [] { 212 serviceNS 213 + "::" 214 + serviceName 215 + "::" 216 + portTypeNS 217 + "::" 218 + portTypeName }; 219 Method m = initContextClass.getMethod("lookup", lookupSig); 220 ws = (WSIFService) m.invoke(ic, lookupArgs); 221 } catch (Exception e) { 222 Trc.exception(e); 223 throw new WSIFException( 224 "Exception while looking up JNDI factory: " + e.getMessage(), 225 e); 226 } 227 Trc.exit(ws); 228 return ws; 229 } 230 231 public static Service selectService( 232 Definition def, 233 String serviceNS, 234 String serviceName) 235 throws WSIFException { 236 Trc.entry(null, def, serviceNS, serviceName); 237 Map services = getAllItems(def, "Service"); 238 QName serviceQName = 239 ((serviceNS != null && serviceName != null) 240 ? new QName (serviceNS, serviceName) 241 : null); 242 Service service = 243 (Service) getNamedItem(services, serviceQName, "Service"); 244 245 Trc.exit(service); 246 return service; 247 } 248 249 public static PortType selectPortType( 250 Definition def, 251 String portTypeNS, 252 String portTypeName) 253 throws WSIFException { 254 Trc.entry(null, def, portTypeNS, portTypeName); 255 Map portTypes = getAllItems(def, "PortType"); 256 QName portTypeQName = 257 ((portTypeNS != null && portTypeName != null) 258 ? new QName (portTypeNS, portTypeName) 259 : null); 260 PortType portType = 261 (PortType) getNamedItem(portTypes, portTypeQName, "PortType"); 262 263 Trc.exit(portType); 264 return portType; 265 } 266 267 public static void addDefinedItems( 268 Map fromItems, 269 String itemType, 270 Map toItems) { 271 Trc.entry(null, fromItems, itemType, toItems); 272 273 if (fromItems != null) { 274 Iterator entryIterator = fromItems.entrySet().iterator(); 275 276 if (itemType.equals("Message")) { 277 while (entryIterator.hasNext()) { 278 Map.Entry entry = (Map.Entry ) entryIterator.next(); 279 Message message = (Message) entry.getValue(); 280 281 if (!message.isUndefined()) { 282 toItems.put(entry.getKey(), message); 283 } 284 } 285 } else if (itemType.equals("Operation")) { 286 while (entryIterator.hasNext()) { 287 Map.Entry entry = (Map.Entry ) entryIterator.next(); 288 Operation operation = (Operation) entry.getValue(); 289 290 if (!operation.isUndefined()) { 291 toItems.put(entry.getKey(), operation); 292 } 293 } 294 } else if (itemType.equals("PortType")) { 295 while (entryIterator.hasNext()) { 296 Map.Entry entry = (Map.Entry ) entryIterator.next(); 297 PortType portType = (PortType) entry.getValue(); 298 299 if (!portType.isUndefined()) { 300 toItems.put(entry.getKey(), portType); 301 } 302 } 303 } else if (itemType.equals("Binding")) { 304 while (entryIterator.hasNext()) { 305 Map.Entry entry = (Map.Entry ) entryIterator.next(); 306 Binding binding = (Binding) entry.getValue(); 307 308 if (!binding.isUndefined()) { 309 toItems.put(entry.getKey(), binding); 310 } 311 } 312 } else if (itemType.equals("Service")) { 313 while (entryIterator.hasNext()) { 314 Map.Entry entry = (Map.Entry ) entryIterator.next(); 315 Service service = (Service) entry.getValue(); 316 317 toItems.put(entry.getKey(), service); 318 } 319 } 320 } 321 Trc.exit(); 322 } 323 324 private static void getAllItems( 325 Definition def, 326 String itemType, 327 Map toItems) { 328 Trc.entry(null, def, itemType, toItems); 329 Map items = null; 330 331 if (itemType.equals("PortType")) { 332 items = def.getPortTypes(); 333 } else if (itemType.equals("Service")) { 334 items = def.getServices(); 335 } else { 336 throw new IllegalArgumentException ( 337 "Don't know how to find all " + itemType + "s."); 338 } 339 340 addDefinedItems(items, itemType, toItems); 341 342 Map imports = def.getImports(); 343 344 if (imports != null) { 345 Iterator valueIterator = imports.values().iterator(); 346 347 while (valueIterator.hasNext()) { 348 List importList = (List ) valueIterator.next(); 349 350 if (importList != null) { 351 Iterator importIterator = importList.iterator(); 352 353 while (importIterator.hasNext()) { 354 Import tempImport = (Import) importIterator.next(); 355 356 if (tempImport != null) { 357 Definition importedDef = tempImport.getDefinition(); 358 359 if (importedDef != null) { 360 getAllItems(importedDef, itemType, toItems); 361 } 362 } 363 } 364 } 365 } 366 } 367 Trc.exit(); 368 } 369 370 public static Map getAllItems(Definition def, String itemType) { 371 Trc.entry(null, def, itemType); 372 Map ret = new HashMap (); 373 374 getAllItems(def, itemType, ret); 375 376 Trc.exit(ret); 377 return ret; 378 } 379 380 public static Object getNamedItem(Map items, QName qname, String itemType) 381 throws WSIFException { 382 Trc.entry(null, items, qname, itemType); 383 if (qname != null) { 384 Object item = items.get(qname); 385 386 if (item != null) { 387 Trc.exit(item); 388 return item; 389 } else { 390 throw new WSIFException( 391 itemType 392 + " '" 393 + qname 394 + "' not found. Choices are: " 395 + getCommaListFromQNameMap(items)); 396 } 397 } else { 398 int size = items.size(); 399 400 if (size == 1) { 401 Iterator valueIterator = items.values().iterator(); 402 403 Object o = valueIterator.next(); 404 Trc.exit(o); 405 return o; 406 } else if (size == 0) { 407 throw new WSIFException( 408 "WSDL document contains no " + itemType + "s."); 409 } else { 410 throw new WSIFException( 411 "Please specify a " 412 + itemType 413 + ". Choices are: " 414 + getCommaListFromQNameMap(items)); 415 } 416 } 417 } 418 419 private static String getCommaListFromQNameMap(Map qnameMap) { 420 StringBuffer strBuf = new StringBuffer ("{"); 421 Set keySet = qnameMap.keySet(); 422 Iterator keyIterator = keySet.iterator(); 423 int index = 0; 424 425 while (keyIterator.hasNext()) { 426 QName key = (QName ) keyIterator.next(); 427 428 strBuf.append((index > 0 ? ", " : "") + key); 429 index++; 430 } 431 432 strBuf.append("}"); 433 434 return strBuf.toString(); 435 } 436 437 442 public static Definition readWSDL(String contextURL, String wsdlLoc) 443 throws WSDLException { 444 Trc.entry(null, contextURL, wsdlLoc); 445 446 initializeProviders(); 447 448 WSDLFactory factory = WSDLFactory.newInstance( 449 WSIFConstants.WSIF_WSDLFACTORY); 450 WSDLReader wsdlReader = factory.newWSDLReader(); 451 wsdlReader.setFeature(Constants.FEATURE_VERBOSE, false); 452 try { 453 Definition def = wsdlReader.readWSDL(contextURL, wsdlLoc); 454 Trc.exitExpandWsdl(def); 455 return def; 456 } catch (WSDLException e) { 457 Trc.exception(e); 458 MessageLogger.log("WSIF.0002E", wsdlLoc); 459 throw e; 460 } 461 } 462 463 469 public static Definition readWSDL( 470 URL documentBase, 471 Reader reader, 472 ClassLoader cl) 473 throws WSDLException { 474 String base = (documentBase == null) ? null : documentBase.toString(); 475 return readWSDL(base, reader, cl); 476 } 477 478 484 public static Definition readWSDL( 485 String documentBase, 486 Reader reader, 487 ClassLoader cl) 488 throws WSDLException { 489 Trc.entry(null, documentBase, reader, cl); 490 491 initializeProviders(); 492 493 WSDLFactory factory = WSDLFactory.newInstance( 494 WSIFConstants.WSIF_WSDLFACTORY); 495 WSDLReader wsdlReader = factory.newWSDLReader(); 496 wsdlReader.setFeature(Constants.FEATURE_VERBOSE, false); 497 WSIFWSDLLocatorImpl lo = null; 498 try { 499 lo = new WSIFWSDLLocatorImpl(documentBase, reader, cl); 500 Definition def = wsdlReader.readWSDL(lo); 501 Trc.exitExpandWsdl(def); 502 return def; 503 } catch (WSDLException e) { 504 Trc.exception(e); 505 MessageLogger.log("WSIF.0002E", documentBase); 506 throw e; 507 } finally { 508 try { 509 if (lo != null) lo.close(); 510 } catch (IOException ioe) { 511 Trc.ignoredException(ioe); 513 } 514 } 515 } 516 517 523 public static Definition readWSDL( 524 URL contextURL, 525 String wsdlLoc, 526 ClassLoader cl) 527 throws WSDLException { 528 Trc.entry(null, contextURL, wsdlLoc, cl); 529 530 initializeProviders(); 531 532 WSDLFactory factory = WSDLFactory.newInstance( 533 WSIFConstants.WSIF_WSDLFACTORY); 534 WSDLReader wsdlReader = factory.newWSDLReader(); 535 wsdlReader.setFeature(Constants.FEATURE_VERBOSE, false); 536 WSIFWSDLLocatorImpl lo = null; 537 538 try { 539 String url = (contextURL == null) ? null : contextURL.toString(); 540 lo = new WSIFWSDLLocatorImpl(url, wsdlLoc, cl); 541 Definition def = wsdlReader.readWSDL(lo); 542 Trc.exitExpandWsdl(def); 543 return def; 544 } catch (WSDLException e) { 545 Trc.exception(e); 546 MessageLogger.log("WSIF.0002E", wsdlLoc); 547 throw e; 548 } finally { 549 try { 550 if (lo != null) lo.close(); 551 } catch (IOException ioe) { 552 Trc.ignoredException(ioe); 554 } 555 } 556 } 557 558 563 public static Definition readWSDL(String contextURL, Reader reader) 564 throws WSDLException { 565 Trc.entry(null, contextURL, reader); 566 567 initializeProviders(); 568 569 WSDLFactory factory = WSDLFactory.newInstance( 570 WSIFConstants.WSIF_WSDLFACTORY); 571 WSDLReader wsdlReader = factory.newWSDLReader(); 572 wsdlReader.setFeature(Constants.FEATURE_VERBOSE, false); 573 Definition def = 574 wsdlReader.readWSDL(contextURL, new InputSource (reader)); 575 Trc.exitExpandWsdl(def); 576 return def; 577 } 578 579 584 public static Definition readWSDL(String contextURL, Document wsdlDocument) 585 throws WSDLException { 586 Trc.entry(null, contextURL, wsdlDocument); 587 588 initializeProviders(); 589 590 WSDLFactory factory = WSDLFactory.newInstance( 591 WSIFConstants.WSIF_WSDLFACTORY); 592 WSDLReader wsdlReader = factory.newWSDLReader(); 593 wsdlReader.setFeature(Constants.FEATURE_VERBOSE, false); 594 Definition def = wsdlReader.readWSDL(contextURL, wsdlDocument); 595 596 Trc.exitExpandWsdl(def); 597 return def; 598 } 599 600 605 public static Definition readWSDL( 606 String contextURL, 607 Element wsdlServicesElement) 608 throws WSDLException { 609 Trc.entry(null, contextURL, wsdlServicesElement); 610 611 initializeProviders(); 612 613 WSDLFactory factory = WSDLFactory.newInstance( 614 WSIFConstants.WSIF_WSDLFACTORY); 615 WSDLReader wsdlReader = factory.newWSDLReader(); 616 wsdlReader.setFeature(Constants.FEATURE_VERBOSE, false); 617 Definition def = wsdlReader.readWSDL(contextURL, wsdlServicesElement); 618 619 Trc.exitExpandWsdl(def); 620 return def; 621 } 622 623 628 public static void writeWSDL(Definition def, Writer sink) 629 throws WSDLException { 630 Trc.entry(null, def, sink); 631 632 WSDLFactory factory = WSDLFactory.newInstance( 633 WSIFConstants.WSIF_WSDLFACTORY); 634 WSDLWriter wsdlWriter = factory.newWSDLWriter(); 635 wsdlWriter.writeWSDL(def, sink); 636 637 Trc.exit(); 638 } 639 640 public static Definition getDefinitionFromLocation( 641 String contextURL, 642 String location) 643 throws WSIFException { 644 Trc.entry(null, contextURL, location); 645 646 if (location == null) { 647 throw new WSIFException("WSDL location must not be null."); 648 } 649 650 Definition def = null; 651 try { 652 def = WSIFUtils.readWSDL(contextURL, location); 653 } catch (WSDLException e) { 654 Trc.exception(e); 655 throw new WSIFException("Problem reading WSDL document.", e); 656 } 657 Trc.exitExpandWsdl(def); 658 return def; 659 } 660 661 public static Definition getDefinitionFromContent( 662 String contextURL, 663 String content) 664 throws WSIFException { 665 Trc.entry(null, contextURL, content); 666 if (content == null) { 667 throw new WSIFException("WSDL content must not be null."); 668 } 669 670 Definition def = null; 671 try { 672 def = WSIFUtils.readWSDL(contextURL, new StringReader (content)); 673 } catch (WSDLException e) { 674 Trc.exception(e); 675 throw new WSIFException("Problem reading WSDL document.", e); 676 } 677 Trc.exitExpandWsdl(def); 678 return def; 679 } 680 681 687 public static void initializeProviders() { 688 synchronized (providersInitialized) { 689 if (!providersInitialized.booleanValue()) { 690 WSIFPluggableProviders.getProvider("/"); 691 providersInitialized = new Boolean (true); 692 } 693 } 694 } 695 696 699 public static void createSimpleTypesMap() { 700 synchronized (simpleTypesMapCreated) { 701 if (!simpleTypesMapCreated.booleanValue()) { 702 new Schema2Java( 703 WSIFConstants.NS_URI_1999_SCHEMA_XSD).getRegistry( 704 simpleTypesMap); 705 new Schema2Java( 706 WSIFConstants.NS_URI_2000_SCHEMA_XSD).getRegistry( 707 simpleTypesMap); 708 new Schema2Java( 709 WSIFConstants.NS_URI_2001_SCHEMA_XSD).getRegistry( 710 simpleTypesMap); 711 simpleTypesMapCreated = new Boolean (true); 712 } 713 } 714 } 715 716 720 public static Map getSimpleTypesMap() { 721 if (!simpleTypesMapCreated.booleanValue()) { 722 createSimpleTypesMap(); 723 } 724 return simpleTypesMap; 725 } 726 727 public static WSIFFormatHandler getFormatHandler( 729 Part part, 730 Definition definition, 731 javax.wsdl.Binding binding) 732 throws 733 java.lang.InstantiationException , 734 java.lang.IllegalAccessException , 735 java.lang.ClassNotFoundException { 736 Trc.entry(null, part, definition, binding); 737 WSIFFormatHandler formatHandler = null; 738 javax.xml.namespace.QName partTypeQName = part.getTypeName(); 739 if (partTypeQName == null) 740 partTypeQName = part.getElementName(); 741 if (partTypeQName == null) 742 throw new ClassNotFoundException (part.getName()); 743 744 String typePackageName = 745 getPackageNameFromNamespaceURI(partTypeQName.getNamespaceURI()); 746 String formatHandlerName = typePackageName; 747 748 String bindingShortName = 749 getPackageNameFromXMLName( 750 definition.getPrefix(getBindingNamespace(binding))); 751 if (bindingShortName != null) 752 formatHandlerName = formatHandlerName + DOT + bindingShortName; 753 754 if (getFormatStylePackage(binding) != null) 755 formatHandlerName = 756 formatHandlerName + DOT + getFormatStylePackage(binding); 757 758 String formatHandlerShortName = 759 formatHandlerName 760 + DOT 761 + getJavaClassNameFromXMLName(partTypeQName.getLocalPart()); 762 763 try { 764 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 765 formatHandler = 766 (WSIFFormatHandler) cl 767 .loadClass(formatHandlerShortName + FORMAT_HANDLER) 768 .newInstance(); 769 } catch (ClassNotFoundException exn1) { 770 Trc.ignoredException(exn1); 771 try { 772 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 773 formatHandler = 774 (WSIFFormatHandler) cl 775 .loadClass( 776 formatHandlerShortName + ELEMENT_FORMAT_HANDLER) 777 .newInstance(); 778 } catch (ClassNotFoundException exn2) { 779 Trc.ignoredException(exn2); 780 try { 781 formatHandler = 782 (WSIFFormatHandler) Class 783 .forName(formatHandlerShortName + FORMAT_HANDLER) 784 .newInstance(); 785 } catch (ClassNotFoundException exn3) { 786 Trc.ignoredException(exn3); 787 formatHandler = 789 (WSIFFormatHandler) Class 790 .forName( 791 formatHandlerShortName 792 + ELEMENT_FORMAT_HANDLER) 793 .newInstance(); 794 } 799 } 800 } 801 if (formatHandler == null) 805 throw new ClassNotFoundException (formatHandlerName); 807 else { 808 Trc.exit(formatHandler); 809 return formatHandler; 810 } 811 } 812 813 public static String getPackageNameFromNamespaceURI(String namespaceURI) { 814 Trc.entry(null, namespaceURI); 815 List segments = getNamespaceURISegments(namespaceURI); 817 818 StringBuffer packageNameBuffer = new StringBuffer (); 819 for (int i = 0; i < segments.size(); i++) { 820 String name; 821 822 if (i == 0) { 824 825 name = getPackageNameFromXMLName((String ) segments.get(i)); 827 828 StringTokenizer tokenizer = new StringTokenizer (name, "."); 830 List host = new ArrayList (); 831 for (; tokenizer.hasMoreTokens();) { 832 String nextT = tokenizer.nextToken(); 833 host.add(0, nextT); 834 } 835 StringBuffer buffer = new StringBuffer (); 836 for (Iterator hi = host.iterator(); hi.hasNext();) { 837 if (buffer.length() != 0) 838 buffer.append('.'); 839 String nextSegment = (String ) hi.next(); 840 if (!Character 841 .isJavaIdentifierStart(nextSegment.toCharArray()[0])) 842 nextSegment = UNDERSCORE + nextSegment; 843 if (isJavaKeyword(nextSegment)) 844 nextSegment = UNDERSCORE + nextSegment; 845 buffer.append(nextSegment); 846 } 847 name = buffer.toString(); 848 } else { 849 850 name = getJavaNameFromXMLName((String ) segments.get(i)); 852 853 } 854 855 if (name.length() == 0) 857 continue; 858 if (packageNameBuffer.length() != 0) 859 packageNameBuffer.append('.'); 860 packageNameBuffer.append(name); 861 } 862 Trc.exit(packageNameBuffer.toString()); 863 return packageNameBuffer.toString(); 864 } 865 866 public static String getJavaNameFromXMLName( 867 String xmlName, 868 String delims) { 869 Trc.entry(null, xmlName, delims); 870 StringTokenizer tokenizer = new StringTokenizer (xmlName, delims); 871 StringBuffer buffer = new StringBuffer (); 872 while (tokenizer.hasMoreTokens()) { 873 buffer.append(tokenizer.nextToken()); 874 } 875 String result = buffer.toString(); 876 if (!Character.isJavaIdentifierStart(result.toCharArray()[0])) 877 result = UNDERSCORE + result; 878 if (isJavaKeyword(result)) 879 result = UNDERSCORE + result; 880 Trc.exit(result); 881 return result; 882 } 883 884 public static String getJavaNameFromXMLName(String xmlName) { 885 Trc.entry(null, xmlName); 886 String s = getJavaNameFromXMLName(xmlName, XMLSEPARATORS); 887 Trc.exit(s); 888 return s; 889 } 890 891 public static String getPackageNameFromXMLName(String xmlName) { 892 Trc.entry(null, xmlName); 893 894 String name = getJavaNameFromXMLName(xmlName, XMLSEPARATORS_NODOT); 896 897 StringTokenizer tokenizer = new StringTokenizer (name, DOT); 900 StringBuffer buffer = new StringBuffer (); 901 for (; tokenizer.hasMoreTokens();) { 902 if (buffer.length() != 0) 903 buffer.append('.'); 904 String nextSegment = (String ) tokenizer.nextToken(); 906 if (!Character.isJavaIdentifierStart(nextSegment.toCharArray()[0])) 907 nextSegment = UNDERSCORE + nextSegment; 908 if (isJavaKeyword(nextSegment)) 909 nextSegment = UNDERSCORE + nextSegment; 910 buffer.append(nextSegment); 911 912 } 915 Trc.exit(buffer.toString()); 916 return buffer.toString(); 917 } 918 919 private static List getNamespaceURISegments(String namespaceURI) { 920 Trc.entry(null, namespaceURI); 921 922 List segments = new ArrayList (); 924 StringTokenizer tokenizer = new StringTokenizer (namespaceURI, ":/"); 925 while (tokenizer.hasMoreTokens()) { 926 segments.add(tokenizer.nextToken()); 927 } 928 929 if (!segments.isEmpty()) { 931 try { 932 URL url = new URL (namespaceURI); 933 if (segments.get(0).equals(url.getProtocol())) 934 segments.remove(0); 935 } catch (MalformedURLException exn) { 936 Trc.ignoredException(exn); 937 } 938 } 939 Trc.exit(segments); 940 return segments; 941 } 942 943 private static String getBindingNamespace(Binding bindingModel) { 944 Trc.entry(null, bindingModel); 945 Iterator iterator = bindingModel.getExtensibilityElements().iterator(); 946 String returnNamespace = null; 947 while (iterator.hasNext()) { 948 ExtensibilityElement ee = (ExtensibilityElement) iterator.next(); 949 if (returnNamespace == null) { 950 String namespace = ee.getElementType().getNamespaceURI(); 951 if (!namespace.endsWith(PHYSICALREP) 952 && !namespace.endsWith(FORMATBINDING)) { 953 returnNamespace = namespace; 954 } 955 } 956 } 957 Trc.exit(returnNamespace); 958 return returnNamespace; 959 } 960 961 public static String getFormatStylePackage(Binding bindingModel) { 962 Trc.entry(null, bindingModel); 963 964 Iterator iterator = bindingModel.getExtensibilityElements().iterator(); 965 String formatPackageName = null; 966 while (iterator.hasNext()) { 967 ExtensibilityElement ee = (ExtensibilityElement) iterator.next(); 968 if (ee instanceof TypeMapping) { 969 TypeMapping typeMapping = (TypeMapping) ee; 970 formatPackageName = typeMapping.getEncoding(); 971 if (typeMapping.getStyle() != null) 972 formatPackageName += typeMapping.getStyle(); 973 } 974 if (formatPackageName != null) 975 break; 976 } 977 String s = null; 978 if (formatPackageName != null) { 979 s = getPackageNameFromXMLName(formatPackageName); 981 } else { 982 s = formatPackageName; 983 } 984 Trc.exit(s); 985 return s; 986 } 987 988 public static String getFormatHandlerName( 989 Part part, 990 Definition definition, 991 Binding binding) 992 throws 993 java.lang.InstantiationException , 994 java.lang.IllegalAccessException , 995 java.lang.ClassNotFoundException { 996 Trc.entry(null, part, definition, binding); 997 998 javax.xml.namespace.QName partTypeQName = part.getTypeName(); 999 if (partTypeQName == null) 1000 partTypeQName = part.getElementName(); 1001 if (partTypeQName == null) 1002 throw new ClassNotFoundException (part.getName()); 1003 1004 String typePackageName = 1005 getPackageNameFromNamespaceURI(partTypeQName.getNamespaceURI()); 1006 String formatHandlerName = typePackageName; 1007 1008 String bindingShortName = 1009 getPackageNameFromXMLName( 1010 definition.getPrefix(getBindingNamespace(binding))); 1011 if (bindingShortName != null) 1012 formatHandlerName = formatHandlerName + DOT + bindingShortName; 1013 1014 if (getFormatStylePackage(binding) != null) 1015 formatHandlerName = 1016 formatHandlerName + DOT + getFormatStylePackage(binding); 1017 1018 formatHandlerName = 1019 formatHandlerName 1020 + DOT 1021 + getJavaClassNameFromXMLName(partTypeQName.getLocalPart()) 1022 + FORMAT_HANDLER; 1023 1024 Trc.exit(formatHandlerName); 1025 return formatHandlerName; 1026 } 1027 1028 public static String getJavaClassNameFromXMLName(String xmlName) { 1029 Trc.entry(null, xmlName); 1030 String s = getJavaClassNameFromXMLName(xmlName, XMLSEPARATORS); 1031 Trc.exit(s); 1032 return s; 1033 } 1034 1035 public static String getJavaClassNameFromXMLName( 1036 String xmlName, 1037 String delims) { 1038 Trc.entry(null, xmlName, delims); 1039 StringTokenizer tokenizer = new StringTokenizer (xmlName, delims); 1040 StringBuffer buffer = new StringBuffer (); 1041 while (tokenizer.hasMoreTokens()) { 1042 String nextSegment = (String ) tokenizer.nextToken(); 1043 if (nextSegment.length() > 0) { 1044 nextSegment = 1045 Character.toUpperCase((nextSegment.toCharArray())[0]) 1046 + nextSegment.substring(1); 1047 } 1048 buffer.append(nextSegment); 1049 } 1050 String result = buffer.toString(); 1051 if (!Character.isJavaIdentifierStart(result.toCharArray()[0])) 1052 result = UNDERSCORE + result; 1053 Trc.exit(result); 1054 if (isJavaKeyword(result)) 1055 return UNDERSCORE + result; 1056 else 1057 return result; 1058 } 1059 1060 public static String getXSDNamespaceFromPackageName(String packageName) { 1061 1062 String result = ""; 1063 StringTokenizer tokenizer = new java.util.StringTokenizer (packageName, "."); 1064 while (tokenizer.hasMoreTokens()) { 1065 String nextT = tokenizer.nextToken(); 1066 result = nextT + "." + result; 1067 } 1068 if (result.endsWith(".")) 1069 return "http://" + result.substring(0, result.length() - 1) + "/"; 1070 return "http://" + result + "/"; 1071 } 1072 1073 1081 public static BindingOperation getBindingOperation( 1082 Binding binding, 1083 Operation portTypeOp) throws WSIFException { 1084 1085 Trc.entry(null, binding, portTypeOp); 1086 BindingOperation bop; 1087 if ( portTypeOp == null ) { 1088 bop = null; 1089 } else { 1090 bop = getBindingOperation( 1091 binding, 1092 portTypeOp.getName(), 1093 portTypeOp.getInput()==null ? null : portTypeOp.getInput().getName(), 1094 portTypeOp.getOutput()==null ? null : portTypeOp.getOutput().getName() ); 1095 } 1096 Trc.exit(bop); 1097 return bop; 1098 } 1099 1100 1109 public static BindingOperation getBindingOperation( 1110 Binding binding, 1111 String opName, 1112 String inName, 1113 String outName) throws WSIFException { 1114 1115 Trc.entry(null, binding, opName, inName, outName); 1116 BindingOperation op = null; 1117 if (binding != null && opName != null) { 1118 ArrayList matchingOps = new ArrayList (); 1119 List bops = binding.getBindingOperations(); 1120 if (bops != null) { 1121 for (Iterator i = bops.iterator(); i.hasNext();) { 1122 BindingOperation bop = (BindingOperation) i.next(); 1123 if ( opName.equalsIgnoreCase(bop.getName()) ) { 1124 matchingOps.add(bop); 1125 } 1126 } 1127 if (matchingOps.size() == 1) { 1128 op = (BindingOperation) matchingOps.get(0); 1129 } else if (matchingOps.size() > 1) { 1130 op = chooseBindingOperation(matchingOps, inName, outName); 1131 } 1132 } 1133 } 1134 Trc.exit(op); 1135 return op; 1136 } 1137 1138 private static BindingOperation chooseBindingOperation( 1139 ArrayList bindingOps, 1140 String inName, 1141 String outName) throws WSIFException { 1142 1143 BindingOperation choosenOp = null; 1144 for (Iterator i = bindingOps.iterator(); i.hasNext(); ) { 1145 BindingOperation bop = (BindingOperation) i.next(); 1146 String binName = (bop.getBindingInput() == null) ? 1147 null : 1148 bop.getBindingInput().getName(); 1149 String boutName = (bop.getBindingOutput() == null) ? 1150 null : 1151 bop.getBindingOutput().getName(); 1152 if ((inName == null) ? binName == null : inName.equalsIgnoreCase(binName)) { 1153 if ((outName == null) 1154 ? boutName == null 1155 : outName.equalsIgnoreCase(boutName)) { 1156 if ( choosenOp == null ) { 1157 choosenOp = bop; 1158 } else { 1159 throw new WSIFException( 1160 "duplicate operation in binding: " + 1161 bop.getName() + 1162 ":" + inName + 1163 ":" + outName ); 1164 } 1165 } 1166 } 1167 } 1168 return choosenOp; 1169 } 1170 1171 private static boolean isJavaKeyword(String identifier) { 1172 if (keywordMap == null) { 1173 Object value = new Object (); 1174 keywordMap = new HashMap (); 1175 keywordMap.put("abstract", value); 1176 keywordMap.put("default", value); 1177 keywordMap.put("if", value); 1178 keywordMap.put("private", value); 1179 keywordMap.put("this", value); 1180 keywordMap.put("boolean", value); 1181 keywordMap.put("do", value); 1182 keywordMap.put("implements", value); 1183 keywordMap.put("protected", value); 1184 keywordMap.put("throw", value); 1185 keywordMap.put("break", value); 1186 keywordMap.put("double", value); 1187 keywordMap.put("import", value); 1188 keywordMap.put("public", value); 1189 keywordMap.put("throws", value); 1190 keywordMap.put("byte", value); 1191 keywordMap.put("else", value); 1192 keywordMap.put("instanceof", value); 1193 keywordMap.put("return", value); 1194 keywordMap.put("transient", value); 1195 keywordMap.put("case", value); 1196 keywordMap.put("extends", value); 1197 keywordMap.put("int", value); 1198 keywordMap.put("short", value); 1199 keywordMap.put("try", value); 1200 keywordMap.put("catch", value); 1201 keywordMap.put("final", value); 1202 keywordMap.put("interface", value); 1203 keywordMap.put("static", value); 1204 keywordMap.put("void", value); 1205 keywordMap.put("char", value); 1206 keywordMap.put("finally", value); 1207 keywordMap.put("long", value); 1208 keywordMap.put("strictfp", value); 1209 keywordMap.put("volatile", value); 1210 keywordMap.put("class", value); 1211 keywordMap.put("float", value); 1212 keywordMap.put("native", value); 1213 keywordMap.put("super", value); 1214 keywordMap.put("while", value); 1215 keywordMap.put("const", value); 1216 keywordMap.put("for", value); 1217 keywordMap.put("new", value); 1218 keywordMap.put("switch", value); 1219 keywordMap.put("continue", value); 1220 keywordMap.put("goto", value); 1221 keywordMap.put("package", value); 1222 keywordMap.put("synchronized", value); 1223 keywordMap.put("null", value); 1224 keywordMap.put("true", value); 1225 keywordMap.put("false", value); 1226 keywordMap.put("assert", value); 1227 } 1228 return keywordMap.containsKey(identifier); 1229 1230 } 1242 1243 1250 public static boolean wildcardCompare(String s1, String s2, char wild) { 1251 if (s1 == null) { 1252 return false; 1253 } 1254 String w = wild + ""; 1255 return cmp(new StringTokenizer (s1, w, true), s2, w); 1256 } 1257 1258 private static boolean cmp(StringTokenizer st, String s, String wild) { 1259 if ( s == null || s.equals( "" ) ) { 1260 return !st.hasMoreTokens(); 1261 } 1262 if ( st.hasMoreTokens() ) { 1263 String s2 = st.nextToken(); 1264 if ( wild.equals( s2 ) ) { 1265 if ( !st.hasMoreTokens() ) { 1266 return true; } 1268 s2 = st.nextToken(); 1269 if ( s.equals( s2 ) ) { 1270 return false; } 1272 } 1273 int i = s.indexOf( s2 ); 1274 if ( i < 0 ) { 1275 return false; } 1277 i += s2.length(); 1278 if ( i < s.length() ) { 1279 return cmp( st, s.substring( i ), wild ); 1280 } else { 1281 return cmp( st, "", wild ); 1282 } 1283 } 1284 return false; } 1286 1287 1297 public static boolean isWrappedDocLiteral(List parts, String operationName) { 1298 boolean wrapped = !(parts==null); 1299 Part elementPart = null; 1300 for (int i = 0; wrapped && i < parts.size(); i++) { 1301 Part p = (Part) parts.get(i); 1302 if (p.getElementName() != null) { 1303 if (elementPart == null) { 1304 elementPart = p; 1305 String pName = p.getElementName().getLocalPart(); 1306 if (!operationName.equals(pName)) { 1307 wrapped = false; 1308 } 1309 } else { 1310 wrapped = false; 1311 } 1312 } 1313 } 1314 return wrapped; 1315 } 1316 1317 1327 public static Part getWrappedDocLiteralPart(List parts, String operationName) { 1328 boolean wrapped = !(parts==null); 1329 Part elementPart = null; 1330 for (int i = 0; wrapped && i < parts.size(); i++) { 1331 Part p = (Part) parts.get(i); 1332 if (p.getElementName() != null) { 1333 if (elementPart == null) { 1334 elementPart = p; 1335 String pName = p.getElementName().getLocalPart(); 1336 if (!operationName.equals(pName)) { 1337 wrapped = false; 1338 } 1339 } else { 1340 wrapped = false; 1341 } 1342 } 1343 } 1344 if (!wrapped) { 1345 elementPart = null; 1346 } 1347 return elementPart; 1348 } 1349 1350 1353 public static List unWrapPart(Part p, Definition def) throws WSIFException { 1354 1355 ArrayList l = new ArrayList (); 1356 Parser.getAllSchemaTypes(def, l, null); 1357 if (l == null || l.size()<1) { 1358 throw new WSIFException("no schema elements found"); 1359 } 1360 1361 QName partQN = p.getElementName(); 1362 if (partQN == null) { 1363 throw new WSIFException("part has no QName"); 1364 } 1365 1366 ElementType et = null; 1367 for (int i=0; i<l.size() && et==null; i++ ){ 1368 Object o = l.get(i); 1369 if ( o instanceof ElementType ) { 1370 QName etQN = ((ElementType)o).getTypeName(); 1371 if ( partQN.equals(etQN) ){ 1372 et = (ElementType)o; 1373 } 1374 } 1375 } 1376 if (et == null) { 1377 throw new WSIFException("no ElementType found for part: " + p); 1378 } 1379 1380 List children = et.getChildren(); 1381 if (children == null || l.size()<1) { 1382 throw new WSIFException("no ComplexType children on elementType: " + et); 1383 } 1384 1385 ComplexType ct = (ComplexType) children.get(0); 1386 SequenceElement[] se = ct.getSequenceElements(); 1387 if (se == null) { 1388 throw new WSIFException("no sequence elements found on: " + ct); 1389 } 1390 1391 ArrayList unWrappedParts = new ArrayList (); 1392 for (int i=0; i< se.length; i++) { 1393 PartImpl np = new PartImpl(); 1394 QName type = se[i].getTypeName(); 1395 if (type==null) { 1396 throw new WSIFException("sequence element has no type name: " + se[i]); 1397 } 1398 np.setName(type.getLocalPart()); 1399 np.setElementName(se[i].getElementType()); 1400 unWrappedParts.add(np); 1401 } 1402 1403 return unWrappedParts; 1404 } 1405 1406 1412 public static ExtensionRegistry getExtensionRegistry() { 1413 Trc.entry(null); 1414 initializeProviders(); 1415 ExtensionRegistry er = WSIFServiceImpl.getCompositeExtensionRegistry(); 1416 Trc.exit(er); 1417 return er; 1418 } 1419 1420} | Popular Tags |