1 57 58 package com.sun.org.apache.xerces.internal.util; 59 60 import com.sun.org.apache.xerces.internal.dom.AttrImpl; 61 import com.sun.org.apache.xerces.internal.dom.DocumentImpl; 62 63 import org.w3c.dom.Attr ; 64 import org.w3c.dom.Document ; 65 import org.w3c.dom.DOMException ; 66 import org.w3c.dom.Element ; 67 import org.w3c.dom.NamedNodeMap ; 68 import org.w3c.dom.Node ; 69 70 81 public class DOMUtil { 82 83 87 88 protected DOMUtil() {} 89 90 94 101 public static void copyInto(Node src, Node dest) throws DOMException { 102 103 Document factory = dest.getOwnerDocument(); 105 boolean domimpl = factory instanceof DocumentImpl; 106 107 Node start = src; 109 Node parent = src; 110 Node place = src; 111 112 while (place != null) { 114 115 Node node = null; 117 int type = place.getNodeType(); 118 switch (type) { 119 case Node.CDATA_SECTION_NODE: { 120 node = factory.createCDATASection(place.getNodeValue()); 121 break; 122 } 123 case Node.COMMENT_NODE: { 124 node = factory.createComment(place.getNodeValue()); 125 break; 126 } 127 case Node.ELEMENT_NODE: { 128 Element element = factory.createElement(place.getNodeName()); 129 node = element; 130 NamedNodeMap attrs = place.getAttributes(); 131 int attrCount = attrs.getLength(); 132 for (int i = 0; i < attrCount; i++) { 133 Attr attr = (Attr )attrs.item(i); 134 String attrName = attr.getNodeName(); 135 String attrValue = attr.getNodeValue(); 136 element.setAttribute(attrName, attrValue); 137 if (domimpl && !attr.getSpecified()) { 138 ((AttrImpl)element.getAttributeNode(attrName)).setSpecified(false); 139 } 140 } 141 break; 142 } 143 case Node.ENTITY_REFERENCE_NODE: { 144 node = factory.createEntityReference(place.getNodeName()); 145 break; 146 } 147 case Node.PROCESSING_INSTRUCTION_NODE: { 148 node = factory.createProcessingInstruction(place.getNodeName(), 149 place.getNodeValue()); 150 break; 151 } 152 case Node.TEXT_NODE: { 153 node = factory.createTextNode(place.getNodeValue()); 154 break; 155 } 156 default: { 157 throw new IllegalArgumentException ("can't copy node type, "+ 158 type+" ("+ 159 node.getNodeName()+')'); 160 } 161 } 162 dest.appendChild(node); 163 164 if (place.hasChildNodes()) { 166 parent = place; 167 place = place.getFirstChild(); 168 dest = node; 169 } 170 171 else { 173 place = place.getNextSibling(); 174 while (place == null && parent != start) { 175 place = parent.getNextSibling(); 176 parent = parent.getParentNode(); 177 dest = dest.getParentNode(); 178 } 179 } 180 181 } 182 183 } 185 186 public static Element getFirstChildElement(Node parent) { 187 188 Node child = parent.getFirstChild(); 190 while (child != null) { 191 if (child.getNodeType() == Node.ELEMENT_NODE) { 192 return (Element )child; 193 } 194 child = child.getNextSibling(); 195 } 196 197 return null; 199 200 } 202 203 public static Element getFirstVisibleChildElement(Node parent) { 204 205 Node child = parent.getFirstChild(); 207 while (child != null) { 208 if (child.getNodeType() == Node.ELEMENT_NODE && 209 !isHidden(child)) { 210 return (Element )child; 211 } 212 child = child.getNextSibling(); 213 } 214 215 return null; 217 218 } 220 221 public static Element getLastChildElement(Node parent) { 222 223 Node child = parent.getLastChild(); 225 while (child != null) { 226 if (child.getNodeType() == Node.ELEMENT_NODE) { 227 return (Element )child; 228 } 229 child = child.getPreviousSibling(); 230 } 231 232 return null; 234 235 } 237 238 public static Element getLastVisibleChildElement(Node parent) { 239 240 Node child = parent.getLastChild(); 242 while (child != null) { 243 if (child.getNodeType() == Node.ELEMENT_NODE && 244 !isHidden(child)) { 245 return (Element )child; 246 } 247 child = child.getPreviousSibling(); 248 } 249 250 return null; 252 253 } 255 256 public static Element getNextSiblingElement(Node node) { 257 258 Node sibling = node.getNextSibling(); 260 while (sibling != null) { 261 if (sibling.getNodeType() == Node.ELEMENT_NODE) { 262 return (Element )sibling; 263 } 264 sibling = sibling.getNextSibling(); 265 } 266 267 return null; 269 270 } 272 public static Element getNextVisibleSiblingElement(Node node) { 274 275 Node sibling = node.getNextSibling(); 277 while (sibling != null) { 278 if (sibling.getNodeType() == Node.ELEMENT_NODE && 279 !isHidden(sibling)) { 280 return (Element )sibling; 281 } 282 sibling = sibling.getNextSibling(); 283 } 284 285 return null; 287 288 } 290 public static void setHidden(Node node) { 292 if (node instanceof com.sun.org.apache.xerces.internal.impl.xs.opti.NodeImpl) 293 ((com.sun.org.apache.xerces.internal.impl.xs.opti.NodeImpl)node).setReadOnly(true, false); 294 else if (node instanceof com.sun.org.apache.xerces.internal.dom.NodeImpl) 295 ((com.sun.org.apache.xerces.internal.dom.NodeImpl)node).setReadOnly(true, false); 296 } 298 public static void setVisible(Node node) { 300 if (node instanceof com.sun.org.apache.xerces.internal.impl.xs.opti.NodeImpl) 301 ((com.sun.org.apache.xerces.internal.impl.xs.opti.NodeImpl)node).setReadOnly(false, false); 302 else if (node instanceof com.sun.org.apache.xerces.internal.dom.NodeImpl) 303 ((com.sun.org.apache.xerces.internal.dom.NodeImpl)node).setReadOnly(false, false); 304 } 306 public static boolean isHidden(Node node) { 308 if (node instanceof com.sun.org.apache.xerces.internal.impl.xs.opti.NodeImpl) 309 return ((com.sun.org.apache.xerces.internal.impl.xs.opti.NodeImpl)node).getReadOnly(); 310 else if (node instanceof com.sun.org.apache.xerces.internal.dom.NodeImpl) 311 return ((com.sun.org.apache.xerces.internal.dom.NodeImpl)node).getReadOnly(); 312 return false; 313 } 315 316 public static Element getFirstChildElement(Node parent, String elemName) { 317 318 Node child = parent.getFirstChild(); 320 while (child != null) { 321 if (child.getNodeType() == Node.ELEMENT_NODE) { 322 if (child.getNodeName().equals(elemName)) { 323 return (Element )child; 324 } 325 } 326 child = child.getNextSibling(); 327 } 328 329 return null; 331 332 } 334 335 public static Element getLastChildElement(Node parent, String elemName) { 336 337 Node child = parent.getLastChild(); 339 while (child != null) { 340 if (child.getNodeType() == Node.ELEMENT_NODE) { 341 if (child.getNodeName().equals(elemName)) { 342 return (Element )child; 343 } 344 } 345 child = child.getPreviousSibling(); 346 } 347 348 return null; 350 351 } 353 354 public static Element getNextSiblingElement(Node node, String elemName) { 355 356 Node sibling = node.getNextSibling(); 358 while (sibling != null) { 359 if (sibling.getNodeType() == Node.ELEMENT_NODE) { 360 if (sibling.getNodeName().equals(elemName)) { 361 return (Element )sibling; 362 } 363 } 364 sibling = sibling.getNextSibling(); 365 } 366 367 return null; 369 370 } 372 373 public static Element getFirstChildElementNS(Node parent, 374 String uri, String localpart) { 375 376 Node child = parent.getFirstChild(); 378 while (child != null) { 379 if (child.getNodeType() == Node.ELEMENT_NODE) { 380 String childURI = child.getNamespaceURI(); 381 if (childURI != null && childURI.equals(uri) && 382 child.getLocalName().equals(localpart)) { 383 return (Element )child; 384 } 385 } 386 child = child.getNextSibling(); 387 } 388 389 return null; 391 392 } 394 395 public static Element getLastChildElementNS(Node parent, 396 String uri, String localpart) { 397 398 Node child = parent.getLastChild(); 400 while (child != null) { 401 if (child.getNodeType() == Node.ELEMENT_NODE) { 402 String childURI = child.getNamespaceURI(); 403 if (childURI != null && childURI.equals(uri) && 404 child.getLocalName().equals(localpart)) { 405 return (Element )child; 406 } 407 } 408 child = child.getPreviousSibling(); 409 } 410 411 return null; 413 414 } 416 417 public static Element getNextSiblingElementNS(Node node, 418 String uri, String localpart) { 419 420 Node sibling = node.getNextSibling(); 422 while (sibling != null) { 423 if (sibling.getNodeType() == Node.ELEMENT_NODE) { 424 String siblingURI = sibling.getNamespaceURI(); 425 if (siblingURI != null && siblingURI.equals(uri) && 426 sibling.getLocalName().equals(localpart)) { 427 return (Element )sibling; 428 } 429 } 430 sibling = sibling.getNextSibling(); 431 } 432 433 return null; 435 436 } 438 439 public static Element getFirstChildElement(Node parent, String elemNames[]) { 440 441 Node child = parent.getFirstChild(); 443 while (child != null) { 444 if (child.getNodeType() == Node.ELEMENT_NODE) { 445 for (int i = 0; i < elemNames.length; i++) { 446 if (child.getNodeName().equals(elemNames[i])) { 447 return (Element )child; 448 } 449 } 450 } 451 child = child.getNextSibling(); 452 } 453 454 return null; 456 457 } 459 460 public static Element getLastChildElement(Node parent, String elemNames[]) { 461 462 Node child = parent.getLastChild(); 464 while (child != null) { 465 if (child.getNodeType() == Node.ELEMENT_NODE) { 466 for (int i = 0; i < elemNames.length; i++) { 467 if (child.getNodeName().equals(elemNames[i])) { 468 return (Element )child; 469 } 470 } 471 } 472 child = child.getPreviousSibling(); 473 } 474 475 return null; 477 478 } 480 481 public static Element getNextSiblingElement(Node node, String elemNames[]) { 482 483 Node sibling = node.getNextSibling(); 485 while (sibling != null) { 486 if (sibling.getNodeType() == Node.ELEMENT_NODE) { 487 for (int i = 0; i < elemNames.length; i++) { 488 if (sibling.getNodeName().equals(elemNames[i])) { 489 return (Element )sibling; 490 } 491 } 492 } 493 sibling = sibling.getNextSibling(); 494 } 495 496 return null; 498 499 } 501 502 public static Element getFirstChildElementNS(Node parent, 503 String [][] elemNames) { 504 505 Node child = parent.getFirstChild(); 507 while (child != null) { 508 if (child.getNodeType() == Node.ELEMENT_NODE) { 509 for (int i = 0; i < elemNames.length; i++) { 510 String uri = child.getNamespaceURI(); 511 if (uri != null && uri.equals(elemNames[i][0]) && 512 child.getLocalName().equals(elemNames[i][1])) { 513 return (Element )child; 514 } 515 } 516 } 517 child = child.getNextSibling(); 518 } 519 520 return null; 522 523 } 525 526 public static Element getLastChildElementNS(Node parent, 527 String [][] elemNames) { 528 529 Node child = parent.getLastChild(); 531 while (child != null) { 532 if (child.getNodeType() == Node.ELEMENT_NODE) { 533 for (int i = 0; i < elemNames.length; i++) { 534 String uri = child.getNamespaceURI(); 535 if (uri != null && uri.equals(elemNames[i][0]) && 536 child.getLocalName().equals(elemNames[i][1])) { 537 return (Element )child; 538 } 539 } 540 } 541 child = child.getPreviousSibling(); 542 } 543 544 return null; 546 547 } 549 550 public static Element getNextSiblingElementNS(Node node, 551 String [][] elemNames) { 552 553 Node sibling = node.getNextSibling(); 555 while (sibling != null) { 556 if (sibling.getNodeType() == Node.ELEMENT_NODE) { 557 for (int i = 0; i < elemNames.length; i++) { 558 String uri = sibling.getNamespaceURI(); 559 if (uri != null && uri.equals(elemNames[i][0]) && 560 sibling.getLocalName().equals(elemNames[i][1])) { 561 return (Element )sibling; 562 } 563 } 564 } 565 sibling = sibling.getNextSibling(); 566 } 567 568 return null; 570 571 } 573 577 public static Element getFirstChildElement(Node parent, 578 String elemName, 579 String attrName, 580 String attrValue) { 581 582 Node child = parent.getFirstChild(); 584 while (child != null) { 585 if (child.getNodeType() == Node.ELEMENT_NODE) { 586 Element element = (Element )child; 587 if (element.getNodeName().equals(elemName) && 588 element.getAttribute(attrName).equals(attrValue)) { 589 return element; 590 } 591 } 592 child = child.getNextSibling(); 593 } 594 595 return null; 597 598 } 600 604 public static Element getLastChildElement(Node parent, 605 String elemName, 606 String attrName, 607 String attrValue) { 608 609 Node child = parent.getLastChild(); 611 while (child != null) { 612 if (child.getNodeType() == Node.ELEMENT_NODE) { 613 Element element = (Element )child; 614 if (element.getNodeName().equals(elemName) && 615 element.getAttribute(attrName).equals(attrValue)) { 616 return element; 617 } 618 } 619 child = child.getPreviousSibling(); 620 } 621 622 return null; 624 625 } 627 632 public static Element getNextSiblingElement(Node node, 633 String elemName, 634 String attrName, 635 String attrValue) { 636 637 Node sibling = node.getNextSibling(); 639 while (sibling != null) { 640 if (sibling.getNodeType() == Node.ELEMENT_NODE) { 641 Element element = (Element )sibling; 642 if (element.getNodeName().equals(elemName) && 643 element.getAttribute(attrName).equals(attrValue)) { 644 return element; 645 } 646 } 647 sibling = sibling.getNextSibling(); 648 } 649 650 return null; 652 653 } 655 664 public static String getChildText(Node node) { 665 666 if (node == null) { 668 return null; 669 } 670 671 StringBuffer str = new StringBuffer (); 673 Node child = node.getFirstChild(); 674 while (child != null) { 675 short type = child.getNodeType(); 676 if (type == Node.TEXT_NODE) { 677 str.append(child.getNodeValue()); 678 } 679 else if (type == Node.CDATA_SECTION_NODE) { 680 str.append(getChildText(child)); 681 } 682 child = child.getNextSibling(); 683 } 684 685 return str.toString(); 687 688 } 690 public static String getName(Node node) { 692 return node.getNodeName(); 693 } 695 698 public static String getLocalName(Node node) { 699 String name = node.getLocalName(); 700 return (name!=null)? name:node.getNodeName(); 701 } 703 public static Element getParent(Element elem) { 704 Node parent = elem.getParentNode(); 705 if (parent instanceof Element ) 706 return (Element )parent; 707 return null; 708 } 710 public static Document getDocument(Node node) { 712 return node.getOwnerDocument(); 713 } 715 public static Element getRoot(Document doc) { 717 return doc.getDocumentElement(); 718 } 720 722 public static Attr getAttr(Element elem, String name) { 724 return elem.getAttributeNode(name); 725 } 727 public static Attr getAttrNS(Element elem, String nsUri, 729 String localName) { 730 return elem.getAttributeNodeNS(nsUri, localName); 731 } 733 public static Attr [] getAttrs(Element elem) { 735 NamedNodeMap attrMap = elem.getAttributes(); 736 Attr [] attrArray = new Attr [attrMap.getLength()]; 737 for (int i=0; i<attrMap.getLength(); i++) 738 attrArray[i] = (Attr )attrMap.item(i); 739 return attrArray; 740 } 742 public static String getValue(Attr attribute) { 744 return attribute.getValue(); 745 } 747 752 public static String getAttrValue(Element elem, String name) { 755 return elem.getAttribute(name); 756 } 758 public static String getAttrValueNS(Element elem, String nsUri, 761 String localName) { 762 return elem.getAttributeNS(nsUri, localName); 763 } 765 public static String getNamespaceURI(Node node) { 767 return node.getNamespaceURI(); 768 } 769 } | Popular Tags |