1 23 24 package org.enhydra.xml.xmlc; 25 26 import org.enhydra.xml.dom.DOMOps; 27 import org.enhydra.xml.io.DOMFormatter; 28 import org.enhydra.xml.io.DocumentInfo; 29 import org.enhydra.xml.xmlc.dom.XMLCDomFactory; 30 import org.w3c.dom.Attr ; 31 import org.w3c.dom.CDATASection ; 32 import org.w3c.dom.Comment ; 33 import org.w3c.dom.DOMConfiguration ; 34 import org.w3c.dom.DOMException ; 35 import org.w3c.dom.DOMImplementation ; 36 import org.w3c.dom.Document ; 37 import org.w3c.dom.DocumentFragment ; 38 import org.w3c.dom.DocumentType ; 39 import org.w3c.dom.Element ; 40 import org.w3c.dom.EntityReference ; 41 import org.w3c.dom.NamedNodeMap ; 42 import org.w3c.dom.Node ; 43 import org.w3c.dom.NodeList ; 44 import org.w3c.dom.ProcessingInstruction ; 45 import org.w3c.dom.Text ; 46 import org.w3c.dom.UserDataHandler ; 47 48 51 55 public abstract class XMLObjectImpl implements XMLObject, Document , DocumentInfo { 56 60 private static DOMFormatter fFormatter = null; 61 62 65 private Document fDocument; 66 67 70 private String fMIMEType; 71 72 75 private XMLObject fDelegate; 76 77 81 protected XMLObjectImpl() { 82 } 83 84 90 protected void setDocument(Document document, 91 String mimeType, 92 String encoding) { 93 fDocument = document; 94 fMIMEType = mimeType; 95 setEncoding(encoding); 96 97 if (document instanceof XMLObjectLink) { 99 ((XMLObjectLink)document).setXMLObject(this); 100 } 101 } 102 103 107 protected abstract XMLCDomFactory getDomFactory(); 108 109 112 public Document getDocument() { 113 if (fDelegate != null) { 114 return fDelegate.getDocument(); 115 } else { 116 return fDocument; 117 } 118 } 119 120 123 public String getMIMEType() { 124 if (fDelegate != null) { 125 return fDelegate.getMIMEType(); 126 } else { 127 return fMIMEType; 128 } 129 } 130 131 134 public void setDelegate(XMLObject delegate) { 135 fDelegate = delegate; 136 } 137 138 141 public XMLObject getDelegate() { 142 return fDelegate; 143 } 144 145 149 protected void cloneDeepCheck(boolean deep) { 150 if (!deep) { 151 throw new XMLCError("Must use deep clone when cloning entire document"); 152 } 153 } 154 155 162 abstract public Node cloneNode(boolean deep); 163 164 167 public DocumentType getDoctype() { 168 if (fDelegate != null) { 169 return fDelegate.getDoctype(); 170 } else { 171 return fDocument.getDoctype(); 172 } 173 } 174 175 178 public DOMImplementation getImplementation() { 179 if (fDelegate != null) { 180 return fDelegate.getImplementation(); 181 } else { 182 return fDocument.getImplementation(); 183 } 184 } 185 186 189 public Element getDocumentElement() { 190 if (fDelegate != null) { 191 return fDelegate.getDocumentElement(); 192 } else { 193 return fDocument.getDocumentElement(); 194 } 195 } 196 197 200 public Node importNode(Node importedNode, 201 boolean deep) 202 throws DOMException { 203 if (fDelegate != null) { 204 return fDelegate.importNode(importedNode, deep); 205 } else { 206 return fDocument.importNode(importedNode, deep); 207 } 208 } 209 210 213 public Element createElement(String tagName) 214 throws DOMException { 215 if (fDelegate != null) { 216 return fDelegate.createElement(tagName); 217 } else { 218 return fDocument.createElement(tagName); 219 } 220 } 221 222 225 public Element createElementNS(String namespaceURI, 226 String qualifiedName) 227 throws DOMException { 228 if (fDelegate != null) { 229 return fDelegate.createElementNS(namespaceURI, qualifiedName); 230 } else { 231 return fDocument.createElementNS(namespaceURI, qualifiedName); 232 } 233 } 234 235 238 public DocumentFragment createDocumentFragment() { 239 if (fDelegate != null) { 240 return fDelegate.createDocumentFragment(); 241 } else { 242 return fDocument.createDocumentFragment(); 243 } 244 } 245 246 249 public Text createTextNode(String data) { 250 if (fDelegate != null) { 251 return fDelegate.createTextNode(data); 252 } else { 253 return fDocument.createTextNode(data); 254 } 255 } 256 257 260 public Comment createComment(String data) { 261 if (fDelegate != null) { 262 return fDelegate.createComment(data); 263 } else { 264 return fDocument.createComment(data); 265 } 266 } 267 268 271 public CDATASection createCDATASection(String data) 272 throws DOMException { 273 if (fDelegate != null) { 274 return fDelegate.createCDATASection(data); 275 } else { 276 return fDocument.createCDATASection(data); 277 } 278 } 279 280 283 public ProcessingInstruction createProcessingInstruction(String target, 284 String data) 285 throws DOMException { 286 if (fDelegate != null) { 287 return fDelegate.createProcessingInstruction(target, data); 288 } else { 289 return fDocument.createProcessingInstruction(target, data); 290 } 291 } 292 293 296 public Attr createAttribute(String qualifiedName) 297 throws DOMException { 298 if (fDelegate != null) { 299 return fDelegate.createAttribute(qualifiedName); 300 } else { 301 return fDocument.createAttribute(qualifiedName); 302 } 303 } 304 305 308 public Attr createAttributeNS(String namespaceURI, 309 String qualifiedName) 310 throws DOMException { 311 if (fDelegate != null) { 312 return fDelegate.createAttributeNS(namespaceURI, qualifiedName); 313 } else { 314 return fDocument.createAttributeNS(namespaceURI, qualifiedName); 315 } 316 } 317 318 321 public EntityReference createEntityReference(String name) 322 throws DOMException { 323 if (fDelegate != null) { 324 return fDelegate.createEntityReference(name); 325 } else { 326 return fDocument.createEntityReference(name); 327 } 328 } 329 330 333 public NodeList getElementsByTagName(String tagname) { 334 if (fDelegate != null) { 335 return fDelegate.getElementsByTagName(tagname); 336 } else { 337 return fDocument.getElementsByTagName(tagname); 338 } 339 } 340 341 344 public NodeList getElementsByTagNameNS(String namespaceURI, 345 String localName) { 346 if (fDelegate != null) { 347 return fDelegate.getElementsByTagNameNS(namespaceURI, localName); 348 } else { 349 return fDocument.getElementsByTagNameNS(namespaceURI, localName); 350 } 351 } 352 353 356 public Element getElementById(String elementId) { 357 if (fDelegate != null) { 358 return fDelegate.getElementById(elementId); 359 } else { 360 return fDocument.getElementById(elementId); 361 } 362 } 363 364 367 public String getEncoding() { 368 if (fDelegate != null) { 369 return fDelegate.getEncoding(); 370 } else { 371 return DOMOps.getEncoding(fDocument); 372 } 373 } 374 375 378 public void setEncoding(String encoding) { 379 if (fDelegate != null) { 380 fDelegate.setEncoding(encoding); 381 } else { 382 DOMOps.setEncoding(fDocument, encoding); 383 } 384 } 385 386 389 public boolean getStandalone() { 390 if (fDelegate != null) { 391 return fDelegate.getStandalone(); 392 } else { 393 return DOMOps.getStandalone(fDocument); 394 } 395 } 396 397 400 public void setStandalone(boolean standalone) { 401 if (fDelegate != null) { 402 fDelegate.setStandalone(standalone); 403 } else { 404 DOMOps.setStandalone(fDocument, standalone); 405 } 406 } 407 408 411 public boolean getStrictErrorChecking() { 412 if (fDelegate != null) { 413 return fDelegate.getStrictErrorChecking(); 414 } else { 415 return DOMOps.getStrictErrorChecking(fDocument); 416 } 417 } 418 419 422 public void setStrictErrorChecking(boolean strictErrorChecking) { 423 if (fDelegate != null) { 424 fDelegate.setStrictErrorChecking(strictErrorChecking); 425 } else { 426 DOMOps.setStrictErrorChecking(fDocument, strictErrorChecking); 427 } 428 } 429 430 433 public String getVersion() { 434 if (fDelegate != null) { 435 return fDelegate.getVersion(); 436 } else { 437 return DOMOps.getVersion(fDocument); 438 } 439 } 440 441 444 public void setVersion(String version) { 445 if (fDelegate != null) { 446 fDelegate.setVersion(version); 447 } else { 448 DOMOps.setVersion(fDocument, version); 449 } 450 } 451 452 455 public Node adoptNode(Node source) throws DOMException { 456 if (fDelegate != null) { 457 return fDelegate.adoptNode(source); 458 } else { 459 return DOMOps.adoptNode(fDocument, source); 460 } 461 } 462 463 466 public String getNodeName() { 467 if (fDelegate != null) { 468 return fDelegate.getNodeName(); 469 } else { 470 return fDocument.getNodeName(); 471 } 472 } 473 474 477 public String getNodeValue() 478 throws DOMException { 479 if (fDelegate != null) { 480 return fDelegate.getNodeValue(); 481 } else { 482 return fDocument.getNodeValue(); 483 } 484 } 485 486 489 public void setNodeValue(String nodeValue) 490 throws DOMException { 491 if (fDelegate != null) { 492 fDelegate.setNodeValue(nodeValue); 493 } else { 494 fDocument.setNodeValue(nodeValue); 495 } 496 } 497 498 501 public short getNodeType() { 502 if (fDelegate != null) { 503 return fDelegate.getNodeType(); 504 } else { 505 return fDocument.getNodeType(); 506 } 507 } 508 509 512 public Node getParentNode() { 513 if (fDelegate != null) { 514 return fDelegate.getParentNode(); 515 } else { 516 return fDocument.getParentNode(); 517 } 518 } 519 520 523 public NodeList getChildNodes() { 524 if (fDelegate != null) { 525 return fDelegate.getChildNodes(); 526 } else { 527 return fDocument.getChildNodes(); 528 } 529 } 530 531 534 public Node getFirstChild() { 535 if (fDelegate != null) { 536 return fDelegate.getFirstChild(); 537 } else { 538 return fDocument.getFirstChild(); 539 } 540 } 541 542 545 public Node getLastChild() { 546 if (fDelegate != null) { 547 return fDelegate.getLastChild(); 548 } else { 549 return fDocument.getLastChild(); 550 } 551 } 552 553 556 public Node getPreviousSibling() { 557 if (fDelegate != null) { 558 return fDelegate.getPreviousSibling(); 559 } else { 560 return fDocument.getPreviousSibling(); 561 } 562 } 563 564 567 public Node getNextSibling() { 568 if (fDelegate != null) { 569 return fDelegate.getNextSibling(); 570 } else { 571 return fDocument.getNextSibling(); 572 } 573 } 574 575 578 public NamedNodeMap getAttributes() { 579 if (fDelegate != null) { 580 return fDelegate.getAttributes(); 581 } else { 582 return fDocument.getAttributes(); 583 } 584 } 585 586 589 public Document getOwnerDocument() { 590 if (fDelegate != null) { 591 return fDelegate.getOwnerDocument(); 592 } else { 593 return fDocument.getOwnerDocument(); 594 } 595 } 596 597 600 public Node insertBefore(Node newChild, 601 Node refChild) 602 throws DOMException { 603 if (fDelegate != null) { 604 return fDelegate.insertBefore(newChild, refChild); 605 } else { 606 return fDocument.insertBefore(newChild, refChild); 607 } 608 } 609 610 613 public Node replaceChild(Node newChild, 614 Node oldChild) 615 throws DOMException { 616 if (fDelegate != null) { 617 return fDelegate.replaceChild(newChild, oldChild); 618 } else { 619 return fDocument.replaceChild(newChild, oldChild); 620 } 621 } 622 623 626 public Node removeChild(Node oldChild) 627 throws DOMException { 628 if (fDelegate != null) { 629 return fDelegate.removeChild(oldChild); 630 } else { 631 return fDocument.removeChild(oldChild); 632 } 633 } 634 635 638 public Node appendChild(Node newChild) 639 throws DOMException { 640 if (fDelegate != null) { 641 return fDelegate.appendChild(newChild); 642 } else { 643 return fDocument.appendChild(newChild); 644 } 645 } 646 647 650 public void normalize() { 651 if (fDelegate != null) { 652 fDelegate.normalize(); 653 } else { 654 fDocument.normalize(); 655 } 656 } 657 658 661 public boolean isSupported(String feature, 662 String version) { 663 if (fDelegate != null) { 664 return fDelegate.isSupported(feature, version); 665 } else { 666 return fDocument.isSupported(feature, version); 667 } 668 } 669 670 673 public String getNamespaceURI() { 674 if (fDelegate != null) { 675 return fDelegate.getNamespaceURI(); 676 } else { 677 return fDocument.getNamespaceURI(); 678 } 679 } 680 681 684 public String getPrefix() { 685 if (fDelegate != null) { 686 return fDelegate.getPrefix(); 687 } else { 688 return fDocument.getPrefix(); 689 } 690 } 691 692 695 public void setPrefix(String prefix) { 696 if (fDelegate != null) { 697 fDelegate.setPrefix(prefix); 698 } else { 699 fDocument.setPrefix(prefix); 700 } 701 } 702 703 706 public String getLocalName() { 707 if (fDelegate != null) { 708 return fDelegate.getLocalName(); 709 } else { 710 return fDocument.getLocalName(); 711 } 712 } 713 714 717 public boolean hasChildNodes() { 718 if (fDelegate != null) { 719 return fDelegate.hasChildNodes(); 720 } else { 721 return fDocument.hasChildNodes(); 722 } 723 } 724 725 728 public boolean hasAttributes() { 729 if (fDelegate != null) { 730 return fDelegate.hasAttributes(); 731 } else { 732 return fDocument.hasAttributes(); 733 } 734 } 735 736 739 public String toDocument() { 740 if (fFormatter == null) { 742 fFormatter = new DOMFormatter(DOMFormatter.getDefaultOutputOptions(getDocument())); 743 } 744 745 if (fDelegate != null) { 746 return fFormatter.toString(fDelegate); 747 } else { 748 return fFormatter.toString(this); 749 } 750 } 751 752 756 abstract protected void syncWithDocument(Node node); 757 758 761 private void syncAccessMethods(Node node) { 762 syncWithDocument(node); 763 for (Node child = node.getFirstChild(); child != null; 764 child = child.getNextSibling()) { 765 syncAccessMethods(child); 766 } 767 } 768 769 772 public void syncAccessMethods() { 773 if (fDelegate != null) { 774 fDelegate.syncAccessMethods(); 775 } else { 776 syncAccessMethods(fDocument); 777 } 778 } 779 780 788 public void initFields() { 789 syncAccessMethods(); 790 } 791 792 795 public boolean isURLAttribute(Element element, 796 String attrName) { 797 return getDomFactory().isURLAttribute(element, attrName); 798 } 799 800 804 protected final void doSetText(Element element, 805 String text) { 806 if (text == null) { 807 throw new IllegalArgumentException ("attempt to set a DOM text node value to null"); 808 } 809 Text textNode = XMLCUtil.findFirstText(element); 816 if (textNode == null) { 817 if (fDelegate != null) { 818 textNode = fDelegate.createTextNode(""); 819 } else { 820 textNode = fDocument.createTextNode(""); 821 } 822 element.appendChild(textNode); 823 } 824 textNode.setData(text); 825 } 826 827 828 830 833 public String getDocumentURI() { 834 if (fDelegate != null) { 835 return fDelegate.getDocumentURI(); 836 } else { 837 return fDocument.getDocumentURI(); 838 } 839 } 840 843 public DOMConfiguration getDomConfig() { 844 if (fDelegate != null) { 845 return fDelegate.getDomConfig(); 846 } else { 847 return fDocument.getDomConfig(); 848 } 849 } 850 853 public String getInputEncoding() { 854 if (fDelegate != null) { 855 return fDelegate.getInputEncoding(); 856 } else { 857 return fDocument.getInputEncoding(); 858 } 859 } 860 863 public String getXmlEncoding() { 864 if (fDelegate != null) { 865 return fDelegate.getXmlEncoding(); 866 } else { 867 return fDocument.getXmlEncoding(); 868 } 869 } 870 873 public boolean getXmlStandalone() { 874 if (fDelegate != null) { 875 return fDelegate.getXmlStandalone(); 876 } else { 877 return fDocument.getXmlStandalone(); 878 } 879 } 880 883 public String getXmlVersion() { 884 if (fDelegate != null) { 885 return fDelegate.getXmlVersion(); 886 } else { 887 return fDocument.getXmlVersion(); 888 } 889 } 890 893 public void normalizeDocument() { 894 if (fDelegate != null) { 895 fDelegate.normalizeDocument(); 896 } else { 897 fDocument.normalizeDocument(); 898 } 899 } 900 903 public Node renameNode(Node arg0, String arg1, String arg2) 904 throws DOMException { 905 if (fDelegate != null) { 906 return fDelegate.renameNode(arg0, arg1, arg2); 907 } else { 908 return fDocument.renameNode(arg0, arg1, arg2); 909 } 910 } 911 914 public void setDocumentURI(String arg0) { 915 if (fDelegate != null) { 916 fDelegate.setDocumentURI(arg0); 917 } else { 918 fDocument.setDocumentURI(arg0); 919 } 920 } 921 924 public void setXmlStandalone(boolean arg0) throws DOMException { 925 if (fDelegate != null) { 926 fDelegate.setXmlStandalone(arg0); 927 } else { 928 fDocument.setXmlStandalone(arg0); 929 } 930 } 931 934 public void setXmlVersion(String arg0) throws DOMException { 935 if (fDelegate != null) { 936 fDelegate.setXmlVersion(arg0); 937 } else { 938 fDocument.setXmlVersion(arg0); 939 } 940 } 941 944 public short compareDocumentPosition(Node arg0) throws DOMException { 945 if (fDelegate != null) { 946 return fDelegate.compareDocumentPosition(arg0); 947 } else { 948 return fDocument.compareDocumentPosition(arg0); 949 } 950 } 951 954 public String getBaseURI() { 955 if (fDelegate != null) { 956 return fDelegate.getBaseURI(); 957 } else { 958 return fDocument.getBaseURI(); 959 } 960 } 961 964 public Object getFeature(String arg0, String arg1) { 965 if (fDelegate != null) { 966 return fDelegate.getFeature(arg0, arg1); 967 } else { 968 return fDocument.getFeature(arg0, arg1); 969 } 970 } 971 974 public String getTextContent() throws DOMException { 975 if (fDelegate != null) { 976 return fDelegate.getTextContent(); 977 } else { 978 return fDocument.getTextContent(); 979 } 980 } 981 984 public Object getUserData(String arg0) { 985 if (fDelegate != null) { 986 return fDelegate.getUserData(arg0); 987 } else { 988 return fDocument.getUserData(arg0); 989 } 990 } 991 994 public boolean isDefaultNamespace(String arg0) { 995 if (fDelegate != null) { 996 return fDelegate.isDefaultNamespace(arg0); 997 } else { 998 return fDocument.isDefaultNamespace(arg0); 999 } 1000 } 1001 1004 public boolean isEqualNode(Node arg0) { 1005 if (fDelegate != null) { 1006 return fDelegate.isEqualNode(arg0); 1007 } else { 1008 return fDocument.isEqualNode(arg0); 1009 } 1010 } 1011 1014 public boolean isSameNode(Node arg0) { 1015 if (fDelegate != null) { 1016 return fDelegate.isSameNode(arg0); 1017 } else { 1018 return fDocument.isSameNode(arg0); 1019 } 1020 } 1021 1024 public String lookupNamespaceURI(String arg0) { 1025 if (fDelegate != null) { 1026 return fDelegate.lookupNamespaceURI(arg0); 1027 } else { 1028 return fDocument.lookupNamespaceURI(arg0); 1029 } 1030 } 1031 1034 public String lookupPrefix(String arg0) { 1035 if (fDelegate != null) { 1036 return fDelegate.lookupPrefix(arg0); 1037 } else { 1038 return fDocument.lookupPrefix(arg0); 1039 } 1040 } 1041 1044 public void setTextContent(String arg0) throws DOMException { 1045 if (fDelegate != null) { 1046 fDelegate.setTextContent(arg0); 1047 } else { 1048 fDocument.setTextContent(arg0); 1049 } 1050 } 1051 1054 public Object setUserData(String arg0, Object arg1, UserDataHandler arg2) { 1055 if (fDelegate != null) { 1056 return fDelegate.setUserData(arg0, arg1, arg2); 1057 } else { 1058 return fDocument.setUserData(arg0, arg1, arg2); 1059 } 1060 } 1061 1062} 1063 | Popular Tags |