1 57 58 package org.xquark.xpath.datamodel.xerces.dom; 59 60 import java.io.IOException ; 61 import java.io.ObjectOutputStream ; 62 import java.io.Serializable ; 63 import java.util.Vector ; 64 65 import org.w3c.dom.*; 67 import org.w3c.dom.events.*; 68 import org.xml.sax.SAXException ; 69 import org.xquark.xpath.datamodel.xerces.dom.events.EventImpl; 70 import org.xquark.xpath.datamodel.xerces.dom.events.MutationEventImpl; 71 72 111 public abstract class NodeImpl implements Node, NodeList, EventTarget, Cloneable , Serializable { 112 113 117 118 static final long serialVersionUID = -6316591992167219696L; 119 120 122 123 public static final short ELEMENT_DEFINITION_NODE = -1; 124 125 129 131 protected NodeImpl ownerNode; 133 135 protected short flags; 136 137 protected final static short READONLY = 0x1 << 0; 138 protected final static short SYNCDATA = 0x1 << 1; 139 protected final static short SYNCCHILDREN = 0x1 << 2; 140 protected final static short OWNED = 0x1 << 3; 141 protected final static short FIRSTCHILD = 0x1 << 4; 142 protected final static short SPECIFIED = 0x1 << 5; 143 protected final static short IGNORABLEWS = 0x1 << 6; 144 protected final static short SETVALUE = 0x1 << 7; 145 protected final static short HASSTRING = 0x1 << 8; 146 protected final static short UNNORMALIZED = 0x1 << 9; 147 148 152 158 protected NodeImpl(DocumentImpl ownerDocument) { 159 ownerNode = ownerDocument; 161 } 163 164 public NodeImpl() { 165 } 166 167 171 175 public abstract short getNodeType(); 176 177 180 public abstract String getNodeName(); 181 182 185 public String getNodeValue() { 186 return null; } 188 189 193 public void setNodeValue(String x) throws DOMException { 194 } 196 197 217 public Node appendChild(Node newChild) throws DOMException { 218 return insertBefore(newChild, null); 219 } 220 221 244 public Node cloneNode(boolean deep) { 245 246 if (needsSyncData()) { 247 synchronizeData(); 248 } 249 250 NodeImpl newnode; 251 try { 252 newnode = (NodeImpl) clone(); 253 } catch (CloneNotSupportedException e) { 254 return null; 257 } 258 259 newnode.ownerNode = ownerDocument(); 261 newnode.isOwned(false); 262 263 newnode.isReadOnly(false); 265 266 return newnode; 267 268 } 270 275 public Document getOwnerDocument() { 276 if (isOwned()) { 279 return ownerNode.ownerDocument(); 280 } else { 281 return (Document) ownerNode; 282 } 283 } 284 285 289 DocumentImpl ownerDocument() { 290 if (isOwned()) { 293 return ownerNode.ownerDocument(); 294 } else { 295 return (DocumentImpl) ownerNode; 296 } 297 } 298 299 303 void setOwnerDocument(DocumentImpl doc) { 304 if (needsSyncData()) { 305 synchronizeData(); 306 } 307 if (!isOwned()) { 310 ownerNode = doc; 311 } 312 } 313 314 320 public Node getParentNode() { 321 return null; } 323 324 327 NodeImpl parentNode() { 328 return null; 329 } 330 331 332 public Node getNextSibling() { 333 return null; } 335 336 337 public Node getPreviousSibling() { 338 return null; } 340 341 ChildNode previousSibling() { 342 return null; } 344 345 352 public NamedNodeMap getAttributes() { 353 return null; } 355 356 363 public boolean hasAttributes() { 364 return false; } 366 367 374 public boolean hasChildNodes() { 375 return false; 376 } 377 378 391 public NodeList getChildNodes() { 392 return this; 393 } 394 395 400 public Node getFirstChild() { 401 return null; 402 } 403 404 409 public Node getLastChild() { 410 return null; 411 } 412 413 444 public Node insertBefore(Node newChild, Node refChild) throws DOMException { 445 throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "DOM006 Hierarchy request error"); 446 } 447 448 463 public Node removeChild(Node oldChild) throws DOMException { 464 throw new DOMException(DOMException.NOT_FOUND_ERR, "DOM008 Not found"); 465 } 466 467 491 public Node replaceChild(Node newChild, Node oldChild) throws DOMException { 492 throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "DOM006 Hierarchy request error"); 493 } 494 495 499 507 public int getLength() { 508 return 0; 509 } 510 511 521 public Node item(int index) { 522 return null; 523 } 524 525 529 547 public void normalize() { 548 550 } 551 552 567 public boolean isSupported(String feature, String version) { 568 return ownerDocument().getImplementation().hasFeature(feature, version); 569 } 570 571 588 public String getNamespaceURI() { 589 return null; 590 } 591 592 606 public String getPrefix() { 607 return null; 608 } 609 610 631 public void setPrefix(String prefix) throws DOMException { 632 throw new DOMException(DOMException.NAMESPACE_ERR, "DOM003 Namespace error"); 633 } 634 635 647 public String getLocalName() { 648 return null; 649 } 650 651 660 protected final static boolean MUTATIONEVENTS = true; 661 662 666 protected final static int MUTATION_NONE = 0x00; 667 protected final static int MUTATION_LOCAL = 0x01; 668 protected final static int MUTATION_AGGREGATE = 0x02; 669 protected final static int MUTATION_ALL = 0xffff; 670 671 680 class LEntry { 681 String type; 682 EventListener listener; 683 boolean useCapture; 684 685 691 LEntry(String type, EventListener listener, boolean useCapture) { 692 this.type = type; 693 this.listener = listener; 694 this.useCapture = useCapture; 695 } 696 }; 698 707 public void addEventListener(String type, EventListener listener, boolean useCapture) { 708 if (type == null || type.equals("") || listener == null) 711 return; 712 713 removeEventListener(type, listener, useCapture); 716 717 Vector nodeListeners = ownerDocument().getEventListeners(this); 718 if (nodeListeners == null) { 719 nodeListeners = new Vector (); 720 ownerDocument().setEventListeners(this, nodeListeners); 721 } 722 nodeListeners.addElement(new LEntry(type, listener, useCapture)); 723 724 LCount lc = LCount.lookup(type); 726 if (useCapture) 727 ++lc.captures; 728 else 729 ++lc.bubbles; 730 731 } 733 743 public void removeEventListener(String type, EventListener listener, boolean useCapture) { 744 Vector nodeListeners = ownerDocument().getEventListeners(this); 746 if (nodeListeners == null || type == null || type.equals("") || listener == null) 747 return; 748 749 for (int i = nodeListeners.size() - 1; i >= 0; --i) { 753 LEntry le = (LEntry) (nodeListeners.elementAt(i)); 754 if (le.useCapture == useCapture && le.listener == listener && le.type.equals(type)) { 755 nodeListeners.removeElementAt(i); 756 if (nodeListeners.size() == 0) 758 ownerDocument().setEventListeners(this, null); 759 760 LCount lc = LCount.lookup(type); 762 if (useCapture) 763 --lc.captures; 764 else 765 --lc.bubbles; 766 767 break; } 769 } 770 } 772 803 804 853 public boolean dispatchEvent(Event event) { 854 if (event == null) 855 return false; 856 857 EventImpl evt = (EventImpl) event; 860 861 if (!evt.initialized || evt.type == null || evt.type.equals("")) 864 throw new EventException(EventException.UNSPECIFIED_EVENT_TYPE_ERR, "DOM010 Unspecified event type"); 865 866 LCount lc = LCount.lookup(evt.getType()); 868 if (lc.captures + lc.bubbles + lc.defaults == 0) 869 return evt.preventDefault; 870 871 evt.target = this; 876 evt.stopPropagation = false; 877 evt.preventDefault = false; 878 879 Vector pv = new Vector (10, 10); 889 Node p = this, n = p.getParentNode(); 890 while (n != null) { 891 pv.addElement(n); 892 p = n; 893 n = n.getParentNode(); 894 } 895 896 if (lc.captures > 0) { 898 evt.eventPhase = Event.CAPTURING_PHASE; 899 for (int j = pv.size() - 1; j >= 0; --j) { 902 if (evt.stopPropagation) 903 break; 905 NodeImpl nn = (NodeImpl) pv.elementAt(j); 907 evt.currentTarget = nn; 908 Vector nodeListeners = ownerDocument().getEventListeners(nn); 909 if (nodeListeners != null) { 910 Vector nl = (Vector ) (nodeListeners.clone()); 911 for (int i = nl.size() - 1; i >= 0; --i) { 913 LEntry le = (LEntry) (nl.elementAt(i)); 914 if (le.useCapture && le.type.equals(evt.type)) 915 le.listener.handleEvent(evt); 916 } 917 } 918 } 919 } 920 921 if (lc.bubbles > 0) { 923 evt.eventPhase = Event.AT_TARGET; 927 evt.currentTarget = this; 928 Vector nodeListeners = ownerDocument().getEventListeners(this); 929 if (!evt.stopPropagation && nodeListeners != null) { 930 Vector nl = (Vector ) nodeListeners.clone(); 931 for (int i = nl.size() - 1; i >= 0; --i) { 933 LEntry le = (LEntry) nl.elementAt(i); 934 if (le != null && !le.useCapture && le.type.equals(evt.type)) 935 le.listener.handleEvent(evt); 936 } 937 } 938 if (evt.bubbles) { 944 evt.eventPhase = Event.BUBBLING_PHASE; 945 for (int j = 0; j < pv.size(); ++j) { 946 if (evt.stopPropagation) 947 break; 949 NodeImpl nn = (NodeImpl) pv.elementAt(j); 951 evt.currentTarget = nn; 952 nodeListeners = ownerDocument().getEventListeners(nn); 953 if (nodeListeners != null) { 954 Vector nl = (Vector ) (nodeListeners.clone()); 955 for (int i = nl.size() - 1; i >= 0; --i) { 957 LEntry le = (LEntry) (nl.elementAt(i)); 958 if (!le.useCapture && le.type.equals(evt.type)) 959 le.listener.handleEvent(evt); 960 } 961 } 962 } 963 } 964 } 965 966 if (lc.defaults > 0 && (!evt.cancelable || !evt.preventDefault)) { 972 } 976 977 return evt.preventDefault; 978 } 980 991 void dispatchEventToSubtree(Node n, Event e) { 992 if (MUTATIONEVENTS && ownerDocument().mutationEvents) { 993 Vector nodeListeners = ownerDocument().getEventListeners(this); 994 if (nodeListeners == null || n == null) 995 return; 996 997 ((NodeImpl) n).dispatchEvent(e); 1001 if (n.getNodeType() == Node.ELEMENT_NODE) { 1002 NamedNodeMap a = n.getAttributes(); 1003 for (int i = a.getLength() - 1; i >= 0; --i) 1004 dispatchEventToSubtree(a.item(i), e); 1005 } 1006 dispatchEventToSubtree(n.getFirstChild(), e); 1007 dispatchEventToSubtree(n.getNextSibling(), e); 1008 } 1009 } 1011 1015 class EnclosingAttr { 1016 AttrImpl node; 1017 String oldvalue; 1018 } 1020 1026 EnclosingAttr getEnclosingAttr() { 1027 if (MUTATIONEVENTS && ownerDocument().mutationEvents) { 1028 NodeImpl eventAncestor = this; 1029 while (true) { 1030 if (eventAncestor == null) 1031 return null; 1032 int type = eventAncestor.getNodeType(); 1033 if (type == Node.ATTRIBUTE_NODE) { 1034 EnclosingAttr retval = new EnclosingAttr(); 1035 retval.node = (AttrImpl) eventAncestor; 1036 retval.oldvalue = retval.node.getNodeValue(); 1037 return retval; 1038 } else if (type == Node.ENTITY_REFERENCE_NODE) 1039 eventAncestor = eventAncestor.parentNode(); 1040 else 1041 return null; 1042 } 1044 } 1045 return null; } 1048 1053 void dispatchAggregateEvents(EnclosingAttr ea) { 1054 if (ea != null) 1055 dispatchAggregateEvents(ea.node, ea.oldvalue, MutationEvent.MODIFICATION); 1056 else 1057 dispatchAggregateEvents(null, null, (short) 0); 1058 1059 } 1061 1083 void dispatchAggregateEvents(AttrImpl enclosingAttr, String oldvalue, short change) { 1084 if (MUTATIONEVENTS && ownerDocument().mutationEvents) { 1085 NodeImpl owner = null; 1087 if (enclosingAttr != null) { 1088 LCount lc = LCount.lookup(MutationEventImpl.DOM_ATTR_MODIFIED); 1089 owner = ((NodeImpl) (enclosingAttr.getOwnerElement())); 1090 if (lc.captures + lc.bubbles + lc.defaults > 0) { 1091 if (owner != null) { 1092 MutationEventImpl me = new MutationEventImpl(); 1093 me.initMutationEvent(MutationEventImpl.DOM_ATTR_MODIFIED, true, false, enclosingAttr, oldvalue, enclosingAttr.getNodeValue(), enclosingAttr.getNodeName(), change); 1094 owner.dispatchEvent(me); 1095 } 1096 } 1097 } 1098 1099 LCount lc = LCount.lookup(MutationEventImpl.DOM_SUBTREE_MODIFIED); 1104 if (lc.captures + lc.bubbles + lc.defaults > 0) { 1105 MutationEvent me = new MutationEventImpl(); 1106 me.initMutationEvent(MutationEventImpl.DOM_SUBTREE_MODIFIED, true, false, null, null, null, null, (short) 0); 1107 1108 if (enclosingAttr != null) { 1112 enclosingAttr.dispatchEvent(me); 1113 if (owner != null) 1114 owner.dispatchEvent(me); 1115 } else 1116 dispatchEvent(me); 1117 } 1118 } 1119 } 1121 1125 1143 public void setReadOnly(boolean readOnly, boolean deep) { 1144 1145 if (needsSyncData()) { 1146 synchronizeData(); 1147 } 1148 isReadOnly(readOnly); 1149 1150 } 1152 1156 public boolean getReadOnly() { 1157 1158 if (needsSyncData()) { 1159 synchronizeData(); 1160 } 1161 return isReadOnly(); 1162 1163 } 1165 1177 public void setUserData(Object data) { 1178 ownerDocument().setUserData(this, data); 1179 } 1180 1181 1185 public Object getUserData() { 1186 return ownerDocument().getUserData(this); 1187 } 1188 1189 1193 1196 protected void changed() { 1197 ownerDocument().changed(); 1201 } 1202 1203 1206 protected int changes() { 1207 return ownerDocument().changes(); 1211 } 1212 1213 1217 protected void synchronizeData() { 1218 needsSyncData(false); 1220 } 1221 1222 1225 1226 final boolean isReadOnly() { 1227 return (flags & READONLY) != 0; 1228 } 1229 1230 final void isReadOnly(boolean value) { 1231 flags = (short) (value ? flags | READONLY : flags & ~READONLY); 1232 } 1233 1234 final boolean needsSyncData() { 1235 return (flags & SYNCDATA) != 0; 1236 } 1237 1238 final void needsSyncData(boolean value) { 1239 flags = (short) (value ? flags | SYNCDATA : flags & ~SYNCDATA); 1240 } 1241 1242 final boolean needsSyncChildren() { 1243 return (flags & SYNCCHILDREN) != 0; 1244 } 1245 1246 final void needsSyncChildren(boolean value) { 1247 flags = (short) (value ? flags | SYNCCHILDREN : flags & ~SYNCCHILDREN); 1248 } 1249 1250 final boolean isOwned() { 1251 return (flags & OWNED) != 0; 1252 } 1253 1254 final void isOwned(boolean value) { 1255 flags = (short) (value ? flags | OWNED : flags & ~OWNED); 1256 } 1257 1258 final boolean isFirstChild() { 1259 return (flags & FIRSTCHILD) != 0; 1260 } 1261 1262 final void isFirstChild(boolean value) { 1263 flags = (short) (value ? flags | FIRSTCHILD : flags & ~FIRSTCHILD); 1264 } 1265 1266 final boolean isSpecified() { 1267 return (flags & SPECIFIED) != 0; 1268 } 1269 1270 final void isSpecified(boolean value) { 1271 flags = (short) (value ? flags | SPECIFIED : flags & ~SPECIFIED); 1272 } 1273 1274 final boolean internalIsIgnorableWhitespace() { 1276 return (flags & IGNORABLEWS) != 0; 1277 } 1278 1279 final void isIgnorableWhitespace(boolean value) { 1280 flags = (short) (value ? flags | IGNORABLEWS : flags & ~IGNORABLEWS); 1281 } 1282 1283 final boolean setValueCalled() { 1284 return (flags & SETVALUE) != 0; 1285 } 1286 1287 final void setValueCalled(boolean value) { 1288 flags = (short) (value ? flags | SETVALUE : flags & ~SETVALUE); 1289 } 1290 1291 final boolean hasStringValue() { 1292 return (flags & HASSTRING) != 0; 1293 } 1294 1295 final void hasStringValue(boolean value) { 1296 flags = (short) (value ? flags | HASSTRING : flags & ~HASSTRING); 1297 } 1298 1299 final boolean isNormalized() { 1300 return (flags & UNNORMALIZED) == 0; 1301 } 1302 1303 final void isNormalized(boolean value) { 1304 if (!value && isNormalized() && ownerNode != null) { 1306 ownerNode.isNormalized(false); 1307 } 1308 flags = (short) (value ? flags & ~UNNORMALIZED : flags | UNNORMALIZED); 1309 } 1310 1311 1315 1316 public String toString() { 1317 return "[" + getNodeName() + ": " + getNodeValue() + "]"; 1318 } 1319 1320 1324 1325 private void writeObject(ObjectOutputStream out) throws IOException { 1326 1327 if (needsSyncData()) { 1329 synchronizeData(); 1330 } 1331 out.defaultWriteObject(); 1333 1334 } 1336} | Popular Tags |