1 57 58 package org.apache.wsif.schema; 59 60 import java.io.Reader ; 61 import java.util.ArrayList ; 62 import java.util.Hashtable ; 63 import java.util.Iterator ; 64 import java.util.List ; 65 import java.util.Map ; 66 67 import javax.wsdl.Definition; 68 import javax.wsdl.Import; 69 import javax.wsdl.Types; 70 import javax.wsdl.extensions.UnknownExtensibilityElement; 71 import javax.wsdl.xml.WSDLLocator; 72 import javax.xml.namespace.QName ; 73 import javax.xml.parsers.DocumentBuilder ; 74 import javax.xml.parsers.DocumentBuilderFactory ; 75 76 import org.apache.wsif.WSIFConstants; 77 import org.apache.wsif.WSIFException; 78 import org.apache.wsif.logging.Trc; 79 import org.apache.wsif.util.WSIFUtils; 80 import org.apache.wsif.wsdl.WSIFWSDLLocatorImpl; 81 import org.w3c.dom.Document ; 82 import org.w3c.dom.Element ; 83 import org.xml.sax.InputSource ; 84 85 import com.ibm.wsdl.util.xml.QNameUtils; 86 87 94 public class Parser { 95 96 private static final QName schema1999 = 97 new QName (WSIFConstants.NS_URI_1999_SCHEMA_XSD, "schema"); 98 private static final QName schema2000 = 99 new QName (WSIFConstants.NS_URI_2000_SCHEMA_XSD, "schema"); 100 private static final QName schema2001 = 101 new QName (WSIFConstants.NS_URI_2001_SCHEMA_XSD, "schema"); 102 103 109 public static void getTypeMappings(Definition def, Map table) throws WSIFException { 110 getTypeMappings(def, table, true, null); 111 } 112 113 120 public static void getTypeMappings(Definition def, Map table, ClassLoader loader) throws WSIFException { 121 WSDLLocator locator = new WSIFWSDLLocatorImpl((String ) null, (String ) null, loader); 122 getTypeMappings(def, table, true, locator); 123 } 124 125 134 public static void getTypeMappings( 135 Definition def, 136 Map table, 137 ClassLoader loader, 138 boolean includeStandardMappings) throws WSIFException { 139 WSDLLocator locator = new WSIFWSDLLocatorImpl((String ) null, (String ) null, loader); 140 getTypeMappings(def, table, includeStandardMappings, locator); 141 } 142 143 150 public static void getTypeMappings(Definition def, Map table, WSDLLocator loc) throws WSIFException { 151 getTypeMappings(def, table, true, loc); 152 } 153 154 162 public static void getTypeMappings( 163 Definition def, 164 Map table, 165 boolean includeStandardMappings) throws WSIFException { 166 167 getTypeMappings(def, table, includeStandardMappings, null); 168 } 169 170 179 public static void getTypeMappings( 180 Definition def, 181 Map table, 182 boolean includeStandardMappings, 183 WSDLLocator loc) throws WSIFException { 184 185 Trc.entry(null, def, table, new Boolean (includeStandardMappings), loc); 186 if (loc == null) { 187 loc = new WSIFWSDLLocatorImpl((String ) null, (String ) null, null); 188 } 189 190 ArrayList schemaList = new ArrayList (); 191 getTypesSchemas(def, schemaList, loc); 192 193 Hashtable standards = null; 194 195 if (includeStandardMappings) { 196 populateWithStandardMappings( 198 table, 199 WSIFConstants.NS_URI_1999_SCHEMA_XSD, 200 true); 201 populateWithStandardMappings( 202 table, 203 WSIFConstants.NS_URI_2000_SCHEMA_XSD, 204 false); 205 populateWithStandardMappings( 206 table, 207 WSIFConstants.NS_URI_2001_SCHEMA_XSD, 208 false); 209 } else { 210 standards = new Hashtable (); 212 populateWithStandardMappings( 213 standards, 214 WSIFConstants.NS_URI_1999_SCHEMA_XSD, 215 true); 216 populateWithStandardMappings( 217 standards, 218 WSIFConstants.NS_URI_2000_SCHEMA_XSD, 219 false); 220 populateWithStandardMappings( 221 standards, 222 WSIFConstants.NS_URI_2001_SCHEMA_XSD, 223 false); 224 } 225 226 List arrays = new ArrayList (); 229 230 List elements = new ArrayList (); 233 234 Iterator si = schemaList.iterator(); 236 while (si.hasNext()) { 237 Schema ts = (Schema) si.next(); 238 if (ts != null) { 239 List types = ts.getTypes(); 241 Iterator ti = types.iterator(); 242 while (ti.hasNext()) { 243 SchemaType st = (SchemaType) ti.next(); 244 if (st == null) 246 continue; 247 QName typeName = st.getTypeName(); 248 if (typeName == null) 249 continue; 250 251 if (st.isArray()) { 252 arrays.add(st); 253 } else { 254 if (st instanceof ElementType) { 256 QName baseType = ((ElementType) st).getElementType(); 257 258 if (baseType != null) { 259 if (((ElementType) st).isNillable()) { 260 String wrapperClass = getWrapperClassName(baseType); 261 if (wrapperClass != null) { 262 table.put(typeName, wrapperClass); 263 continue; 264 } 265 } 266 String baseClassName = 267 (String ) table.get(baseType); 268 if (baseClassName == null 269 && !includeStandardMappings) { 270 baseClassName = 271 (String ) standards.get(baseType); 272 } 273 if (baseClassName != null) { 274 table.put(typeName, baseClassName); 275 } else { 276 elements.add(st); 277 } 278 } else { 279 String className = resolveClassName(typeName); 280 className = className + "Element"; 283 if (className != null) { 284 table.put(typeName, className); 285 } 286 } 287 } else { 288 String className = resolveClassName(typeName); 290 if (className != null) { 291 table.put(typeName, className); 292 } 293 } 294 } 295 } 296 } 297 } 298 299 ArrayList multiArrays = new ArrayList (); 301 302 Iterator ai = arrays.iterator(); 304 while (ai.hasNext()) { 305 SchemaType st = (SchemaType) ai.next(); 306 QName theType = st.getTypeName(); 308 if (theType == null) continue; 309 310 QName arrayType = st.getArrayType(); 311 if (arrayType != null && theType != null) { 312 String baseClass = (String ) table.get(arrayType); 313 if (baseClass == null && standards != null) { 314 baseClass = (String ) standards.get(arrayType); 316 } 317 if (baseClass == null) { 318 String lp = arrayType.getLocalPart(); 319 if (lp != null && lp.startsWith("ArrayOf")) { 320 multiArrays.add(st); 324 } 325 continue; 326 } 327 String extraDims = ""; 329 for (int x = 1; x < st.getArrayDimension(); x++) { 330 extraDims += "["; 331 } 332 if (baseClass != null) { 333 if (baseClass.equals("int")) { 335 table.put(theType, extraDims + "[I"); 336 } else if (baseClass.equals("float")) { 337 table.put(theType, extraDims + "[F"); 338 } else if (baseClass.equals("long")) { 339 table.put(theType, extraDims + "[J"); 340 } else if (baseClass.equals("double")) { 341 table.put(theType, extraDims + "[D"); 342 } else if (baseClass.equals("boolean")) { 343 table.put(theType, extraDims + "[Z"); 344 } else if (baseClass.equals("byte")) { 345 table.put(theType, extraDims + "[B"); 346 } else if (baseClass.equals("short")) { 347 table.put(theType, extraDims + "[S"); 348 } else if (baseClass.startsWith("[")) { 349 String arrayOfBase = "[" + baseClass; 351 table.put(theType, arrayOfBase); 352 } else { 353 String arrayOfBase = extraDims + "[L" + baseClass + ";"; 354 table.put(theType, arrayOfBase); 355 } 356 } 357 } 358 } 359 360 Iterator mi = multiArrays.iterator(); 362 while (mi.hasNext()) { 363 SchemaType st = (SchemaType) mi.next(); 364 QName theType = st.getTypeName(); 365 if (theType == null) continue; 366 367 QName arrayType = st.getArrayType(); 368 if (arrayType != null && theType != null) { 369 String extraDims = ""; 370 for (int x = 1; x < st.getArrayDimension(); x++) { 371 extraDims += "["; 372 } 373 String baseClass = (String ) table.get(arrayType); 374 if (baseClass != null) { 375 if (baseClass.startsWith("[")) { 377 String arrayOfBase = "[" + baseClass; 378 table.put(theType, arrayOfBase); 379 } 380 } 381 } 382 } 383 384 Iterator ei = elements.iterator(); 386 while (ei.hasNext()) { 387 SchemaType st = (SchemaType) ei.next(); 388 QName theType = st.getTypeName(); 389 if (theType == null) 390 continue; 391 392 QName baseType = null; 393 if (st instanceof ElementType) { 394 baseType = ((ElementType) st).getElementType(); 395 } 396 if (baseType != null) { 397 String baseClassName = (String ) table.get(baseType); 398 if (baseClassName != null) { 399 table.put(theType, baseClassName); 400 } 401 } 402 } 403 404 Trc.exit(); 405 } 406 407 416 public static void getAllSchemaTypes(Definition def, List schemaTypes, WSDLLocator loc) throws WSIFException { 417 try { 418 ArrayList schemas = new ArrayList (); 419 Parser.getTypesSchemas(def, schemas, loc); 420 Iterator si = schemas.iterator(); 421 while (si.hasNext()) { 422 Schema ts = (Schema) si.next(); 423 if (ts != null) { 424 List types = ts.getTypes(); 426 Iterator ti = types.iterator(); 427 while (ti.hasNext()) { 428 SchemaType st = (SchemaType) ti.next(); 429 if (st == null) 431 continue; 432 schemaTypes.add(st); 433 } 434 } 435 } 436 } catch (WSIFException e) { 437 } 438 } 439 440 443 private static void populateWithStandardMappings( 444 Map t, 445 String schemaURI, 446 boolean oneTimeAdds) { 447 448 t.put(new QName (schemaURI, "string"), "java.lang.String"); 449 t.put(new QName (schemaURI, "integer"), "java.math.BigInteger"); 450 t.put(new QName (schemaURI, "boolean"), "boolean"); 451 t.put(new QName (schemaURI, "float"), "float"); 452 t.put(new QName (schemaURI, "double"), "double"); 453 t.put(new QName (schemaURI, "base64Binary"), "[B"); 454 t.put(new QName (schemaURI, "hexBinary"), "[B"); 455 t.put(new QName (schemaURI, "long"), "long"); 456 t.put(new QName (schemaURI, "int"), "int"); 457 t.put(new QName (schemaURI, "short"), "short"); 458 t.put(new QName (schemaURI, "decimal"), "java.math.BigDecimal"); 459 t.put(new QName (schemaURI, "byte"), "byte"); 460 t.put(new QName (schemaURI, "QName"), "javax.xml.namespace.QName"); 461 462 if (schemaURI.equals(WSIFConstants.NS_URI_2001_SCHEMA_XSD)) { 464 t.put(new QName (schemaURI, "dateTime"), "java.util.Calendar"); 465 } else { 466 t.put(new QName (schemaURI, "timeInstant"), "java.util.Calendar"); 467 } 468 469 if (oneTimeAdds) { 471 t.put( 473 new QName (WSIFConstants.NS_URI_SOAP_ENC, "string"), 474 "java.lang.String"); 475 t.put( 476 new QName (WSIFConstants.NS_URI_SOAP_ENC, "boolean"), 477 "java.lang.Boolean"); 478 t.put( 479 new QName (WSIFConstants.NS_URI_SOAP_ENC, "float"), 480 "java.lang.Float"); 481 t.put( 482 new QName (WSIFConstants.NS_URI_SOAP_ENC, "double"), 483 "java.lang.Double"); 484 t.put( 485 new QName (WSIFConstants.NS_URI_SOAP_ENC, "decimal"), 486 "java.math.BigDecimal"); 487 t.put( 488 new QName (WSIFConstants.NS_URI_SOAP_ENC, "int"), 489 "java.lang.Integer"); 490 t.put( 491 new QName (WSIFConstants.NS_URI_SOAP_ENC, "short"), 492 "java.lang.Short"); 493 t.put( 494 new QName (WSIFConstants.NS_URI_SOAP_ENC, "byte"), 495 "java.lang.Byte"); 496 t.put(new QName (WSIFConstants.NS_URI_SOAP_ENC, "base64"), "[B"); 497 498 t.put( 500 new QName (WSIFConstants.NS_URI_APACHE_SOAP, "Map"), 501 "java.util.Map"); 502 t.put( 503 new QName (WSIFConstants.NS_URI_APACHE_SOAP, "Vector"), 504 "java.util.Vector"); 505 t.put( 506 new QName (WSIFConstants.NS_URI_APACHE_SOAP, "Hashtable"), 507 "java.util.Hashtable"); 508 } 509 } 510 511 514 private static void getTypesSchemas(Definition def, List schemas, WSDLLocator loc) throws WSIFException { 515 Types types = def.getTypes(); 516 if (types != null) { 517 Iterator extEleIt = types.getExtensibilityElements().iterator(); 518 519 while (extEleIt.hasNext()) { 520 UnknownExtensibilityElement typesElement = 521 (UnknownExtensibilityElement) extEleIt.next(); 522 523 Element schemaEl = typesElement.getElement(); 524 if (QNameUtils.matches(schema2001, schemaEl) 525 || QNameUtils.matches(schema2000, schemaEl) 526 || QNameUtils.matches(schema1999, schemaEl)) { 527 Schema sc = new Schema(schemaEl); 528 schemas.add(sc); 529 String docBase = def.getDocumentBaseURI(); 530 if (docBase != null && loc != null) { 531 String [] importsAndIncludes = sc.getImportsAndIncludes(); 532 for (int i=0; i<importsAndIncludes.length; i++) { 533 String sl = importsAndIncludes[i]; 534 getImportedSchemas(docBase, sl, loc, schemas); 535 } 536 } 537 } 538 } 539 } 540 541 Map imports = def.getImports(); 542 543 if (imports != null) { 544 Iterator valueIterator = imports.values().iterator(); 545 546 while (valueIterator.hasNext()) { 547 List importList = (List ) valueIterator.next(); 548 549 if (importList != null) { 550 Iterator importIterator = importList.iterator(); 551 552 while (importIterator.hasNext()) { 553 Import tempImport = (Import) importIterator.next(); 554 555 if (tempImport != null) { 556 Definition importedDef = tempImport.getDefinition(); 557 558 if (importedDef != null) { 559 getTypesSchemas(importedDef, schemas, loc); 560 } else { 561 String baseLoc = def.getDocumentBaseURI(); 562 String importLoc = tempImport.getLocationURI(); 563 if (baseLoc != null && importLoc != null && loc != null) { 564 getImportedSchemas(baseLoc, importLoc, loc, schemas); 565 } 566 } 567 } 568 } 569 } 570 } 571 } 572 } 573 574 577 private static void getImportedSchemas(String base, String rel, WSDLLocator loc, List schemaList) throws WSIFException { 578 try { 579 Reader reader = loc.getImportReader(base, rel); 580 if (reader == null) { 581 throw new WSIFException("Unable to read schema file "+rel+" relative to "+base); 582 } 583 InputSource inputSource = new InputSource (reader); 584 DocumentBuilderFactory factory = 585 DocumentBuilderFactory.newInstance(); 586 587 factory.setNamespaceAware(true); 588 factory.setValidating(false); 589 590 DocumentBuilder builder = factory.newDocumentBuilder(); 591 Document doc = builder.parse(inputSource); 592 reader.close(); 593 594 Element el = doc.getDocumentElement(); 595 if (el != null) { 596 if (QNameUtils.matches(schema2001, el) 597 || QNameUtils.matches(schema2000, el) 598 || QNameUtils.matches(schema1999, el)) { 599 Schema sc = new Schema(el); 600 schemaList.add(sc); 601 String [] importsAndIncludes = sc.getImportsAndIncludes(); 602 String lastURI = loc.getLatestImportURI(); 603 for (int i=0; i<importsAndIncludes.length; i++) { 604 String sl = importsAndIncludes[i]; 605 getImportedSchemas(lastURI, sl, loc, schemaList); 606 } 607 } 608 } 609 } catch (Exception e) { 610 Trc.exception(e); 611 if (e instanceof WSIFException) { 612 throw (WSIFException) e; 613 } else { 614 throw new WSIFException("Error when getting imported schemas", e); 615 } 616 } 617 } 618 619 622 private static String resolveClassName(QName qn) { 623 String namespace = qn.getNamespaceURI(); 624 String localPart = qn.getLocalPart(); 625 String packageName = 626 WSIFUtils.getPackageNameFromNamespaceURI(namespace); 627 String className = WSIFUtils.getJavaClassNameFromXMLName(localPart); 628 if (packageName != null 629 && !packageName.equals("") 630 && className != null 631 && !className.equals("")) { 632 return packageName + "." + className; 633 } 634 return null; 635 } 636 637 642 private static String getWrapperClassName(QName qn) { 643 if (qn == null) return null; 644 String ns = qn.getNamespaceURI(); 645 if (WSIFConstants.NS_URI_1999_SCHEMA_XSD.equals(ns) 646 || WSIFConstants.NS_URI_2000_SCHEMA_XSD.equals(ns) 647 || WSIFConstants.NS_URI_2001_SCHEMA_XSD.equals(ns)) { 648 String lp = qn.getLocalPart(); 649 if (lp == null) return null; 650 if (lp.equals("int")) { 651 return "java.lang.Integer"; 652 } else if (lp.equals("long")) { 653 return "java.lang.Long"; 654 } else if (lp.equals("float")) { 655 return "java.lang.Float"; 656 } else if (lp.equals("short")) { 657 return "java.lang.Short"; 658 } else if (lp.equals("double")) { 659 return "java.lang.Double"; 660 } else if (lp.equals("boolean")) { 661 return "java.lang.Boolean"; 662 } else if (lp.equals("byte")) { 663 return "java.lang.Byte"; 664 } 665 } 666 return null; 667 } 668 } 669 | Popular Tags |