1 55 package org.jboss.axis.wsdl.symbolTable; 56 57 import org.jboss.axis.Constants; 58 import org.jboss.axis.utils.JavaUtils; 59 import org.w3c.dom.NamedNodeMap ; 60 import org.w3c.dom.Node ; 61 62 import javax.xml.namespace.QName ; 63 import javax.xml.rpc.holders.BooleanHolder ; 64 import java.util.HashMap ; 65 import java.util.HashSet ; 66 import java.util.Iterator ; 67 import java.util.Map ; 68 import java.util.Vector ; 69 70 76 public class Utils 77 { 78 81 static final Map nsmap = new HashMap (); 82 83 89 static QName findQName(String namespace, String localName) 90 { 91 QName qname = null; 92 93 Map ln2qn = (Map )nsmap.get(namespace); 95 if (null == ln2qn) 96 { ln2qn = new HashMap (); 98 nsmap.put(namespace, ln2qn); 99 100 qname = new QName (namespace, localName); 101 ln2qn.put(localName, qname); 102 } 103 else 104 { qname = (QName )ln2qn.get(localName); 106 if (null == qname) 107 { qname = new QName (namespace, localName); 109 ln2qn.put(localName, qname); 110 } 111 else 112 { 113 } 115 } 116 return qname; 117 } 118 119 131 public static QName getNillableQName(QName qName) 132 { 133 QName rc = qName; 134 if (Constants.isSchemaXSD(rc.getNamespaceURI())) 135 { 136 String localName = rc.getLocalPart(); 137 if (localName.equals("int") || 138 localName.equals("long") || 139 localName.equals("short") || 140 localName.equals("float") || 141 localName.equals("double") || 142 localName.equals("boolean") || 143 localName.equals("byte")) 144 { 145 rc = findQName(Constants.URI_DEFAULT_SOAP_ENC, 146 qName.getLocalPart()); 147 } 148 else if (localName.equals("base64Binary")) 149 { 150 rc = findQName(Constants.URI_DEFAULT_SOAP_ENC, "base64"); 151 } 152 else if (localName.equals("hexBinary")) 153 { 154 rc = findQName(Constants.URI_DEFAULT_SCHEMA_XSD, "hexBinary"); 155 } 156 } 157 return rc; 158 } 159 160 166 public static String getScopedAttribute(Node node, String attr) 167 { 168 if (node == null) 169 { 170 return null; 171 } 172 173 if (node.getAttributes() == null) 174 return getScopedAttribute(node.getParentNode(), attr); 175 176 Node attrNode = node.getAttributes().getNamedItem(attr); 177 if (attrNode != null) 178 { 179 return attrNode.getNodeValue(); 180 } 181 else 182 { 183 return getScopedAttribute(node.getParentNode(), attr); 184 } 185 } 186 187 191 public static String getAttribute(Node node, String attr) 192 { 193 if (node == null || node.getAttributes() == null) 194 { 195 return null; 196 } 197 198 Node attrNode = node.getAttributes().getNamedItem(attr); 199 if (attrNode != null) 200 { 201 return attrNode.getNodeValue(); 202 } 203 else 204 { 205 return null; 206 } 207 } 208 209 213 public static Vector getAttributesWithLocalName(Node node, String localName) 214 { 215 Vector v = new Vector (); 216 if (node == null) 217 { 218 return v; 219 } 220 221 NamedNodeMap map = node.getAttributes(); 222 if (map != null) 223 { 224 for (int i = 0; i < map.getLength(); i++) 225 { 226 Node attrNode = map.item(i); 227 if (attrNode != null && 228 attrNode.getLocalName().equals(localName)) 229 { 230 v.add(attrNode); 231 } 232 } 233 } 234 return v; 235 } 236 237 242 public static QName getNodeQName(Node node) 243 { 244 if (node == null) 245 { 246 return null; 247 } 248 249 String localName = node.getLocalName(); 250 if (localName == null) 251 { 252 return null; 253 } 254 String namespace = node.getNamespaceURI(); 255 256 return (findQName(namespace, localName)); 257 } 258 259 264 public static QName getNodeNameQName(Node node) 265 { 266 if (node == null) 267 { 268 return null; 269 } 270 String localName = null; 271 String namespace = null; 272 273 localName = getAttribute(node, "name"); 275 276 if (localName == null) 278 { 279 QName ref = getTypeQNameFromAttr(node, "ref"); 280 if (ref != null) 281 { 282 localName = ref.getLocalPart(); 283 namespace = ref.getNamespaceURI(); 284 } 285 } 286 287 if (localName == null) 291 { 292 localName = ""; 293 Node search = node.getParentNode(); 294 while (search != null) 295 { 296 QName kind = getNodeQName(search); 297 if (kind.getLocalPart().equals("schema")) 298 { 299 search = null; 300 } 301 else if (kind.getLocalPart().equals("element") || 302 kind.getLocalPart().equals("attribute")) 303 { 304 localName = SymbolTable.ANON_TOKEN + 305 getNodeNameQName(search).getLocalPart(); 306 search = search.getParentNode(); 307 } 308 else if (kind.getLocalPart().equals("complexType") || 309 kind.getLocalPart().equals("simpleType")) 310 { 311 localName = getNodeNameQName(search).getLocalPart() + localName; 312 search = null; 313 } 314 else 315 { 316 search = search.getParentNode(); 317 } 318 } 319 } 320 if (localName == null) 321 return null; 322 323 if (namespace == null) 325 { 326 namespace = getScopedAttribute(node, "targetNamespace"); 327 } 328 return (findQName(namespace, localName)); 329 } 330 331 356 public static QName getTypeQName(Node node, BooleanHolder forElement, boolean ignoreMaxOccurs) 357 { 358 if (node == null) return null; 359 forElement.value = false; 361 QName qName = getTypeQNameFromAttr(node, "type"); 364 365 if (qName == null) 367 { 368 forElement.value = true; 369 qName = getTypeQNameFromAttr(node, "ref"); 370 } 371 372 if (!ignoreMaxOccurs) 378 { 379 if (qName != null) 380 { 381 String maxOccursValue = getAttribute(node, "maxOccurs"); 382 String minOccursValue = getAttribute(node, "minOccurs"); 383 if (maxOccursValue == null) 384 { 385 maxOccursValue = "1"; 386 } 387 if (minOccursValue == null) 388 { 389 minOccursValue = "1"; 390 } 391 if (minOccursValue.equals("0") && maxOccursValue.equals("1")) 392 { 393 qName = getNillableQName(qName); 396 } 397 else if (!maxOccursValue.equals("1") || !minOccursValue.equals("1")) 398 { 399 String localPart = qName.getLocalPart(); 400 localPart += "[" + maxOccursValue + "]"; 401 qName = findQName(qName.getNamespaceURI(), localPart); 402 } 403 } 404 } 405 406 if (qName == null) 408 { 409 forElement.value = true; 410 qName = getTypeQNameFromAttr(node, "element"); 411 } 412 413 if (qName == null) 415 { 416 forElement.value = false; 417 qName = getTypeQNameFromAttr(node, "base"); 418 } 419 return qName; 420 } 421 422 441 private static QName getTypeQNameFromAttr(Node node, String typeAttrName) 442 { 443 if (node == null) 444 { 445 return null; 446 } 447 String prefixedName = getAttribute(node, typeAttrName); 449 450 if (prefixedName == null && 454 typeAttrName.equals("type")) 455 { 456 if (getAttribute(node, "ref") == null && 457 getAttribute(node, "base") == null && 458 getAttribute(node, "element") == null) 459 { 460 461 QName anonQName = SchemaUtils.getElementAnonQName(node); 463 if (anonQName == null) 464 { 465 anonQName = SchemaUtils.getAttributeAnonQName(node); 466 } 467 if (anonQName != null) 468 { 469 return anonQName; 470 } 471 472 QName nodeName = getNodeQName(node); 474 if (nodeName != null && 475 Constants.isSchemaXSD(nodeName.getNamespaceURI()) && 476 (nodeName.getLocalPart().equals("element") || 477 nodeName.getLocalPart().equals("attribute"))) 478 { 479 return Constants.XSD_ANYTYPE; 480 } 481 } 482 } 483 484 if (prefixedName == null) 486 { 487 return null; 488 } 489 QName qName = getQNameFromPrefixedName(node, prefixedName); 491 492 if (typeAttrName.equals("type")) 494 { 495 if (JavaUtils.isTrueExplicitly(getAttribute(node, "nillable"))) 496 { 497 qName = getNillableQName(qName); 498 } 499 } 500 return qName; 501 } 502 503 506 public static QName getQNameFromPrefixedName(Node node, String prefixedName) 507 { 508 509 String localName = prefixedName.substring(prefixedName.lastIndexOf(":") + 1); 510 String namespace = null; 511 if (prefixedName.length() == localName.length()) 513 { 514 namespace = getScopedAttribute(node, "xmlns"); } 516 else 517 { 518 namespace = getScopedAttribute(node, "xmlns:" + prefixedName.substring(0, prefixedName.lastIndexOf(":"))); 519 } 520 return (findQName(namespace, localName)); 521 } 522 523 527 public static HashSet getDerivedTypes(TypeEntry type, SymbolTable symbolTable) 528 { 529 HashSet types = new HashSet (); 530 if (type != null && type.getNode() != null) 531 { 532 getDerivedTypes(type, types, symbolTable); 533 } 534 else if (Constants.isSchemaXSD(type.getQName().getNamespaceURI()) && 535 (type.getQName().getLocalPart().equals("anyType") || 536 type.getQName().getLocalPart().equals("any"))) 537 { 538 types.addAll(symbolTable.getTypeIndex().values()); 540 } 541 return types; 542 } 544 private static void getDerivedTypes(TypeEntry type, HashSet types, SymbolTable symbolTable) 545 { 546 547 if (types.size() == symbolTable.getTypeEntryCount()) 549 { 550 return; 551 } 552 553 for (Iterator it = symbolTable.getTypeIndex().values().iterator(); it.hasNext();) 555 { 556 Type t = (Type)it.next(); 557 if (t instanceof DefinedType && 558 t.getNode() != null && 559 !types.contains(t) && 560 (((DefinedType)t).getComplexTypeExtensionBase(symbolTable) == type)) 561 { 562 types.add(t); 563 getDerivedTypes(t, types, symbolTable); 564 } 565 } 566 } 568 579 public static HashSet getNestedTypes(TypeEntry type, SymbolTable symbolTable, 580 boolean derivedFlag) 581 { 582 HashSet types = new HashSet (); 583 getNestedTypes(type, types, symbolTable, derivedFlag); 584 return types; 585 } 587 private static void getNestedTypes(TypeEntry type, HashSet types, SymbolTable symbolTable, 588 boolean derivedFlag) 589 { 590 591 if (type == null) 592 { 593 return; 594 } 595 596 if (types.size() == symbolTable.getTypeEntryCount()) 598 { 599 return; 600 } 601 602 if (derivedFlag) 604 { 605 HashSet derivedTypes = getDerivedTypes(type, symbolTable); 606 Iterator it = derivedTypes.iterator(); 607 while (it.hasNext()) 608 { 609 TypeEntry derivedType = (TypeEntry)it.next(); 610 if (!types.contains(derivedType)) 611 { 612 types.add(derivedType); 613 getNestedTypes(derivedType, types, symbolTable, derivedFlag); 614 } 615 } 616 } 617 618 if (type.getNode() == null) 620 { 621 return; 622 } 623 Node node = type.getNode(); 624 625 Vector v = SchemaUtils.getContainedElementDeclarations(node, symbolTable); 627 if (v != null) 628 { 629 for (int i = 0; i < v.size(); i++) 630 { 631 ElementDecl elem = (ElementDecl)v.get(i); 632 if (!types.contains(elem.getType())) 633 { 634 types.add(elem.getType()); 635 getNestedTypes(elem.getType(), 636 types, 637 symbolTable, derivedFlag); 638 } 639 } 640 } 641 642 v = SchemaUtils.getContainedAttributeTypes(node, symbolTable); 644 if (v != null) 645 { 646 for (int i = 0; i < v.size(); i += 2) 647 { 648 if (!types.contains(v.get(i))) 649 { 650 types.add(v.get(i)); 651 getNestedTypes(((TypeEntry)v.get(i)), types, symbolTable, derivedFlag); 652 } 653 } 654 } 655 656 if (type.getRefType() != null && 658 !types.contains(type.getRefType())) 659 { 660 types.add(type.getRefType()); 661 getNestedTypes(type.getRefType(), types, symbolTable, derivedFlag); 662 } 663 664 684 685 TypeEntry extendType = SchemaUtils.getComplexElementExtensionBase(node, symbolTable); 687 if (extendType != null) 688 { 689 if (!types.contains(extendType)) 690 { 691 types.add(extendType); 692 getNestedTypes(extendType, types, symbolTable, derivedFlag); 693 } 694 } 695 696 711 712 } 714 723 public static String genQNameAttributeString(QName qname, String prefix) 724 { 725 if (qname.getNamespaceURI() == null || qname.getNamespaceURI().equals("")) 726 return qname.getLocalPart(); 727 728 return prefix + ":" + qname.getLocalPart() + "\" xmlns:" + prefix + 729 "=\"" + qname.getNamespaceURI(); 730 } 731 732 } 733 734 | Popular Tags |