1 57 58 package org.enhydra.apache.xerces.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 93 public class ElementImpl 94 extends ParentNode 95 implements Element { 96 97 101 102 static final long serialVersionUID = 3717253516652722278L; 103 107 108 protected String name; 109 110 111 protected AttributeMap attributes; 112 113 117 118 public ElementImpl(CoreDocumentImpl ownerDoc, String name) { 119 super(ownerDoc); 120 this.name = name; 121 needsSyncData(true); } 123 124 protected ElementImpl() {} 126 127 131 132 136 public short getNodeType() { 137 return Node.ELEMENT_NODE; 138 } 139 140 143 public String getNodeName() { 144 if (needsSyncData()) { 145 synchronizeData(); 146 } 147 return name; 148 } 149 150 156 public NamedNodeMap getAttributes() { 157 158 if (needsSyncData()) { 159 synchronizeData(); 160 } 161 if (attributes == null) { 162 attributes = new AttributeMap(this, null); 163 } 164 return attributes; 165 166 } 168 175 public Node cloneNode(boolean deep) { 176 177 ElementImpl newnode = (ElementImpl) super.cloneNode(deep); 178 if (attributes != null) { 180 newnode.attributes = (AttributeMap) attributes.cloneMap(newnode); 181 } 182 return newnode; 183 184 } 186 187 191 void setOwnerDocument(CoreDocumentImpl doc) { 192 super.setOwnerDocument(doc); 193 if (attributes != null) { 194 attributes.setOwnerDocument(doc); 195 } 196 } 197 198 202 211 public String getAttribute(String name) { 212 213 if (needsSyncData()) { 214 synchronizeData(); 215 } 216 if (attributes == null) { 217 return ""; 218 } 219 Attr attr = (Attr )(attributes.getNamedItem(name)); 220 return (attr == null) ? "" : attr.getValue(); 221 222 } 224 225 232 public Attr getAttributeNode(String name) { 233 234 if (needsSyncData()) { 235 synchronizeData(); 236 } 237 if (attributes == null) { 238 return null; 239 } 240 return (Attr )attributes.getNamedItem(name); 241 242 } 244 245 260 public NodeList getElementsByTagName(String tagname) { 261 return new DeepNodeListImpl(this,tagname); 262 } 263 264 271 public String getTagName() { 272 if (needsSyncData()) { 273 synchronizeData(); 274 } 275 return name; 276 } 277 278 292 public void normalize() { 293 if (isNormalized()) { 295 return; 296 } 297 if (needsSyncChildren()) { 298 synchronizeChildren(); 299 } 300 ChildNode kid, next; 301 for (kid = firstChild; kid != null; kid = next) { 302 next = kid.nextSibling; 303 304 if ( kid.getNodeType() == Node.TEXT_NODE ) 310 { 311 if ( next!=null && next.getNodeType() == Node.TEXT_NODE ) 313 { 314 ((Text )kid).appendData(next.getNodeValue()); 315 removeChild( next ); 316 next = kid; } 318 else 319 { 320 if ( kid.getNodeValue().length()==0 ) 322 removeChild( kid ); 323 } 324 } 325 326 else if (kid.getNodeType() == Node.ELEMENT_NODE) { 328 kid.normalize(); 329 } 330 } 331 332 if ( attributes!=null ) 334 { 335 for( int i=0; i<attributes.getLength(); ++i ) 336 { 337 Node attr = attributes.item(i); 338 attr.normalize(); 339 } 340 } 341 342 345 isNormalized(true); 346 } 348 363 public void removeAttribute(String name) { 364 365 if (ownerDocument.errorChecking && isReadOnly()) { 366 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, 367 "DOM001 Modification not allowed"); 368 } 369 370 if (needsSyncData()) { 371 synchronizeData(); 372 } 373 374 if (attributes == null) { 375 return; 376 } 377 378 attributes.safeRemoveNamedItem(name); 379 380 } 382 383 399 public Attr removeAttributeNode(Attr oldAttr) 400 throws DOMException { 401 402 if (ownerDocument.errorChecking && isReadOnly()) { 403 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, 404 "DOM001 Modification not allowed"); 405 } 406 407 if (needsSyncData()) { 408 synchronizeData(); 409 } 410 411 if (attributes == null) { 412 throw new DOMException (DOMException.NOT_FOUND_ERR, 413 "DOM008 Not found"); 414 } 415 return (Attr ) attributes.removeNamedItem(oldAttr.getName()); 416 417 } 419 420 439 public void setAttribute(String name, String value) { 440 441 if (ownerDocument.errorChecking && isReadOnly()) { 442 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, 443 "DOM001 Modification not allowed"); 444 } 445 446 if (needsSyncData()) { 447 synchronizeData(); 448 } 449 450 Attr newAttr = getAttributeNode(name); 451 if (newAttr == null) { 452 newAttr = getOwnerDocument().createAttribute(name); 453 454 if (attributes == null) { 455 attributes = new AttributeMap(this, null); 456 } 457 458 newAttr.setNodeValue(value); 459 attributes.setNamedItem(newAttr); 460 } 461 else { 462 newAttr.setNodeValue(value); 463 } 464 465 } 467 480 public Attr setAttributeNode(Attr newAttr) 481 throws DOMException 482 { 483 484 if (needsSyncData()) { 485 synchronizeData(); 486 } 487 488 if (ownerDocument.errorChecking) { 489 if (isReadOnly()) { 490 throw new DOMException ( 491 DOMException.NO_MODIFICATION_ALLOWED_ERR, 492 "DOM001 Modification not allowed"); 493 } 494 495 if (newAttr.getOwnerDocument() != ownerDocument) { 496 throw new DOMException (DOMException.WRONG_DOCUMENT_ERR, 497 "DOM005 Wrong document"); 498 } 499 } 500 501 if (attributes == null) { 502 attributes = new AttributeMap(this, null); 503 } 504 return (Attr ) attributes.setNamedItem(newAttr); 506 507 } 509 513 527 public String getAttributeNS(String namespaceURI, String localName) { 528 529 if (needsSyncData()) { 530 synchronizeData(); 531 } 532 533 if (attributes == null) { 534 return ""; 535 } 536 537 Attr attr = (Attr )(attributes.getNamedItemNS(namespaceURI, localName)); 538 return (attr == null) ? "" : attr.getValue(); 539 540 } 542 583 public void setAttributeNS(String namespaceURI, String localName, String value) { 584 585 if (ownerDocument.errorChecking && isReadOnly()) { 586 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, 587 "DOM001 Modification not allowed"); 588 } 589 590 if (needsSyncData()) { 591 synchronizeData(); 592 } 593 594 Attr newAttr = getAttributeNodeNS(namespaceURI, localName); 595 if (newAttr == null) { 596 newAttr = 597 getOwnerDocument().createAttributeNS(namespaceURI, localName); 598 599 if (attributes == null) { 600 attributes = new AttributeMap(this, null); 601 } 602 newAttr.setNodeValue(value); 603 attributes.setNamedItemNS(newAttr); 604 } 605 else { 606 newAttr.setNodeValue(value); 607 } 608 609 } 611 626 public void removeAttributeNS(String namespaceURI, String localName) { 627 628 if (ownerDocument.errorChecking && isReadOnly()) { 629 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, 630 "DOM001 Modification not allowed"); 631 } 632 633 if (needsSyncData()) { 634 synchronizeData(); 635 } 636 637 if (attributes == null) { 638 return; 639 } 640 641 attributes.safeRemoveNamedItemNS(namespaceURI, localName); 642 643 } 645 656 public Attr getAttributeNodeNS(String namespaceURI, String localName){ 657 658 if (needsSyncData()) { 659 synchronizeData(); 660 } 661 if (attributes == null) { 662 return null; 663 } 664 return (Attr )attributes.getNamedItemNS(namespaceURI, localName); 665 666 } 668 695 public Attr setAttributeNodeNS(Attr newAttr) 696 throws DOMException 697 { 698 699 if (needsSyncData()) { 700 synchronizeData(); 701 } 702 if (ownerDocument.errorChecking) { 703 if (isReadOnly()) { 704 throw new DOMException ( 705 DOMException.NO_MODIFICATION_ALLOWED_ERR, 706 "DOM001 Modification not allowed"); 707 } 708 if (newAttr.getOwnerDocument() != ownerDocument) { 709 throw new DOMException (DOMException.WRONG_DOCUMENT_ERR, 710 "DOM005 Wrong document"); 711 } 712 } 713 714 if (attributes == null) { 715 attributes = new AttributeMap(this, null); 716 } 717 return (Attr ) attributes.setNamedItemNS(newAttr); 719 720 } 722 725 public boolean hasAttributes() { 726 if (needsSyncData()) { 727 synchronizeData(); 728 } 729 return (attributes != null && attributes.getLength() != 0); 730 } 731 732 735 public boolean hasAttribute(String name) { 736 return getAttributeNode(name) != null; 737 } 738 739 742 public boolean hasAttributeNS(String namespaceURI, String localName) { 743 return getAttributeNodeNS(namespaceURI, localName) != null; 744 } 745 746 764 public NodeList getElementsByTagNameNS(String namespaceURI, 765 String localName) { 766 return new DeepNodeListImpl(this, namespaceURI, localName); 767 } 768 769 773 777 public void setReadOnly(boolean readOnly, boolean deep) { 778 super.setReadOnly(readOnly,deep); 779 if (attributes != null) { 780 attributes.setReadOnly(readOnly,true); 781 } 782 } 783 784 788 789 protected void synchronizeData() { 790 791 needsSyncData(false); 793 794 boolean orig = ownerDocument.getMutationEvents(); 796 ownerDocument.setMutationEvents(false); 797 798 setupDefaultAttributes(); 800 801 ownerDocument.setMutationEvents(orig); 803 804 } 806 807 protected void setupDefaultAttributes() { 808 NamedNodeMapImpl defaults = getDefaultAttributes(); 809 if (defaults != null) { 810 attributes = new AttributeMap(this, defaults); 811 } 812 } 813 814 815 protected void reconcileDefaultAttributes() { 816 NamedNodeMapImpl defaults = getDefaultAttributes(); 817 if (defaults != null) { 818 attributes.reconcileDefaults(defaults); 819 } 820 } 821 822 823 protected NamedNodeMapImpl getDefaultAttributes() { 824 825 DocumentTypeImpl doctype = 826 (DocumentTypeImpl) ownerDocument.getDoctype(); 827 if (doctype == null) { 828 return null; 829 } 830 ElementDefinitionImpl eldef = 831 (ElementDefinitionImpl)doctype.getElements() 832 .getNamedItem(getNodeName()); 833 if (eldef == null) { 834 return null; 835 } 836 return (NamedNodeMapImpl) eldef.getAttributes(); 837 838 } 840 841 844 public TypeInfo getSchemaTypeInfo() { 845 return null; 847 } 848 851 public void setIdAttribute(String name, boolean isId) throws DOMException { 852 854 } 855 858 public void setIdAttributeNS(String namespaceURI, String localName, boolean isId) throws DOMException { 859 861 } 862 865 public void setIdAttributeNode(Attr idAttr, boolean isId) throws DOMException { 866 868 } 869 870 } | Popular Tags |