1 57 58 package org.xquark.xpath.datamodel.xerces.dom; 59 60 import org.w3c.dom.*; 61 62 86 public class ElementImpl 87 extends ParentNode 88 implements Element { 89 90 94 95 static final long serialVersionUID = 3717253516652722278L; 96 100 101 protected String name; 102 103 104 protected AttributeMap attributes; 105 106 110 111 public ElementImpl(DocumentImpl ownerDoc, String name) { 112 super(ownerDoc); 113 this.name = name; 114 needsSyncData(true); } 116 117 protected ElementImpl() {} 119 120 124 125 129 public short getNodeType() { 130 return Node.ELEMENT_NODE; 131 } 132 133 136 public String getNodeName() { 137 if (needsSyncData()) { 138 synchronizeData(); 139 } 140 return name; 141 } 142 143 149 public NamedNodeMap getAttributes() { 150 151 if (needsSyncData()) { 152 synchronizeData(); 153 } 154 if (attributes == null) { 155 attributes = new AttributeMap(this, null); 156 } 157 return attributes; 158 159 } 161 168 public Node cloneNode(boolean deep) { 169 170 ElementImpl newnode = (ElementImpl) super.cloneNode(deep); 171 if (attributes != null) { 173 newnode.attributes = (AttributeMap) attributes.cloneMap(newnode); 174 } 175 return newnode; 176 177 } 179 180 184 void setOwnerDocument(DocumentImpl doc) { 185 super.setOwnerDocument(doc); 186 if (attributes != null) { 187 attributes.setOwnerDocument(doc); 188 } 189 } 190 191 195 204 public String getAttribute(String name) { 205 206 if (needsSyncData()) { 207 synchronizeData(); 208 } 209 if (attributes == null) { 210 return ""; 211 } 212 Attr attr = (Attr)(attributes.getNamedItem(name)); 213 return (attr == null) ? "" : attr.getValue(); 214 215 } 217 218 225 public Attr getAttributeNode(String name) { 226 227 if (needsSyncData()) { 228 synchronizeData(); 229 } 230 if (attributes == null) { 231 return null; 232 } 233 return (Attr)attributes.getNamedItem(name); 234 235 } 237 238 253 public NodeList getElementsByTagName(String tagname) { 254 return new DeepNodeListImpl(this,tagname); 255 } 256 257 264 public String getTagName() { 265 if (needsSyncData()) { 266 synchronizeData(); 267 } 268 return name; 269 } 270 271 285 public void normalize() { 286 if (isNormalized()) { 288 return; 289 } 290 if (needsSyncChildren()) { 291 synchronizeChildren(); 292 } 293 ChildNode kid, next; 294 for (kid = firstChild; kid != null; kid = next) { 295 next = kid.nextSibling; 296 297 if ( kid.getNodeType() == Node.TEXT_NODE ) 303 { 304 if ( next!=null && next.getNodeType() == Node.TEXT_NODE ) 306 { 307 ((Text)kid).appendData(next.getNodeValue()); 308 removeChild( next ); 309 next = kid; } 311 else 312 { 313 if ( kid.getNodeValue().length()==0 ) 315 removeChild( kid ); 316 } 317 } 318 319 else if (kid.getNodeType() == Node.ELEMENT_NODE) { 321 kid.normalize(); 322 } 323 } 324 325 if ( attributes!=null ) 327 { 328 for( int i=0; i<attributes.getLength(); ++i ) 329 { 330 Node attr = attributes.item(i); 331 attr.normalize(); 332 } 333 } 334 335 338 isNormalized(true); 339 } 341 356 public void removeAttribute(String name) { 357 358 if (ownerDocument.errorChecking && isReadOnly()) { 359 throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, 360 "DOM001 Modification not allowed"); 361 } 362 363 if (needsSyncData()) { 364 synchronizeData(); 365 } 366 367 if (attributes == null) { 368 return; 369 } 370 371 attributes.safeRemoveNamedItem(name); 372 373 } 375 376 392 public Attr removeAttributeNode(Attr oldAttr) 393 throws DOMException { 394 395 if (ownerDocument.errorChecking && isReadOnly()) { 396 throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, 397 "DOM001 Modification not allowed"); 398 } 399 400 if (needsSyncData()) { 401 synchronizeData(); 402 } 403 404 if (attributes == null) { 405 throw new DOMException(DOMException.NOT_FOUND_ERR, 406 "DOM008 Not found"); 407 } 408 return (Attr) attributes.removeNamedItem(oldAttr.getName()); 409 410 } 412 413 432 public void setAttribute(String name, String value) { 433 434 if (ownerDocument.errorChecking && isReadOnly()) { 435 throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, 436 "DOM001 Modification not allowed"); 437 } 438 439 if (needsSyncData()) { 440 synchronizeData(); 441 } 442 443 Attr newAttr = getAttributeNode(name); 444 if (newAttr == null) { 445 newAttr = getOwnerDocument().createAttribute(name); 446 447 if (attributes == null) { 448 attributes = new AttributeMap(this, null); 449 } 450 451 newAttr.setNodeValue(value); 452 attributes.setNamedItem(newAttr); 453 } 454 else { 455 newAttr.setNodeValue(value); 456 } 457 458 } 460 473 public Attr setAttributeNode(Attr newAttr) 474 throws DOMException 475 { 476 477 if (needsSyncData()) { 478 synchronizeData(); 479 } 480 481 if (ownerDocument.errorChecking) { 482 if (isReadOnly()) { 483 throw new DOMException( 484 DOMException.NO_MODIFICATION_ALLOWED_ERR, 485 "DOM001 Modification not allowed"); 486 } 487 488 if (newAttr.getOwnerDocument() != ownerDocument) { 489 throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, 490 "DOM005 Wrong document"); 491 } 492 } 493 494 if (attributes == null) { 495 attributes = new AttributeMap(this, null); 496 } 497 return (Attr) attributes.setNamedItem(newAttr); 499 500 } 502 506 520 public String getAttributeNS(String namespaceURI, String localName) { 521 522 if (needsSyncData()) { 523 synchronizeData(); 524 } 525 526 if (attributes == null) { 527 return ""; 528 } 529 530 Attr attr = (Attr)(attributes.getNamedItemNS(namespaceURI, localName)); 531 return (attr == null) ? "" : attr.getValue(); 532 533 } 535 575 public void setAttributeNS(String namespaceURI, String localName, String value) { 576 577 if (ownerDocument.errorChecking && isReadOnly()) { 578 throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, 579 "DOM001 Modification not allowed"); 580 } 581 582 if (needsSyncData()) { 583 synchronizeData(); 584 } 585 586 Attr newAttr = getAttributeNodeNS(namespaceURI, localName); 587 if (newAttr == null) { 588 newAttr = 589 getOwnerDocument().createAttributeNS(namespaceURI, localName); 590 591 if (attributes == null) { 592 attributes = new AttributeMap(this, null); 593 } 594 newAttr.setNodeValue(value); 595 attributes.setNamedItemNS(newAttr); 596 } 597 else { 598 newAttr.setNodeValue(value); 599 } 600 601 } 603 618 public void removeAttributeNS(String namespaceURI, String localName) { 619 620 if (ownerDocument.errorChecking && isReadOnly()) { 621 throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, 622 "DOM001 Modification not allowed"); 623 } 624 625 if (needsSyncData()) { 626 synchronizeData(); 627 } 628 629 if (attributes == null) { 630 return; 631 } 632 633 attributes.safeRemoveNamedItemNS(namespaceURI, localName); 634 635 } 637 648 public Attr getAttributeNodeNS(String namespaceURI, String localName){ 649 650 if (needsSyncData()) { 651 synchronizeData(); 652 } 653 if (attributes == null) { 654 return null; 655 } 656 return (Attr)attributes.getNamedItemNS(namespaceURI, localName); 657 658 } 660 686 public Attr setAttributeNodeNS(Attr newAttr) 687 throws DOMException 688 { 689 690 if (needsSyncData()) { 691 synchronizeData(); 692 } 693 if (ownerDocument.errorChecking) { 694 if (isReadOnly()) { 695 throw new DOMException( 696 DOMException.NO_MODIFICATION_ALLOWED_ERR, 697 "DOM001 Modification not allowed"); 698 } 699 if (newAttr.getOwnerDocument() != ownerDocument) { 700 throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, 701 "DOM005 Wrong document"); 702 } 703 } 704 705 if (attributes == null) { 706 attributes = new AttributeMap(this, null); 707 } 708 return (Attr) attributes.setNamedItemNS(newAttr); 710 711 } 713 716 public boolean hasAttributes() { 717 if (needsSyncData()) { 718 synchronizeData(); 719 } 720 return (attributes != null && attributes.getLength() != 0); 721 } 722 723 726 public boolean hasAttribute(String name) { 727 return getAttributeNode(name) != null; 728 } 729 730 733 public boolean hasAttributeNS(String namespaceURI, String localName) { 734 return getAttributeNodeNS(namespaceURI, localName) != null; 735 } 736 737 754 public NodeList getElementsByTagNameNS(String namespaceURI, String localName) { 755 return new DeepNodeListImpl(this, namespaceURI, localName); 756 } 757 758 762 766 public void setReadOnly(boolean readOnly, boolean deep) { 767 super.setReadOnly(readOnly,deep); 768 if (attributes != null) { 769 attributes.setReadOnly(readOnly,true); 770 } 771 } 772 773 777 778 protected void synchronizeData() { 779 780 needsSyncData(false); 782 783 boolean orig = ownerDocument.mutationEvents; 785 ownerDocument.mutationEvents = false; 786 787 setupDefaultAttributes(); 789 790 ownerDocument.mutationEvents = orig; 792 793 } 795 796 protected void setupDefaultAttributes() { 797 NamedNodeMapImpl defaults = getDefaultAttributes(); 798 if (defaults != null) { 799 attributes = new AttributeMap(this, defaults); 800 } 801 } 802 803 804 protected void reconcileDefaultAttributes() { 805 NamedNodeMapImpl defaults = getDefaultAttributes(); 806 if (defaults != null) { 807 attributes.reconcileDefaults(defaults); 808 } 809 } 810 811 812 protected NamedNodeMapImpl getDefaultAttributes() { 813 814 DocumentTypeImpl doctype = 815 (DocumentTypeImpl) ownerDocument.getDoctype(); 816 if (doctype == null) { 817 return null; 818 } 819 ElementDefinitionImpl eldef = 820 (ElementDefinitionImpl)doctype.getElements() 821 .getNamedItem(getNodeName()); 822 if (eldef == null) { 823 return null; 824 } 825 return (NamedNodeMapImpl) eldef.getAttributes(); 826 827 } 829 } | Popular Tags |