1 16 17 package org.apache.xerces.util; 18 19 import java.util.Hashtable ; 20 21 import org.apache.xerces.dom.AttrImpl; 22 import org.apache.xerces.dom.DocumentImpl; 23 import org.apache.xerces.impl.xs.opti.ElementImpl; 24 25 import org.w3c.dom.Attr ; 26 import org.w3c.dom.Document ; 27 import org.w3c.dom.DOMException ; 28 import org.w3c.dom.Element ; 29 import org.w3c.dom.NamedNodeMap ; 30 import org.w3c.dom.Node ; 31 32 43 public class DOMUtil { 44 45 49 50 protected DOMUtil() {} 51 52 56 63 public static void copyInto(Node src, Node dest) throws DOMException { 64 65 Document factory = dest.getOwnerDocument(); 67 boolean domimpl = factory instanceof DocumentImpl; 68 69 Node start = src; 71 Node parent = src; 72 Node place = src; 73 74 while (place != null) { 76 77 Node node = null; 79 int type = place.getNodeType(); 80 switch (type) { 81 case Node.CDATA_SECTION_NODE: { 82 node = factory.createCDATASection(place.getNodeValue()); 83 break; 84 } 85 case Node.COMMENT_NODE: { 86 node = factory.createComment(place.getNodeValue()); 87 break; 88 } 89 case Node.ELEMENT_NODE: { 90 Element element = factory.createElement(place.getNodeName()); 91 node = element; 92 NamedNodeMap attrs = place.getAttributes(); 93 int attrCount = attrs.getLength(); 94 for (int i = 0; i < attrCount; i++) { 95 Attr attr = (Attr )attrs.item(i); 96 String attrName = attr.getNodeName(); 97 String attrValue = attr.getNodeValue(); 98 element.setAttribute(attrName, attrValue); 99 if (domimpl && !attr.getSpecified()) { 100 ((AttrImpl)element.getAttributeNode(attrName)).setSpecified(false); 101 } 102 } 103 break; 104 } 105 case Node.ENTITY_REFERENCE_NODE: { 106 node = factory.createEntityReference(place.getNodeName()); 107 break; 108 } 109 case Node.PROCESSING_INSTRUCTION_NODE: { 110 node = factory.createProcessingInstruction(place.getNodeName(), 111 place.getNodeValue()); 112 break; 113 } 114 case Node.TEXT_NODE: { 115 node = factory.createTextNode(place.getNodeValue()); 116 break; 117 } 118 default: { 119 throw new IllegalArgumentException ("can't copy node type, "+ 120 type+" ("+ 121 node.getNodeName()+')'); 122 } 123 } 124 dest.appendChild(node); 125 126 if (place.hasChildNodes()) { 128 parent = place; 129 place = place.getFirstChild(); 130 dest = node; 131 } 132 133 else { 135 place = place.getNextSibling(); 136 while (place == null && parent != start) { 137 place = parent.getNextSibling(); 138 parent = parent.getParentNode(); 139 dest = dest.getParentNode(); 140 } 141 } 142 143 } 144 145 } 147 148 public static Element getFirstChildElement(Node parent) { 149 150 Node child = parent.getFirstChild(); 152 while (child != null) { 153 if (child.getNodeType() == Node.ELEMENT_NODE) { 154 return (Element )child; 155 } 156 child = child.getNextSibling(); 157 } 158 159 return null; 161 162 } 164 165 public static Element getFirstVisibleChildElement(Node parent) { 166 167 Node child = parent.getFirstChild(); 169 while (child != null) { 170 if (child.getNodeType() == Node.ELEMENT_NODE && 171 !isHidden(child)) { 172 return (Element )child; 173 } 174 child = child.getNextSibling(); 175 } 176 177 return null; 179 180 } 182 183 public static Element getFirstVisibleChildElement(Node parent, Hashtable hiddenNodes) { 184 185 Node child = parent.getFirstChild(); 187 while (child != null) { 188 if (child.getNodeType() == Node.ELEMENT_NODE && 189 !isHidden(child, hiddenNodes)) { 190 return (Element )child; 191 } 192 child = child.getNextSibling(); 193 } 194 195 return null; 197 198 } 200 203 public static Element getLastChildElement(Node parent) { 204 205 Node child = parent.getLastChild(); 207 while (child != null) { 208 if (child.getNodeType() == Node.ELEMENT_NODE) { 209 return (Element )child; 210 } 211 child = child.getPreviousSibling(); 212 } 213 214 return null; 216 217 } 219 220 public static Element getLastVisibleChildElement(Node parent) { 221 222 Node child = parent.getLastChild(); 224 while (child != null) { 225 if (child.getNodeType() == Node.ELEMENT_NODE && 226 !isHidden(child)) { 227 return (Element )child; 228 } 229 child = child.getPreviousSibling(); 230 } 231 232 return null; 234 235 } 237 240 public static Element getLastVisibleChildElement(Node parent, Hashtable hiddenNodes) { 241 242 Node child = parent.getLastChild(); 244 while (child != null) { 245 if (child.getNodeType() == Node.ELEMENT_NODE && 246 !isHidden(child, hiddenNodes)) { 247 return (Element )child; 248 } 249 child = child.getPreviousSibling(); 250 } 251 252 return null; 254 255 } 257 public static Element getNextSiblingElement(Node node) { 258 259 Node sibling = node.getNextSibling(); 261 while (sibling != null) { 262 if (sibling.getNodeType() == Node.ELEMENT_NODE) { 263 return (Element )sibling; 264 } 265 sibling = sibling.getNextSibling(); 266 } 267 268 return null; 270 271 } 273 public static Element getNextVisibleSiblingElement(Node node) { 275 276 Node sibling = node.getNextSibling(); 278 while (sibling != null) { 279 if (sibling.getNodeType() == Node.ELEMENT_NODE && 280 !isHidden(sibling)) { 281 return (Element )sibling; 282 } 283 sibling = sibling.getNextSibling(); 284 } 285 286 return null; 288 289 } 291 public static Element getNextVisibleSiblingElement(Node node, Hashtable hiddenNodes) { 293 294 Node sibling = node.getNextSibling(); 296 while (sibling != null) { 297 if (sibling.getNodeType() == Node.ELEMENT_NODE && 298 !isHidden(sibling, hiddenNodes)) { 299 return (Element )sibling; 300 } 301 sibling = sibling.getNextSibling(); 302 } 303 304 return null; 306 307 } 309 public static void setHidden(Node node) { 311 if (node instanceof org.apache.xerces.impl.xs.opti.NodeImpl) 312 ((org.apache.xerces.impl.xs.opti.NodeImpl)node).setReadOnly(true, false); 313 else if (node instanceof org.apache.xerces.dom.NodeImpl) 314 ((org.apache.xerces.dom.NodeImpl)node).setReadOnly(true, false); 315 } 317 public static void setHidden(Node node, Hashtable hiddenNodes) { 319 if (node instanceof org.apache.xerces.impl.xs.opti.NodeImpl) 320 ((org.apache.xerces.impl.xs.opti.NodeImpl)node).setReadOnly(true, false); 321 else if (node instanceof org.apache.xerces.dom.NodeImpl) 322 ((org.apache.xerces.dom.NodeImpl)node).setReadOnly(true, false); 323 else 324 hiddenNodes.put(node, ""); 325 } 327 public static void setVisible(Node node) { 329 if (node instanceof org.apache.xerces.impl.xs.opti.NodeImpl) 330 ((org.apache.xerces.impl.xs.opti.NodeImpl)node).setReadOnly(false, false); 331 else if (node instanceof org.apache.xerces.dom.NodeImpl) 332 ((org.apache.xerces.dom.NodeImpl)node).setReadOnly(false, false); 333 } 335 public static void setVisible(Node node, Hashtable hiddenNodes) { 337 if (node instanceof org.apache.xerces.impl.xs.opti.NodeImpl) 338 ((org.apache.xerces.impl.xs.opti.NodeImpl)node).setReadOnly(false, false); 339 else if (node instanceof org.apache.xerces.dom.NodeImpl) 340 ((org.apache.xerces.dom.NodeImpl)node).setReadOnly(false, false); 341 else 342 hiddenNodes.remove(node); 343 } 345 public static boolean isHidden(Node node) { 347 if (node instanceof org.apache.xerces.impl.xs.opti.NodeImpl) 348 return ((org.apache.xerces.impl.xs.opti.NodeImpl)node).getReadOnly(); 349 else if (node instanceof org.apache.xerces.dom.NodeImpl) 350 return ((org.apache.xerces.dom.NodeImpl)node).getReadOnly(); 351 return false; 352 } 354 public static boolean isHidden(Node node, Hashtable hiddenNodes) { 356 if (node instanceof org.apache.xerces.impl.xs.opti.NodeImpl) 357 return ((org.apache.xerces.impl.xs.opti.NodeImpl)node).getReadOnly(); 358 else if (node instanceof org.apache.xerces.dom.NodeImpl) 359 return ((org.apache.xerces.dom.NodeImpl)node).getReadOnly(); 360 else 361 return hiddenNodes.containsKey(node); 362 } 364 365 public static Element getFirstChildElement(Node parent, String elemName) { 366 367 Node child = parent.getFirstChild(); 369 while (child != null) { 370 if (child.getNodeType() == Node.ELEMENT_NODE) { 371 if (child.getNodeName().equals(elemName)) { 372 return (Element )child; 373 } 374 } 375 child = child.getNextSibling(); 376 } 377 378 return null; 380 381 } 383 384 public static Element getLastChildElement(Node parent, String elemName) { 385 386 Node child = parent.getLastChild(); 388 while (child != null) { 389 if (child.getNodeType() == Node.ELEMENT_NODE) { 390 if (child.getNodeName().equals(elemName)) { 391 return (Element )child; 392 } 393 } 394 child = child.getPreviousSibling(); 395 } 396 397 return null; 399 400 } 402 403 public static Element getNextSiblingElement(Node node, String elemName) { 404 405 Node sibling = node.getNextSibling(); 407 while (sibling != null) { 408 if (sibling.getNodeType() == Node.ELEMENT_NODE) { 409 if (sibling.getNodeName().equals(elemName)) { 410 return (Element )sibling; 411 } 412 } 413 sibling = sibling.getNextSibling(); 414 } 415 416 return null; 418 419 } 421 422 public static Element getFirstChildElementNS(Node parent, 423 String uri, String localpart) { 424 425 Node child = parent.getFirstChild(); 427 while (child != null) { 428 if (child.getNodeType() == Node.ELEMENT_NODE) { 429 String childURI = child.getNamespaceURI(); 430 if (childURI != null && childURI.equals(uri) && 431 child.getLocalName().equals(localpart)) { 432 return (Element )child; 433 } 434 } 435 child = child.getNextSibling(); 436 } 437 438 return null; 440 441 } 443 444 public static Element getLastChildElementNS(Node parent, 445 String uri, String localpart) { 446 447 Node child = parent.getLastChild(); 449 while (child != null) { 450 if (child.getNodeType() == Node.ELEMENT_NODE) { 451 String childURI = child.getNamespaceURI(); 452 if (childURI != null && childURI.equals(uri) && 453 child.getLocalName().equals(localpart)) { 454 return (Element )child; 455 } 456 } 457 child = child.getPreviousSibling(); 458 } 459 460 return null; 462 463 } 465 466 public static Element getNextSiblingElementNS(Node node, 467 String uri, String localpart) { 468 469 Node sibling = node.getNextSibling(); 471 while (sibling != null) { 472 if (sibling.getNodeType() == Node.ELEMENT_NODE) { 473 String siblingURI = sibling.getNamespaceURI(); 474 if (siblingURI != null && siblingURI.equals(uri) && 475 sibling.getLocalName().equals(localpart)) { 476 return (Element )sibling; 477 } 478 } 479 sibling = sibling.getNextSibling(); 480 } 481 482 return null; 484 485 } 487 488 public static Element getFirstChildElement(Node parent, String elemNames[]) { 489 490 Node child = parent.getFirstChild(); 492 while (child != null) { 493 if (child.getNodeType() == Node.ELEMENT_NODE) { 494 for (int i = 0; i < elemNames.length; i++) { 495 if (child.getNodeName().equals(elemNames[i])) { 496 return (Element )child; 497 } 498 } 499 } 500 child = child.getNextSibling(); 501 } 502 503 return null; 505 506 } 508 509 public static Element getLastChildElement(Node parent, String elemNames[]) { 510 511 Node child = parent.getLastChild(); 513 while (child != null) { 514 if (child.getNodeType() == Node.ELEMENT_NODE) { 515 for (int i = 0; i < elemNames.length; i++) { 516 if (child.getNodeName().equals(elemNames[i])) { 517 return (Element )child; 518 } 519 } 520 } 521 child = child.getPreviousSibling(); 522 } 523 524 return null; 526 527 } 529 530 public static Element getNextSiblingElement(Node node, String elemNames[]) { 531 532 Node sibling = node.getNextSibling(); 534 while (sibling != null) { 535 if (sibling.getNodeType() == Node.ELEMENT_NODE) { 536 for (int i = 0; i < elemNames.length; i++) { 537 if (sibling.getNodeName().equals(elemNames[i])) { 538 return (Element )sibling; 539 } 540 } 541 } 542 sibling = sibling.getNextSibling(); 543 } 544 545 return null; 547 548 } 550 551 public static Element getFirstChildElementNS(Node parent, 552 String [][] elemNames) { 553 554 Node child = parent.getFirstChild(); 556 while (child != null) { 557 if (child.getNodeType() == Node.ELEMENT_NODE) { 558 for (int i = 0; i < elemNames.length; i++) { 559 String uri = child.getNamespaceURI(); 560 if (uri != null && uri.equals(elemNames[i][0]) && 561 child.getLocalName().equals(elemNames[i][1])) { 562 return (Element )child; 563 } 564 } 565 } 566 child = child.getNextSibling(); 567 } 568 569 return null; 571 572 } 574 575 public static Element getLastChildElementNS(Node parent, 576 String [][] elemNames) { 577 578 Node child = parent.getLastChild(); 580 while (child != null) { 581 if (child.getNodeType() == Node.ELEMENT_NODE) { 582 for (int i = 0; i < elemNames.length; i++) { 583 String uri = child.getNamespaceURI(); 584 if (uri != null && uri.equals(elemNames[i][0]) && 585 child.getLocalName().equals(elemNames[i][1])) { 586 return (Element )child; 587 } 588 } 589 } 590 child = child.getPreviousSibling(); 591 } 592 593 return null; 595 596 } 598 599 public static Element getNextSiblingElementNS(Node node, 600 String [][] elemNames) { 601 602 Node sibling = node.getNextSibling(); 604 while (sibling != null) { 605 if (sibling.getNodeType() == Node.ELEMENT_NODE) { 606 for (int i = 0; i < elemNames.length; i++) { 607 String uri = sibling.getNamespaceURI(); 608 if (uri != null && uri.equals(elemNames[i][0]) && 609 sibling.getLocalName().equals(elemNames[i][1])) { 610 return (Element )sibling; 611 } 612 } 613 } 614 sibling = sibling.getNextSibling(); 615 } 616 617 return null; 619 620 } 622 626 public static Element getFirstChildElement(Node parent, 627 String elemName, 628 String attrName, 629 String attrValue) { 630 631 Node child = parent.getFirstChild(); 633 while (child != null) { 634 if (child.getNodeType() == Node.ELEMENT_NODE) { 635 Element element = (Element )child; 636 if (element.getNodeName().equals(elemName) && 637 element.getAttribute(attrName).equals(attrValue)) { 638 return element; 639 } 640 } 641 child = child.getNextSibling(); 642 } 643 644 return null; 646 647 } 649 653 public static Element getLastChildElement(Node parent, 654 String elemName, 655 String attrName, 656 String attrValue) { 657 658 Node child = parent.getLastChild(); 660 while (child != null) { 661 if (child.getNodeType() == Node.ELEMENT_NODE) { 662 Element element = (Element )child; 663 if (element.getNodeName().equals(elemName) && 664 element.getAttribute(attrName).equals(attrValue)) { 665 return element; 666 } 667 } 668 child = child.getPreviousSibling(); 669 } 670 671 return null; 673 674 } 676 681 public static Element getNextSiblingElement(Node node, 682 String elemName, 683 String attrName, 684 String attrValue) { 685 686 Node sibling = node.getNextSibling(); 688 while (sibling != null) { 689 if (sibling.getNodeType() == Node.ELEMENT_NODE) { 690 Element element = (Element )sibling; 691 if (element.getNodeName().equals(elemName) && 692 element.getAttribute(attrName).equals(attrValue)) { 693 return element; 694 } 695 } 696 sibling = sibling.getNextSibling(); 697 } 698 699 return null; 701 702 } 704 713 public static String getChildText(Node node) { 714 715 if (node == null) { 717 return null; 718 } 719 720 StringBuffer str = new StringBuffer (); 722 Node child = node.getFirstChild(); 723 while (child != null) { 724 short type = child.getNodeType(); 725 if (type == Node.TEXT_NODE) { 726 str.append(child.getNodeValue()); 727 } 728 else if (type == Node.CDATA_SECTION_NODE) { 729 str.append(getChildText(child)); 730 } 731 child = child.getNextSibling(); 732 } 733 734 return str.toString(); 736 737 } 739 public static String getName(Node node) { 741 return node.getNodeName(); 742 } 744 747 public static String getLocalName(Node node) { 748 String name = node.getLocalName(); 749 return (name!=null)? name:node.getNodeName(); 750 } 752 public static Element getParent(Element elem) { 753 Node parent = elem.getParentNode(); 754 if (parent instanceof Element ) 755 return (Element )parent; 756 return null; 757 } 759 public static Document getDocument(Node node) { 761 return node.getOwnerDocument(); 762 } 764 public static Element getRoot(Document doc) { 766 return doc.getDocumentElement(); 767 } 769 771 public static Attr getAttr(Element elem, String name) { 773 return elem.getAttributeNode(name); 774 } 776 public static Attr getAttrNS(Element elem, String nsUri, 778 String localName) { 779 return elem.getAttributeNodeNS(nsUri, localName); 780 } 782 public static Attr [] getAttrs(Element elem) { 784 NamedNodeMap attrMap = elem.getAttributes(); 785 Attr [] attrArray = new Attr [attrMap.getLength()]; 786 for (int i=0; i<attrMap.getLength(); i++) 787 attrArray[i] = (Attr )attrMap.item(i); 788 return attrArray; 789 } 791 public static String getValue(Attr attribute) { 793 return attribute.getValue(); 794 } 796 801 public static String getAttrValue(Element elem, String name) { 804 return elem.getAttribute(name); 805 } 807 public static String getAttrValueNS(Element elem, String nsUri, 810 String localName) { 811 return elem.getAttributeNS(nsUri, localName); 812 } 814 public static String getPrefix(Node node) { 816 return node.getPrefix(); 817 } 818 819 public static String getNamespaceURI(Node node) { 821 return node.getNamespaceURI(); 822 } 823 824 public static String getSyntheticAnnotation(Node node) { 826 if(node instanceof ElementImpl) { 827 return ((ElementImpl)node).getSyntheticAnnotation(); 828 } 829 return null; 830 } 831 } | Popular Tags |