1 57 58 package com.sun.org.apache.xerces.internal.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 163 static final String DTD_URI = "http://www.w3.org/TR/REC-xml"; 164 165 169 170 protected Object value = null; 171 172 173 protected String name; 174 175 176 transient org.w3c.dom.TypeInfo type; 178 179 protected static TextImpl textNode = null; 180 181 185 189 protected AttrImpl(CoreDocumentImpl ownerDocument, String name) { 190 super(ownerDocument); 191 this.name = name; 192 193 isSpecified(true); 194 hasStringValue(true); 195 } 196 197 protected AttrImpl() {} 199 200 void rename(String name) { 205 if (needsSyncData()) { 206 synchronizeData(); 207 } 208 this.name = name; 209 } 210 211 protected void makeChildNode() { 213 if (hasStringValue()) { 214 if (value != null) { 215 TextImpl text = 216 (TextImpl) ownerDocument().createTextNode((String ) value); 217 value = text; 218 text.isFirstChild(true); 219 text.previousSibling = text; 220 text.ownerNode = this; 221 text.isOwned(true); 222 } 223 hasStringValue(false); 224 } 225 } 226 227 231 void setOwnerDocument(CoreDocumentImpl doc) { 232 if (needsSyncChildren()) { 233 synchronizeChildren(); 234 } 235 super.setOwnerDocument(doc); 236 if (!hasStringValue()) { 237 for (ChildNode child = (ChildNode) value; 238 child != null; child = child.nextSibling) { 239 child.setOwnerDocument(doc); 240 } 241 } 242 } 243 244 249 public void setIdAttribute(boolean id){ 250 if (needsSyncData()) { 251 synchronizeData(); 252 } 253 isIdAttribute(id); 254 } 255 256 public boolean isId(){ 257 return isIdAttribute(); 260 } 261 262 263 267 public Node cloneNode(boolean deep) { 268 269 if (needsSyncChildren()) { 270 synchronizeChildren(); 271 } 272 AttrImpl clone = (AttrImpl) super.cloneNode(deep); 273 274 if (!clone.hasStringValue()) { 276 277 clone.value = null; 279 280 for (Node child = (Node ) value; child != null; 284 child = child.getNextSibling()) { 285 clone.appendChild(child.cloneNode(true)); 286 } 287 } 288 clone.isSpecified(true); 289 return clone; 290 } 291 292 296 public short getNodeType() { 297 return Node.ATTRIBUTE_NODE; 298 } 299 300 303 public String getNodeName() { 304 if (needsSyncData()) { 305 synchronizeData(); 306 } 307 return name; 308 } 309 310 316 public void setNodeValue(String value) throws DOMException { 317 setValue(value); 318 } 319 320 323 public String getTypeName() { 324 if(type != null) 325 return type.getTypeName(); 326 return null; 327 } 328 329 332 public String getTypeNamespace() { 333 if (type != null) { 334 return DTD_URI; 335 } 336 return null; 337 } 338 339 343 public TypeInfo getSchemaTypeInfo(){ 344 if (needsSyncData()) { 345 synchronizeData(); 346 } 347 return type; 348 } 349 350 356 public String getNodeValue() { 357 return getValue(); 358 } 359 360 364 368 public String getName() { 369 370 if (needsSyncData()) { 371 synchronizeData(); 372 } 373 return name; 374 375 } 377 382 public void setValue(String newvalue) { 383 384 if (isReadOnly()) { 385 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 386 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 387 } 388 CoreDocumentImpl ownerDocument = ownerDocument(); 389 Element ownerElement = getOwnerElement(); 390 String oldvalue = ""; 391 if (needsSyncData()) { 392 synchronizeData(); 393 } 394 if (needsSyncChildren()) { 395 synchronizeChildren(); 396 } 397 if (value != null) { 398 if (ownerDocument.getMutationEvents()) { 399 if (hasStringValue()) { 402 oldvalue = (String ) value; 403 if (textNode == null) { 406 textNode = (TextImpl) 407 ownerDocument.createTextNode((String ) value); 408 } 409 else { 410 textNode.data = (String ) value; 411 } 412 value = textNode; 413 textNode.isFirstChild(true); 414 textNode.previousSibling = textNode; 415 textNode.ownerNode = this; 416 textNode.isOwned(true); 417 hasStringValue(false); 418 internalRemoveChild(textNode, true); 419 } 420 else { 421 oldvalue = getValue(); 422 while (value != null) { 423 internalRemoveChild((Node ) value, true); 424 } 425 } 426 } 427 else { 428 if (hasStringValue()) { 429 oldvalue = (String ) value; 430 } 431 else { 432 oldvalue = getValue(); 434 ChildNode firstChild = (ChildNode) value; 436 firstChild.previousSibling = null; 437 firstChild.isFirstChild(false); 438 firstChild.ownerNode = ownerDocument; 439 } 440 value = null; 442 needsSyncChildren(false); 443 } 444 if (isIdAttribute() && ownerElement != null) { 445 ownerDocument.removeIdentifier(oldvalue); 446 } 447 } 448 449 isSpecified(true); 455 if (ownerDocument.getMutationEvents()) { 456 internalInsertBefore(ownerDocument.createTextNode(newvalue), 458 null, true); 459 hasStringValue(false); 460 ownerDocument.modifiedAttrValue(this, oldvalue); 462 } else { 463 value = newvalue; 465 hasStringValue(true); 466 changed(); 467 } 468 if (isIdAttribute() && ownerElement != null) { 469 ownerDocument.putIdentifier(newvalue, ownerElement); 470 } 471 472 } 474 478 public String getValue() { 479 480 if (needsSyncData()) { 481 synchronizeData(); 482 } 483 if (needsSyncChildren()) { 484 synchronizeChildren(); 485 } 486 if (value == null) { 487 return ""; 488 } 489 if (hasStringValue()) { 490 return (String ) value; 491 } 492 493 ChildNode firstChild = ((ChildNode) value); 494 495 String data = null; 496 if (firstChild.getNodeType() == Node.ENTITY_REFERENCE_NODE){ 497 data = ((EntityReferenceImpl)firstChild).getEntityRefValue(); 498 } 499 else { 500 data = firstChild.getNodeValue(); 501 } 502 503 ChildNode node = firstChild.nextSibling; 504 505 if (node == null || data == null) return (data == null)?"":data; 506 507 StringBuffer value = new StringBuffer (data); 508 while (node != null) { 509 if (node.getNodeType() == Node.ENTITY_REFERENCE_NODE){ 510 data = ((EntityReferenceImpl)node).getEntityRefValue(); 511 if (data == null) return ""; 512 value.append(data); 513 } 514 else { 515 value.append(node.getNodeValue()); 516 } 517 node = node.nextSibling; 518 } 519 return value.toString(); 520 521 } 523 524 535 public boolean getSpecified() { 536 537 if (needsSyncData()) { 538 synchronizeData(); 539 } 540 return isSpecified(); 541 542 } 544 548 557 public Element getElement() { 558 return (Element ) (isOwned() ? ownerNode : null); 561 } 562 563 569 public Element getOwnerElement() { 570 return (Element ) (isOwned() ? ownerNode : null); 573 } 574 575 public void normalize() { 576 577 if (isNormalized() || hasStringValue()) 580 return; 581 582 Node kid, next; 583 ChildNode firstChild = (ChildNode)value; 584 for (kid = firstChild; kid != null; kid = next) { 585 next = kid.getNextSibling(); 586 587 if ( kid.getNodeType() == Node.TEXT_NODE ) 593 { 594 if ( next!=null && next.getNodeType() == Node.TEXT_NODE ) 596 { 597 ((Text )kid).appendData(next.getNodeValue()); 598 removeChild( next ); 599 next = kid; } 601 else 602 { 603 if ( kid.getNodeValue().length()==0 ) 605 removeChild( kid ); 606 } 607 } 608 } 609 610 isNormalized(true); 611 } 613 617 618 public void setSpecified(boolean arg) { 619 620 if (needsSyncData()) { 621 synchronizeData(); 622 } 623 isSpecified(arg); 624 625 } 627 631 public void setType (org.w3c.dom.TypeInfo type){ 632 this.type = type; 633 } 634 635 639 640 public String toString() { 641 return getName() + "=" + "\"" + getValue() + "\""; 642 } 643 644 648 public boolean hasChildNodes() { 649 if (needsSyncChildren()) { 650 synchronizeChildren(); 651 } 652 return value != null; 653 } 654 655 668 public NodeList getChildNodes() { 669 671 if (needsSyncChildren()) { 672 synchronizeChildren(); 673 } 674 return this; 675 676 } 678 679 public Node getFirstChild() { 680 681 if (needsSyncChildren()) { 682 synchronizeChildren(); 683 } 684 makeChildNode(); 685 return (Node ) value; 686 687 } 689 690 public Node getLastChild() { 691 692 if (needsSyncChildren()) { 693 synchronizeChildren(); 694 } 695 return lastChild(); 696 697 } 699 final ChildNode lastChild() { 700 makeChildNode(); 702 return value != null ? ((ChildNode) value).previousSibling : null; 703 } 704 705 final void lastChild(ChildNode node) { 706 if (value != null) { 708 ((ChildNode) value).previousSibling = node; 709 } 710 } 711 712 740 public Node insertBefore(Node newChild, Node refChild) 741 throws DOMException { 742 return internalInsertBefore(newChild, refChild, false); 744 } 746 751 Node internalInsertBefore(Node newChild, Node refChild, boolean replace) 752 throws DOMException { 753 754 CoreDocumentImpl ownerDocument = ownerDocument(); 755 boolean errorChecking = ownerDocument.errorChecking; 756 757 if (newChild.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE) { 758 765 773 if (errorChecking) { 776 for (Node kid = newChild.getFirstChild(); kid != null; kid = kid.getNextSibling()) { 778 779 if (!ownerDocument.isKidOK(this, kid)) { 780 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null); 781 throw new DOMException (DOMException.HIERARCHY_REQUEST_ERR, msg); 782 } 783 } 784 } 785 786 while (newChild.hasChildNodes()) { 787 insertBefore(newChild.getFirstChild(), refChild); 788 } 789 return newChild; 790 } 791 792 if (newChild == refChild) { 793 refChild = refChild.getNextSibling(); 795 removeChild(newChild); 796 insertBefore(newChild, refChild); 797 return newChild; 798 } 799 800 if (needsSyncChildren()) { 801 synchronizeChildren(); 802 } 803 804 if (errorChecking) { 805 if (isReadOnly()) { 806 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 807 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 808 } 809 if (newChild.getOwnerDocument() != ownerDocument) { 810 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null); 811 throw new DOMException (DOMException.WRONG_DOCUMENT_ERR, msg); 812 } 813 if (!ownerDocument.isKidOK(this, newChild)) { 814 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null); 815 throw new DOMException (DOMException.HIERARCHY_REQUEST_ERR, msg); 816 } 817 if (refChild != null && refChild.getParentNode() != this) { 819 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null); 820 throw new DOMException (DOMException.NOT_FOUND_ERR, msg); 821 } 822 823 boolean treeSafe = true; 827 for (NodeImpl a = this; treeSafe && a != null; a = a.parentNode()) 828 { 829 treeSafe = newChild != a; 830 } 831 if (!treeSafe) { 832 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null); 833 throw new DOMException (DOMException.HIERARCHY_REQUEST_ERR, msg); 834 } 835 } 836 837 makeChildNode(); 839 ownerDocument.insertingNode(this, replace); 841 842 ChildNode newInternal = (ChildNode)newChild; 844 845 Node oldparent = newInternal.parentNode(); 846 if (oldparent != null) { 847 oldparent.removeChild(newInternal); 848 } 849 850 ChildNode refInternal = (ChildNode) refChild; 852 853 newInternal.ownerNode = this; 855 newInternal.isOwned(true); 856 857 ChildNode firstChild = (ChildNode) value; 860 if (firstChild == null) { 861 value = newInternal; newInternal.isFirstChild(true); 864 newInternal.previousSibling = newInternal; 865 } 866 else { 867 if (refInternal == null) { 868 ChildNode lastChild = firstChild.previousSibling; 870 lastChild.nextSibling = newInternal; 871 newInternal.previousSibling = lastChild; 872 firstChild.previousSibling = newInternal; 873 } 874 else { 875 if (refChild == firstChild) { 877 firstChild.isFirstChild(false); 879 newInternal.nextSibling = firstChild; 880 newInternal.previousSibling = firstChild.previousSibling; 881 firstChild.previousSibling = newInternal; 882 value = newInternal; newInternal.isFirstChild(true); 884 } 885 else { 886 ChildNode prev = refInternal.previousSibling; 888 newInternal.nextSibling = refInternal; 889 prev.nextSibling = newInternal; 890 refInternal.previousSibling = newInternal; 891 newInternal.previousSibling = prev; 892 } 893 } 894 } 895 896 changed(); 897 898 ownerDocument.insertedNode(this, newInternal, replace); 900 901 checkNormalizationAfterInsert(newInternal); 902 903 return newChild; 904 905 } 907 919 public Node removeChild(Node oldChild) 920 throws DOMException { 921 if (hasStringValue()) { 923 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null); 925 throw new DOMException (DOMException.NOT_FOUND_ERR, msg); 926 } 927 return internalRemoveChild(oldChild, false); 928 } 930 935 Node internalRemoveChild(Node oldChild, boolean replace) 936 throws DOMException { 937 938 CoreDocumentImpl ownerDocument = ownerDocument(); 939 if (ownerDocument.errorChecking) { 940 if (isReadOnly()) { 941 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 942 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 943 } 944 if (oldChild != null && oldChild.getParentNode() != this) { 945 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null); 946 throw new DOMException (DOMException.NOT_FOUND_ERR, msg); 947 } 948 } 949 950 ChildNode oldInternal = (ChildNode) oldChild; 951 952 ownerDocument.removingNode(this, oldInternal, replace); 954 955 if (oldInternal == value) { oldInternal.isFirstChild(false); 960 value = oldInternal.nextSibling; 962 ChildNode firstChild = (ChildNode) value; 963 if (firstChild != null) { 964 firstChild.isFirstChild(true); 965 firstChild.previousSibling = oldInternal.previousSibling; 966 } 967 } else { 968 ChildNode prev = oldInternal.previousSibling; 969 ChildNode next = oldInternal.nextSibling; 970 prev.nextSibling = next; 971 if (next == null) { 972 ChildNode firstChild = (ChildNode) value; 974 firstChild.previousSibling = prev; 975 } else { 976 next.previousSibling = prev; 978 } 979 } 980 981 ChildNode oldPreviousSibling = oldInternal.previousSibling(); 983 984 oldInternal.ownerNode = ownerDocument; 986 oldInternal.isOwned(false); 987 oldInternal.nextSibling = null; 988 oldInternal.previousSibling = null; 989 990 changed(); 991 992 ownerDocument.removedNode(this, replace); 994 995 checkNormalizationAfterRemove(oldPreviousSibling); 996 997 return oldInternal; 998 999 } 1001 1022 public Node replaceChild(Node newChild, Node oldChild) 1023 throws DOMException { 1024 1025 makeChildNode(); 1026 1027 1033 CoreDocumentImpl ownerDocument = ownerDocument(); 1035 ownerDocument.replacingNode(this); 1036 1037 internalInsertBefore(newChild, oldChild, true); 1038 if (newChild != oldChild) { 1039 internalRemoveChild(oldChild, true); 1040 } 1041 1042 ownerDocument.replacedNode(this); 1044 1045 return oldChild; 1046 } 1047 1048 1052 1056 public int getLength() { 1057 1058 if (hasStringValue()) { 1059 return 1; 1060 } 1061 ChildNode node = (ChildNode) value; 1062 int length = 0; 1063 for (; node != null; node = node.nextSibling) { 1064 length++; 1065 } 1066 return length; 1067 1068 } 1070 1076 public Node item(int index) { 1077 1078 if (hasStringValue()) { 1079 if (index != 0 || value == null) { 1080 return null; 1081 } 1082 else { 1083 makeChildNode(); 1084 return (Node ) value; 1085 } 1086 } 1087 ChildNode node = (ChildNode) value; 1088 for (int i = 0; i < index && node != null; i++) { 1089 node = node.nextSibling; 1090 } 1091 return node; 1092 1093 } 1095 1099 1104 public boolean isEqualNode(Node arg) { 1105 return super.isEqualNode(arg); 1106 } 1107 1108 1112 public boolean isDerivedFrom(String typeNamespaceArg, 1113 String typeNameArg, 1114 int derivationMethod) { 1115 1116 return false; 1117 } 1118 1119 1120 1124 1132 public void setReadOnly(boolean readOnly, boolean deep) { 1133 1134 super.setReadOnly(readOnly, deep); 1135 1136 if (deep) { 1137 1138 if (needsSyncChildren()) { 1139 synchronizeChildren(); 1140 } 1141 1142 if (hasStringValue()) { 1143 return; 1144 } 1145 for (ChildNode mykid = (ChildNode) value; 1147 mykid != null; 1148 mykid = mykid.nextSibling) { 1149 if (mykid.getNodeType() != Node.ENTITY_REFERENCE_NODE) { 1150 mykid.setReadOnly(readOnly,true); 1151 } 1152 } 1153 } 1154 } 1156 1160 1164 protected void synchronizeChildren() { 1165 needsSyncChildren(false); 1167 } 1168 1169 1184 void checkNormalizationAfterInsert(ChildNode insertedChild) { 1185 if (insertedChild.getNodeType() == Node.TEXT_NODE) { 1187 ChildNode prev = insertedChild.previousSibling(); 1188 ChildNode next = insertedChild.nextSibling; 1189 if ((prev != null && prev.getNodeType() == Node.TEXT_NODE) || 1192 (next != null && next.getNodeType() == Node.TEXT_NODE)) { 1193 isNormalized(false); 1194 } 1195 } 1196 else { 1197 if (!insertedChild.isNormalized()) { 1200 isNormalized(false); 1201 } 1202 } 1203 } 1205 1217 void checkNormalizationAfterRemove(ChildNode previousSibling) { 1218 if (previousSibling != null && 1222 previousSibling.getNodeType() == Node.TEXT_NODE) { 1223 1224 ChildNode next = previousSibling.nextSibling; 1225 if (next != null && next.getNodeType() == Node.TEXT_NODE) { 1226 isNormalized(false); 1227 } 1228 } 1229 } 1231 1235 1236 private void writeObject(ObjectOutputStream out) throws IOException { 1237 1238 if (needsSyncChildren()) { 1240 synchronizeChildren(); 1241 } 1242 out.defaultWriteObject(); 1244 1245 } 1247 1248 private void readObject(ObjectInputStream ois) 1249 throws ClassNotFoundException , IOException { 1250 1251 ois.defaultReadObject(); 1253 1254 needsSyncChildren(false); 1258 1259 } 1261 1262} | Popular Tags |