1 57 58 package com.sun.org.apache.xerces.internal.dom; 59 60 import org.w3c.dom.Attr ; 61 import org.w3c.dom.DOMException ; 62 import org.w3c.dom.Element ; 63 import org.w3c.dom.NamedNodeMap ; 64 import org.w3c.dom.Node ; 65 import org.w3c.dom.NodeList ; 66 import org.w3c.dom.Text ; 67 import org.w3c.dom.TypeInfo ; 68 69 import com.sun.org.apache.xerces.internal.util.URI; 70 71 95 public class ElementImpl 96 extends ParentNode 97 implements Element { 98 99 103 104 static final long serialVersionUID = 3717253516652722278L; 105 109 110 protected String name; 111 112 113 protected AttributeMap attributes; 114 115 116 transient TypeInfo type; 118 119 123 124 public ElementImpl(CoreDocumentImpl ownerDoc, String name) { 125 super(ownerDoc); 126 this.name = name; 127 needsSyncData(true); } 129 130 protected ElementImpl() {} 132 133 void rename(String name) { 137 if (needsSyncData()) { 138 synchronizeData(); 139 } 140 this.name = name; 141 reconcileDefaultAttributes(); 142 } 143 144 148 149 153 public short getNodeType() { 154 return Node.ELEMENT_NODE; 155 } 156 157 160 public String getNodeName() { 161 if (needsSyncData()) { 162 synchronizeData(); 163 } 164 return name; 165 } 166 167 173 public NamedNodeMap getAttributes() { 174 175 if (needsSyncData()) { 176 synchronizeData(); 177 } 178 if (attributes == null) { 179 attributes = new AttributeMap(this, null); 180 } 181 return attributes; 182 183 } 185 192 public Node cloneNode(boolean deep) { 193 194 ElementImpl newnode = (ElementImpl) super.cloneNode(deep); 195 if (attributes != null) { 197 newnode.attributes = (AttributeMap) attributes.cloneMap(newnode); 198 } 199 return newnode; 200 201 } 203 207 public String getBaseURI() { 208 209 if (needsSyncData()) { 210 synchronizeData(); 211 } 212 if (attributes != null) { 217 Attr attrNode = (Attr )attributes.getNamedItem("xml:base"); 218 if (attrNode != null) { 219 String uri = attrNode.getNodeValue(); 220 if (uri.length() != 0 ) { try { 222 uri = new URI(uri).toString(); 223 } 224 catch (com.sun.org.apache.xerces.internal.util.URI.MalformedURIException e){ 225 return null; 226 } 227 return uri; 228 } 229 } 230 } 231 232 237 String baseURI = (this.ownerNode != null) ? this.ownerNode.getBaseURI() : null ; 239 if(baseURI != null){ 241 try { 242 return new URI(baseURI).toString(); 244 } 245 catch (com.sun.org.apache.xerces.internal.util.URI.MalformedURIException e){ 246 return null; 247 } 248 } 249 return null; 250 } 252 253 254 258 void setOwnerDocument(CoreDocumentImpl doc) { 259 super.setOwnerDocument(doc); 260 if (attributes != null) { 261 attributes.setOwnerDocument(doc); 262 } 263 } 264 265 269 278 public String getAttribute(String name) { 279 280 if (needsSyncData()) { 281 synchronizeData(); 282 } 283 if (attributes == null) { 284 return ""; 285 } 286 Attr attr = (Attr )(attributes.getNamedItem(name)); 287 return (attr == null) ? "" : attr.getValue(); 288 289 } 291 292 299 public Attr getAttributeNode(String name) { 300 301 if (needsSyncData()) { 302 synchronizeData(); 303 } 304 if (attributes == null) { 305 return null; 306 } 307 return (Attr )attributes.getNamedItem(name); 308 309 } 311 312 327 public NodeList getElementsByTagName(String tagname) { 328 return new DeepNodeListImpl(this,tagname); 329 } 330 331 338 public String getTagName() { 339 if (needsSyncData()) { 340 synchronizeData(); 341 } 342 return name; 343 } 344 345 359 public void normalize() { 360 if (isNormalized()) { 362 return; 363 } 364 if (needsSyncChildren()) { 365 synchronizeChildren(); 366 } 367 ChildNode kid, next; 368 for (kid = firstChild; kid != null; kid = next) { 369 next = kid.nextSibling; 370 371 if ( kid.getNodeType() == Node.TEXT_NODE ) 377 { 378 if ( next!=null && next.getNodeType() == Node.TEXT_NODE ) 380 { 381 ((Text )kid).appendData(next.getNodeValue()); 382 removeChild( next ); 383 next = kid; } 385 else 386 { 387 if ( kid.getNodeValue().length()==0 ) 389 removeChild( kid ); 390 } 391 } 392 393 else if (kid.getNodeType() == Node.ELEMENT_NODE) { 395 kid.normalize(); 396 } 397 } 398 399 if ( attributes!=null ) 401 { 402 for( int i=0; i<attributes.getLength(); ++i ) 403 { 404 Node attr = attributes.item(i); 405 attr.normalize(); 406 } 407 } 408 409 412 isNormalized(true); 413 } 415 430 public void removeAttribute(String name) { 431 432 if (ownerDocument.errorChecking && isReadOnly()) { 433 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 434 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 435 } 436 437 if (needsSyncData()) { 438 synchronizeData(); 439 } 440 441 if (attributes == null) { 442 return; 443 } 444 445 attributes.safeRemoveNamedItem(name); 446 447 } 449 450 466 public Attr removeAttributeNode(Attr oldAttr) 467 throws DOMException { 468 469 if (ownerDocument.errorChecking && isReadOnly()) { 470 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 471 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 472 } 473 474 if (needsSyncData()) { 475 synchronizeData(); 476 } 477 478 if (attributes == null) { 479 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null); 480 throw new DOMException (DOMException.NOT_FOUND_ERR, msg); 481 } 482 return (Attr ) attributes.removeItem(oldAttr, true); 483 484 } 486 487 506 public void setAttribute(String name, String value) { 507 508 if (ownerDocument.errorChecking && isReadOnly()) { 509 String msg = 510 DOMMessageFormatter.formatMessage( 511 DOMMessageFormatter.DOM_DOMAIN, 512 "NO_MODIFICATION_ALLOWED_ERR", 513 null); 514 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 515 } 516 517 if (needsSyncData()) { 518 synchronizeData(); 519 } 520 521 Attr newAttr = getAttributeNode(name); 522 if (newAttr == null) { 523 newAttr = getOwnerDocument().createAttribute(name); 524 525 if (attributes == null) { 526 attributes = new AttributeMap(this, null); 527 } 528 529 newAttr.setNodeValue(value); 530 attributes.setNamedItem(newAttr); 531 } 532 else { 533 newAttr.setNodeValue(value); 534 } 535 536 } 538 551 public Attr setAttributeNode(Attr newAttr) 552 throws DOMException 553 { 554 555 if (needsSyncData()) { 556 synchronizeData(); 557 } 558 559 if (ownerDocument.errorChecking) { 560 if (isReadOnly()) { 561 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 562 throw new DOMException ( 563 DOMException.NO_MODIFICATION_ALLOWED_ERR, 564 msg); 565 } 566 567 if (newAttr.getOwnerDocument() != ownerDocument) { 568 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null); 569 throw new DOMException (DOMException.WRONG_DOCUMENT_ERR, msg); 570 } 571 } 572 573 if (attributes == null) { 574 attributes = new AttributeMap(this, null); 575 } 576 return (Attr ) attributes.setNamedItem(newAttr); 578 579 } 581 585 599 public String getAttributeNS(String namespaceURI, String localName) { 600 601 if (needsSyncData()) { 602 synchronizeData(); 603 } 604 605 if (attributes == null) { 606 return ""; 607 } 608 609 Attr attr = (Attr )(attributes.getNamedItemNS(namespaceURI, localName)); 610 return (attr == null) ? "" : attr.getValue(); 611 612 } 614 655 public void setAttributeNS(String namespaceURI,String qualifiedName, 656 String value) { 657 if (ownerDocument.errorChecking && isReadOnly()) { 658 String msg = 659 DOMMessageFormatter.formatMessage( 660 DOMMessageFormatter.DOM_DOMAIN, 661 "NO_MODIFICATION_ALLOWED_ERR", 662 null); 663 throw new DOMException ( 664 DOMException.NO_MODIFICATION_ALLOWED_ERR, 665 msg); 666 } 667 if (needsSyncData()) { 668 synchronizeData(); 669 } 670 int index = qualifiedName.indexOf(':'); 671 String prefix, localName; 672 if (index < 0) { 673 prefix = null; 674 localName = qualifiedName; 675 } 676 else { 677 prefix = qualifiedName.substring(0, index); 678 localName = qualifiedName.substring(index + 1); 679 } 680 Attr newAttr = getAttributeNodeNS(namespaceURI, localName); 681 if (newAttr == null) { 682 newAttr = getOwnerDocument().createAttributeNS( 685 namespaceURI, 686 qualifiedName); 687 if (attributes == null) { 688 attributes = new AttributeMap(this, null); 689 } 690 newAttr.setNodeValue(value); 691 attributes.setNamedItemNS(newAttr); 692 } 693 else { 694 if (newAttr instanceof AttrNSImpl){ 695 ((AttrNSImpl)newAttr).name= (prefix!=null)?(prefix+":"+localName):localName; 697 } 698 else { 699 newAttr = new AttrNSImpl((CoreDocumentImpl)getOwnerDocument(), namespaceURI, qualifiedName, localName); 706 attributes.setNamedItemNS(newAttr); 707 } 708 709 newAttr.setNodeValue(value); 710 } 711 712 } 714 715 730 public void removeAttributeNS(String namespaceURI, String localName) { 731 732 if (ownerDocument.errorChecking && isReadOnly()) { 733 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 734 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 735 } 736 737 if (needsSyncData()) { 738 synchronizeData(); 739 } 740 741 if (attributes == null) { 742 return; 743 } 744 745 attributes.safeRemoveNamedItemNS(namespaceURI, localName); 746 747 } 749 760 public Attr getAttributeNodeNS(String namespaceURI, String localName){ 761 762 if (needsSyncData()) { 763 synchronizeData(); 764 } 765 if (attributes == null) { 766 return null; 767 } 768 return (Attr )attributes.getNamedItemNS(namespaceURI, localName); 769 770 } 772 799 public Attr setAttributeNodeNS(Attr newAttr) 800 throws DOMException 801 { 802 803 if (needsSyncData()) { 804 synchronizeData(); 805 } 806 if (ownerDocument.errorChecking) { 807 if (isReadOnly()) { 808 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 809 throw new DOMException ( 810 DOMException.NO_MODIFICATION_ALLOWED_ERR, 811 msg); 812 } 813 if (newAttr.getOwnerDocument() != ownerDocument) { 814 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null); 815 throw new DOMException (DOMException.WRONG_DOCUMENT_ERR, msg); 816 } 817 } 818 819 if (attributes == null) { 820 attributes = new AttributeMap(this, null); 821 } 822 return (Attr ) attributes.setNamedItemNS(newAttr); 824 825 } 827 830 protected int setXercesAttributeNode (Attr attr){ 831 832 if (needsSyncData()) { 833 synchronizeData(); 834 } 835 836 if (attributes == null) { 837 attributes = new AttributeMap(this, null); 838 } 839 return attributes.addItem(attr); 840 841 } 842 843 846 protected int getXercesAttribute(String namespaceURI, String localName){ 847 848 if (needsSyncData()) { 849 synchronizeData(); 850 } 851 if (attributes == null) { 852 return -1; 853 } 854 return attributes.getNamedItemIndex(namespaceURI, localName); 855 856 } 857 858 861 public boolean hasAttributes() { 862 if (needsSyncData()) { 863 synchronizeData(); 864 } 865 return (attributes != null && attributes.getLength() != 0); 866 } 867 868 871 public boolean hasAttribute(String name) { 872 return getAttributeNode(name) != null; 873 } 874 875 878 public boolean hasAttributeNS(String namespaceURI, String localName) { 879 return getAttributeNodeNS(namespaceURI, localName) != null; 880 } 881 882 900 public NodeList getElementsByTagNameNS(String namespaceURI, 901 String localName) { 902 return new DeepNodeListImpl(this, namespaceURI, localName); 903 } 904 905 910 public boolean isEqualNode(Node arg) { 911 if (!super.isEqualNode(arg)) { 912 return false; 913 } 914 boolean hasAttrs = hasAttributes(); 915 if (hasAttrs != ((Element ) arg).hasAttributes()) { 916 return false; 917 } 918 if (hasAttrs) { 919 NamedNodeMap map1 = getAttributes(); 920 NamedNodeMap map2 = ((Element ) arg).getAttributes(); 921 int len = map1.getLength(); 922 if (len != map2.getLength()) { 923 return false; 924 } 925 for (int i = 0; i < len; i++) { 926 Node n1 = map1.item(i); 927 if (n1.getLocalName() == null) { Node n2 = map2.getNamedItem(n1.getNodeName()); 929 if (n2 == null || !((NodeImpl) n1).isEqualNode(n2)) { 930 return false; 931 } 932 } 933 else { 934 Node n2 = map2.getNamedItemNS(n1.getNamespaceURI(), 935 n1.getLocalName()); 936 if (n2 == null || !((NodeImpl) n1).isEqualNode(n2)) { 937 return false; 938 } 939 } 940 } 941 } 942 return true; 943 } 944 945 948 public void setIdAttributeNode(Attr at, boolean makeId) { 949 if (needsSyncData()) { 950 synchronizeData(); 951 } 952 if (ownerDocument.errorChecking) { 953 if (isReadOnly()) { 954 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 955 throw new DOMException ( 956 DOMException.NO_MODIFICATION_ALLOWED_ERR, 957 msg); 958 } 959 960 if (at.getOwnerElement() != this) { 961 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null); 962 throw new DOMException (DOMException.NOT_FOUND_ERR, msg); 963 } 964 } 965 ((AttrImpl) at).isIdAttribute(makeId); 966 if (!makeId) { 967 ownerDocument.removeIdentifier(at.getValue()); 968 } 969 else { 970 ownerDocument.putIdentifier(at.getValue(), this); 971 } 972 } 973 974 977 public void setIdAttribute(String name, boolean makeId) { 978 if (needsSyncData()) { 979 synchronizeData(); 980 } 981 Attr at = getAttributeNode(name); 982 983 if( at == null){ 984 String msg = DOMMessageFormatter.formatMessage( 985 DOMMessageFormatter.DOM_DOMAIN, 986 "NOT_FOUND_ERR", null); 987 throw new DOMException (DOMException.NOT_FOUND_ERR, msg); 988 } 989 990 if (ownerDocument.errorChecking) { 991 if (isReadOnly()) { 992 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 993 throw new DOMException ( 994 DOMException.NO_MODIFICATION_ALLOWED_ERR, 995 msg); 996 } 997 998 if (at.getOwnerElement() != this) { 999 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null); 1000 throw new DOMException (DOMException.NOT_FOUND_ERR, msg); 1001 } 1002 } 1003 1004 ((AttrImpl) at).isIdAttribute(makeId); 1005 if (!makeId) { 1006 ownerDocument.removeIdentifier(at.getValue()); 1007 } 1008 else { 1009 ownerDocument.putIdentifier(at.getValue(), this); 1010 } 1011 } 1012 1013 1016 public void setIdAttributeNS(String namespaceURI, String localName, 1017 boolean makeId) { 1018 if (needsSyncData()) { 1019 synchronizeData(); 1020 } 1021 if (namespaceURI != null) { 1023 namespaceURI = (namespaceURI.length() == 0)? null : namespaceURI; 1024 } 1025 Attr at = getAttributeNodeNS(namespaceURI, localName); 1026 1027 if( at == null){ 1028 String msg = DOMMessageFormatter.formatMessage( 1029 DOMMessageFormatter.DOM_DOMAIN, 1030 "NOT_FOUND_ERR", null); 1031 throw new DOMException (DOMException.NOT_FOUND_ERR, msg); 1032 } 1033 1034 if (ownerDocument.errorChecking) { 1035 if (isReadOnly()) { 1036 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 1037 throw new DOMException ( 1038 DOMException.NO_MODIFICATION_ALLOWED_ERR, 1039 msg); 1040 } 1041 1042 if (at.getOwnerElement() != this) { 1043 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null); 1044 throw new DOMException (DOMException.NOT_FOUND_ERR, msg); 1045 } 1046 } 1047 ((AttrImpl) at).isIdAttribute(makeId); 1048 if (!makeId) { 1049 ownerDocument.removeIdentifier(at.getValue()); 1050 } 1051 else { 1052 ownerDocument.putIdentifier(at.getValue(), this); 1053 } 1054 } 1055 1056 1057 1058 1062 public void setType(TypeInfo type) { 1063 this.type = type; 1064 } 1065 1066 1070 public TypeInfo getSchemaTypeInfo(){ 1071 if (needsSyncData()) { 1072 synchronizeData(); 1073 } 1074 return type; 1075 } 1076 1077 1081 1085 public void setReadOnly(boolean readOnly, boolean deep) { 1086 super.setReadOnly(readOnly,deep); 1087 if (attributes != null) { 1088 attributes.setReadOnly(readOnly,true); 1089 } 1090 } 1091 1092 1093 1094 1098 1099 protected void synchronizeData() { 1100 1101 needsSyncData(false); 1103 1104 boolean orig = ownerDocument.getMutationEvents(); 1106 ownerDocument.setMutationEvents(false); 1107 1108 setupDefaultAttributes(); 1110 1111 ownerDocument.setMutationEvents(orig); 1113 1114 } 1116 void moveSpecifiedAttributes(ElementImpl el) { 1119 if (needsSyncData()) { 1120 synchronizeData(); 1121 } 1122 if (el.hasAttributes()) { 1123 if (attributes == null) { 1124 attributes = new AttributeMap(this, null); 1125 } 1126 attributes.moveSpecifiedAttributes(el.attributes); 1127 } 1128 } 1129 1130 1131 protected void setupDefaultAttributes() { 1132 NamedNodeMapImpl defaults = getDefaultAttributes(); 1133 if (defaults != null) { 1134 attributes = new AttributeMap(this, defaults); 1135 } 1136 } 1137 1138 1139 protected void reconcileDefaultAttributes() { 1140 if (attributes != null) { 1141 NamedNodeMapImpl defaults = getDefaultAttributes(); 1142 attributes.reconcileDefaults(defaults); 1143 } 1144 } 1145 1146 1147 protected NamedNodeMapImpl getDefaultAttributes() { 1148 1149 DocumentTypeImpl doctype = 1150 (DocumentTypeImpl) ownerDocument.getDoctype(); 1151 if (doctype == null) { 1152 return null; 1153 } 1154 ElementDefinitionImpl eldef = 1155 (ElementDefinitionImpl)doctype.getElements() 1156 .getNamedItem(getNodeName()); 1157 if (eldef == null) { 1158 return null; 1159 } 1160 return (NamedNodeMapImpl) eldef.getAttributes(); 1161 1162 } 1164} | Popular Tags |