1 16 17 package org.apache.xerces.dom; 18 19 import org.w3c.dom.Attr ; 20 import org.w3c.dom.DOMException ; 21 import org.w3c.dom.Element ; 22 import org.w3c.dom.NamedNodeMap ; 23 import org.w3c.dom.Node ; 24 import org.w3c.dom.NodeList ; 25 import org.w3c.dom.Text ; 26 27 import org.w3c.dom.TypeInfo ; 28 import org.apache.xerces.util.URI; 29 30 56 public class ElementImpl 57 extends ParentNode 58 implements Element , TypeInfo { 59 60 64 65 static final long serialVersionUID = 3717253516652722278L; 66 70 71 protected String name; 72 73 74 protected AttributeMap attributes; 75 76 80 81 public ElementImpl(CoreDocumentImpl ownerDoc, String name) { 82 super(ownerDoc); 83 this.name = name; 84 needsSyncData(true); } 86 87 protected ElementImpl() {} 89 90 void rename(String name) { 94 if (needsSyncData()) { 95 synchronizeData(); 96 } 97 this.name = name; 98 reconcileDefaultAttributes(); 99 } 100 101 105 106 110 public short getNodeType() { 111 return Node.ELEMENT_NODE; 112 } 113 114 117 public String getNodeName() { 118 if (needsSyncData()) { 119 synchronizeData(); 120 } 121 return name; 122 } 123 124 130 public NamedNodeMap getAttributes() { 131 132 if (needsSyncData()) { 133 synchronizeData(); 134 } 135 if (attributes == null) { 136 attributes = new AttributeMap(this, null); 137 } 138 return attributes; 139 140 } 142 149 public Node cloneNode(boolean deep) { 150 151 ElementImpl newnode = (ElementImpl) super.cloneNode(deep); 152 if (attributes != null) { 154 newnode.attributes = (AttributeMap) attributes.cloneMap(newnode); 155 } 156 return newnode; 157 158 } 160 164 public String getBaseURI() { 165 166 if (needsSyncData()) { 167 synchronizeData(); 168 } 169 if (attributes != null) { 174 Attr attrNode = (Attr )attributes.getNamedItem("xml:base"); 175 if (attrNode != null) { 176 String uri = attrNode.getNodeValue(); 177 if (uri.length() != 0 ) { try { 179 uri = new URI(uri).toString(); 180 } 181 catch (org.apache.xerces.util.URI.MalformedURIException e) { 182 184 String parentBaseURI = (this.ownerNode != null) ? this.ownerNode.getBaseURI() : null; 186 if (parentBaseURI != null){ 187 try{ 188 uri = new URI(new URI(parentBaseURI), uri).toString(); 189 } 190 catch (org.apache.xerces.util.URI.MalformedURIException ex){ 191 return null; 193 } 194 return uri; 195 } 196 return null; 197 } 198 return uri; 199 } 200 } 201 } 202 203 208 String baseURI = (this.ownerNode != null) ? this.ownerNode.getBaseURI() : null ; 210 if(baseURI != null){ 212 try { 213 return new URI(baseURI).toString(); 215 } 216 catch (org.apache.xerces.util.URI.MalformedURIException e){ 217 return null; 218 } 219 } 220 return null; 221 } 223 224 225 229 void setOwnerDocument(CoreDocumentImpl doc) { 230 super.setOwnerDocument(doc); 231 if (attributes != null) { 232 attributes.setOwnerDocument(doc); 233 } 234 } 235 236 240 249 public String getAttribute(String name) { 250 251 if (needsSyncData()) { 252 synchronizeData(); 253 } 254 if (attributes == null) { 255 return ""; 256 } 257 Attr attr = (Attr )(attributes.getNamedItem(name)); 258 return (attr == null) ? "" : attr.getValue(); 259 260 } 262 263 270 public Attr getAttributeNode(String name) { 271 272 if (needsSyncData()) { 273 synchronizeData(); 274 } 275 if (attributes == null) { 276 return null; 277 } 278 return (Attr )attributes.getNamedItem(name); 279 280 } 282 283 298 public NodeList getElementsByTagName(String tagname) { 299 return new DeepNodeListImpl(this,tagname); 300 } 301 302 309 public String getTagName() { 310 if (needsSyncData()) { 311 synchronizeData(); 312 } 313 return name; 314 } 315 316 330 public void normalize() { 331 if (isNormalized()) { 333 return; 334 } 335 if (needsSyncChildren()) { 336 synchronizeChildren(); 337 } 338 ChildNode kid, next; 339 for (kid = firstChild; kid != null; kid = next) { 340 next = kid.nextSibling; 341 342 if ( kid.getNodeType() == Node.TEXT_NODE ) 348 { 349 if ( next!=null && next.getNodeType() == Node.TEXT_NODE ) 351 { 352 ((Text )kid).appendData(next.getNodeValue()); 353 removeChild( next ); 354 next = kid; } 356 else 357 { 358 if ( kid.getNodeValue() == null || kid.getNodeValue().length() == 0 ) { 360 removeChild( kid ); 361 } 362 } 363 } 364 365 else if (kid.getNodeType() == Node.ELEMENT_NODE) { 367 kid.normalize(); 368 } 369 } 370 371 if ( attributes!=null ) 373 { 374 for( int i=0; i<attributes.getLength(); ++i ) 375 { 376 Node attr = attributes.item(i); 377 attr.normalize(); 378 } 379 } 380 381 384 isNormalized(true); 385 } 387 402 public void removeAttribute(String name) { 403 404 if (ownerDocument.errorChecking && isReadOnly()) { 405 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 406 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 407 } 408 409 if (needsSyncData()) { 410 synchronizeData(); 411 } 412 413 if (attributes == null) { 414 return; 415 } 416 417 attributes.safeRemoveNamedItem(name); 418 419 } 421 422 438 public Attr removeAttributeNode(Attr oldAttr) 439 throws DOMException { 440 441 if (ownerDocument.errorChecking && isReadOnly()) { 442 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 443 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 444 } 445 446 if (needsSyncData()) { 447 synchronizeData(); 448 } 449 450 if (attributes == null) { 451 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null); 452 throw new DOMException (DOMException.NOT_FOUND_ERR, msg); 453 } 454 return (Attr ) attributes.removeItem(oldAttr, true); 455 456 } 458 459 478 public void setAttribute(String name, String value) { 479 480 if (ownerDocument.errorChecking && isReadOnly()) { 481 String msg = 482 DOMMessageFormatter.formatMessage( 483 DOMMessageFormatter.DOM_DOMAIN, 484 "NO_MODIFICATION_ALLOWED_ERR", 485 null); 486 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 487 } 488 489 if (needsSyncData()) { 490 synchronizeData(); 491 } 492 493 Attr newAttr = getAttributeNode(name); 494 if (newAttr == null) { 495 newAttr = getOwnerDocument().createAttribute(name); 496 497 if (attributes == null) { 498 attributes = new AttributeMap(this, null); 499 } 500 501 newAttr.setNodeValue(value); 502 attributes.setNamedItem(newAttr); 503 } 504 else { 505 newAttr.setNodeValue(value); 506 } 507 508 } 510 523 public Attr setAttributeNode(Attr newAttr) 524 throws DOMException 525 { 526 527 if (needsSyncData()) { 528 synchronizeData(); 529 } 530 531 if (ownerDocument.errorChecking) { 532 if (isReadOnly()) { 533 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 534 throw new DOMException ( 535 DOMException.NO_MODIFICATION_ALLOWED_ERR, 536 msg); 537 } 538 539 if (newAttr.getOwnerDocument() != ownerDocument) { 540 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null); 541 throw new DOMException (DOMException.WRONG_DOCUMENT_ERR, msg); 542 } 543 } 544 545 if (attributes == null) { 546 attributes = new AttributeMap(this, null); 547 } 548 return (Attr ) attributes.setNamedItem(newAttr); 550 551 } 553 557 571 public String getAttributeNS(String namespaceURI, String localName) { 572 573 if (needsSyncData()) { 574 synchronizeData(); 575 } 576 577 if (attributes == null) { 578 return ""; 579 } 580 581 Attr attr = (Attr )(attributes.getNamedItemNS(namespaceURI, localName)); 582 return (attr == null) ? "" : attr.getValue(); 583 584 } 586 627 public void setAttributeNS(String namespaceURI,String qualifiedName, 628 String value) { 629 if (ownerDocument.errorChecking && isReadOnly()) { 630 String msg = 631 DOMMessageFormatter.formatMessage( 632 DOMMessageFormatter.DOM_DOMAIN, 633 "NO_MODIFICATION_ALLOWED_ERR", 634 null); 635 throw new DOMException ( 636 DOMException.NO_MODIFICATION_ALLOWED_ERR, 637 msg); 638 } 639 if (needsSyncData()) { 640 synchronizeData(); 641 } 642 int index = qualifiedName.indexOf(':'); 643 String prefix, localName; 644 if (index < 0) { 645 prefix = null; 646 localName = qualifiedName; 647 } 648 else { 649 prefix = qualifiedName.substring(0, index); 650 localName = qualifiedName.substring(index + 1); 651 } 652 Attr newAttr = getAttributeNodeNS(namespaceURI, localName); 653 if (newAttr == null) { 654 newAttr = getOwnerDocument().createAttributeNS( 657 namespaceURI, 658 qualifiedName); 659 if (attributes == null) { 660 attributes = new AttributeMap(this, null); 661 } 662 newAttr.setNodeValue(value); 663 attributes.setNamedItemNS(newAttr); 664 } 665 else { 666 if (newAttr instanceof AttrNSImpl){ 667 ((AttrNSImpl)newAttr).name= (prefix!=null)?(prefix+":"+localName):localName; 669 } 670 else { 671 newAttr = new AttrNSImpl((CoreDocumentImpl)getOwnerDocument(), namespaceURI, qualifiedName, localName); 678 attributes.setNamedItemNS(newAttr); 679 } 680 681 newAttr.setNodeValue(value); 682 } 683 684 } 686 687 702 public void removeAttributeNS(String namespaceURI, String localName) { 703 704 if (ownerDocument.errorChecking && isReadOnly()) { 705 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 706 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 707 } 708 709 if (needsSyncData()) { 710 synchronizeData(); 711 } 712 713 if (attributes == null) { 714 return; 715 } 716 717 attributes.safeRemoveNamedItemNS(namespaceURI, localName); 718 719 } 721 732 public Attr getAttributeNodeNS(String namespaceURI, String localName){ 733 734 if (needsSyncData()) { 735 synchronizeData(); 736 } 737 if (attributes == null) { 738 return null; 739 } 740 return (Attr )attributes.getNamedItemNS(namespaceURI, localName); 741 742 } 744 771 public Attr setAttributeNodeNS(Attr newAttr) 772 throws DOMException 773 { 774 775 if (needsSyncData()) { 776 synchronizeData(); 777 } 778 if (ownerDocument.errorChecking) { 779 if (isReadOnly()) { 780 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 781 throw new DOMException ( 782 DOMException.NO_MODIFICATION_ALLOWED_ERR, 783 msg); 784 } 785 if (newAttr.getOwnerDocument() != ownerDocument) { 786 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null); 787 throw new DOMException (DOMException.WRONG_DOCUMENT_ERR, msg); 788 } 789 } 790 791 if (attributes == null) { 792 attributes = new AttributeMap(this, null); 793 } 794 return (Attr ) attributes.setNamedItemNS(newAttr); 796 797 } 799 802 protected int setXercesAttributeNode (Attr attr){ 803 804 if (needsSyncData()) { 805 synchronizeData(); 806 } 807 808 if (attributes == null) { 809 attributes = new AttributeMap(this, null); 810 } 811 return attributes.addItem(attr); 812 813 } 814 815 818 protected int getXercesAttribute(String namespaceURI, String localName){ 819 820 if (needsSyncData()) { 821 synchronizeData(); 822 } 823 if (attributes == null) { 824 return -1; 825 } 826 return attributes.getNamedItemIndex(namespaceURI, localName); 827 828 } 829 830 833 public boolean hasAttributes() { 834 if (needsSyncData()) { 835 synchronizeData(); 836 } 837 return (attributes != null && attributes.getLength() != 0); 838 } 839 840 843 public boolean hasAttribute(String name) { 844 return getAttributeNode(name) != null; 845 } 846 847 850 public boolean hasAttributeNS(String namespaceURI, String localName) { 851 return getAttributeNodeNS(namespaceURI, localName) != null; 852 } 853 854 872 public NodeList getElementsByTagNameNS(String namespaceURI, 873 String localName) { 874 return new DeepNodeListImpl(this, namespaceURI, localName); 875 } 876 877 882 public boolean isEqualNode(Node arg) { 883 if (!super.isEqualNode(arg)) { 884 return false; 885 } 886 boolean hasAttrs = hasAttributes(); 887 if (hasAttrs != ((Element ) arg).hasAttributes()) { 888 return false; 889 } 890 if (hasAttrs) { 891 NamedNodeMap map1 = getAttributes(); 892 NamedNodeMap map2 = ((Element ) arg).getAttributes(); 893 int len = map1.getLength(); 894 if (len != map2.getLength()) { 895 return false; 896 } 897 for (int i = 0; i < len; i++) { 898 Node n1 = map1.item(i); 899 if (n1.getLocalName() == null) { Node n2 = map2.getNamedItem(n1.getNodeName()); 901 if (n2 == null || !((NodeImpl) n1).isEqualNode(n2)) { 902 return false; 903 } 904 } 905 else { 906 Node n2 = map2.getNamedItemNS(n1.getNamespaceURI(), 907 n1.getLocalName()); 908 if (n2 == null || !((NodeImpl) n1).isEqualNode(n2)) { 909 return false; 910 } 911 } 912 } 913 } 914 return true; 915 } 916 917 920 public void setIdAttributeNode(Attr at, boolean makeId) { 921 if (needsSyncData()) { 922 synchronizeData(); 923 } 924 if (ownerDocument.errorChecking) { 925 if (isReadOnly()) { 926 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 927 throw new DOMException ( 928 DOMException.NO_MODIFICATION_ALLOWED_ERR, 929 msg); 930 } 931 932 if (at.getOwnerElement() != this) { 933 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null); 934 throw new DOMException (DOMException.NOT_FOUND_ERR, msg); 935 } 936 } 937 ((AttrImpl) at).isIdAttribute(makeId); 938 if (!makeId) { 939 ownerDocument.removeIdentifier(at.getValue()); 940 } 941 else { 942 ownerDocument.putIdentifier(at.getValue(), this); 943 } 944 } 945 946 949 public void setIdAttribute(String name, boolean makeId) { 950 if (needsSyncData()) { 951 synchronizeData(); 952 } 953 Attr at = getAttributeNode(name); 954 955 if( at == null){ 956 String msg = DOMMessageFormatter.formatMessage( 957 DOMMessageFormatter.DOM_DOMAIN, 958 "NOT_FOUND_ERR", null); 959 throw new DOMException (DOMException.NOT_FOUND_ERR, msg); 960 } 961 962 if (ownerDocument.errorChecking) { 963 if (isReadOnly()) { 964 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 965 throw new DOMException ( 966 DOMException.NO_MODIFICATION_ALLOWED_ERR, 967 msg); 968 } 969 970 if (at.getOwnerElement() != this) { 971 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null); 972 throw new DOMException (DOMException.NOT_FOUND_ERR, msg); 973 } 974 } 975 976 ((AttrImpl) at).isIdAttribute(makeId); 977 if (!makeId) { 978 ownerDocument.removeIdentifier(at.getValue()); 979 } 980 else { 981 ownerDocument.putIdentifier(at.getValue(), this); 982 } 983 } 984 985 988 public void setIdAttributeNS(String namespaceURI, String localName, 989 boolean makeId) { 990 if (needsSyncData()) { 991 synchronizeData(); 992 } 993 Attr at = getAttributeNodeNS(namespaceURI, localName); 994 995 if( at == null){ 996 String msg = DOMMessageFormatter.formatMessage( 997 DOMMessageFormatter.DOM_DOMAIN, 998 "NOT_FOUND_ERR", null); 999 throw new DOMException (DOMException.NOT_FOUND_ERR, msg); 1000 } 1001 1002 if (ownerDocument.errorChecking) { 1003 if (isReadOnly()) { 1004 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 1005 throw new DOMException ( 1006 DOMException.NO_MODIFICATION_ALLOWED_ERR, 1007 msg); 1008 } 1009 1010 if (at.getOwnerElement() != this) { 1011 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null); 1012 throw new DOMException (DOMException.NOT_FOUND_ERR, msg); 1013 } 1014 } 1015 ((AttrImpl) at).isIdAttribute(makeId); 1016 if (!makeId) { 1017 ownerDocument.removeIdentifier(at.getValue()); 1018 } 1019 else { 1020 ownerDocument.putIdentifier(at.getValue(), this); 1021 } 1022 } 1023 1024 1027 public String getTypeName() { 1028 return null; 1029 } 1030 1031 1034 public String getTypeNamespace() { 1035 return null; 1036 } 1037 1038 1053 public boolean isDerivedFrom(String typeNamespaceArg, 1054 String typeNameArg, 1055 int derivationMethod) { 1056 1057 return false; 1058 } 1059 1060 1064 public TypeInfo getSchemaTypeInfo(){ 1065 if(needsSyncData()) { 1066 synchronizeData(); 1067 } 1068 return this; 1069 } 1070 1071 1075 1079 public void setReadOnly(boolean readOnly, boolean deep) { 1080 super.setReadOnly(readOnly,deep); 1081 if (attributes != null) { 1082 attributes.setReadOnly(readOnly,true); 1083 } 1084 } 1085 1086 1087 1088 1092 1093 protected void synchronizeData() { 1094 1095 needsSyncData(false); 1097 1098 boolean orig = ownerDocument.getMutationEvents(); 1100 ownerDocument.setMutationEvents(false); 1101 1102 setupDefaultAttributes(); 1104 1105 ownerDocument.setMutationEvents(orig); 1107 1108 } 1110 void moveSpecifiedAttributes(ElementImpl el) { 1113 if (needsSyncData()) { 1114 synchronizeData(); 1115 } 1116 if (el.hasAttributes()) { 1117 if (attributes == null) { 1118 attributes = new AttributeMap(this, null); 1119 } 1120 attributes.moveSpecifiedAttributes(el.attributes); 1121 } 1122 } 1123 1124 1125 protected void setupDefaultAttributes() { 1126 NamedNodeMapImpl defaults = getDefaultAttributes(); 1127 if (defaults != null) { 1128 attributes = new AttributeMap(this, defaults); 1129 } 1130 } 1131 1132 1133 protected void reconcileDefaultAttributes() { 1134 if (attributes != null) { 1135 NamedNodeMapImpl defaults = getDefaultAttributes(); 1136 attributes.reconcileDefaults(defaults); 1137 } 1138 } 1139 1140 1141 protected NamedNodeMapImpl getDefaultAttributes() { 1142 1143 DocumentTypeImpl doctype = 1144 (DocumentTypeImpl) ownerDocument.getDoctype(); 1145 if (doctype == null) { 1146 return null; 1147 } 1148 ElementDefinitionImpl eldef = 1149 (ElementDefinitionImpl)doctype.getElements() 1150 .getNamedItem(getNodeName()); 1151 if (eldef == null) { 1152 return null; 1153 } 1154 return (NamedNodeMapImpl) eldef.getAttributes(); 1155 1156 } 1158} | Popular Tags |