1 57 58 package org.xquark.xpath.datamodel.xerces.dom; 59 60 import java.io.IOException ; 61 import java.io.ObjectInputStream ; 62 import java.io.ObjectOutputStream ; 63 64 import org.w3c.dom.*; 65 import org.w3c.dom.events.MutationEvent ; 66 import org.xquark.xpath.datamodel.xerces.dom.events.MutationEventImpl; 67 68 144 public class AttrImpl 145 extends NodeImpl 146 implements Attr { 147 148 152 153 static final long serialVersionUID = 7277707688218972102L; 154 155 159 160 protected Object value = null; 161 162 163 protected String name; 164 165 protected static TextImpl textNode = null; 166 167 171 175 protected AttrImpl(DocumentImpl ownerDocument, String name) { 176 super(ownerDocument); 177 this.name = name; 178 179 isSpecified(true); 180 hasStringValue(true); 181 } 182 183 protected AttrImpl() {} 185 186 protected void makeChildNode() { 188 if (hasStringValue()) { 189 if (value != null) { 190 TextImpl text = 191 (TextImpl) ownerDocument().createTextNode((String ) value); 192 value = text; 193 text.isFirstChild(true); 194 text.previousSibling = text; 195 text.ownerNode = this; 196 text.isOwned(true); 197 } 198 hasStringValue(false); 199 } 200 } 201 202 206 void setOwnerDocument(DocumentImpl doc) { 207 if (needsSyncChildren()) { 208 synchronizeChildren(); 209 } 210 super.setOwnerDocument(doc); 211 if (!hasStringValue()) { 212 for (ChildNode child = (ChildNode) value; 213 child != null; child = child.nextSibling) { 214 child.setOwnerDocument(doc); 215 } 216 } 217 } 218 219 223 public Node cloneNode(boolean deep) { 224 225 if (needsSyncChildren()) { 226 synchronizeChildren(); 227 } 228 AttrImpl clone = (AttrImpl) super.cloneNode(deep); 229 230 if (!clone.hasStringValue()) { 232 233 clone.value = null; 235 236 if (deep) { 238 for (Node child = (Node) value; child != null; 239 child = child.getNextSibling()) { 240 clone.appendChild(child.cloneNode(true)); 241 } 242 } 243 } 244 clone.isSpecified(true); 245 return clone; 246 } 247 248 252 public short getNodeType() { 253 return Node.ATTRIBUTE_NODE; 254 } 255 256 259 public String getNodeName() { 260 if (needsSyncData()) { 261 synchronizeData(); 262 } 263 return name; 264 } 265 266 272 public void setNodeValue(String value) throws DOMException { 273 setValue(value); 274 } 275 276 282 public String getNodeValue() { 283 return getValue(); 284 } 285 286 290 294 public String getName() { 295 296 if (needsSyncData()) { 297 synchronizeData(); 298 } 299 return name; 300 301 } 303 308 public void setValue(String newvalue) { 309 310 if (isReadOnly()) { 311 throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, 312 "DOM001 Modification not allowed"); 313 } 314 315 LCount lc=null; 316 String oldvalue=""; 317 DocumentImpl ownerDocument = ownerDocument(); 318 if(MUTATIONEVENTS && ownerDocument.mutationEvents) 319 { 320 lc=LCount.lookup(MutationEventImpl.DOM_ATTR_MODIFIED); 325 if(lc.captures+lc.bubbles+lc.defaults>0 && ownerNode!=null) 326 { 327 oldvalue=getValue(); 328 } 329 330 } 332 if(MUTATIONEVENTS && ownerDocument.mutationEvents) 333 { 334 if (needsSyncChildren()) { 337 synchronizeChildren(); 338 } 339 if (value != null) { 340 if (hasStringValue()) { 341 if (textNode == null) { 344 textNode = (TextImpl) 345 ownerDocument.createTextNode((String ) value); 346 } 347 else { 348 textNode.data = (String ) value; 349 } 350 value = textNode; 351 textNode.isFirstChild(true); 352 textNode.previousSibling = textNode; 353 textNode.ownerNode = this; 354 textNode.isOwned(true); 355 hasStringValue(false); 356 internalRemoveChild(textNode, MUTATION_LOCAL); 357 } 358 else { 359 while (value != null) { 360 internalRemoveChild((Node) value, MUTATION_LOCAL); 361 } 362 } 363 } 364 } 365 else 366 { 367 if (!hasStringValue() && value != null) { 369 ChildNode firstChild = (ChildNode) value; 371 firstChild.previousSibling = null; 372 firstChild.isFirstChild(false); 373 } 374 value = null; 376 needsSyncChildren(false); 377 } 378 379 isSpecified(true); 385 if(MUTATIONEVENTS && ownerDocument.mutationEvents) { 386 internalInsertBefore(ownerDocument.createTextNode(newvalue), 388 null, MUTATION_LOCAL); 389 hasStringValue(false); 390 } else { 391 value = newvalue; 393 hasStringValue(true); 394 } 395 396 changed(); 398 if(MUTATIONEVENTS && ownerDocument.mutationEvents) 399 { 400 dispatchAggregateEvents(this,oldvalue,MutationEvent.MODIFICATION); 402 } 403 404 } 406 410 public String getValue() { 411 412 if (needsSyncChildren()) { 413 synchronizeChildren(); 414 } 415 if (value == null) { 416 return ""; 417 } 418 if (hasStringValue()) { 419 return (String ) value; 420 } 421 ChildNode firstChild = ((ChildNode) value); 422 ChildNode node = firstChild.nextSibling; 423 if (node == null) { 424 return firstChild.getNodeValue(); 425 } 426 StringBuffer value = new StringBuffer (firstChild.getNodeValue()); 427 while (node != null) { 428 value.append(node.getNodeValue()); 429 node = node.nextSibling; 430 } 431 return value.toString(); 432 433 } 435 446 public boolean getSpecified() { 447 448 if (needsSyncData()) { 449 synchronizeData(); 450 } 451 return isSpecified(); 452 453 } 455 459 468 public Element getElement() { 469 return (Element) (isOwned() ? ownerNode : null); 472 } 473 474 480 public Element getOwnerElement() { 481 return (Element) (isOwned() ? ownerNode : null); 484 } 485 486 public void normalize() { 487 488 if (isNormalized() || hasStringValue()) 491 return; 492 493 Node kid, next; 494 ChildNode firstChild = (ChildNode)value; 495 for (kid = firstChild; kid != null; kid = next) { 496 next = kid.getNextSibling(); 497 498 if ( kid.getNodeType() == Node.TEXT_NODE ) 504 { 505 if ( next!=null && next.getNodeType() == Node.TEXT_NODE ) 507 { 508 ((Text)kid).appendData(next.getNodeValue()); 509 removeChild( next ); 510 next = kid; } 512 else 513 { 514 if ( kid.getNodeValue().length()==0 ) 516 removeChild( kid ); 517 } 518 } 519 } 520 521 isNormalized(true); 522 } 524 528 529 public void setSpecified(boolean arg) { 530 531 if (needsSyncData()) { 532 synchronizeData(); 533 } 534 isSpecified(arg); 535 536 } 538 542 543 public String toString() { 544 return getName() + "=" + "\"" + getValue() + "\""; 545 } 546 547 551 public boolean hasChildNodes() { 552 if (needsSyncChildren()) { 553 synchronizeChildren(); 554 } 555 return value != null; 556 } 557 558 571 public NodeList getChildNodes() { 572 574 if (needsSyncChildren()) { 575 synchronizeChildren(); 576 } 577 return this; 578 579 } 581 582 public Node getFirstChild() { 583 584 if (needsSyncChildren()) { 585 synchronizeChildren(); 586 } 587 makeChildNode(); 588 return (Node) value; 589 590 } 592 593 public Node getLastChild() { 594 595 if (needsSyncChildren()) { 596 synchronizeChildren(); 597 } 598 return lastChild(); 599 600 } 602 final ChildNode lastChild() { 603 makeChildNode(); 605 return value != null ? ((ChildNode) value).previousSibling : null; 606 } 607 608 final void lastChild(ChildNode node) { 609 if (value != null) { 611 ((ChildNode) value).previousSibling = node; 612 } 613 } 614 615 643 public Node insertBefore(Node newChild, Node refChild) 644 throws DOMException { 645 return internalInsertBefore(newChild,refChild,MUTATION_ALL); 647 } 649 654 Node internalInsertBefore(Node newChild, Node refChild,int mutationMask) 655 throws DOMException { 656 657 DocumentImpl ownerDocument = ownerDocument(); 658 boolean errorChecking = ownerDocument.errorChecking; 659 660 if (newChild.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE) { 661 668 676 if (errorChecking) { 679 for (Node kid = newChild.getFirstChild(); kid != null; kid = kid.getNextSibling()) { 681 682 if (!ownerDocument.isKidOK(this, kid)) { 683 throw new DOMException( 684 DOMException.HIERARCHY_REQUEST_ERR, 685 "DOM006 Hierarchy request error"); 686 } 687 } 688 } 689 690 while (newChild.hasChildNodes()) { 691 insertBefore(newChild.getFirstChild(), refChild); 692 } 693 return newChild; 694 } 695 696 if (newChild == refChild) { 697 refChild = refChild.getNextSibling(); 699 removeChild(newChild); 700 insertBefore(newChild, refChild); 701 return newChild; 702 } 703 704 if (needsSyncChildren()) { 705 synchronizeChildren(); 706 } 707 708 if (errorChecking) { 709 if (isReadOnly()) { 710 throw new DOMException( 711 DOMException.NO_MODIFICATION_ALLOWED_ERR, 712 "DOM001 Modification not allowed"); 713 } 714 if (newChild.getOwnerDocument() != ownerDocument) { 715 throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, 716 "DOM005 Wrong document"); 717 } 718 if (!ownerDocument.isKidOK(this, newChild)) { 719 throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, 720 "DOM006 Hierarchy request error"); 721 } 722 if (refChild != null && refChild.getParentNode() != this) { 724 throw new DOMException(DOMException.NOT_FOUND_ERR, 725 "DOM008 Not found"); 726 } 727 728 boolean treeSafe = true; 732 for (NodeImpl a = this; treeSafe && a != null; a = a.parentNode()) 733 { 734 treeSafe = newChild != a; 735 } 736 if (!treeSafe) { 737 throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, 738 "DOM006 Hierarchy request error"); 739 } 740 } 741 742 makeChildNode(); 744 EnclosingAttr enclosingAttr=null; 745 if (MUTATIONEVENTS && ownerDocument.mutationEvents 746 && (mutationMask&MUTATION_AGGREGATE)!=0) { 747 LCount lc=LCount.lookup(MutationEventImpl.DOM_ATTR_MODIFIED); 752 if (lc.captures+lc.bubbles+lc.defaults>0) { 753 enclosingAttr=getEnclosingAttr(); 754 } 755 } 756 757 ChildNode newInternal = (ChildNode)newChild; 759 760 Node oldparent = newInternal.parentNode(); 761 if (oldparent != null) { 762 oldparent.removeChild(newInternal); 763 } 764 765 ChildNode refInternal = (ChildNode) refChild; 767 768 newInternal.ownerNode = this; 770 newInternal.isOwned(true); 771 772 ChildNode firstChild = (ChildNode) value; 775 if (firstChild == null) { 776 value = newInternal; newInternal.isFirstChild(true); 779 newInternal.previousSibling = newInternal; 780 } 781 else { 782 if (refInternal == null) { 783 ChildNode lastChild = firstChild.previousSibling; 785 lastChild.nextSibling = newInternal; 786 newInternal.previousSibling = lastChild; 787 firstChild.previousSibling = newInternal; 788 } 789 else { 790 if (refChild == firstChild) { 792 firstChild.isFirstChild(false); 794 newInternal.nextSibling = firstChild; 795 newInternal.previousSibling = firstChild.previousSibling; 796 firstChild.previousSibling = newInternal; 797 value = newInternal; newInternal.isFirstChild(true); 799 } 800 else { 801 ChildNode prev = refInternal.previousSibling; 803 newInternal.nextSibling = refInternal; 804 prev.nextSibling = newInternal; 805 refInternal.previousSibling = newInternal; 806 newInternal.previousSibling = prev; 807 } 808 } 809 } 810 811 changed(); 812 813 if (MUTATIONEVENTS && ownerDocument.mutationEvents) { 814 if ((mutationMask&MUTATION_LOCAL) != 0) { 817 LCount lc = LCount.lookup(MutationEventImpl.DOM_NODE_INSERTED); 819 if (lc.captures+lc.bubbles+lc.defaults>0) { 820 MutationEvent me= new MutationEventImpl(); 821 me.initMutationEvent(MutationEventImpl.DOM_NODE_INSERTED, 822 true,false,this,null, 823 null,null,(short)0); 824 newInternal.dispatchEvent(me); 825 } 826 827 lc=LCount.lookup( 830 MutationEventImpl.DOM_NODE_INSERTED_INTO_DOCUMENT); 831 if (lc.captures+lc.bubbles+lc.defaults>0) { 832 NodeImpl eventAncestor=this; 833 if (enclosingAttr!=null) 834 eventAncestor= 835 (NodeImpl)(enclosingAttr.node.getOwnerElement()); 836 if (eventAncestor!=null) { NodeImpl p=eventAncestor; 838 while (p!=null) { 839 eventAncestor=p; if (p.getNodeType()==ATTRIBUTE_NODE) { 843 p=(ElementImpl)((AttrImpl)p).getOwnerElement(); 844 } 845 else { 846 p=p.parentNode(); 847 } 848 } 849 if (eventAncestor.getNodeType()==Node.DOCUMENT_NODE) { 850 MutationEvent me= new MutationEventImpl(); 851 me.initMutationEvent(MutationEventImpl 852 .DOM_NODE_INSERTED_INTO_DOCUMENT, 853 false,false,null,null, 854 null,null,(short)0); 855 dispatchEventToSubtree(newInternal,me); 856 } 857 } 858 } 859 } 860 861 if ((mutationMask&MUTATION_AGGREGATE) != 0) { 864 dispatchAggregateEvents(enclosingAttr); 865 } 866 } 867 868 checkNormalizationAfterInsert(newInternal); 869 870 return newChild; 871 872 } 874 886 public Node removeChild(Node oldChild) 887 throws DOMException { 888 if (hasStringValue()) { 890 throw new DOMException(DOMException.NOT_FOUND_ERR, 892 "DOM008 Not found"); 893 } 894 return internalRemoveChild(oldChild,MUTATION_ALL); 895 } 897 902 Node internalRemoveChild(Node oldChild,int mutationMask) 903 throws DOMException { 904 905 DocumentImpl ownerDocument = ownerDocument(); 906 if (ownerDocument.errorChecking) { 907 if (isReadOnly()) { 908 throw new DOMException( 909 DOMException.NO_MODIFICATION_ALLOWED_ERR, 910 "DOM001 Modification not allowed"); 911 } 912 if (oldChild != null && oldChild.getParentNode() != this) { 913 throw new DOMException(DOMException.NOT_FOUND_ERR, 914 "DOM008 Not found"); 915 } 916 } 917 918 ownerDocument.removedChildNode(oldChild); 920 921 ChildNode oldInternal = (ChildNode) oldChild; 922 923 EnclosingAttr enclosingAttr=null; 924 if(MUTATIONEVENTS && ownerDocument.mutationEvents) 925 { 926 LCount lc=LCount.lookup(MutationEventImpl.DOM_ATTR_MODIFIED); 931 if(lc.captures+lc.bubbles+lc.defaults>0) 932 { 933 enclosingAttr=getEnclosingAttr(); 934 } 935 936 if( (mutationMask&MUTATION_LOCAL) != 0) 937 { 938 lc=LCount.lookup(MutationEventImpl.DOM_NODE_REMOVED); 940 if(lc.captures+lc.bubbles+lc.defaults>0) 941 { 942 MutationEvent me= new MutationEventImpl(); 943 me.initMutationEvent(MutationEventImpl.DOM_NODE_REMOVED, 944 true,false,this,null, 945 null,null,(short)0); 946 oldInternal.dispatchEvent(me); 947 } 948 949 lc=LCount.lookup( 952 MutationEventImpl.DOM_NODE_REMOVED_FROM_DOCUMENT); 953 if(lc.captures+lc.bubbles+lc.defaults>0) 954 { 955 NodeImpl eventAncestor=this; 956 if(enclosingAttr!=null) 957 eventAncestor= 958 (NodeImpl) enclosingAttr.node.getOwnerElement(); 959 if(eventAncestor!=null) { 961 for(NodeImpl p=eventAncestor.parentNode(); 962 p!=null; 963 p=p.parentNode()) 964 { 965 eventAncestor=p; } 967 if(eventAncestor.getNodeType()==Node.DOCUMENT_NODE) 968 { 969 MutationEvent me= new MutationEventImpl(); 970 me.initMutationEvent(MutationEventImpl 971 .DOM_NODE_REMOVED_FROM_DOCUMENT, 972 false,false, 973 null,null,null,null,(short)0); 974 dispatchEventToSubtree(oldInternal,me); 975 } 976 } 977 } 978 } 979 } 981 if (oldInternal == value) { oldInternal.isFirstChild(false); 986 value = oldInternal.nextSibling; ChildNode firstChild = (ChildNode) value; 988 if (firstChild != null) { 989 firstChild.isFirstChild(true); 990 firstChild.previousSibling = oldInternal.previousSibling; 991 } 992 } else { 993 ChildNode prev = oldInternal.previousSibling; 994 ChildNode next = oldInternal.nextSibling; 995 prev.nextSibling = next; 996 if (next == null) { 997 ChildNode firstChild = (ChildNode) value; 999 firstChild.previousSibling = prev; 1000 } else { 1001 next.previousSibling = prev; 1003 } 1004 } 1005 1006 ChildNode oldPreviousSibling = oldInternal.previousSibling(); 1008 1009 oldInternal.ownerNode = ownerDocument; 1011 oldInternal.isOwned(false); 1012 oldInternal.nextSibling = null; 1013 oldInternal.previousSibling = null; 1014 1015 changed(); 1016 1017 if(MUTATIONEVENTS && ownerDocument.mutationEvents) 1018 { 1019 if( (mutationMask&MUTATION_AGGREGATE) != 0) 1023 dispatchAggregateEvents(enclosingAttr); 1024 } 1026 checkNormalizationAfterRemove(oldPreviousSibling); 1027 1028 return oldInternal; 1029 1030 } 1032 1053 public Node replaceChild(Node newChild, Node oldChild) 1054 throws DOMException { 1055 1056 makeChildNode(); 1057 1058 1064 EnclosingAttr enclosingAttr=null; 1065 DocumentImpl ownerDocument = ownerDocument(); 1066 if(MUTATIONEVENTS && ownerDocument.mutationEvents) 1067 { 1068 LCount lc=LCount.lookup(MutationEventImpl.DOM_ATTR_MODIFIED); 1073 if(lc.captures+lc.bubbles+lc.defaults>0) 1074 { 1075 enclosingAttr=getEnclosingAttr(); 1076 } 1077 } 1079 internalInsertBefore(newChild, oldChild,MUTATION_LOCAL); 1080 if (newChild != oldChild) { 1081 internalRemoveChild(oldChild,MUTATION_LOCAL); 1082 } 1083 1084 if(MUTATIONEVENTS && ownerDocument.mutationEvents) 1085 { 1086 dispatchAggregateEvents(enclosingAttr); 1087 } 1088 1089 return oldChild; 1090 } 1091 1092 1096 1100 public int getLength() { 1101 1102 if (hasStringValue()) { 1103 return 1; 1104 } 1105 ChildNode node = (ChildNode) value; 1106 int length = 0; 1107 for (; node != null; node = node.nextSibling) { 1108 length++; 1109 } 1110 return length; 1111 1112 } 1114 1120 public Node item(int index) { 1121 1122 if (hasStringValue()) { 1123 if (index != 0 || value == null) { 1124 return null; 1125 } 1126 else { 1127 makeChildNode(); 1128 return (Node) value; 1129 } 1130 } 1131 ChildNode node = (ChildNode) value; 1132 for (int i = 0; i < index && node != null; i++) { 1133 node = node.nextSibling; 1134 } 1135 return node; 1136 1137 } 1139 1143 1147 1155 public void setReadOnly(boolean readOnly, boolean deep) { 1156 1157 super.setReadOnly(readOnly, deep); 1158 1159 if (deep) { 1160 1161 if (needsSyncChildren()) { 1162 synchronizeChildren(); 1163 } 1164 1165 if (hasStringValue()) { 1166 return; 1167 } 1168 for (ChildNode mykid = (ChildNode) value; 1170 mykid != null; 1171 mykid = mykid.nextSibling) { 1172 if (mykid.getNodeType() != Node.ENTITY_REFERENCE_NODE) { 1173 mykid.setReadOnly(readOnly,true); 1174 } 1175 } 1176 } 1177 } 1179 1183 1187 protected void synchronizeChildren() { 1188 needsSyncChildren(false); 1190 } 1191 1192 1193 1208 void checkNormalizationAfterInsert(ChildNode insertedChild) { 1209 if (insertedChild.getNodeType() == Node.TEXT_NODE) { 1211 ChildNode prev = insertedChild.previousSibling(); 1212 ChildNode next = insertedChild.nextSibling; 1213 if ((prev != null && prev.getNodeType() == Node.TEXT_NODE) || 1216 (next != null && next.getNodeType() == Node.TEXT_NODE)) { 1217 isNormalized(false); 1218 } 1219 } 1220 else { 1221 if (!insertedChild.isNormalized()) { 1224 isNormalized(false); 1225 } 1226 } 1227 } 1229 1241 void checkNormalizationAfterRemove(ChildNode previousSibling) { 1242 if (previousSibling != null && 1246 previousSibling.getNodeType() == Node.TEXT_NODE) { 1247 1248 ChildNode next = previousSibling.nextSibling; 1249 if (next != null && next.getNodeType() == Node.TEXT_NODE) { 1250 isNormalized(false); 1251 } 1252 } 1253 } 1255 1259 1260 private void writeObject(ObjectOutputStream out) throws IOException { 1261 1262 if (needsSyncChildren()) { 1264 synchronizeChildren(); 1265 } 1266 out.defaultWriteObject(); 1268 1269 } 1271 1272 private void readObject(ObjectInputStream ois) 1273 throws ClassNotFoundException , IOException { 1274 1275 ois.defaultReadObject(); 1277 1278 1281 needsSyncChildren(false); 1282 1283 } 1285} | Popular Tags |