1 16 package org.apache.axis.wsdl.symbolTable; 17 18 import org.apache.axis.Constants; 19 import org.apache.axis.utils.XMLUtils; 20 import org.w3c.dom.NamedNodeMap ; 21 import org.w3c.dom.Node ; 22 23 import javax.xml.namespace.QName ; 24 import javax.xml.rpc.holders.BooleanHolder ; 25 import java.util.*; 26 27 33 public class Utils { 34 35 36 static final Map nsmap = new HashMap(); 37 38 45 static QName findQName(String namespace, String localName) { 46 47 QName qname = null; 48 49 Map ln2qn = (Map ) nsmap.get(namespace); 51 52 if (null == ln2qn) { ln2qn = new HashMap(); 54 55 nsmap.put(namespace, ln2qn); 56 57 qname = new QName (namespace, localName); 58 59 ln2qn.put(localName, qname); 60 } else { qname = (QName ) ln2qn.get(localName); 62 63 if (null == qname) { qname = new QName (namespace, localName); 65 66 ln2qn.put(localName, qname); 67 } else { 68 69 } 71 } 72 73 return qname; 74 } 75 76 86 public static String getScopedAttribute(Node node, String attr) { 87 88 if (node == null) { 89 return null; 90 } 91 92 if (node.getAttributes() == null) { 93 return getScopedAttribute(node.getParentNode(), attr); 94 } 95 96 Node attrNode = node.getAttributes().getNamedItem(attr); 97 98 if (attrNode != null) { 99 return attrNode.getNodeValue(); 100 } else { 101 return getScopedAttribute(node.getParentNode(), attr); 102 } 103 } 104 105 113 public static String getAttribute(Node node, String attr) { 114 115 if ((node == null) || (node.getAttributes() == null)) { 116 return null; 117 } 118 119 Node attrNode = node.getAttributes().getNamedItem(attr); 120 121 if (attrNode != null) { 122 return attrNode.getNodeValue(); 123 } else { 124 return null; 125 } 126 } 127 128 136 public static Vector getAttributesWithLocalName(Node node, 137 String localName) { 138 139 Vector v = new Vector(); 140 141 if (node == null) { 142 return v; 143 } 144 145 NamedNodeMap map = node.getAttributes(); 146 147 if (map != null) { 148 for (int i = 0; i < map.getLength(); i++) { 149 Node attrNode = map.item(i); 150 151 if ((attrNode != null) 152 && attrNode.getLocalName().equals(localName)) { 153 v.add(attrNode); 154 } 155 } 156 } 157 158 return v; 159 } 160 161 169 public static QName getNodeQName(Node node) { 170 171 if (node == null) { 172 return null; 173 } 174 175 String localName = node.getLocalName(); 176 177 if (localName == null) { 178 return null; 179 } 180 181 String namespace = node.getNamespaceURI(); 182 183 return (findQName(namespace, localName)); 184 } 185 186 194 public static QName getNodeNameQName(Node node) { 195 196 if (node == null) { 197 return null; 198 } 199 200 String localName = null; 201 String namespace = null; 202 203 localName = getAttribute(node, "name"); 205 206 if (localName == null) { 208 QName ref = getTypeQNameFromAttr(node, "ref"); 209 210 if (ref != null) { 211 localName = ref.getLocalPart(); 212 namespace = ref.getNamespaceURI(); 213 } 214 } 215 216 220 Node search = node.getParentNode(); 221 222 while (search != null) { 223 String ln = search.getLocalName(); 224 225 if (ln.equals("schema")) { 226 search = null; 227 } else if (ln.equals("element") 228 || ln.equals("attribute")) { 229 localName = SymbolTable.ANON_TOKEN 230 + getNodeNameQName(search).getLocalPart(); 231 search = null; 232 } else if (ln.equals("complexType") 233 || ln.equals("simpleType")) { 234 localName = getNodeNameQName(search).getLocalPart() 235 + SymbolTable.ANON_TOKEN + localName; 236 search = null; 237 } else { 238 search = search.getParentNode(); 239 } 240 } 241 242 if (localName == null) { 243 return null; 244 } 245 246 if (namespace == null) { 248 namespace = getScopedAttribute(node, "targetNamespace"); 249 } 250 251 return (findQName(namespace, localName)); 252 } 253 254 279 public static QName getTypeQName(Node node, BooleanHolder forElement, 280 boolean ignoreMaxOccurs) { 281 282 if (node == null) { 283 return null; 284 } 285 286 forElement.value = false; 288 QName qName = getTypeQNameFromAttr(node, "type"); 291 292 if (qName == null) { 294 String localName = node.getLocalName(); 295 296 if ((localName != null) 299 && !(localName.equals("attributeGroup") || 300 localName.equals("group") || 301 localName.equals("list"))) { 302 forElement.value = true; 303 } 304 305 qName = getTypeQNameFromAttr(node, "ref"); 306 } 307 308 if (qName == null) { 310 qName = getTypeQNameFromAttr(node, "itemType"); 311 } 312 313 if (!ignoreMaxOccurs) { 319 if (qName != null) { 320 String maxOccursValue = getAttribute(node, "maxOccurs"); 321 String minOccursValue = getAttribute(node, "minOccurs"); 322 323 if (maxOccursValue == null) { 324 maxOccursValue = "1"; 325 } 326 327 if (minOccursValue == null) { 328 minOccursValue = "1"; 329 } 330 331 if (minOccursValue.equals("0") && maxOccursValue.equals("1")) { 332 333 } else if (!maxOccursValue.equals("1") 337 || !minOccursValue.equals("1")) { 338 String localPart = qName.getLocalPart(); 339 String range = "["; 340 if (!minOccursValue.equals("1")) { 341 range += minOccursValue; 342 } 343 range += ","; 344 if (!maxOccursValue.equals("1")) { 345 range += maxOccursValue; 346 } 347 range += "]"; 348 localPart += range; 349 qName = findQName(qName.getNamespaceURI(), localPart); 350 } 351 } 352 } 353 354 if (qName == null) { 356 forElement.value = true; 357 qName = getTypeQNameFromAttr(node, "element"); 358 } 359 360 if (qName == null) { 362 forElement.value = false; 363 qName = getTypeQNameFromAttr(node, "base"); 364 } 365 366 return qName; 367 } 368 369 375 public static QName [] getMemberTypeQNames(Node node) { 376 377 String attribute = getAttribute(node, "memberTypes"); 378 379 if (attribute == null) { 380 return null; 381 } 382 383 StringTokenizer tokenizer = new StringTokenizer(attribute, " "); 384 QName [] memberTypes = new QName [tokenizer.countTokens()]; 385 386 for (int i = 0; tokenizer.hasMoreElements(); i++) { 387 String element = (String ) tokenizer.nextElement(); 388 389 memberTypes[i] = XMLUtils.getFullQNameFromString(element, node); 390 } 391 392 return memberTypes; 393 } 394 395 415 private static QName getTypeQNameFromAttr(Node node, String typeAttrName) { 416 417 if (node == null) { 418 return null; 419 } 420 421 String prefixedName = getAttribute(node, typeAttrName); 423 424 if ((prefixedName == null) && typeAttrName.equals("type")) { 428 if ((getAttribute(node, "ref") == null) 429 && (getAttribute(node, "base") == null) 430 && (getAttribute(node, "element") == null)) { 431 432 QName anonQName = SchemaUtils.getElementAnonQName(node); 434 435 if (anonQName == null) { 436 anonQName = SchemaUtils.getAttributeAnonQName(node); 437 } 438 439 if (anonQName != null) { 440 return anonQName; 441 } 442 443 String localName = node.getLocalName(); 445 446 if ((localName != null) 447 && Constants.isSchemaXSD(node.getNamespaceURI()) 448 && (localName.equals("element") || 449 localName.equals("attribute"))) { 450 return Constants.XSD_ANYTYPE; 451 } 452 } 453 } 454 455 if (prefixedName == null) { 457 return null; 458 } 459 460 QName qName = getQNameFromPrefixedName(node, prefixedName); 462 463 470 return qName; 471 } 472 473 480 public static QName getQNameFromPrefixedName(Node node, 481 String prefixedName) { 482 483 String localName = prefixedName.substring(prefixedName.lastIndexOf(":") 484 + 1); 485 String namespace = null; 486 487 if (prefixedName.length() == localName.length()) { 489 namespace = getScopedAttribute( 490 node, "xmlns"); } else { 492 namespace = getScopedAttribute(node, 493 "xmlns:" 494 + prefixedName.substring(0, 495 prefixedName.lastIndexOf(":"))); 496 } 497 498 return (findQName(namespace, localName)); 499 } 500 501 509 public static HashSet getDerivedTypes(TypeEntry type, 510 SymbolTable symbolTable) { 511 512 HashSet types = (HashSet)symbolTable.derivedTypes.get(type); 513 514 if (types != null) { 515 return types; 516 } 517 518 types = new HashSet(); 519 520 symbolTable.derivedTypes.put(type, types); 521 522 if ((type != null) && (type.getNode() != null)) { 523 getDerivedTypes(type, types, symbolTable); 524 } 525 else if (type != null && Constants.isSchemaXSD(type.getQName().getNamespaceURI()) 526 && (type.getQName().getLocalPart().equals("anyType") 527 || type.getQName().getLocalPart().equals("any"))) { 528 529 final Collection typeValues = symbolTable.getTypeIndex().values(); 531 for (Iterator it = typeValues.iterator(); it.hasNext();) { 532 SymTabEntry e = (SymTabEntry) it.next(); 533 if (! e.getQName().getLocalPart().startsWith(SymbolTable.ANON_TOKEN)) 534 types.add(e); 535 } 536 } 537 538 return types; 539 } 541 548 private static void getDerivedTypes(TypeEntry type, HashSet types, 549 SymbolTable symbolTable) { 550 551 if (types.size() == symbolTable.getTypeEntryCount()) { 553 return; 554 } 555 556 for (Iterator it = symbolTable.getTypeIndex().values().iterator(); 558 it.hasNext();) { 559 Type t = (Type) it.next(); 560 561 if ((t instanceof DefinedType) && (t.getNode() != null) 562 && !types.contains(t) 563 && (((DefinedType) t).getComplexTypeExtensionBase(symbolTable) 564 == type)) { 565 types.add(t); 566 getDerivedTypes(t, types, symbolTable); 567 } 568 } 569 } 571 583 protected static HashSet getNestedTypes(TypeEntry type, 584 SymbolTable symbolTable, 585 boolean derivedFlag) { 586 587 HashSet types = new HashSet(); 588 589 getNestedTypes(type, types, symbolTable, derivedFlag); 590 591 return types; 592 } 594 602 private static void getNestedTypes(TypeEntry type, HashSet types, 603 SymbolTable symbolTable, 604 boolean derivedFlag) { 605 606 if (type == null) { 607 return; 608 } 609 610 if (types.size() == symbolTable.getTypeEntryCount()) { 612 return; 613 } 614 615 if (derivedFlag) { 617 HashSet derivedTypes = getDerivedTypes(type, symbolTable); 618 Iterator it = derivedTypes.iterator(); 619 620 while (it.hasNext()) { 621 TypeEntry derivedType = (TypeEntry) it.next(); 622 623 if (!types.contains(derivedType)) { 624 types.add(derivedType); 625 getNestedTypes(derivedType, types, symbolTable, 626 derivedFlag); 627 } 628 } 629 } 630 631 if (type.getNode() == null) { 633 return; 634 } 635 636 Node node = type.getNode(); 637 638 Vector v = SchemaUtils.getContainedElementDeclarations(node, 640 symbolTable); 641 642 if (v != null) { 643 for (int i = 0; i < v.size(); i++) { 644 ElementDecl elem = (ElementDecl) v.get(i); 645 646 if (!types.contains(elem.getType())) { 647 types.add(elem.getType()); 648 getNestedTypes(elem.getType(), types, symbolTable, 649 derivedFlag); 650 } 651 } 652 } 653 654 v = SchemaUtils.getContainedAttributeTypes(node, symbolTable); 656 657 if (v != null) { 658 for (int i = 0; i < v.size(); i++) { 659 ContainedAttribute attr = (ContainedAttribute) v.get(i); 660 TypeEntry te = attr.getType(); 661 if (!types.contains(te)) { 662 types.add(te); 663 getNestedTypes(te, types, symbolTable, derivedFlag); 664 } 665 } 666 } 667 668 if ((type.getRefType() != null) && !types.contains(type.getRefType())) { 670 types.add(type.getRefType()); 671 getNestedTypes(type.getRefType(), types, symbolTable, derivedFlag); 672 } 673 674 695 696 TypeEntry extendType = SchemaUtils.getComplexElementExtensionBase(node, 698 symbolTable); 699 700 if (extendType != null) { 701 if (!types.contains(extendType)) { 702 types.add(extendType); 703 getNestedTypes(extendType, types, symbolTable, derivedFlag); 704 } 705 } 706 707 723 } 725 738 public static String genQNameAttributeString(QName qname, String prefix) { 739 740 if ((qname.getNamespaceURI() == null) 741 || qname.getNamespaceURI().equals("")) { 742 return qname.getLocalPart(); 743 } 744 745 return prefix + ":" + qname.getLocalPart() + "\" xmlns:" + prefix 746 + "=\"" + qname.getNamespaceURI(); 747 } 748 749 public static String genQNameAttributeStringWithLastLocalPart(QName qname, String prefix) { 750 String lastLocalPart = getLastLocalPart(qname.getLocalPart()); 751 if ((qname.getNamespaceURI() == null) 752 || qname.getNamespaceURI().equals("")) { 753 return lastLocalPart; 754 } 755 756 return prefix + ":" + lastLocalPart + "\" xmlns:" + prefix 757 + "=\"" + qname.getNamespaceURI(); 758 } 759 760 public static String getLastLocalPart(String localPart) { 761 int anonymousDelimitorIndex = localPart.lastIndexOf('>'); 762 if (anonymousDelimitorIndex > -1 && anonymousDelimitorIndex < localPart.length()-1) { 763 localPart = localPart.substring(anonymousDelimitorIndex + 1); 764 } 765 return localPart; 766 767 } 768 } 769 | Popular Tags |