1 57 58 package org.enhydra.apache.xerces.dom; 59 60 import java.io.IOException ; 61 import java.io.ObjectInputStream ; 62 import java.io.ObjectOutputStream ; 63 64 import org.w3c.dom.Attr ; 65 import org.w3c.dom.DOMException ; 66 import org.w3c.dom.Element ; 67 import org.w3c.dom.Node ; 68 import org.w3c.dom.NodeList ; 69 import org.w3c.dom.Text ; 70 import org.w3c.dom.TypeInfo ; 71 72 151 public class AttrImpl 152 extends NodeImpl 153 implements Attr { 154 155 159 160 static final long serialVersionUID = 7277707688218972102L; 161 162 166 167 protected Object value = null; 168 169 170 protected String name; 171 172 protected static TextImpl textNode = null; 173 174 178 182 protected AttrImpl(CoreDocumentImpl ownerDocument, String name) { 183 super(ownerDocument); 184 this.name = name; 185 186 isSpecified(true); 187 hasStringValue(true); 188 } 189 190 protected AttrImpl() {} 192 193 protected void makeChildNode() { 195 if (hasStringValue()) { 196 if (value != null) { 197 TextImpl text = 198 (TextImpl) ownerDocument().createTextNode((String ) value); 199 value = text; 200 text.isFirstChild(true); 201 text.previousSibling = text; 202 text.ownerNode = this; 203 text.isOwned(true); 204 } 205 hasStringValue(false); 206 } 207 } 208 209 213 void setOwnerDocument(CoreDocumentImpl doc) { 214 if (needsSyncChildren()) { 215 synchronizeChildren(); 216 } 217 super.setOwnerDocument(doc); 218 if (!hasStringValue()) { 219 for (ChildNode child = (ChildNode) value; 220 child != null; child = child.nextSibling) { 221 child.setOwnerDocument(doc); 222 } 223 } 224 } 225 226 230 public Node cloneNode(boolean deep) { 231 232 if (needsSyncChildren()) { 233 synchronizeChildren(); 234 } 235 AttrImpl clone = (AttrImpl) super.cloneNode(deep); 236 237 if (!clone.hasStringValue()) { 239 240 clone.value = null; 242 243 if (deep) { 245 for (Node child = (Node ) value; child != null; 246 child = child.getNextSibling()) { 247 clone.appendChild(child.cloneNode(true)); 248 } 249 } 250 } 251 clone.isSpecified(true); 252 return clone; 253 } 254 255 259 public short getNodeType() { 260 return Node.ATTRIBUTE_NODE; 261 } 262 263 266 public String getNodeName() { 267 if (needsSyncData()) { 268 synchronizeData(); 269 } 270 return name; 271 } 272 273 279 public void setNodeValue(String value) throws DOMException { 280 setValue(value); 281 } 282 283 289 public String getNodeValue() { 290 return getValue(); 291 } 292 293 297 301 public String getName() { 302 303 if (needsSyncData()) { 304 synchronizeData(); 305 } 306 return name; 307 308 } 310 315 public void setValue(String newvalue) { 316 317 if (isReadOnly()) { 318 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, 319 "DOM001 Modification not allowed"); 320 } 321 CoreDocumentImpl ownerDocument = ownerDocument(); 322 String oldvalue = ""; 323 if (ownerDocument.getMutationEvents()) { 324 if (needsSyncChildren()) { 327 synchronizeChildren(); 328 } 329 if (value != null) { 330 if (hasStringValue()) { 331 oldvalue = (String ) value; 332 if (textNode == null) { 335 textNode = (TextImpl) 336 ownerDocument.createTextNode((String ) value); 337 } 338 else { 339 textNode.data = (String ) value; 340 } 341 value = textNode; 342 textNode.isFirstChild(true); 343 textNode.previousSibling = textNode; 344 textNode.ownerNode = this; 345 textNode.isOwned(true); 346 hasStringValue(false); 347 internalRemoveChild(textNode, true); 348 } 349 else { 350 oldvalue = getValue(); 351 while (value != null) { 352 internalRemoveChild((Node ) value, true); 353 } 354 } 355 } 356 } 357 else 358 { 359 if (!hasStringValue() && value != null) { 361 ChildNode firstChild = (ChildNode) value; 363 firstChild.previousSibling = null; 364 firstChild.isFirstChild(false); 365 } 366 value = null; 368 needsSyncChildren(false); 369 } 370 371 isSpecified(true); 377 if (ownerDocument.getMutationEvents()) { 378 internalInsertBefore(ownerDocument.createTextNode(newvalue), 380 null, true); 381 hasStringValue(false); 382 ownerDocument.modifiedAttrValue(this, oldvalue); 384 } else { 385 value = newvalue; 387 hasStringValue(true); 388 changed(); 389 } 390 391 } 393 397 public String getValue() { 398 399 if (needsSyncChildren()) { 400 synchronizeChildren(); 401 } 402 if (value == null) { 403 return ""; 404 } 405 if (hasStringValue()) { 406 return (String ) value; 407 } 408 ChildNode firstChild = ((ChildNode) value); 409 ChildNode node = firstChild.nextSibling; 410 if (node == null) { 411 return firstChild.getNodeValue(); 412 } 413 StringBuffer value = new StringBuffer (firstChild.getNodeValue()); 414 while (node != null) { 415 value.append(node.getNodeValue()); 416 node = node.nextSibling; 417 } 418 return value.toString(); 419 420 } 422 433 public boolean getSpecified() { 434 435 if (needsSyncData()) { 436 synchronizeData(); 437 } 438 return isSpecified(); 439 440 } 442 446 455 public Element getElement() { 456 return (Element ) (isOwned() ? ownerNode : null); 459 } 460 461 467 public Element getOwnerElement() { 468 return (Element ) (isOwned() ? ownerNode : null); 471 } 472 473 public void normalize() { 474 475 if (isNormalized() || hasStringValue()) 478 return; 479 480 Node kid, next; 481 ChildNode firstChild = (ChildNode)value; 482 for (kid = firstChild; kid != null; kid = next) { 483 next = kid.getNextSibling(); 484 485 if ( kid.getNodeType() == Node.TEXT_NODE ) 491 { 492 if ( next!=null && next.getNodeType() == Node.TEXT_NODE ) 494 { 495 ((Text )kid).appendData(next.getNodeValue()); 496 removeChild( next ); 497 next = kid; } 499 else 500 { 501 if ( kid.getNodeValue().length()==0 ) 503 removeChild( kid ); 504 } 505 } 506 } 507 508 isNormalized(true); 509 } 511 515 516 public void setSpecified(boolean arg) { 517 518 if (needsSyncData()) { 519 synchronizeData(); 520 } 521 isSpecified(arg); 522 523 } 525 529 530 public String toString() { 531 return getName() + "=" + "\"" + getValue() + "\""; 532 } 533 534 538 public boolean hasChildNodes() { 539 if (needsSyncChildren()) { 540 synchronizeChildren(); 541 } 542 return value != null; 543 } 544 545 558 public NodeList getChildNodes() { 559 561 if (needsSyncChildren()) { 562 synchronizeChildren(); 563 } 564 return this; 565 566 } 568 569 public Node getFirstChild() { 570 571 if (needsSyncChildren()) { 572 synchronizeChildren(); 573 } 574 makeChildNode(); 575 return (Node ) value; 576 577 } 579 580 public Node getLastChild() { 581 582 if (needsSyncChildren()) { 583 synchronizeChildren(); 584 } 585 return lastChild(); 586 587 } 589 final ChildNode lastChild() { 590 makeChildNode(); 592 return value != null ? ((ChildNode) value).previousSibling : null; 593 } 594 595 final void lastChild(ChildNode node) { 596 if (value != null) { 598 ((ChildNode) value).previousSibling = node; 599 } 600 } 601 602 630 public Node insertBefore(Node newChild, Node refChild) 631 throws DOMException { 632 return internalInsertBefore(newChild, refChild, false); 634 } 636 641 Node internalInsertBefore(Node newChild, Node refChild, boolean replace) 642 throws DOMException { 643 644 CoreDocumentImpl ownerDocument = ownerDocument(); 645 boolean errorChecking = ownerDocument.errorChecking; 646 647 if (newChild.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE) { 648 655 663 if (errorChecking) { 666 for (Node kid = newChild.getFirstChild(); kid != null; kid = kid.getNextSibling()) { 668 669 if (!ownerDocument.isKidOK(this, kid)) { 670 throw new DOMException ( 671 DOMException.HIERARCHY_REQUEST_ERR, 672 "DOM006 Hierarchy request error"); 673 } 674 } 675 } 676 677 while (newChild.hasChildNodes()) { 678 insertBefore(newChild.getFirstChild(), refChild); 679 } 680 return newChild; 681 } 682 683 if (newChild == refChild) { 684 refChild = refChild.getNextSibling(); 686 removeChild(newChild); 687 insertBefore(newChild, refChild); 688 return newChild; 689 } 690 691 if (needsSyncChildren()) { 692 synchronizeChildren(); 693 } 694 695 if (errorChecking) { 696 if (isReadOnly()) { 697 throw new DOMException ( 698 DOMException.NO_MODIFICATION_ALLOWED_ERR, 699 "DOM001 Modification not allowed"); 700 } 701 if (newChild.getOwnerDocument() != ownerDocument) { 702 throw new DOMException (DOMException.WRONG_DOCUMENT_ERR, 703 "DOM005 Wrong document"); 704 } 705 if (!ownerDocument.isKidOK(this, newChild)) { 706 throw new DOMException (DOMException.HIERARCHY_REQUEST_ERR, 707 "DOM006 Hierarchy request error"); 708 } 709 if (refChild != null && refChild.getParentNode() != this) { 711 throw new DOMException (DOMException.NOT_FOUND_ERR, 712 "DOM008 Not found"); 713 } 714 715 boolean treeSafe = true; 719 for (NodeImpl a = this; treeSafe && a != null; a = a.parentNode()) 720 { 721 treeSafe = newChild != a; 722 } 723 if (!treeSafe) { 724 throw new DOMException (DOMException.HIERARCHY_REQUEST_ERR, 725 "DOM006 Hierarchy request error"); 726 } 727 } 728 729 makeChildNode(); 731 ownerDocument.insertingNode(this, replace); 733 734 ChildNode newInternal = (ChildNode)newChild; 736 737 Node oldparent = newInternal.parentNode(); 738 if (oldparent != null) { 739 oldparent.removeChild(newInternal); 740 } 741 742 ChildNode refInternal = (ChildNode) refChild; 744 745 newInternal.ownerNode = this; 747 newInternal.isOwned(true); 748 749 ChildNode firstChild = (ChildNode) value; 752 if (firstChild == null) { 753 value = newInternal; newInternal.isFirstChild(true); 756 newInternal.previousSibling = newInternal; 757 } 758 else { 759 if (refInternal == null) { 760 ChildNode lastChild = firstChild.previousSibling; 762 lastChild.nextSibling = newInternal; 763 newInternal.previousSibling = lastChild; 764 firstChild.previousSibling = newInternal; 765 } 766 else { 767 if (refChild == firstChild) { 769 firstChild.isFirstChild(false); 771 newInternal.nextSibling = firstChild; 772 newInternal.previousSibling = firstChild.previousSibling; 773 firstChild.previousSibling = newInternal; 774 value = newInternal; newInternal.isFirstChild(true); 776 } 777 else { 778 ChildNode prev = refInternal.previousSibling; 780 newInternal.nextSibling = refInternal; 781 prev.nextSibling = newInternal; 782 refInternal.previousSibling = newInternal; 783 newInternal.previousSibling = prev; 784 } 785 } 786 } 787 788 changed(); 789 790 ownerDocument.insertedNode(this, newInternal, replace); 792 793 checkNormalizationAfterInsert(newInternal); 794 795 return newChild; 796 797 } 799 811 public Node removeChild(Node oldChild) 812 throws DOMException { 813 if (hasStringValue()) { 815 throw new DOMException (DOMException.NOT_FOUND_ERR, 817 "DOM008 Not found"); 818 } 819 return internalRemoveChild(oldChild, false); 820 } 822 827 Node internalRemoveChild(Node oldChild, boolean replace) 828 throws DOMException { 829 830 CoreDocumentImpl ownerDocument = ownerDocument(); 831 if (ownerDocument.errorChecking) { 832 if (isReadOnly()) { 833 throw new DOMException ( 834 DOMException.NO_MODIFICATION_ALLOWED_ERR, 835 "DOM001 Modification not allowed"); 836 } 837 if (oldChild != null && oldChild.getParentNode() != this) { 838 throw new DOMException (DOMException.NOT_FOUND_ERR, 839 "DOM008 Not found"); 840 } 841 } 842 843 ChildNode oldInternal = (ChildNode) oldChild; 844 845 ownerDocument.removingNode(this, oldInternal, replace); 847 848 if (oldInternal == value) { oldInternal.isFirstChild(false); 853 value = oldInternal.nextSibling; 855 ChildNode firstChild = (ChildNode) value; 856 if (firstChild != null) { 857 firstChild.isFirstChild(true); 858 firstChild.previousSibling = oldInternal.previousSibling; 859 } 860 } else { 861 ChildNode prev = oldInternal.previousSibling; 862 ChildNode next = oldInternal.nextSibling; 863 prev.nextSibling = next; 864 if (next == null) { 865 ChildNode firstChild = (ChildNode) value; 867 firstChild.previousSibling = prev; 868 } else { 869 next.previousSibling = prev; 871 } 872 } 873 874 ChildNode oldPreviousSibling = oldInternal.previousSibling(); 876 877 oldInternal.ownerNode = ownerDocument; 879 oldInternal.isOwned(false); 880 oldInternal.nextSibling = null; 881 oldInternal.previousSibling = null; 882 883 changed(); 884 885 ownerDocument.removedNode(this, replace); 887 888 checkNormalizationAfterRemove(oldPreviousSibling); 889 890 return oldInternal; 891 892 } 894 915 public Node replaceChild(Node newChild, Node oldChild) 916 throws DOMException { 917 918 makeChildNode(); 919 920 926 CoreDocumentImpl ownerDocument = ownerDocument(); 928 ownerDocument.replacingNode(this); 929 930 internalInsertBefore(newChild, oldChild, true); 931 if (newChild != oldChild) { 932 internalRemoveChild(oldChild, true); 933 } 934 935 ownerDocument.replacedNode(this); 937 938 return oldChild; 939 } 940 941 945 949 public int getLength() { 950 951 if (hasStringValue()) { 952 return 1; 953 } 954 ChildNode node = (ChildNode) value; 955 int length = 0; 956 for (; node != null; node = node.nextSibling) { 957 length++; 958 } 959 return length; 960 961 } 963 969 public Node item(int index) { 970 971 if (hasStringValue()) { 972 if (index != 0 || value == null) { 973 return null; 974 } 975 else { 976 makeChildNode(); 977 return (Node ) value; 978 } 979 } 980 ChildNode node = (ChildNode) value; 981 for (int i = 0; i < index && node != null; i++) { 982 node = node.nextSibling; 983 } 984 return node; 985 986 } 988 992 996 1004 public void setReadOnly(boolean readOnly, boolean deep) { 1005 1006 super.setReadOnly(readOnly, deep); 1007 1008 if (deep) { 1009 1010 if (needsSyncChildren()) { 1011 synchronizeChildren(); 1012 } 1013 1014 if (hasStringValue()) { 1015 return; 1016 } 1017 for (ChildNode mykid = (ChildNode) value; 1019 mykid != null; 1020 mykid = mykid.nextSibling) { 1021 if (mykid.getNodeType() != Node.ENTITY_REFERENCE_NODE) { 1022 mykid.setReadOnly(readOnly,true); 1023 } 1024 } 1025 } 1026 } 1028 1032 1036 protected void synchronizeChildren() { 1037 needsSyncChildren(false); 1039 } 1040 1041 1056 void checkNormalizationAfterInsert(ChildNode insertedChild) { 1057 if (insertedChild.getNodeType() == Node.TEXT_NODE) { 1059 ChildNode prev = insertedChild.previousSibling(); 1060 ChildNode next = insertedChild.nextSibling; 1061 if ((prev != null && prev.getNodeType() == Node.TEXT_NODE) || 1064 (next != null && next.getNodeType() == Node.TEXT_NODE)) { 1065 isNormalized(false); 1066 } 1067 } 1068 else { 1069 if (!insertedChild.isNormalized()) { 1072 isNormalized(false); 1073 } 1074 } 1075 } 1077 1089 void checkNormalizationAfterRemove(ChildNode previousSibling) { 1090 if (previousSibling != null && 1094 previousSibling.getNodeType() == Node.TEXT_NODE) { 1095 1096 ChildNode next = previousSibling.nextSibling; 1097 if (next != null && next.getNodeType() == Node.TEXT_NODE) { 1098 isNormalized(false); 1099 } 1100 } 1101 } 1103 1107 1108 private void writeObject(ObjectOutputStream out) throws IOException { 1109 1110 if (needsSyncChildren()) { 1112 synchronizeChildren(); 1113 } 1114 out.defaultWriteObject(); 1116 1117 } 1119 1120 private void readObject(ObjectInputStream ois) 1121 throws ClassNotFoundException , IOException { 1122 1123 ois.defaultReadObject(); 1125 1126 needsSyncChildren(false); 1130 1131 } 1133 1134 1137 public TypeInfo getSchemaTypeInfo() { 1138 return null; 1140 } 1141 1144 public boolean isId() { 1145 return false; 1147 } 1148 1149} | Popular Tags |