1 16 17 package org.apache.xerces.dom; 18 19 import java.io.IOException ; 20 import java.io.ObjectInputStream ; 21 import java.io.ObjectOutputStream ; 22 23 import org.w3c.dom.TypeInfo ; 24 import org.w3c.dom.Attr ; 25 import org.w3c.dom.DOMException ; 26 import org.w3c.dom.Element ; 27 import org.w3c.dom.Node ; 28 import org.w3c.dom.NodeList ; 29 import org.w3c.dom.Text ; 30 31 112 public class AttrImpl 113 extends NodeImpl 114 implements Attr , TypeInfo { 115 116 120 121 static final long serialVersionUID = 7277707688218972102L; 122 123 124 static final String DTD_URI = "http://www.w3.org/TR/REC-xml"; 125 126 130 131 protected Object value = null; 132 133 134 protected String name; 135 136 137 transient Object type; 139 140 protected static TextImpl textNode = null; 141 142 146 150 protected AttrImpl(CoreDocumentImpl ownerDocument, String name) { 151 super(ownerDocument); 152 this.name = name; 153 154 isSpecified(true); 155 hasStringValue(true); 156 } 157 158 protected AttrImpl() {} 160 161 void rename(String name) { 166 if (needsSyncData()) { 167 synchronizeData(); 168 } 169 this.name = name; 170 } 171 172 protected void makeChildNode() { 174 if (hasStringValue()) { 175 if (value != null) { 176 TextImpl text = 177 (TextImpl) ownerDocument().createTextNode((String ) value); 178 value = text; 179 text.isFirstChild(true); 180 text.previousSibling = text; 181 text.ownerNode = this; 182 text.isOwned(true); 183 } 184 hasStringValue(false); 185 } 186 } 187 188 192 void setOwnerDocument(CoreDocumentImpl doc) { 193 if (needsSyncChildren()) { 194 synchronizeChildren(); 195 } 196 super.setOwnerDocument(doc); 197 if (!hasStringValue()) { 198 for (ChildNode child = (ChildNode) value; 199 child != null; child = child.nextSibling) { 200 child.setOwnerDocument(doc); 201 } 202 } 203 } 204 205 210 public void setIdAttribute(boolean id){ 211 if (needsSyncData()) { 212 synchronizeData(); 213 } 214 isIdAttribute(id); 215 } 216 217 public boolean isId(){ 218 return isIdAttribute(); 221 } 222 223 224 228 public Node cloneNode(boolean deep) { 229 230 if (needsSyncChildren()) { 231 synchronizeChildren(); 232 } 233 AttrImpl clone = (AttrImpl) super.cloneNode(deep); 234 235 if (!clone.hasStringValue()) { 237 238 clone.value = null; 240 241 for (Node child = (Node ) value; child != null; 245 child = child.getNextSibling()) { 246 clone.appendChild(child.cloneNode(true)); 247 } 248 } 249 clone.isSpecified(true); 250 return clone; 251 } 252 253 257 public short getNodeType() { 258 return Node.ATTRIBUTE_NODE; 259 } 260 261 264 public String getNodeName() { 265 if (needsSyncData()) { 266 synchronizeData(); 267 } 268 return name; 269 } 270 271 277 public void setNodeValue(String value) throws DOMException { 278 setValue(value); 279 } 280 281 284 public String getTypeName() { 285 return (String )type; 286 } 287 288 291 public String getTypeNamespace() { 292 if (type != null) { 293 return DTD_URI; 294 } 295 return null; 296 } 297 298 302 public TypeInfo getSchemaTypeInfo(){ 303 return this; 304 } 305 306 312 public String getNodeValue() { 313 return getValue(); 314 } 315 316 320 324 public String getName() { 325 326 if (needsSyncData()) { 327 synchronizeData(); 328 } 329 return name; 330 331 } 333 338 public void setValue(String newvalue) { 339 340 CoreDocumentImpl ownerDocument = ownerDocument(); 341 342 if (ownerDocument.errorChecking && isReadOnly()) { 343 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 344 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 345 } 346 347 Element ownerElement = getOwnerElement(); 348 String oldvalue = ""; 349 if (needsSyncData()) { 350 synchronizeData(); 351 } 352 if (needsSyncChildren()) { 353 synchronizeChildren(); 354 } 355 if (value != null) { 356 if (ownerDocument.getMutationEvents()) { 357 if (hasStringValue()) { 360 oldvalue = (String ) value; 361 if (textNode == null) { 364 textNode = (TextImpl) 365 ownerDocument.createTextNode((String ) value); 366 } 367 else { 368 textNode.data = (String ) value; 369 } 370 value = textNode; 371 textNode.isFirstChild(true); 372 textNode.previousSibling = textNode; 373 textNode.ownerNode = this; 374 textNode.isOwned(true); 375 hasStringValue(false); 376 internalRemoveChild(textNode, true); 377 } 378 else { 379 oldvalue = getValue(); 380 while (value != null) { 381 internalRemoveChild((Node ) value, true); 382 } 383 } 384 } 385 else { 386 if (hasStringValue()) { 387 oldvalue = (String ) value; 388 } 389 else { 390 oldvalue = getValue(); 392 ChildNode firstChild = (ChildNode) value; 394 firstChild.previousSibling = null; 395 firstChild.isFirstChild(false); 396 firstChild.ownerNode = ownerDocument; 397 } 398 value = null; 400 needsSyncChildren(false); 401 } 402 if (isIdAttribute() && ownerElement != null) { 403 ownerDocument.removeIdentifier(oldvalue); 404 } 405 } 406 407 isSpecified(true); 413 if (ownerDocument.getMutationEvents()) { 414 internalInsertBefore(ownerDocument.createTextNode(newvalue), 416 null, true); 417 hasStringValue(false); 418 ownerDocument.modifiedAttrValue(this, oldvalue); 420 } else { 421 value = newvalue; 423 hasStringValue(true); 424 changed(); 425 } 426 if (isIdAttribute() && ownerElement != null) { 427 ownerDocument.putIdentifier(newvalue, ownerElement); 428 } 429 430 } 432 436 public String getValue() { 437 438 if (needsSyncData()) { 439 synchronizeData(); 440 } 441 if (needsSyncChildren()) { 442 synchronizeChildren(); 443 } 444 if (value == null) { 445 return ""; 446 } 447 if (hasStringValue()) { 448 return (String ) value; 449 } 450 451 ChildNode firstChild = ((ChildNode) value); 452 453 String data = null; 454 if (firstChild.getNodeType() == Node.ENTITY_REFERENCE_NODE){ 455 data = ((EntityReferenceImpl)firstChild).getEntityRefValue(); 456 } 457 else { 458 data = firstChild.getNodeValue(); 459 } 460 461 ChildNode node = firstChild.nextSibling; 462 463 if (node == null || data == null) return (data == null)?"":data; 464 465 StringBuffer value = new StringBuffer (data); 466 while (node != null) { 467 if (node.getNodeType() == Node.ENTITY_REFERENCE_NODE){ 468 data = ((EntityReferenceImpl)node).getEntityRefValue(); 469 if (data == null) return ""; 470 value.append(data); 471 } 472 else { 473 value.append(node.getNodeValue()); 474 } 475 node = node.nextSibling; 476 } 477 return value.toString(); 478 479 } 481 482 493 public boolean getSpecified() { 494 495 if (needsSyncData()) { 496 synchronizeData(); 497 } 498 return isSpecified(); 499 500 } 502 506 515 public Element getElement() { 516 return (Element ) (isOwned() ? ownerNode : null); 519 } 520 521 527 public Element getOwnerElement() { 528 return (Element ) (isOwned() ? ownerNode : null); 531 } 532 533 public void normalize() { 534 535 if (isNormalized() || hasStringValue()) 538 return; 539 540 Node kid, next; 541 ChildNode firstChild = (ChildNode)value; 542 for (kid = firstChild; kid != null; kid = next) { 543 next = kid.getNextSibling(); 544 545 if ( kid.getNodeType() == Node.TEXT_NODE ) 551 { 552 if ( next!=null && next.getNodeType() == Node.TEXT_NODE ) 554 { 555 ((Text )kid).appendData(next.getNodeValue()); 556 removeChild( next ); 557 next = kid; } 559 else 560 { 561 if ( kid.getNodeValue() == null || kid.getNodeValue().length() == 0 ) { 563 removeChild( kid ); 564 } 565 } 566 } 567 } 568 569 isNormalized(true); 570 } 572 576 577 public void setSpecified(boolean arg) { 578 579 if (needsSyncData()) { 580 synchronizeData(); 581 } 582 isSpecified(arg); 583 584 } 586 590 public void setType (Object type){ 591 this.type = type; 592 } 593 594 598 599 public String toString() { 600 return getName() + "=" + "\"" + getValue() + "\""; 601 } 602 603 607 public boolean hasChildNodes() { 608 if (needsSyncChildren()) { 609 synchronizeChildren(); 610 } 611 return value != null; 612 } 613 614 627 public NodeList getChildNodes() { 628 630 if (needsSyncChildren()) { 631 synchronizeChildren(); 632 } 633 return this; 634 635 } 637 638 public Node getFirstChild() { 639 640 if (needsSyncChildren()) { 641 synchronizeChildren(); 642 } 643 makeChildNode(); 644 return (Node ) value; 645 646 } 648 649 public Node getLastChild() { 650 651 if (needsSyncChildren()) { 652 synchronizeChildren(); 653 } 654 return lastChild(); 655 656 } 658 final ChildNode lastChild() { 659 makeChildNode(); 661 return value != null ? ((ChildNode) value).previousSibling : null; 662 } 663 664 final void lastChild(ChildNode node) { 665 if (value != null) { 667 ((ChildNode) value).previousSibling = node; 668 } 669 } 670 671 699 public Node insertBefore(Node newChild, Node refChild) 700 throws DOMException { 701 return internalInsertBefore(newChild, refChild, false); 703 } 705 710 Node internalInsertBefore(Node newChild, Node refChild, boolean replace) 711 throws DOMException { 712 713 CoreDocumentImpl ownerDocument = ownerDocument(); 714 boolean errorChecking = ownerDocument.errorChecking; 715 716 if (newChild.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE) { 717 724 732 if (errorChecking) { 735 for (Node kid = newChild.getFirstChild(); kid != null; kid = kid.getNextSibling()) { 737 738 if (!ownerDocument.isKidOK(this, kid)) { 739 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null); 740 throw new DOMException (DOMException.HIERARCHY_REQUEST_ERR, msg); 741 } 742 } 743 } 744 745 while (newChild.hasChildNodes()) { 746 insertBefore(newChild.getFirstChild(), refChild); 747 } 748 return newChild; 749 } 750 751 if (newChild == refChild) { 752 refChild = refChild.getNextSibling(); 754 removeChild(newChild); 755 insertBefore(newChild, refChild); 756 return newChild; 757 } 758 759 if (needsSyncChildren()) { 760 synchronizeChildren(); 761 } 762 763 if (errorChecking) { 764 if (isReadOnly()) { 765 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 766 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 767 } 768 if (newChild.getOwnerDocument() != ownerDocument) { 769 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null); 770 throw new DOMException (DOMException.WRONG_DOCUMENT_ERR, msg); 771 } 772 if (!ownerDocument.isKidOK(this, newChild)) { 773 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null); 774 throw new DOMException (DOMException.HIERARCHY_REQUEST_ERR, msg); 775 } 776 if (refChild != null && refChild.getParentNode() != this) { 778 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null); 779 throw new DOMException (DOMException.NOT_FOUND_ERR, msg); 780 } 781 782 boolean treeSafe = true; 786 for (NodeImpl a = this; treeSafe && a != null; a = a.parentNode()) 787 { 788 treeSafe = newChild != a; 789 } 790 if (!treeSafe) { 791 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null); 792 throw new DOMException (DOMException.HIERARCHY_REQUEST_ERR, msg); 793 } 794 } 795 796 makeChildNode(); 798 ownerDocument.insertingNode(this, replace); 800 801 ChildNode newInternal = (ChildNode)newChild; 803 804 Node oldparent = newInternal.parentNode(); 805 if (oldparent != null) { 806 oldparent.removeChild(newInternal); 807 } 808 809 ChildNode refInternal = (ChildNode) refChild; 811 812 newInternal.ownerNode = this; 814 newInternal.isOwned(true); 815 816 ChildNode firstChild = (ChildNode) value; 819 if (firstChild == null) { 820 value = newInternal; newInternal.isFirstChild(true); 823 newInternal.previousSibling = newInternal; 824 } 825 else { 826 if (refInternal == null) { 827 ChildNode lastChild = firstChild.previousSibling; 829 lastChild.nextSibling = newInternal; 830 newInternal.previousSibling = lastChild; 831 firstChild.previousSibling = newInternal; 832 } 833 else { 834 if (refChild == firstChild) { 836 firstChild.isFirstChild(false); 838 newInternal.nextSibling = firstChild; 839 newInternal.previousSibling = firstChild.previousSibling; 840 firstChild.previousSibling = newInternal; 841 value = newInternal; newInternal.isFirstChild(true); 843 } 844 else { 845 ChildNode prev = refInternal.previousSibling; 847 newInternal.nextSibling = refInternal; 848 prev.nextSibling = newInternal; 849 refInternal.previousSibling = newInternal; 850 newInternal.previousSibling = prev; 851 } 852 } 853 } 854 855 changed(); 856 857 ownerDocument.insertedNode(this, newInternal, replace); 859 860 checkNormalizationAfterInsert(newInternal); 861 862 return newChild; 863 864 } 866 878 public Node removeChild(Node oldChild) 879 throws DOMException { 880 if (hasStringValue()) { 882 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null); 884 throw new DOMException (DOMException.NOT_FOUND_ERR, msg); 885 } 886 return internalRemoveChild(oldChild, false); 887 } 889 894 Node internalRemoveChild(Node oldChild, boolean replace) 895 throws DOMException { 896 897 CoreDocumentImpl ownerDocument = ownerDocument(); 898 if (ownerDocument.errorChecking) { 899 if (isReadOnly()) { 900 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 901 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 902 } 903 if (oldChild != null && oldChild.getParentNode() != this) { 904 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null); 905 throw new DOMException (DOMException.NOT_FOUND_ERR, msg); 906 } 907 } 908 909 ChildNode oldInternal = (ChildNode) oldChild; 910 911 ownerDocument.removingNode(this, oldInternal, replace); 913 914 if (oldInternal == value) { oldInternal.isFirstChild(false); 919 value = oldInternal.nextSibling; 921 ChildNode firstChild = (ChildNode) value; 922 if (firstChild != null) { 923 firstChild.isFirstChild(true); 924 firstChild.previousSibling = oldInternal.previousSibling; 925 } 926 } else { 927 ChildNode prev = oldInternal.previousSibling; 928 ChildNode next = oldInternal.nextSibling; 929 prev.nextSibling = next; 930 if (next == null) { 931 ChildNode firstChild = (ChildNode) value; 933 firstChild.previousSibling = prev; 934 } else { 935 next.previousSibling = prev; 937 } 938 } 939 940 ChildNode oldPreviousSibling = oldInternal.previousSibling(); 942 943 oldInternal.ownerNode = ownerDocument; 945 oldInternal.isOwned(false); 946 oldInternal.nextSibling = null; 947 oldInternal.previousSibling = null; 948 949 changed(); 950 951 ownerDocument.removedNode(this, replace); 953 954 checkNormalizationAfterRemove(oldPreviousSibling); 955 956 return oldInternal; 957 958 } 960 981 public Node replaceChild(Node newChild, Node oldChild) 982 throws DOMException { 983 984 makeChildNode(); 985 986 992 CoreDocumentImpl ownerDocument = ownerDocument(); 994 ownerDocument.replacingNode(this); 995 996 internalInsertBefore(newChild, oldChild, true); 997 if (newChild != oldChild) { 998 internalRemoveChild(oldChild, true); 999 } 1000 1001 ownerDocument.replacedNode(this); 1003 1004 return oldChild; 1005 } 1006 1007 1011 1015 public int getLength() { 1016 1017 if (hasStringValue()) { 1018 return 1; 1019 } 1020 ChildNode node = (ChildNode) value; 1021 int length = 0; 1022 for (; node != null; node = node.nextSibling) { 1023 length++; 1024 } 1025 return length; 1026 1027 } 1029 1035 public Node item(int index) { 1036 1037 if (hasStringValue()) { 1038 if (index != 0 || value == null) { 1039 return null; 1040 } 1041 else { 1042 makeChildNode(); 1043 return (Node ) value; 1044 } 1045 } 1046 if (index < 0) { 1047 return null; 1048 } 1049 ChildNode node = (ChildNode) value; 1050 for (int i = 0; i < index && node != null; i++) { 1051 node = node.nextSibling; 1052 } 1053 return node; 1054 1055 } 1057 1061 1066 public boolean isEqualNode(Node arg) { 1067 return super.isEqualNode(arg); 1068 } 1069 1070 1085 public boolean isDerivedFrom(String typeNamespaceArg, 1086 String typeNameArg, 1087 int derivationMethod) { 1088 1089 return false; 1090 } 1091 1092 1093 1097 1105 public void setReadOnly(boolean readOnly, boolean deep) { 1106 1107 super.setReadOnly(readOnly, deep); 1108 1109 if (deep) { 1110 1111 if (needsSyncChildren()) { 1112 synchronizeChildren(); 1113 } 1114 1115 if (hasStringValue()) { 1116 return; 1117 } 1118 for (ChildNode mykid = (ChildNode) value; 1120 mykid != null; 1121 mykid = mykid.nextSibling) { 1122 if (mykid.getNodeType() != Node.ENTITY_REFERENCE_NODE) { 1123 mykid.setReadOnly(readOnly,true); 1124 } 1125 } 1126 } 1127 } 1129 1133 1137 protected void synchronizeChildren() { 1138 needsSyncChildren(false); 1140 } 1141 1142 1157 void checkNormalizationAfterInsert(ChildNode insertedChild) { 1158 if (insertedChild.getNodeType() == Node.TEXT_NODE) { 1160 ChildNode prev = insertedChild.previousSibling(); 1161 ChildNode next = insertedChild.nextSibling; 1162 if ((prev != null && prev.getNodeType() == Node.TEXT_NODE) || 1165 (next != null && next.getNodeType() == Node.TEXT_NODE)) { 1166 isNormalized(false); 1167 } 1168 } 1169 else { 1170 if (!insertedChild.isNormalized()) { 1173 isNormalized(false); 1174 } 1175 } 1176 } 1178 1190 void checkNormalizationAfterRemove(ChildNode previousSibling) { 1191 if (previousSibling != null && 1195 previousSibling.getNodeType() == Node.TEXT_NODE) { 1196 1197 ChildNode next = previousSibling.nextSibling; 1198 if (next != null && next.getNodeType() == Node.TEXT_NODE) { 1199 isNormalized(false); 1200 } 1201 } 1202 } 1204 1208 1209 private void writeObject(ObjectOutputStream out) throws IOException { 1210 1211 if (needsSyncChildren()) { 1213 synchronizeChildren(); 1214 } 1215 out.defaultWriteObject(); 1217 1218 } 1220 1221 private void readObject(ObjectInputStream ois) 1222 throws ClassNotFoundException , IOException { 1223 1224 ois.defaultReadObject(); 1226 1227 needsSyncChildren(false); 1231 1232 } 1234 1235} | Popular Tags |