1 57 58 package com.sun.org.apache.xerces.internal.dom; 59 60 import java.io.Serializable ; 61 import java.util.Hashtable ; 62 import java.util.Vector ; 63 64 import com.sun.org.apache.xerces.internal.dom.events.EventImpl; 65 import com.sun.org.apache.xerces.internal.dom.events.MutationEventImpl; 66 import org.w3c.dom.UserDataHandler ; 67 import org.w3c.dom.Attr ; 68 import org.w3c.dom.DOMException ; 69 import org.w3c.dom.DOMImplementation ; 70 import org.w3c.dom.DocumentType ; 71 import org.w3c.dom.Element ; 72 import org.w3c.dom.NamedNodeMap ; 73 import org.w3c.dom.Node ; 74 import org.w3c.dom.events.DocumentEvent ; 75 import org.w3c.dom.events.Event ; 76 import org.w3c.dom.events.EventException ; 77 import org.w3c.dom.events.EventListener ; 78 import org.w3c.dom.events.MutationEvent ; 79 import org.w3c.dom.ranges.DocumentRange; 80 import org.w3c.dom.ranges.Range; 81 import org.w3c.dom.traversal.DocumentTraversal; 82 import org.w3c.dom.traversal.NodeFilter; 83 import org.w3c.dom.traversal.NodeIterator; 84 import org.w3c.dom.traversal.TreeWalker; 85 86 87 117 public class DocumentImpl 118 extends CoreDocumentImpl 119 implements DocumentTraversal, DocumentEvent , DocumentRange { 120 121 125 126 static final long serialVersionUID = 515687835542616694L; 127 128 132 133 protected Vector iterators; 135 136 137 protected Vector ranges; 139 140 141 protected Hashtable eventListeners; 142 143 144 protected boolean mutationEvents = false; 145 146 150 154 public DocumentImpl() { 155 super(); 156 } 157 158 159 public DocumentImpl(boolean grammarAccess) { 160 super(grammarAccess); 161 } 162 163 167 public DocumentImpl(DocumentType doctype) 168 { 169 super(doctype); 170 } 171 172 173 public DocumentImpl(DocumentType doctype, boolean grammarAccess) { 174 super(doctype, grammarAccess); 175 } 176 177 181 190 public Node cloneNode(boolean deep) { 191 192 DocumentImpl newdoc = new DocumentImpl(); 193 callUserDataHandlers(this, newdoc, UserDataHandler.NODE_CLONED); 194 cloneNode(newdoc, deep); 195 196 newdoc.mutationEvents = mutationEvents; 198 199 return newdoc; 200 201 } 203 209 public DOMImplementation getImplementation() { 210 return DOMImplementationImpl.getDOMImplementation(); 213 } 214 215 219 229 public NodeIterator createNodeIterator(Node root, 230 short whatToShow, 231 NodeFilter filter) 232 { 233 return createNodeIterator(root, whatToShow, filter, true); 234 } 235 236 248 public NodeIterator createNodeIterator(Node root, 249 int whatToShow, 250 NodeFilter filter, 251 boolean entityReferenceExpansion) 252 { 253 254 if (root == null) { 255 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_SUPPORTED_ERR", null); 256 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, msg); 257 } 258 259 NodeIterator iterator = new NodeIteratorImpl(this, 260 root, 261 whatToShow, 262 filter, 263 entityReferenceExpansion); 264 if (iterators == null) { 265 iterators = new Vector (); 266 } 267 268 iterators.addElement(iterator); 269 270 return iterator; 271 } 272 273 281 public TreeWalker createTreeWalker(Node root, 282 short whatToShow, 283 NodeFilter filter) 284 { 285 return createTreeWalker(root, whatToShow, filter, true); 286 } 287 297 public TreeWalker createTreeWalker(Node root, 298 int whatToShow, 299 NodeFilter filter, 300 boolean entityReferenceExpansion) 301 { 302 if (root == null) { 303 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_SUPPORTED_ERR", null); 304 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, msg); 305 } 306 return new TreeWalkerImpl(root, whatToShow, filter, 307 entityReferenceExpansion); 308 } 309 310 314 320 void removeNodeIterator(NodeIterator nodeIterator) { 321 322 if (nodeIterator == null) return; 323 if (iterators == null) return; 324 325 iterators.removeElement(nodeIterator); 326 } 327 328 333 public Range createRange() { 334 335 if (ranges == null) { 336 ranges = new Vector (); 337 } 338 339 Range range = new RangeImpl(this); 340 341 ranges.addElement(range); 342 343 return range; 344 345 } 346 347 351 void removeRange(Range range) { 352 353 if (range == null) return; 354 if (ranges == null) return; 355 356 ranges.removeElement(range); 357 } 358 359 363 void replacedText(NodeImpl node) { 364 if (ranges != null) { 366 int size = ranges.size(); 367 for (int i = 0; i != size; i++) { 368 ((RangeImpl)ranges.elementAt(i)).receiveReplacedText(node); 369 } 370 } 371 } 372 373 377 void deletedText(NodeImpl node, int offset, int count) { 378 if (ranges != null) { 380 int size = ranges.size(); 381 for (int i = 0; i != size; i++) { 382 ((RangeImpl)ranges.elementAt(i)).receiveDeletedText(node, 383 offset, count); 384 } 385 } 386 } 387 388 392 void insertedText(NodeImpl node, int offset, int count) { 393 if (ranges != null) { 395 int size = ranges.size(); 396 for (int i = 0; i != size; i++) { 397 ((RangeImpl)ranges.elementAt(i)).receiveInsertedText(node, 398 offset, count); 399 } 400 } 401 } 402 403 407 void splitData(Node node, Node newNode, int offset) { 408 if (ranges != null) { 410 int size = ranges.size(); 411 for (int i = 0; i != size; i++) { 412 ((RangeImpl)ranges.elementAt(i)).receiveSplitData(node, 413 newNode, offset); 414 } 415 } 416 } 417 418 422 441 public Event createEvent(String type) 442 throws DOMException { 443 if (type.equalsIgnoreCase("Events") || "Event".equals(type)) 444 return new EventImpl(); 445 if (type.equalsIgnoreCase("MutationEvents") || 446 "MutationEvent".equals(type)) 447 return new MutationEventImpl(); 448 else { 449 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_SUPPORTED_ERR", null); 450 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, msg); 451 } 452 } 453 454 458 void setMutationEvents(boolean set) { 459 mutationEvents = set; 460 } 461 462 465 boolean getMutationEvents() { 466 return mutationEvents; 467 } 468 469 475 protected void setEventListeners(NodeImpl n, Vector listeners) { 476 if (eventListeners == null) { 477 eventListeners = new Hashtable (); 478 } 479 if (listeners == null) { 480 eventListeners.remove(n); 481 if (eventListeners.isEmpty()) { 482 mutationEvents = false; 484 } 485 } else { 486 eventListeners.put(n, listeners); 487 mutationEvents = true; 489 } 490 } 491 492 495 protected Vector getEventListeners(NodeImpl n) { 496 if (eventListeners == null) { 497 return null; 498 } 499 return (Vector ) eventListeners.get(n); 500 } 501 502 506 510 520 class LEntry implements Serializable 521 { 522 String type; 523 EventListener listener; 524 boolean useCapture; 525 526 532 LEntry(String type, EventListener listener, boolean useCapture) 533 { 534 this.type = type; 535 this.listener = listener; 536 this.useCapture = useCapture; 537 } 538 539 } 541 552 protected void addEventListener(NodeImpl node, String type, 553 EventListener listener, boolean useCapture) 554 { 555 if (type == null || type.equals("") || listener == null) 558 return; 559 560 removeEventListener(node, type, listener, useCapture); 563 564 Vector nodeListeners = getEventListeners(node); 565 if(nodeListeners == null) { 566 nodeListeners = new Vector (); 567 setEventListeners(node, nodeListeners); 568 } 569 nodeListeners.addElement(new LEntry(type, listener, useCapture)); 570 571 LCount lc = LCount.lookup(type); 573 if (useCapture) 574 ++lc.captures; 575 else 576 ++lc.bubbles; 577 578 } 580 591 protected void removeEventListener(NodeImpl node, String type, 592 EventListener listener, 593 boolean useCapture) 594 { 595 if (type == null || type.equals("") || listener == null) 597 return; 598 Vector nodeListeners = getEventListeners(node); 599 if (nodeListeners == null) 600 return; 601 602 for (int i = nodeListeners.size() - 1; i >= 0; --i) { 606 LEntry le = (LEntry) nodeListeners.elementAt(i); 607 if (le.useCapture == useCapture && le.listener == listener && 608 le.type.equals(type)) { 609 nodeListeners.removeElementAt(i); 610 if (nodeListeners.size() == 0) 612 setEventListeners(node, null); 613 614 LCount lc = LCount.lookup(type); 616 if (useCapture) 617 --lc.captures; 618 else 619 --lc.bubbles; 620 621 break; } 623 } 624 } 626 protected void copyEventListeners(NodeImpl src, NodeImpl tgt) { 627 Vector nodeListeners = getEventListeners(src); 628 if (nodeListeners == null) { 629 return; 630 } 631 setEventListeners(tgt, (Vector ) nodeListeners.clone()); 632 } 633 634 684 protected boolean dispatchEvent(NodeImpl node, Event event) { 685 if (event == null) return false; 686 687 EventImpl evt = (EventImpl)event; 690 691 if(!evt.initialized || evt.type == null || evt.type.equals("")) { 694 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "UNSPECIFIED_EVENT_TYPE_ERR", null); 695 throw new EventException (EventException.UNSPECIFIED_EVENT_TYPE_ERR, msg); 696 } 697 698 LCount lc = LCount.lookup(evt.getType()); 700 if (lc.captures + lc.bubbles + lc.defaults == 0) 701 return evt.preventDefault; 702 703 evt.target = node; 708 evt.stopPropagation = false; 709 evt.preventDefault = false; 710 711 Vector pv = new Vector (10,10); 721 Node p = node; 722 Node n = p.getParentNode(); 723 while (n != null) { 724 pv.addElement(n); 725 p = n; 726 n = n.getParentNode(); 727 } 728 729 if (lc.captures > 0) { 731 evt.eventPhase = Event.CAPTURING_PHASE; 732 for (int j = pv.size() - 1; j >= 0; --j) { 735 if (evt.stopPropagation) 736 break; 738 NodeImpl nn = (NodeImpl) pv.elementAt(j); 740 evt.currentTarget = nn; 741 Vector nodeListeners = getEventListeners(nn); 742 if (nodeListeners != null) { 743 Vector nl = (Vector ) nodeListeners.clone(); 744 int nlsize = nl.size(); 746 for (int i = 0; i < nlsize; i++) { 747 LEntry le = (LEntry) nl.elementAt(i); 748 if (le.useCapture && le.type.equals(evt.type) && 749 nodeListeners.contains(le)) { 750 try { 751 le.listener.handleEvent(evt); 752 } 753 catch (Exception e) { 754 } 756 } 757 } 758 } 759 } 760 } 761 762 if (lc.bubbles > 0) { 764 evt.eventPhase = Event.AT_TARGET; 768 evt.currentTarget = node; 769 Vector nodeListeners = getEventListeners(node); 770 if (!evt.stopPropagation && nodeListeners != null) { 771 Vector nl = (Vector ) nodeListeners.clone(); 772 int nlsize = nl.size(); 774 for (int i = 0; i < nlsize; i++) { 775 LEntry le = (LEntry) nl.elementAt(i); 776 if (!le.useCapture && le.type.equals(evt.type) && 777 nodeListeners.contains(le)) { 778 try { 779 le.listener.handleEvent(evt); 780 } 781 catch (Exception e) { 782 } 784 } 785 } 786 } 787 if (evt.bubbles) { 793 evt.eventPhase = Event.BUBBLING_PHASE; 794 int pvsize = pv.size(); 795 for (int j = 0; j < pvsize; j++) { 796 if (evt.stopPropagation) 797 break; 799 NodeImpl nn = (NodeImpl) pv.elementAt(j); 801 evt.currentTarget = nn; 802 nodeListeners = getEventListeners(nn); 803 if (nodeListeners != null) { 804 Vector nl = (Vector ) nodeListeners.clone(); 805 int nlsize = nl.size(); 808 for (int i = 0; i < nlsize; i++) { 809 LEntry le = (LEntry) nl.elementAt(i); 810 if (!le.useCapture && le.type.equals(evt.type) && 811 nodeListeners.contains(le)) { 812 try { 813 le.listener.handleEvent(evt); 814 } 815 catch (Exception e) { 816 } 818 } 819 } 820 } 821 } 822 } 823 } 824 825 if (lc.defaults > 0 && (!evt.cancelable || !evt.preventDefault)) { 831 } 835 836 return evt.preventDefault; 837 } 839 852 protected void dispatchEventToSubtree(NodeImpl node, Node n, Event e) { 853 Vector nodeListeners = getEventListeners(node); 854 if (nodeListeners == null || n == null) 855 return; 856 857 ((NodeImpl) n).dispatchEvent(e); 861 if (n.getNodeType() == Node.ELEMENT_NODE) { 862 NamedNodeMap a = n.getAttributes(); 863 for (int i = a.getLength() - 1; i >= 0; --i) 864 dispatchEventToSubtree(node, a.item(i), e); 865 } 866 dispatchEventToSubtree(node, n.getFirstChild(), e); 867 dispatchEventToSubtree(node, n.getNextSibling(), e); 868 } 870 875 class EnclosingAttr implements Serializable 876 { 877 AttrImpl node; 878 String oldvalue; 879 } 880 881 EnclosingAttr savedEnclosingAttr; 882 883 890 protected void dispatchAggregateEvents(NodeImpl node, EnclosingAttr ea) { 891 if (ea != null) 892 dispatchAggregateEvents(node, ea.node, ea.oldvalue, 893 MutationEvent.MODIFICATION); 894 else 895 dispatchAggregateEvents(node, null, null, (short) 0); 896 897 } 899 923 protected void dispatchAggregateEvents(NodeImpl node, 924 AttrImpl enclosingAttr, 925 String oldvalue, short change) { 926 NodeImpl owner = null; 928 if (enclosingAttr != null) { 929 LCount lc = LCount.lookup(MutationEventImpl.DOM_ATTR_MODIFIED); 930 owner = (NodeImpl) enclosingAttr.getOwnerElement(); 931 if (lc.captures + lc.bubbles + lc.defaults > 0) { 932 if (owner != null) { 933 MutationEventImpl me = new MutationEventImpl(); 934 me.initMutationEvent(MutationEventImpl.DOM_ATTR_MODIFIED, 935 true, false, enclosingAttr, 936 oldvalue, 937 enclosingAttr.getNodeValue(), 938 enclosingAttr.getNodeName(), 939 change); 940 owner.dispatchEvent(me); 941 } 942 } 943 } 944 LCount lc = LCount.lookup(MutationEventImpl.DOM_SUBTREE_MODIFIED); 949 if (lc.captures + lc.bubbles + lc.defaults > 0) { 950 MutationEvent me = new MutationEventImpl(); 951 me.initMutationEvent(MutationEventImpl.DOM_SUBTREE_MODIFIED, 952 true, false, null, null, 953 null, null, (short) 0); 954 955 if (enclosingAttr != null) { 959 dispatchEvent(enclosingAttr, me); 960 if (owner != null) 961 dispatchEvent(owner, me); 962 } 963 else 964 dispatchEvent(node, me); 965 } 966 } 968 975 protected void saveEnclosingAttr(NodeImpl node) { 976 savedEnclosingAttr = null; 977 LCount lc = LCount.lookup(MutationEventImpl.DOM_ATTR_MODIFIED); 982 if (lc.captures + lc.bubbles + lc.defaults > 0) { 983 NodeImpl eventAncestor = node; 984 while (true) { 985 if (eventAncestor == null) 986 return; 987 int type = eventAncestor.getNodeType(); 988 if (type == Node.ATTRIBUTE_NODE) { 989 EnclosingAttr retval = new EnclosingAttr(); 990 retval.node = (AttrImpl) eventAncestor; 991 retval.oldvalue = retval.node.getNodeValue(); 992 savedEnclosingAttr = retval; 993 return; 994 } 995 else if (type == Node.ENTITY_REFERENCE_NODE) 996 eventAncestor = eventAncestor.parentNode(); 997 else 998 return; 999 } 1001 } 1002 } 1004 1007 void modifyingCharacterData(NodeImpl node) { 1008 if (mutationEvents) { 1009 saveEnclosingAttr(node); 1010 } 1011 } 1012 1013 1016 void modifiedCharacterData(NodeImpl node, String oldvalue, String value) { 1017 if (mutationEvents) { 1018 LCount lc = 1020 LCount.lookup(MutationEventImpl.DOM_CHARACTER_DATA_MODIFIED); 1021 if (lc.captures + lc.bubbles + lc.defaults > 0) { 1022 MutationEvent me = new MutationEventImpl(); 1023 me.initMutationEvent( 1024 MutationEventImpl.DOM_CHARACTER_DATA_MODIFIED, 1025 true, false, null, 1026 oldvalue, value, null, (short) 0); 1027 dispatchEvent(node, me); 1028 } 1029 1030 dispatchAggregateEvents(node, savedEnclosingAttr); 1033 } } 1035 1036 1039 void insertingNode(NodeImpl node, boolean replace) { 1040 if (mutationEvents) { 1041 if (!replace) { 1042 saveEnclosingAttr(node); 1043 } 1044 } 1045 } 1046 1047 1050 void insertedNode(NodeImpl node, NodeImpl newInternal, boolean replace) { 1051 if (mutationEvents) { 1052 LCount lc = LCount.lookup(MutationEventImpl.DOM_NODE_INSERTED); 1056 if (lc.captures + lc.bubbles + lc.defaults > 0) { 1057 MutationEventImpl me = new MutationEventImpl(); 1058 me.initMutationEvent(MutationEventImpl.DOM_NODE_INSERTED, 1059 true, false, node, 1060 null, null, null, (short) 0); 1061 dispatchEvent(newInternal, me); 1062 } 1063 1064 lc = LCount.lookup( 1067 MutationEventImpl.DOM_NODE_INSERTED_INTO_DOCUMENT); 1068 if (lc.captures + lc.bubbles + lc.defaults > 0) { 1069 NodeImpl eventAncestor = node; 1070 if (savedEnclosingAttr != null) 1071 eventAncestor = (NodeImpl) 1072 savedEnclosingAttr.node.getOwnerElement(); 1073 if (eventAncestor != null) { NodeImpl p = eventAncestor; 1075 while (p != null) { 1076 eventAncestor = p; if (p.getNodeType() == ATTRIBUTE_NODE) { 1080 p = (NodeImpl) ((AttrImpl)p).getOwnerElement(); 1081 } 1082 else { 1083 p = p.parentNode(); 1084 } 1085 } 1086 if (eventAncestor.getNodeType() == Node.DOCUMENT_NODE){ 1087 MutationEventImpl me = new MutationEventImpl(); 1088 me.initMutationEvent(MutationEventImpl 1089 .DOM_NODE_INSERTED_INTO_DOCUMENT, 1090 false,false,null,null, 1091 null,null,(short)0); 1092 dispatchEventToSubtree(node, newInternal, me); 1093 } 1094 } 1095 } 1096 if (!replace) { 1097 dispatchAggregateEvents(node, savedEnclosingAttr); 1100 } 1101 } 1102 } 1103 1104 1107 void removingNode(NodeImpl node, NodeImpl oldChild, boolean replace) { 1108 1109 if (iterators != null) { 1111 int size = iterators.size(); 1112 for (int i = 0; i != size; i++) { 1113 ((NodeIteratorImpl)iterators.elementAt(i)).removeNode(oldChild); 1114 } 1115 } 1116 1117 if (ranges != null) { 1119 int size = ranges.size(); 1120 for (int i = 0; i != size; i++) { 1121 ((RangeImpl)ranges.elementAt(i)).removeNode(oldChild); 1122 } 1123 } 1124 1125 if (mutationEvents) { 1127 if (!replace) { 1132 saveEnclosingAttr(node); 1133 } 1134 LCount lc = LCount.lookup(MutationEventImpl.DOM_NODE_REMOVED); 1136 if (lc.captures + lc.bubbles + lc.defaults > 0) { 1137 MutationEventImpl me= new MutationEventImpl(); 1138 me.initMutationEvent(MutationEventImpl.DOM_NODE_REMOVED, 1139 true, false, node, null, 1140 null, null, (short) 0); 1141 dispatchEvent(oldChild, me); 1142 } 1143 1144 lc = LCount.lookup( 1147 MutationEventImpl.DOM_NODE_REMOVED_FROM_DOCUMENT); 1148 if (lc.captures + lc.bubbles + lc.defaults > 0) { 1149 NodeImpl eventAncestor = this; 1150 if(savedEnclosingAttr != null) 1151 eventAncestor = (NodeImpl) 1152 savedEnclosingAttr.node.getOwnerElement(); 1153 if (eventAncestor != null) { for (NodeImpl p = eventAncestor.parentNode(); 1155 p != null; p = p.parentNode()) { 1156 eventAncestor = p; } 1158 if (eventAncestor.getNodeType() == Node.DOCUMENT_NODE){ 1159 MutationEventImpl me = new MutationEventImpl(); 1160 me.initMutationEvent( 1161 MutationEventImpl.DOM_NODE_REMOVED_FROM_DOCUMENT, 1162 false, false, null, 1163 null, null, null, (short) 0); 1164 dispatchEventToSubtree(node, oldChild, me); 1165 } 1166 } 1167 } 1168 } } 1170 1171 1174 void removedNode(NodeImpl node, boolean replace) { 1175 if (mutationEvents) { 1176 if (!replace) { 1180 dispatchAggregateEvents(node, savedEnclosingAttr); 1181 } 1182 } } 1184 1185 1188 void replacingNode(NodeImpl node) { 1189 if (mutationEvents) { 1190 saveEnclosingAttr(node); 1191 } 1192 } 1193 1194 1197 void replacedNode(NodeImpl node) { 1198 if (mutationEvents) { 1199 dispatchAggregateEvents(node, savedEnclosingAttr); 1200 } 1201 } 1202 1203 1206 void modifiedAttrValue(AttrImpl attr, String oldvalue) { 1207 if (mutationEvents) { 1208 dispatchAggregateEvents(attr, attr, oldvalue, 1210 MutationEvent.MODIFICATION); 1211 } 1212 } 1213 1214 1217 void setAttrNode(AttrImpl attr, AttrImpl previous) { 1218 if (mutationEvents) { 1219 if (previous == null) { 1221 dispatchAggregateEvents(attr.ownerNode, attr, null, 1222 MutationEvent.ADDITION); 1223 } 1224 else { 1225 dispatchAggregateEvents(attr.ownerNode, attr, 1226 previous.getNodeValue(), 1227 MutationEvent.MODIFICATION); 1228 } 1229 } 1230 } 1231 1232 1235 void removedAttrNode(AttrImpl attr, NodeImpl oldOwner, String name) { 1236 if (mutationEvents) { 1240 LCount lc = LCount.lookup(MutationEventImpl.DOM_ATTR_MODIFIED); 1243 if (lc.captures + lc.bubbles + lc.defaults > 0) { 1244 MutationEventImpl me= new MutationEventImpl(); 1245 me.initMutationEvent(MutationEventImpl.DOM_ATTR_MODIFIED, 1246 true, false, attr, 1247 attr.getNodeValue(), null, name, 1248 MutationEvent.REMOVAL); 1249 dispatchEvent(oldOwner, me); 1250 } 1251 1252 dispatchAggregateEvents(oldOwner, null, null, (short) 0); 1256 } 1257 } 1258 1259 1260 1263 void renamedAttrNode(Attr oldAt, Attr newAt) { 1264 } 1266 1267 1270 void renamedElement(Element oldEl, Element newEl) { 1271 } 1273 1274} | Popular Tags |