1 57 58 package org.enhydra.apache.xerces.dom; 59 60 import java.util.Enumeration ; 61 import java.util.Hashtable ; 62 import java.util.Vector ; 63 64 import org.enhydra.apache.xerces.dom.events.EventImpl; 65 import org.enhydra.apache.xerces.dom.events.MutationEventImpl; 66 import org.w3c.dom.DOMException ; 67 import org.w3c.dom.DOMImplementation ; 68 import org.w3c.dom.DocumentType ; 69 import org.w3c.dom.NamedNodeMap ; 70 import org.w3c.dom.Node ; 71 import org.w3c.dom.events.DocumentEvent ; 72 import org.w3c.dom.events.Event ; 73 import org.w3c.dom.events.EventException ; 74 import org.w3c.dom.events.EventListener ; 75 import org.w3c.dom.events.MutationEvent ; 76 import org.w3c.dom.ranges.DocumentRange; 77 import org.w3c.dom.ranges.Range; 78 import org.w3c.dom.traversal.DocumentTraversal; 79 import org.w3c.dom.traversal.NodeFilter; 80 import org.w3c.dom.traversal.NodeIterator; 81 import org.w3c.dom.traversal.TreeWalker; 82 83 84 114 public class DocumentImpl 115 extends CoreDocumentImpl 116 implements DocumentTraversal, DocumentEvent , DocumentRange { 117 118 122 123 static final long serialVersionUID = 515687835542616694L; 124 125 129 130 protected Vector iterators; 132 133 134 protected Vector ranges; 136 137 138 protected Hashtable userData; 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 cloneNode(newdoc, deep); 194 195 newdoc.mutationEvents = mutationEvents; 197 198 return newdoc; 199 200 } 202 208 public DOMImplementation getImplementation() { 209 return DOMImplementationImpl.getDOMImplementation(); 212 } 213 214 220 protected void setUserData(NodeImpl n, Object data) { 221 if (userData == null) { 222 userData = new Hashtable (); 223 } 224 if (data == null) { 225 userData.remove(n); 226 } else { 227 userData.put(n, data); 228 } 229 } 230 231 234 protected Object getUserData(NodeImpl n) { 235 if (userData == null) { 236 return null; 237 } 238 return userData.get(n); 239 } 240 241 245 257 public NodeIterator createNodeIterator(Node root, 258 short whatToShow, 259 NodeFilter filter) 260 { 261 return createNodeIterator(root, whatToShow, filter, true); 262 } 263 264 278 public NodeIterator createNodeIterator(Node root, 279 int whatToShow, 280 NodeFilter filter, 281 boolean entityReferenceExpansion) 282 { 283 NodeIterator iterator = new NodeIteratorImpl(this, 284 root, 285 whatToShow, 286 filter, 287 entityReferenceExpansion); 288 if (iterators == null) { 289 iterators = new Vector (); 290 } 291 292 iterators.addElement(iterator); 293 294 return iterator; 295 } 296 297 305 public TreeWalker createTreeWalker(Node root, 306 short whatToShow, 307 NodeFilter filter) 308 { 309 return createTreeWalker(root, whatToShow, filter, true); 310 } 311 321 public TreeWalker createTreeWalker(Node root, 322 int whatToShow, 323 NodeFilter filter, 324 boolean entityReferenceExpansion) 325 { 326 if (root == null) { 327 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, 328 "DOM007 Not supported"); 329 } 330 return new TreeWalkerImpl(root, whatToShow, filter, 331 entityReferenceExpansion); 332 } 333 334 338 344 void removeNodeIterator(NodeIterator nodeIterator) { 345 346 if (nodeIterator == null) return; 347 if (iterators == null) return; 348 349 iterators.removeElement(nodeIterator); 350 } 351 352 357 public Range createRange() { 358 359 if (ranges == null) { 360 ranges = new Vector (); 361 } 362 363 Range range = new RangeImpl(this); 364 365 ranges.addElement(range); 366 367 return range; 368 369 } 370 371 375 void removeRange(Range range) { 376 377 if (range == null) return; 378 if (ranges == null) return; 379 380 ranges.removeElement(range); 381 } 382 383 387 void replacedText(NodeImpl node) { 388 if (ranges != null) { 390 Enumeration enumer = ranges.elements(); 391 while (enumer.hasMoreElements()) { 392 ((RangeImpl)enumer.nextElement()).receiveReplacedText(node); 393 } 394 } 395 } 396 397 401 void deletedText(NodeImpl node, int offset, int count) { 402 if (ranges != null) { 404 Enumeration enumer = ranges.elements(); 405 while (enumer.hasMoreElements()) { 406 ((RangeImpl)enumer.nextElement()).receiveDeletedText(node, 407 offset, 408 count); 409 } 410 } 411 } 412 413 417 void insertedText(NodeImpl node, int offset, int count) { 418 if (ranges != null) { 420 Enumeration enumer = ranges.elements(); 421 while (enumer.hasMoreElements()) { 422 ((RangeImpl)enumer.nextElement()).receiveInsertedText(node, 423 offset, 424 count); 425 } 426 } 427 } 428 429 433 void splitData(Node node, Node newNode, int offset) { 434 if (ranges != null) { 436 Enumeration enumer = ranges.elements(); 437 while (enumer.hasMoreElements()) { 438 ((RangeImpl)enumer.nextElement()).receiveSplitData(node, 439 newNode, 440 offset); 441 } 442 } 443 } 444 445 449 468 public Event createEvent(String type) 469 throws DOMException { 470 if (type.equalsIgnoreCase("Events") || "Event".equals(type)) 471 return new EventImpl(); 472 if (type.equalsIgnoreCase("MutationEvents") || 473 "MutationEvent".equals(type)) 474 return new MutationEventImpl(); 475 else 476 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, 477 "DOM007 Not supported"); 478 } 479 480 484 void setMutationEvents(boolean set) { 485 mutationEvents = set; 486 } 487 488 491 boolean getMutationEvents() { 492 return mutationEvents; 493 } 494 495 501 protected void setEventListeners(NodeImpl n, Vector listeners) { 502 if (eventListeners == null) { 503 eventListeners = new Hashtable (); 504 } 505 if (listeners == null) { 506 eventListeners.remove(n); 507 if (eventListeners.isEmpty()) { 508 mutationEvents = false; 510 } 511 } else { 512 eventListeners.put(n, listeners); 513 mutationEvents = true; 515 } 516 } 517 518 521 protected Vector getEventListeners(NodeImpl n) { 522 if (eventListeners == null) { 523 return null; 524 } 525 return (Vector ) eventListeners.get(n); 526 } 527 528 532 536 546 class LEntry 547 { 548 String type; 549 EventListener listener; 550 boolean useCapture; 551 552 558 LEntry(String type, EventListener listener, boolean useCapture) 559 { 560 this.type = type; 561 this.listener = listener; 562 this.useCapture = useCapture; 563 } 564 } 566 577 protected void addEventListener(NodeImpl node, String type, 578 EventListener listener, boolean useCapture) 579 { 580 if (type == null || type.equals("") || listener == null) 583 return; 584 585 removeEventListener(node, type, listener, useCapture); 588 589 Vector nodeListeners = getEventListeners(node); 590 if(nodeListeners == null) { 591 nodeListeners = new Vector (); 592 setEventListeners(node, nodeListeners); 593 } 594 nodeListeners.addElement(new LEntry(type, listener, useCapture)); 595 596 LCount lc = LCount.lookup(type); 598 if (useCapture) 599 ++lc.captures; 600 else 601 ++lc.bubbles; 602 603 } 605 616 protected void removeEventListener(NodeImpl node, String type, 617 EventListener listener, 618 boolean useCapture) 619 { 620 if (type == null || type.equals("") || listener == null) 622 return; 623 Vector nodeListeners = getEventListeners(node); 624 if (nodeListeners == null) 625 return; 626 627 for (int i = nodeListeners.size() - 1; i >= 0; --i) { 631 LEntry le = (LEntry) nodeListeners.elementAt(i); 632 if (le.useCapture == useCapture && le.listener == listener && 633 le.type.equals(type)) { 634 nodeListeners.removeElementAt(i); 635 if (nodeListeners.size() == 0) 637 setEventListeners(node, null); 638 639 LCount lc = LCount.lookup(type); 641 if (useCapture) 642 --lc.captures; 643 else 644 --lc.bubbles; 645 646 break; } 648 } 649 } 651 701 protected boolean dispatchEvent(NodeImpl node, Event event) { 702 if (event == null) return false; 703 704 EventImpl evt = (EventImpl)event; 707 708 if(!evt.initialized || evt.type == null || evt.type.equals("")) 711 throw new EventException (EventException.UNSPECIFIED_EVENT_TYPE_ERR, 712 "DOM010 Unspecified event type"); 713 714 LCount lc = LCount.lookup(evt.getType()); 716 if (lc.captures + lc.bubbles + lc.defaults == 0) 717 return evt.preventDefault; 718 719 evt.target = node; 724 evt.stopPropagation = false; 725 evt.preventDefault = false; 726 727 Vector pv = new Vector (10,10); 737 Node p = node; 738 Node n = p.getParentNode(); 739 while (n != null) { 740 pv.addElement(n); 741 p = n; 742 n = n.getParentNode(); 743 } 744 745 if (lc.captures > 0) { 747 evt.eventPhase = Event.CAPTURING_PHASE; 748 for (int j = pv.size() - 1; j >= 0; --j) { 751 if (evt.stopPropagation) 752 break; 754 NodeImpl nn = (NodeImpl) pv.elementAt(j); 756 evt.currentTarget = nn; 757 Vector nodeListeners = getEventListeners(nn); 758 if (nodeListeners != null) { 759 Vector nl = (Vector ) nodeListeners.clone(); 760 for (int i = nl.size() - 1; i >= 0; --i) { 762 LEntry le = (LEntry) nl.elementAt(i); 763 if (le.useCapture && le.type.equals(evt.type) && 764 nodeListeners.contains(le)) { 765 try { 766 le.listener.handleEvent(evt); 767 } 768 catch (Exception e) { 769 } 771 } 772 } 773 } 774 } 775 } 776 777 if (lc.bubbles > 0) { 779 evt.eventPhase = Event.AT_TARGET; 783 evt.currentTarget = node; 784 Vector nodeListeners = getEventListeners(node); 785 if (!evt.stopPropagation && nodeListeners != null) { 786 Vector nl = (Vector ) nodeListeners.clone(); 787 for (int i = nl.size() - 1; i >= 0; --i) { 789 LEntry le = (LEntry) nl.elementAt(i); 790 if (!le.useCapture && le.type.equals(evt.type) && 791 nodeListeners.contains(le)) { 792 try { 793 le.listener.handleEvent(evt); 794 } 795 catch (Exception e) { 796 } 798 } 799 } 800 } 801 if (evt.bubbles) { 807 evt.eventPhase = Event.BUBBLING_PHASE; 808 for (int j = 0; j < pv.size(); ++j) { 809 if (evt.stopPropagation) 810 break; 812 NodeImpl nn = (NodeImpl) pv.elementAt(j); 814 evt.currentTarget = nn; 815 nodeListeners = getEventListeners(nn); 816 if (nodeListeners != null) { 817 Vector nl = (Vector ) nodeListeners.clone(); 818 for (int i = nl.size() - 1; i >= 0; --i) { 820 LEntry le = (LEntry) nl.elementAt(i); 821 if (!le.useCapture && le.type.equals(evt.type) && 822 nodeListeners.contains(le)) { 823 try { 824 le.listener.handleEvent(evt); 825 } 826 catch (Exception e) { 827 } 829 } 830 } 831 } 832 } 833 } 834 } 835 836 if (lc.defaults > 0 && (!evt.cancelable || !evt.preventDefault)) { 842 } 846 847 return evt.preventDefault; 848 } 850 851 864 protected void dispatchEventToSubtree(NodeImpl node, Node n, Event e) { 865 Vector nodeListeners = getEventListeners(node); 866 if (nodeListeners == null || n == null) 867 return; 868 869 ((NodeImpl) n).dispatchEvent(e); 873 if (n.getNodeType() == Node.ELEMENT_NODE) { 874 NamedNodeMap a = n.getAttributes(); 875 for (int i = a.getLength() - 1; i >= 0; --i) 876 dispatchEventToSubtree(node, a.item(i), e); 877 } 878 dispatchEventToSubtree(node, n.getFirstChild(), e); 879 dispatchEventToSubtree(node, n.getNextSibling(), e); 880 } 882 887 class EnclosingAttr 888 { 889 AttrImpl node; 890 String oldvalue; 891 } 892 893 EnclosingAttr savedEnclosingAttr; 894 895 902 protected void dispatchAggregateEvents(NodeImpl node, EnclosingAttr ea) { 903 if (ea != null) 904 dispatchAggregateEvents(node, ea.node, ea.oldvalue, 905 MutationEvent.MODIFICATION); 906 else 907 dispatchAggregateEvents(node, null, null, (short) 0); 908 909 } 911 935 protected void dispatchAggregateEvents(NodeImpl node, 936 AttrImpl enclosingAttr, 937 String oldvalue, short change) { 938 NodeImpl owner = null; 940 if (enclosingAttr != null) { 941 LCount lc = LCount.lookup(MutationEventImpl.DOM_ATTR_MODIFIED); 942 owner = (NodeImpl) enclosingAttr.getOwnerElement(); 943 if (lc.captures + lc.bubbles + lc.defaults > 0) { 944 if (owner != null) { 945 MutationEventImpl me = new MutationEventImpl(); 946 me.initMutationEvent(MutationEventImpl.DOM_ATTR_MODIFIED, 947 true, false, enclosingAttr, 948 oldvalue, 949 enclosingAttr.getNodeValue(), 950 enclosingAttr.getNodeName(), 951 change); 952 owner.dispatchEvent(me); 953 } 954 } 955 } 956 LCount lc = LCount.lookup(MutationEventImpl.DOM_SUBTREE_MODIFIED); 961 if (lc.captures + lc.bubbles + lc.defaults > 0) { 962 MutationEvent me = new MutationEventImpl(); 963 me.initMutationEvent(MutationEventImpl.DOM_SUBTREE_MODIFIED, 964 true, false, null, null, 965 null, null, (short) 0); 966 967 if (enclosingAttr != null) { 971 dispatchEvent(enclosingAttr, me); 972 if (owner != null) 973 dispatchEvent(owner, me); 974 } 975 else 976 dispatchEvent(node, me); 977 } 978 } 980 987 protected void saveEnclosingAttr(NodeImpl node) { 988 savedEnclosingAttr = null; 989 LCount lc = LCount.lookup(MutationEventImpl.DOM_ATTR_MODIFIED); 994 if (lc.captures + lc.bubbles + lc.defaults > 0) { 995 NodeImpl eventAncestor = node; 996 while (true) { 997 if (eventAncestor == null) 998 return; 999 int type = eventAncestor.getNodeType(); 1000 if (type == Node.ATTRIBUTE_NODE) { 1001 EnclosingAttr retval = new EnclosingAttr(); 1002 retval.node = (AttrImpl) eventAncestor; 1003 retval.oldvalue = retval.node.getNodeValue(); 1004 savedEnclosingAttr = retval; 1005 return; 1006 } 1007 else if (type == Node.ENTITY_REFERENCE_NODE) 1008 eventAncestor = eventAncestor.parentNode(); 1009 else 1010 return; 1011 } 1013 } 1014 } 1016 1019 void modifyingCharacterData(NodeImpl node) { 1020 if (mutationEvents) { 1021 saveEnclosingAttr(node); 1022 } 1023 } 1024 1025 1028 void modifiedCharacterData(NodeImpl node, String oldvalue, String value) { 1029 if (mutationEvents) { 1030 LCount lc = 1032 LCount.lookup(MutationEventImpl.DOM_CHARACTER_DATA_MODIFIED); 1033 if (lc.captures + lc.bubbles + lc.defaults > 0) { 1034 MutationEvent me = new MutationEventImpl(); 1035 me.initMutationEvent( 1036 MutationEventImpl.DOM_CHARACTER_DATA_MODIFIED, 1037 true, false, null, 1038 oldvalue, value, null, (short) 0); 1039 dispatchEvent(me); 1040 } 1041 1042 dispatchAggregateEvents(node, savedEnclosingAttr); 1045 } } 1047 1048 1051 void insertingNode(NodeImpl node, boolean replace) { 1052 if (mutationEvents) { 1053 if (!replace) { 1054 saveEnclosingAttr(node); 1055 } 1056 } 1057 } 1058 1059 1062 void insertedNode(NodeImpl node, NodeImpl newInternal, boolean replace) { 1063 if (mutationEvents) { 1064 LCount lc = LCount.lookup(MutationEventImpl.DOM_NODE_INSERTED); 1068 if (lc.captures + lc.bubbles + lc.defaults > 0) { 1069 MutationEventImpl me = new MutationEventImpl(); 1070 me.initMutationEvent(MutationEventImpl.DOM_NODE_INSERTED, 1071 true, false, node, 1072 null, null, null, (short) 0); 1073 dispatchEvent(newInternal, me); 1074 } 1075 1076 lc = LCount.lookup( 1079 MutationEventImpl.DOM_NODE_INSERTED_INTO_DOCUMENT); 1080 if (lc.captures + lc.bubbles + lc.defaults > 0) { 1081 NodeImpl eventAncestor = node; 1082 if (savedEnclosingAttr != null) 1083 eventAncestor = (NodeImpl) 1084 savedEnclosingAttr.node.getOwnerElement(); 1085 if (eventAncestor != null) { NodeImpl p = eventAncestor; 1087 while (p != null) { 1088 eventAncestor = p; if (p.getNodeType() == ATTRIBUTE_NODE) { 1092 p = (NodeImpl) ((AttrImpl)p).getOwnerElement(); 1093 } 1094 else { 1095 p = p.parentNode(); 1096 } 1097 } 1098 if (eventAncestor.getNodeType() == Node.DOCUMENT_NODE){ 1099 MutationEventImpl me = new MutationEventImpl(); 1100 me.initMutationEvent(MutationEventImpl 1101 .DOM_NODE_INSERTED_INTO_DOCUMENT, 1102 false,false,null,null, 1103 null,null,(short)0); 1104 dispatchEventToSubtree(node, newInternal, me); 1105 } 1106 } 1107 } 1108 if (!replace) { 1109 dispatchAggregateEvents(node, savedEnclosingAttr); 1112 } 1113 } 1114 } 1115 1116 1119 void removingNode(NodeImpl node, NodeImpl oldChild, boolean replace) { 1120 1121 if (iterators != null) { 1123 Enumeration enumer = iterators.elements(); 1124 while (enumer.hasMoreElements()) { 1125 ((NodeIteratorImpl)enumer.nextElement()).removeNode(oldChild); 1126 } 1127 } 1128 1129 if (ranges != null) { 1131 Enumeration enumer = ranges.elements(); 1132 while (enumer.hasMoreElements()) { 1133 ((RangeImpl)enumer.nextElement()).removeNode(oldChild); 1134 } 1135 } 1136 1137 if (mutationEvents) { 1139 if (!replace) { 1144 saveEnclosingAttr(node); 1145 } 1146 LCount lc = LCount.lookup(MutationEventImpl.DOM_NODE_REMOVED); 1148 if (lc.captures + lc.bubbles + lc.defaults > 0) { 1149 MutationEventImpl me= new MutationEventImpl(); 1150 me.initMutationEvent(MutationEventImpl.DOM_NODE_REMOVED, 1151 true, false, node, null, 1152 null, null, (short) 0); 1153 dispatchEvent(oldChild, me); 1154 } 1155 1156 lc = LCount.lookup( 1159 MutationEventImpl.DOM_NODE_REMOVED_FROM_DOCUMENT); 1160 if (lc.captures + lc.bubbles + lc.defaults > 0) { 1161 NodeImpl eventAncestor = this; 1162 if(savedEnclosingAttr != null) 1163 eventAncestor = (NodeImpl) 1164 savedEnclosingAttr.node.getOwnerElement(); 1165 if (eventAncestor != null) { for (NodeImpl p = eventAncestor.parentNode(); 1167 p != null; p = p.parentNode()) { 1168 eventAncestor = p; } 1170 if (eventAncestor.getNodeType() == Node.DOCUMENT_NODE){ 1171 MutationEventImpl me = new MutationEventImpl(); 1172 me.initMutationEvent( 1173 MutationEventImpl.DOM_NODE_REMOVED_FROM_DOCUMENT, 1174 false, false, null, 1175 null, null, null, (short) 0); 1176 dispatchEventToSubtree(node, oldChild, me); 1177 } 1178 } 1179 } 1180 } } 1182 1183 1186 void removedNode(NodeImpl node, boolean replace) { 1187 if (mutationEvents) { 1188 if (!replace) { 1192 dispatchAggregateEvents(node, savedEnclosingAttr); 1193 } 1194 } } 1196 1197 1200 void replacingNode(NodeImpl node) { 1201 if (mutationEvents) { 1202 saveEnclosingAttr(node); 1203 } 1204 } 1205 1206 1209 void replacedNode(NodeImpl node) { 1210 if (mutationEvents) { 1211 dispatchAggregateEvents(node, savedEnclosingAttr); 1212 } 1213 } 1214 1215 1218 void modifiedAttrValue(AttrImpl attr, String oldvalue) { 1219 if (mutationEvents) { 1220 dispatchAggregateEvents(attr, attr, oldvalue, 1222 MutationEvent.MODIFICATION); 1223 } 1224 } 1225 1226 1229 void setAttrNode(AttrImpl attr, AttrImpl previous) { 1230 if (mutationEvents) { 1231 if (previous == null) { 1233 dispatchAggregateEvents(attr.ownerNode, attr, null, 1234 MutationEvent.ADDITION); 1235 } 1236 else { 1237 dispatchAggregateEvents(attr.ownerNode, attr, 1238 previous.getNodeValue(), 1239 MutationEvent.MODIFICATION); 1240 } 1241 } 1242 } 1243 1244 1247 void removedAttrNode(AttrImpl attr, NodeImpl oldOwner, String name) { 1248 if (mutationEvents) { 1252 LCount lc = LCount.lookup(MutationEventImpl.DOM_ATTR_MODIFIED); 1255 if (lc.captures + lc.bubbles + lc.defaults > 0) { 1256 MutationEventImpl me= new MutationEventImpl(); 1257 me.initMutationEvent(MutationEventImpl.DOM_ATTR_MODIFIED, 1258 true, false, null, 1259 attr.getNodeValue(), null, name, 1260 MutationEvent.REMOVAL); 1261 dispatchEvent(oldOwner, me); 1262 } 1263 1264 dispatchAggregateEvents(oldOwner, null, null, (short) 0); 1268 } 1269 } 1270 1271} | Popular Tags |