| 1 16 package org.apache.axis.wsdl.symbolTable; 17 18 import org.apache.axis.Constants; 19 import org.apache.axis.AxisProperties; 20 import org.apache.axis.utils.JavaUtils; 21 import org.w3c.dom.DOMException ; 22 import org.w3c.dom.Element ; 23 import org.w3c.dom.Node ; 24 import org.w3c.dom.NodeList ; 25 26 import javax.xml.namespace.QName ; 27 import javax.xml.rpc.holders.BooleanHolder ; 28 import javax.xml.rpc.holders.IntHolder ; 29 import javax.xml.rpc.holders.BooleanHolder ; 30 import javax.xml.rpc.holders.QNameHolder ; 31 import java.util.Arrays ; 32 import java.util.HashSet ; 33 import java.util.Set ; 34 import java.util.StringTokenizer ; 35 import java.util.Vector ; 36 37 42 public class SchemaUtils { 43 44 45 static final QName VALUE_QNAME = Utils.findQName("", "_value"); 46 47 51 public static boolean isMixed(Node node) { 52 if (isXSDNode(node, "complexType")) { 54 String mixed = ((Element)node).getAttribute("mixed"); 55 if (mixed != null && mixed.length() > 0) { 56 return ("true".equalsIgnoreCase(mixed) || 57 "1".equals(mixed)); 58 } 59 NodeList children = node.getChildNodes(); 62 63 for (int j = 0; j < children.getLength(); j++) { 64 Node kid = children.item(j); 65 if (isXSDNode(kid, "complexContent")) { 66 mixed = ((Element)kid).getAttribute("mixed"); 67 return ("true".equalsIgnoreCase(mixed) || 68 "1".equals(mixed)); 69 } 70 } 71 } 72 return false; 73 } 74 75 public static Node getUnionNode(Node node) { 76 if (isXSDNode(node, "simpleType")) { 78 NodeList children = node.getChildNodes(); 80 for (int j = 0; j < children.getLength(); j++) { 81 Node kid = children.item(j); 82 if (isXSDNode(kid, "union")) { 83 return kid; 84 } 85 } 86 } 87 return null; 88 } 89 90 public static Node getListNode(Node node) { 91 if (isXSDNode(node, "simpleType")) { 93 NodeList children = node.getChildNodes(); 95 for (int j = 0; j < children.getLength(); j++) { 96 Node kid = children.item(j); 97 if (isXSDNode(kid, "list")) { 98 return kid; 99 } 100 } 101 } 102 return null; 103 } 104 105 public static boolean isSimpleTypeWithUnion(Node node) { 106 return (getUnionNode(node) != null); 107 } 108 109 119 public static boolean isWrappedType(Node node) { 120 121 if (node == null) { 122 return false; 123 } 124 125 if (isXSDNode(node, "element")) { 127 NodeList children = node.getChildNodes(); 128 boolean hasComplexType = false; 129 for (int j = 0; j < children.getLength(); j++) { 130 Node kid = children.item(j); 131 if (isXSDNode(kid, "complexType")) { 132 node = kid; 133 hasComplexType = true; 134 break; 135 } 136 } 137 if (!hasComplexType) { 138 return false; 139 } 140 } 141 142 if (isXSDNode(node, "complexType")) { 144 148 NodeList children = node.getChildNodes(); 149 150 for (int j = 0; j < children.getLength(); j++) { 151 Node kid = children.item(j); 152 153 if (isXSDNode(kid, "complexContent")) { 154 return false; 155 } else if (isXSDNode(kid, "simpleContent")) { 156 return false; 157 } 158 } 159 160 children = node.getChildNodes(); 165 int len = children.getLength(); 166 for (int j = 0; j < len; j++) { 167 Node kid = children.item(j); 168 String localName = kid.getLocalName(); 169 if (localName != null && 170 Constants.isSchemaXSD(kid.getNamespaceURI())) { 171 if (localName.equals("sequence")) { 172 Node sequenceNode = kid; 173 NodeList sequenceChildren = sequenceNode.getChildNodes(); 174 int sequenceLen = sequenceChildren.getLength(); 175 for (int k = 0; k < sequenceLen; k++) { 176 Node sequenceKid = sequenceChildren.item(k); 177 String sequenceLocalName = sequenceKid.getLocalName(); 178 if (sequenceLocalName != null && 179 Constants.isSchemaXSD(sequenceKid.getNamespaceURI())) { 180 if (sequenceLocalName.equals("choice")) { 182 Node choiceNode = sequenceKid; 183 NodeList choiceChildren = choiceNode.getChildNodes(); 184 int choiceLen = choiceChildren.getLength(); 185 for (int l = 0; l < choiceLen; l++) { 186 Node choiceKid = choiceChildren.item(l); 187 String choiceLocalName = choiceKid.getLocalName(); 188 if (choiceLocalName != null && 189 Constants.isSchemaXSD(choiceKid.getNamespaceURI())) { 190 if (!choiceLocalName.equals("element")) { 191 return false; 192 } 193 } 194 } 195 } 196 else 197 if (!sequenceLocalName.equals("element")) { 198 return false; 199 } 200 } 201 } 202 return true; 203 } else { 204 return false; 205 } 206 } 207 } 208 } 209 return true; 211 } 212 213 230 public static Vector getContainedElementDeclarations(Node node, 231 SymbolTable symbolTable) { 232 233 if (node == null) { 234 return null; 235 } 236 237 if (isXSDNode(node, "element")) { 239 NodeList children = node.getChildNodes(); 240 241 for (int j = 0; j < children.getLength(); j++) { 242 Node kid = children.item(j); 243 244 if (isXSDNode(kid, "complexType")) { 245 node = kid; 246 247 break; 248 } 249 } 250 } 251 252 if (isXSDNode(node, "complexType")) { 254 255 NodeList children = node.getChildNodes(); 258 Node complexContent = null; 259 Node simpleContent = null; 260 Node extension = null; 261 262 for (int j = 0; j < children.getLength(); j++) { 263 Node kid = children.item(j); 264 265 if (isXSDNode(kid, "complexContent")) { 266 complexContent = kid; 267 268 break; } else if (isXSDNode(kid, "simpleContent")) { 270 simpleContent = kid; 271 } 272 } 273 274 if (complexContent != null) { 275 children = complexContent.getChildNodes(); 276 277 for (int j = 0; 278 (j < children.getLength()) && (extension == null); 279 j++) { 280 Node kid = children.item(j); 281 282 if (isXSDNode(kid, "extension") 283 || isXSDNode(kid, "restriction")) { 284 extension = kid; 285 } 286 } 287 } 288 289 if (simpleContent != null) { 290 children = simpleContent.getChildNodes(); 291 292 int len = children.getLength(); 293 for (int j = 0; 294 (j < len) && (extension == null); 295 j++) { 296 Node kid = children.item(j); 297 String localName = kid.getLocalName(); 298 299 if ((localName != null) 300 && (localName.equals("extension") || localName.equals("restriction")) 301 && Constants.isSchemaXSD(kid.getNamespaceURI())) { 302 303 QName extendsOrRestrictsType = 305 Utils.getTypeQName(children.item(j), 306 new BooleanHolder (), false); 307 308 Vector v = new Vector (); 311 ElementDecl elem = new ElementDecl(symbolTable.getTypeEntry(extendsOrRestrictsType, false), VALUE_QNAME); 312 v.add(elem); 313 314 return v; 315 } 316 } 317 } 318 319 if (extension != null) { 320 node = extension; } 322 323 children = node.getChildNodes(); 326 327 Vector v = new Vector (); 328 int len = children.getLength(); 329 for (int j = 0; j < len; j++) { 330 Node kid = children.item(j); 331 String localName = kid.getLocalName(); 332 if (localName != null && 333 Constants.isSchemaXSD(kid.getNamespaceURI())) { 334 if (localName.equals("sequence")) { 335 v.addAll(processSequenceNode(kid, symbolTable)); 336 } else if (localName.equals("all")) { 337 v.addAll(processAllNode(kid, symbolTable)); 338 } else if (localName.equals("choice")) { 339 v.addAll(processChoiceNode(kid, symbolTable)); 340 } else if (localName.equals("group")) { 341 v.addAll(processGroupNode(kid, symbolTable)); 342 } 343 } 344 } 345 346 return v; 347 } else if (isXSDNode(node, "group")) { 348 373 return null; 374 } else { 375 376 QName [] simpleQName = getContainedSimpleTypes(node); 378 379 if (simpleQName != null) { 380 Vector v = null; 381 382 for (int i = 0; i < simpleQName.length; i++) { 383 384 Type simpleType = symbolTable.getType(simpleQName[i]); 385 386 if (simpleType != null) { 387 if (v == null) { 388 v = new Vector (); 389 } 390 391 QName qname = null; 392 if (simpleQName.length > 1) { 393 qname = new QName ("", simpleQName[i].getLocalPart() + "Value"); 394 } else { 395 qname = new QName ("", "value"); 396 } 397 398 v.add(new ElementDecl(simpleType, qname)); 399 } 400 } 401 402 return v; 403 } 404 } 405 406 return null; 407 } 408 409 417 private static Vector processChoiceNode(Node choiceNode, 418 SymbolTable symbolTable) { 419 420 Vector v = new Vector (); 421 NodeList children = choiceNode.getChildNodes(); 422 int len = children.getLength(); 423 for (int j = 0; j < len; j++) { 424 Node kid = children.item(j); 425 String localName = kid.getLocalName(); 426 if (localName != null && 427 Constants.isSchemaXSD(kid.getNamespaceURI())) { 428 if (localName.equals("choice")) { 429 v.addAll(processChoiceNode(kid, symbolTable)); 430 } else if (localName.equals("sequence")) { 431 v.addAll(processSequenceNode(kid, symbolTable)); 432 } else if (localName.equals("group")) { 433 v.addAll(processGroupNode(kid, symbolTable)); 434 } else if (localName.equals("element")) { 435 ElementDecl elem = processChildElementNode(kid, 436 symbolTable); 437 438 if (elem != null) { 439 elem.setMinOccursIs0(true); 442 443 v.add(elem); 444 } 445 } else if (localName.equals("any")) { 446 Type type = symbolTable.getType(Constants.XSD_ANY); 450 ElementDecl elem = new ElementDecl(type, 451 Utils.findQName("", 452 "any")); 453 454 elem.setAnyElement(true); 455 v.add(elem); 456 } 457 } 458 } 459 460 return v; 461 } 462 463 469 private static Node getChildByName(Node parentNode, String name) throws DOMException { 470 if (parentNode == null) return null; 471 NodeList children = parentNode.getChildNodes(); 472 if (children != null) { 473 for (int i = 0; i < children.getLength(); i++) { 474 Node child = children.item(i); 475 if (child != null) { 476 if (child.getNodeName() != null && name.equals(child.getNodeName())) { 477 return child; 478 } 479 } 480 } 481 } 482 return null; 483 } 484 485 492 public static String getTextByPath(Node root, String path) throws DOMException { 493 StringTokenizer st = new StringTokenizer (path, "/"); 494 Node node = root; 495 while (st.hasMoreTokens()) { 496 String elementName = st.nextToken(); 497 Node child = getChildByName(node, elementName); 498 if (child == null) 499 throw new DOMException (DOMException.NOT_FOUND_ERR, "could not find " + elementName); 500 node = child; 501 } 502 503 String text = ""; 505 NodeList children = node.getChildNodes(); 506 if (children != null) { 507 for (int i = 0; i < children.getLength(); i++) { 508 Node child = children.item(i); 509 if (child != null) { 510 if (child.getNodeName() != null 511 && (child.getNodeName().equals("#text") 512 || child.getNodeName().equals("#cdata-section"))) { 513 text += child.getNodeValue(); 514 } 515 } 516 } 517 } 518 return text; 519 } 520 521 528 public static String getAnnotationDocumentation(Node typeNode) { 529 Node annotationNode = typeNode.getFirstChild(); 530 while (annotationNode != null) { 531 if (isXSDNode(annotationNode, "annotation")) { 532 break; 533 } 534 annotationNode = annotationNode.getNextSibling(); 535 } 536 Node documentationNode; 537 if (annotationNode != null) { 538 documentationNode = annotationNode.getFirstChild(); 539 while (documentationNode != null) { 540 if (isXSDNode(documentationNode, "documentation")) { 541 break; 542 } 543 documentationNode = documentationNode.getNextSibling(); 544 } 545 } else { 546 documentationNode = null; 547 } 548 549 String text = ""; 551 if (documentationNode != null) { 552 NodeList children = documentationNode.getChildNodes(); 553 if (children != null) { 554 for (int i = 0; i < children.getLength(); i++) { 555 Node child = children.item(i); 556 if (child != null) { 557 if (child.getNodeName() != null 558 && (child.getNodeName().equals("#text") 559 || child.getNodeName().equals("#cdata-section"))) { 560 text += child.getNodeValue(); 561 } 562 } 563 } 564 } 565 } 566 return text; 567 } 568 569 577 private static Vector processSequenceNode(Node sequenceNode, 578 SymbolTable symbolTable) { 579 580 Vector v = new Vector (); 581 NodeList children = sequenceNode.getChildNodes(); 582 int len = children.getLength(); 583 for (int j = 0; j < len; j++) { 584 Node kid = children.item(j); 585 String localName = kid.getLocalName(); 586 587 if (localName != null && 588 Constants.isSchemaXSD(kid.getNamespaceURI())) { 589 if (localName.equals("choice")) { 590 v.addAll(processChoiceNode(kid, symbolTable)); 591 } else if (localName.equals("sequence")) { 592 v.addAll(processSequenceNode(kid, symbolTable)); 593 } else if (localName.equals("group")) { 594 v.addAll(processGroupNode(kid, symbolTable)); 595 } else if (localName.equals("any")) { 596 Type type = symbolTable.getType(Constants.XSD_ANY); 600 ElementDecl elem = new ElementDecl(type, 601 Utils.findQName("", 602 "any")); 603 604 elem.setAnyElement(true); 605 v.add(elem); 606 } else if (localName.equals("element")) { 607 ElementDecl elem = processChildElementNode(kid, 608 symbolTable); 609 610 if (elem != null) { 611 v.add(elem); 612 } 613 } 614 } 615 } 616 617 return v; 618 } 619 620 629 private static Vector processGroupNode(Node groupNode, 630 SymbolTable symbolTable) { 631 632 Vector v = new Vector (); 633 if (groupNode.getAttributes().getNamedItem("ref") == null) { 634 NodeList children = groupNode.getChildNodes(); 635 int len = children.getLength(); 636 for (int j = 0; j < len; j++) { 637 Node kid = children.item(j); 638 String localName = kid.getLocalName(); 639 if (localName != null && 640 Constants.isSchemaXSD(kid.getNamespaceURI())) { 641 if (localName.equals("choice")) { 642 v.addAll(processChoiceNode(kid, symbolTable)); 643 } else if (localName.equals("sequence")) { 644 v.addAll(processSequenceNode(kid, symbolTable)); 645 } else if (localName.equals("all")) { 646 v.addAll(processAllNode(kid, symbolTable)); 647 } 648 } 649 } 650 } else { 651 QName nodeName = Utils.getNodeNameQName(groupNode); 652 QName nodeType = Utils.getTypeQName(groupNode, new BooleanHolder (), false); 653 Type type = (Type) symbolTable.getTypeEntry(nodeType, false); 657 658 if (type != null && type.getNode() != null) { 659 Node node = type.getNode(); 661 NodeList children = node.getChildNodes(); 662 for (int j = 0; j < children.getLength(); j++) { 663 QName subNodeKind = Utils.getNodeQName(children.item(j)); 664 if ((subNodeKind != null) 665 && Constants.isSchemaXSD( 666 subNodeKind.getNamespaceURI())) { 667 &nb
|