1 16 17 package org.apache.xerces.dom; 18 19 import java.io.IOException ; 20 import java.io.ObjectOutputStream ; 21 import java.io.Serializable ; 22 import java.util.Hashtable ; 23 24 import org.w3c.dom.UserDataHandler ; 25 import org.w3c.dom.DOMException ; 26 import org.w3c.dom.Document ; 27 import org.w3c.dom.DocumentType ; 28 import org.w3c.dom.NamedNodeMap ; 29 import org.w3c.dom.Node ; 30 import org.w3c.dom.NodeList ; 31 import org.w3c.dom.events.Event ; 32 import org.w3c.dom.events.EventListener ; 33 import org.w3c.dom.events.EventTarget ; 34 35 80 public abstract class NodeImpl 81 implements Node , NodeList , EventTarget , Cloneable , Serializable { 82 83 87 88 93 public static final short TREE_POSITION_PRECEDING = 0x01; 94 97 public static final short TREE_POSITION_FOLLOWING = 0x02; 98 101 public static final short TREE_POSITION_ANCESTOR = 0x04; 102 105 public static final short TREE_POSITION_DESCENDANT = 0x08; 106 111 public static final short TREE_POSITION_EQUIVALENT = 0x10; 112 116 public static final short TREE_POSITION_SAME_NODE = 0x20; 117 121 public static final short TREE_POSITION_DISCONNECTED = 0x00; 122 123 124 public static final short DOCUMENT_POSITION_DISCONNECTED = 0x01; 126 public static final short DOCUMENT_POSITION_PRECEDING = 0x02; 127 public static final short DOCUMENT_POSITION_FOLLOWING = 0x04; 128 public static final short DOCUMENT_POSITION_CONTAINS = 0x08; 129 public static final short DOCUMENT_POSITION_IS_CONTAINED = 0x10; 130 public static final short DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC = 0x20; 131 132 133 static final long serialVersionUID = -6316591992167219696L; 134 135 137 138 public static final short ELEMENT_DEFINITION_NODE = 21; 139 140 144 146 protected NodeImpl ownerNode; 148 150 protected short flags; 151 152 protected final static short READONLY = 0x1<<0; 153 protected final static short SYNCDATA = 0x1<<1; 154 protected final static short SYNCCHILDREN = 0x1<<2; 155 protected final static short OWNED = 0x1<<3; 156 protected final static short FIRSTCHILD = 0x1<<4; 157 protected final static short SPECIFIED = 0x1<<5; 158 protected final static short IGNORABLEWS = 0x1<<6; 159 protected final static short HASSTRING = 0x1<<7; 160 protected final static short NORMALIZED = 0x1<<8; 161 protected final static short ID = 0x1<<9; 162 163 167 173 protected NodeImpl(CoreDocumentImpl ownerDocument) { 174 ownerNode = ownerDocument; 176 } 178 179 public NodeImpl() {} 180 181 185 189 public abstract short getNodeType(); 190 191 194 public abstract String getNodeName(); 195 196 200 public String getNodeValue() 201 throws DOMException { 202 return null; } 204 205 209 public void setNodeValue(String x) 210 throws DOMException { 211 } 213 214 234 public Node appendChild(Node newChild) throws DOMException { 235 return insertBefore(newChild, null); 236 } 237 238 261 public Node cloneNode(boolean deep) { 262 263 if (needsSyncData()) { 264 synchronizeData(); 265 } 266 267 NodeImpl newnode; 268 try { 269 newnode = (NodeImpl)clone(); 270 } 271 catch (CloneNotSupportedException e) { 272 throw new RuntimeException ("**Internal Error**" + e); 275 } 276 277 newnode.ownerNode = ownerDocument(); 279 newnode.isOwned(false); 280 281 newnode.isReadOnly(false); 284 285 ownerDocument().callUserDataHandlers(this, newnode, 286 UserDataHandler.NODE_CLONED); 287 288 return newnode; 289 290 } 292 297 public Document getOwnerDocument() { 298 if (isOwned()) { 301 return ownerNode.ownerDocument(); 302 } else { 303 return (Document ) ownerNode; 304 } 305 } 306 307 311 CoreDocumentImpl ownerDocument() { 312 if (isOwned()) { 315 return ownerNode.ownerDocument(); 316 } else { 317 return (CoreDocumentImpl) ownerNode; 318 } 319 } 320 321 325 void setOwnerDocument(CoreDocumentImpl doc) { 326 if (needsSyncData()) { 327 synchronizeData(); 328 } 329 if (!isOwned()) { 332 ownerNode = doc; 333 } 334 } 335 336 339 protected int getNodeNumber() { 340 int nodeNumber; 341 CoreDocumentImpl cd = (CoreDocumentImpl)(this.getOwnerDocument()); 342 nodeNumber = cd.getNodeNumber(this); 343 return nodeNumber; 344 } 345 346 352 public Node getParentNode() { 353 return null; } 355 356 359 NodeImpl parentNode() { 360 return null; 361 } 362 363 364 public Node getNextSibling() { 365 return null; } 367 368 369 public Node getPreviousSibling() { 370 return null; } 372 373 ChildNode previousSibling() { 374 return null; } 376 377 384 public NamedNodeMap getAttributes() { 385 return null; } 387 388 395 public boolean hasAttributes() { 396 return false; } 398 399 406 public boolean hasChildNodes() { 407 return false; 408 } 409 410 423 public NodeList getChildNodes() { 424 return this; 425 } 426 427 432 public Node getFirstChild() { 433 return null; 434 } 435 436 441 public Node getLastChild() { 442 return null; 443 } 444 445 476 public Node insertBefore(Node newChild, Node refChild) 477 throws DOMException { 478 throw new DOMException (DOMException.HIERARCHY_REQUEST_ERR, 479 DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, 480 "HIERARCHY_REQUEST_ERR", null)); 481 } 482 483 498 public Node removeChild(Node oldChild) 499 throws DOMException { 500 throw new DOMException (DOMException.NOT_FOUND_ERR, 501 DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, 502 "NOT_FOUND_ERR", null)); 503 } 504 505 529 public Node replaceChild(Node newChild, Node oldChild) 530 throws DOMException { 531 throw new DOMException (DOMException.HIERARCHY_REQUEST_ERR, 532 DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, 533 "HIERARCHY_REQUEST_ERR", null)); 534 } 535 536 540 548 public int getLength() { 549 return 0; 550 } 551 552 562 public Node item(int index) { 563 return null; 564 } 565 566 570 588 public void normalize() { 589 591 } 592 593 608 public boolean isSupported(String feature, String version) 609 { 610 return ownerDocument().getImplementation().hasFeature(feature, 611 version); 612 } 613 614 631 public String getNamespaceURI() 632 { 633 return null; 634 } 635 636 650 public String getPrefix() 651 { 652 return null; 653 } 654 655 676 public void setPrefix(String prefix) 677 throws DOMException 678 { 679 throw new DOMException (DOMException.NAMESPACE_ERR, 680 DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, 681 "NAMESPACE_ERR", null)); 682 } 683 684 696 public String getLocalName() 697 { 698 return null; 699 } 700 701 705 public void addEventListener(String type, EventListener listener, 706 boolean useCapture) { 707 ownerDocument().addEventListener(this, type, listener, useCapture); 709 } 710 711 public void removeEventListener(String type, EventListener listener, 712 boolean useCapture) { 713 ownerDocument().removeEventListener(this, type, listener, useCapture); 715 } 716 717 public boolean dispatchEvent(Event event) { 718 return ownerDocument().dispatchEvent(this, event); 720 } 721 722 726 750 public String getBaseURI() { 751 return null; 752 } 753 754 764 public short compareTreePosition(Node other) { 765 800 801 if (this==other) 803 return (TREE_POSITION_SAME_NODE | TREE_POSITION_EQUIVALENT); 804 805 short thisType = this.getNodeType(); 807 short otherType = other.getNodeType(); 808 809 if (thisType == Node.ENTITY_NODE || 811 thisType == Node.NOTATION_NODE || 812 otherType == Node.ENTITY_NODE || 813 otherType == Node.NOTATION_NODE ) { 814 return TREE_POSITION_DISCONNECTED; 815 } 816 817 824 Node node; 825 Node thisAncestor = this; 826 Node otherAncestor = other; 827 int thisDepth=0; 828 int otherDepth=0; 829 for (node=this; node != null; node = node.getParentNode()) { 830 thisDepth +=1; 831 if (node == other) 832 return (TREE_POSITION_ANCESTOR | TREE_POSITION_PRECEDING); 834 thisAncestor = node; 835 } 836 837 for (node=other; node!=null; node=node.getParentNode()) { 838 otherDepth +=1; 839 if (node == this) 840 return (TREE_POSITION_DESCENDANT | TREE_POSITION_FOLLOWING); 842 otherAncestor = node; 843 } 844 845 846 Node thisNode = this; 847 Node otherNode = other; 848 849 int thisAncestorType = thisAncestor.getNodeType(); 850 int otherAncestorType = otherAncestor.getNodeType(); 851 852 855 if (thisAncestorType == Node.ATTRIBUTE_NODE) { 856 thisNode = ((AttrImpl)thisAncestor).getOwnerElement(); 857 } 858 if (otherAncestorType == Node.ATTRIBUTE_NODE) { 859 otherNode = ((AttrImpl)otherAncestor).getOwnerElement(); 860 } 861 862 if (thisAncestorType == Node.ATTRIBUTE_NODE && 865 otherAncestorType == Node.ATTRIBUTE_NODE && 866 thisNode==otherNode) 867 return TREE_POSITION_EQUIVALENT; 868 869 872 if (thisAncestorType == Node.ATTRIBUTE_NODE) { 875 thisDepth=0; 876 for (node=thisNode; node != null; node=node.getParentNode()) { 877 thisDepth +=1; 878 if (node == otherNode) 879 { 881 return TREE_POSITION_PRECEDING; 882 } 883 thisAncestor = node; 884 } 885 } 886 887 if (otherAncestorType == Node.ATTRIBUTE_NODE) { 890 otherDepth=0; 891 for (node=otherNode; node != null; node=node.getParentNode()) { 892 otherDepth +=1; 893 if (node == thisNode) 894 return TREE_POSITION_FOLLOWING; 897 otherAncestor = node; 898 } 899 } 900 901 if (thisAncestor != otherAncestor) 904 return TREE_POSITION_DISCONNECTED; 905 906 907 910 if (thisDepth > otherDepth) { 911 for (int i=0; i<thisDepth - otherDepth; i++) 912 thisNode = thisNode.getParentNode(); 913 if (thisNode == otherNode) 917 return TREE_POSITION_PRECEDING; 918 } 919 920 else { 921 for (int i=0; i<otherDepth - thisDepth; i++) 922 otherNode = otherNode.getParentNode(); 923 if (otherNode == thisNode) 927 return TREE_POSITION_FOLLOWING; 928 } 929 930 Node thisNodeP, otherNodeP; 933 for (thisNodeP=thisNode.getParentNode(), 934 otherNodeP=otherNode.getParentNode(); 935 thisNodeP!=otherNodeP;) { 936 thisNode = thisNodeP; 937 otherNode = otherNodeP; 938 thisNodeP = thisNodeP.getParentNode(); 939 otherNodeP = otherNodeP.getParentNode(); 940 } 941 942 946 for (Node current=thisNodeP.getFirstChild(); 947 current!=null; 948 current=current.getNextSibling()) { 949 if (current==otherNode) { 950 return TREE_POSITION_PRECEDING; 951 } 952 else if (current==thisNode) { 953 return TREE_POSITION_FOLLOWING; 954 } 955 } 956 return 0; 959 960 } 961 969 public short compareDocumentPosition(Node other) throws DOMException { 970 971 if (this==other) 973 return 0; 974 975 try { 977 NodeImpl node = (NodeImpl) other; 978 } catch (ClassCastException e) { 979 String msg = DOMMessageFormatter.formatMessage( 981 DOMMessageFormatter.DOM_DOMAIN, "NOT_SUPPORTED_ERR", null); 982 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, msg); 983 } 984 985 Document thisOwnerDoc, otherOwnerDoc; 986 if (this.getNodeType() == Node.DOCUMENT_NODE) 988 thisOwnerDoc = (Document )this; 989 else 990 thisOwnerDoc = this.getOwnerDocument(); 991 if (other.getNodeType() == Node.DOCUMENT_NODE) 992 otherOwnerDoc = (Document )other; 993 else 994 otherOwnerDoc = other.getOwnerDocument(); 995 996 if (thisOwnerDoc != otherOwnerDoc && 999 thisOwnerDoc !=null && 1000 otherOwnerDoc !=null) 1001 { 1002 int otherDocNum = ((CoreDocumentImpl)otherOwnerDoc).getNodeNumber(); 1003 int thisDocNum = ((CoreDocumentImpl)thisOwnerDoc).getNodeNumber(); 1004 if (otherDocNum > thisDocNum) 1005 return DOCUMENT_POSITION_DISCONNECTED | 1006 DOCUMENT_POSITION_FOLLOWING | 1007 DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC; 1008 else 1009 return DOCUMENT_POSITION_DISCONNECTED | 1010 DOCUMENT_POSITION_PRECEDING | 1011 DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC; 1012 1013 } 1014 1015 1022 Node node; 1023 Node thisAncestor = this; 1024 Node otherAncestor = other; 1025 1026 int thisDepth=0; 1027 int otherDepth=0; 1028 for (node=this; node != null; node = node.getParentNode()) { 1029 thisDepth +=1; 1030 if (node == other) 1031 return (DOCUMENT_POSITION_CONTAINS | 1033 DOCUMENT_POSITION_PRECEDING); 1034 thisAncestor = node; 1035 } 1036 1037 for (node=other; node!=null; node=node.getParentNode()) { 1038 otherDepth +=1; 1039 if (node == this) 1040 return (DOCUMENT_POSITION_IS_CONTAINED | 1042 DOCUMENT_POSITION_FOLLOWING); 1043 otherAncestor = node; 1044 } 1045 1046 1047 1048 int thisAncestorType = thisAncestor.getNodeType(); 1049 int otherAncestorType = otherAncestor.getNodeType(); 1050 Node thisNode = this; 1051 Node otherNode = other; 1052 1053 switch (thisAncestorType) { 1056 case Node.NOTATION_NODE: 1057 case Node.ENTITY_NODE: { 1058 DocumentType container = thisOwnerDoc.getDoctype(); 1059 if (container == otherAncestor) return 1060 (DOCUMENT_POSITION_CONTAINS | DOCUMENT_POSITION_PRECEDING); 1061 switch (otherAncestorType) { 1062 case Node.NOTATION_NODE: 1063 case Node.ENTITY_NODE: { 1064 if (thisAncestorType != otherAncestorType) 1065 return ((thisAncestorType>otherAncestorType) ? 1067 DOCUMENT_POSITION_PRECEDING:DOCUMENT_POSITION_FOLLOWING); 1068 else { 1069 if (thisAncestorType == Node.NOTATION_NODE) 1071 1072 if (((NamedNodeMapImpl)container.getNotations()).precedes(otherAncestor,thisAncestor)) 1073 return (DOCUMENT_POSITION_PRECEDING | 1074 DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC); 1075 else 1076 return (DOCUMENT_POSITION_FOLLOWING | 1077 DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC); 1078 else 1079 if (((NamedNodeMapImpl)container.getEntities()).precedes(otherAncestor,thisAncestor)) 1080 return (DOCUMENT_POSITION_PRECEDING | 1081 DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC); 1082 else 1083 return (DOCUMENT_POSITION_FOLLOWING | 1084 DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC); 1085 } 1086 } 1087 } 1088 thisNode = thisAncestor = thisOwnerDoc; 1089 break; 1090 } 1091 case Node.DOCUMENT_TYPE_NODE: { 1092 if (otherNode == thisOwnerDoc) 1093 return (DOCUMENT_POSITION_PRECEDING | 1094 DOCUMENT_POSITION_CONTAINS); 1095 else if (thisOwnerDoc!=null && thisOwnerDoc==otherOwnerDoc) 1096 return (DOCUMENT_POSITION_FOLLOWING); 1097 break; 1098 } 1099 case Node.ATTRIBUTE_NODE: { 1100 thisNode = ((AttrImpl)thisAncestor).getOwnerElement(); 1101 if (otherAncestorType==Node.ATTRIBUTE_NODE) { 1102 otherNode = ((AttrImpl)otherAncestor).getOwnerElement(); 1103 if (otherNode == thisNode) { 1104 if (((NamedNodeMapImpl)thisNode.getAttributes()).precedes(other,this)) 1105 return (DOCUMENT_POSITION_PRECEDING | 1106 DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC); 1107 else 1108 return (DOCUMENT_POSITION_FOLLOWING | 1109 DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC); 1110 } 1111 } 1112 1113 thisDepth=0; 1115 for (node=thisNode; node != null; node=node.getParentNode()) { 1116 thisDepth +=1; 1117 if (node == otherNode) 1118 { 1119 return (DOCUMENT_POSITION_CONTAINS | 1121 DOCUMENT_POSITION_PRECEDING); 1122 } 1123 thisAncestor = node; 1124 } 1125 } 1126 } 1127 switch (otherAncestorType) { 1128 case Node.NOTATION_NODE: 1129 case Node.ENTITY_NODE: { 1130 DocumentType container = thisOwnerDoc.getDoctype(); 1131 if (container == this) return (DOCUMENT_POSITION_IS_CONTAINED | 1132 DOCUMENT_POSITION_FOLLOWING); 1133 otherNode = otherAncestor = thisOwnerDoc; 1134 break; 1135 } 1136 case Node.DOCUMENT_TYPE_NODE: { 1137 if (thisNode == otherOwnerDoc) 1138 return (DOCUMENT_POSITION_FOLLOWING | 1139 DOCUMENT_POSITION_IS_CONTAINED); 1140 else if (otherOwnerDoc!=null && thisOwnerDoc==otherOwnerDoc) 1141 return (DOCUMENT_POSITION_PRECEDING); 1142 break; 1143 } 1144 case Node.ATTRIBUTE_NODE: { 1145 otherDepth=0; 1146 otherNode = ((AttrImpl)otherAncestor).getOwnerElement(); 1147 for (node=otherNode; node != null; node=node.getParentNode()) { 1148 otherDepth +=1; 1149 if (node == thisNode) 1150 return DOCUMENT_POSITION_FOLLOWING | 1153 DOCUMENT_POSITION_IS_CONTAINED; 1154 otherAncestor = node; 1155 } 1156 1157 } 1158 } 1159 1160 if (thisAncestor != otherAncestor) { 1163 int thisAncestorNum, otherAncestorNum; 1164 thisAncestorNum = ((NodeImpl)thisAncestor).getNodeNumber(); 1165 otherAncestorNum = ((NodeImpl)otherAncestor).getNodeNumber(); 1166 1167 if (thisAncestorNum > otherAncestorNum) 1168 return DOCUMENT_POSITION_DISCONNECTED | 1169 DOCUMENT_POSITION_FOLLOWING | 1170 DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC; 1171 else 1172 return DOCUMENT_POSITION_DISCONNECTED | 1173 DOCUMENT_POSITION_PRECEDING | 1174 DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC; 1175 } 1176 1177 1178 1181 if (thisDepth > otherDepth) { 1182 for (int i=0; i<thisDepth - otherDepth; i++) 1183 thisNode = thisNode.getParentNode(); 1184 if (thisNode == otherNode) 1188{ 1189 return DOCUMENT_POSITION_PRECEDING; 1190 } 1191 } 1192 1193 else { 1194 for (int i=0; i<otherDepth - thisDepth; i++) 1195 otherNode = otherNode.getParentNode(); 1196 if (otherNode == thisNode) 1200 return DOCUMENT_POSITION_FOLLOWING; 1201 } 1202 1203 Node thisNodeP, otherNodeP; 1206 for (thisNodeP=thisNode.getParentNode(), 1207 otherNodeP=otherNode.getParentNode(); 1208 thisNodeP!=otherNodeP;) { 1209 thisNode = thisNodeP; 1210 otherNode = otherNodeP; 1211 thisNodeP = thisNodeP.getParentNode(); 1212 otherNodeP = otherNodeP.getParentNode(); 1213 } 1214 1215 1219 for (Node current=thisNodeP.getFirstChild(); 1220 current!=null; 1221 current=current.getNextSibling()) { 1222 if (current==otherNode) { 1223 return DOCUMENT_POSITION_PRECEDING; 1224 } 1225 else if (current==thisNode) { 1226 return DOCUMENT_POSITION_FOLLOWING; 1227 } 1228 } 1229 return 0; 1232 1233 } 1234 1235 1298 public String getTextContent() throws DOMException { 1299 return getNodeValue(); } 1301 1302 void getTextContent(StringBuffer buf) throws DOMException { 1304 String content = getNodeValue(); 1305 if (content != null) { 1306 buf.append(content); 1307 } 1308 } 1309 1310 1355 public void setTextContent(String textContent) 1356 throws DOMException { 1357 setNodeValue(textContent); 1358 } 1359 1360 1374 public boolean isSameNode(Node other) { 1375 return this == other; 1377 } 1378 1379 1380 1381 1382 1391 public boolean isDefaultNamespace(String namespaceURI){ 1392 short type = this.getNodeType(); 1394 switch (type) { 1395 case Node.ELEMENT_NODE: { 1396 String namespace = this.getNamespaceURI(); 1397 String prefix = this.getPrefix(); 1398 1399 if (prefix == null || prefix.length() == 0) { 1401 if (namespaceURI == null) { 1402 return (namespace == namespaceURI); 1403 } 1404 return namespaceURI.equals(namespace); 1405 } 1406 if (this.hasAttributes()) { 1407 ElementImpl elem = (ElementImpl)this; 1408 NodeImpl attr = (NodeImpl)elem.getAttributeNodeNS("http://www.w3.org/2000/xmlns/", "xmlns"); 1409 if (attr != null) { 1410 String value = attr.getNodeValue(); 1411 if (namespaceURI == null) { 1412 return (namespace == value); 1413 } 1414 return namespaceURI.equals(value); 1415 } 1416 } 1417 1418 NodeImpl ancestor = (NodeImpl)getElementAncestor(this); 1419 if (ancestor != null) { 1420 return ancestor.isDefaultNamespace(namespaceURI); 1421 } 1422 return false; 1423 } 1424 case Node.DOCUMENT_NODE:{ 1425 return((NodeImpl)((Document )this).getDocumentElement()).isDefaultNamespace(namespaceURI); 1426 } 1427 1428 case Node.ENTITY_NODE : 1429 case Node.NOTATION_NODE: 1430 case Node.DOCUMENT_FRAGMENT_NODE: 1431 case Node.DOCUMENT_TYPE_NODE: 1432 return false; 1434 case Node.ATTRIBUTE_NODE:{ 1435 if (this.ownerNode.getNodeType() == Node.ELEMENT_NODE) { 1436 return ownerNode.isDefaultNamespace(namespaceURI); 1437 1438 } 1439 return false; 1440 } 1441 default:{ 1442 NodeImpl ancestor = (NodeImpl)getElementAncestor(this); 1443 if (ancestor != null) { 1444 return ancestor.isDefaultNamespace(namespaceURI); 1445 } 1446 return false; 1447 } 1448 1449 } 1450 1451 1452 } 1453 1454 1455 1463 public String lookupPrefix(String namespaceURI){ 1464 1465 if (namespaceURI == null) { 1468 return null; 1469 } 1470 1471 short type = this.getNodeType(); 1472 1473 switch (type) { 1474 case Node.ELEMENT_NODE: { 1475 1476 String namespace = this.getNamespaceURI(); return lookupNamespacePrefix(namespaceURI, (ElementImpl)this); 1478 } 1479 case Node.DOCUMENT_NODE:{ 1480 return((NodeImpl)((Document )this).getDocumentElement()).lookupPrefix(namespaceURI); 1481 } 1482 1483 case Node.ENTITY_NODE : 1484 case Node.NOTATION_NODE: 1485 case Node.DOCUMENT_FRAGMENT_NODE: 1486 case Node.DOCUMENT_TYPE_NODE: 1487 return null; 1489 case Node.ATTRIBUTE_NODE:{ 1490 if (this.ownerNode.getNodeType() == Node.ELEMENT_NODE) { 1491 return ownerNode.lookupPrefix(namespaceURI); 1492 1493 } 1494 return null; 1495 } 1496 default:{ 1497 NodeImpl ancestor = (NodeImpl)getElementAncestor(this); 1498 if (ancestor != null) { 1499 return ancestor.lookupPrefix(namespaceURI); 1500 } 1501 return null; 1502 } 1503 1504 } 1505 } 1506 1515 public String lookupNamespaceURI(String specifiedPrefix) { 1516 short type = this.getNodeType(); 1517 switch (type) { 1518 case Node.ELEMENT_NODE : { 1519 1520 String namespace = this.getNamespaceURI(); 1521 String prefix = this.getPrefix(); 1522 if (namespace !=null) { 1523 if (specifiedPrefix== null && prefix==specifiedPrefix) { 1525 return namespace; 1527 } else if (prefix != null && prefix.equals(specifiedPrefix)) { 1528 return namespace; 1530 } 1531 } 1532 if (this.hasAttributes()) { 1533 NamedNodeMap map = this.getAttributes(); 1534 int length = map.getLength(); 1535 for (int i=0;i<length;i++) { 1536 Node attr = map.item(i); 1537 String attrPrefix = attr.getPrefix(); 1538 String value = attr.getNodeValue(); 1539 namespace = attr.getNamespaceURI(); 1540 if (namespace !=null && namespace.equals("http://www.w3.org/2000/xmlns/")) { 1541 if (specifiedPrefix == null && 1543 attr.getNodeName().equals("xmlns")) { 1544 return value; 1546 } else if (attrPrefix !=null && 1547 attrPrefix.equals("xmlns") && 1548 attr.getLocalName().equals(specifiedPrefix)) { 1549 return value; 1551 } 1552 } 1553 } 1554 } 1555 NodeImpl ancestor = (NodeImpl)getElementAncestor(this); 1556 if (ancestor != null) { 1557 return ancestor.lookupNamespaceURI(specifiedPrefix); 1558 } 1559 1560 return null; 1561 1562 1563 } 1564 case Node.DOCUMENT_NODE : { 1565 return((NodeImpl)((Document )this).getDocumentElement()).lookupNamespaceURI(specifiedPrefix); 1566 } 1567 case Node.ENTITY_NODE : 1568 case Node.NOTATION_NODE: 1569 case Node.DOCUMENT_FRAGMENT_NODE: 1570 case Node.DOCUMENT_TYPE_NODE: 1571 return null; 1573 case Node.ATTRIBUTE_NODE:{ 1574 if (this.ownerNode.getNodeType() == Node.ELEMENT_NODE) { 1575 return ownerNode.lookupNamespaceURI(specifiedPrefix); 1576 1577 } 1578 return null; 1579 } 1580 default:{ 1581 NodeImpl ancestor = (NodeImpl)getElementAncestor(this); 1582 if (ancestor != null) { 1583 return ancestor.lookupNamespaceURI(specifiedPrefix); 1584 } 1585 return null; 1586 } 1587 1588 } 1589 } 1590 1591 1592 Node getElementAncestor (Node currentNode){ 1593 Node parent = currentNode.getParentNode(); 1594 if (parent != null) { 1595 short type = parent.getNodeType(); 1596 if (type == Node.ELEMENT_NODE) { 1597 return parent; 1598 } 1599 return getElementAncestor(parent); 1600 } 1601 return null; 1602 } 1603 1604 String lookupNamespacePrefix(String namespaceURI, ElementImpl el){ 1605 String namespace = this.getNamespaceURI(); 1606 String prefix = this.getPrefix(); 1609 1610 if (namespace!=null && namespace.equals(namespaceURI)) { 1611 if (prefix != null) { 1612 String foundNamespace = el.lookupNamespaceURI(prefix); 1613 if (foundNamespace !=null && foundNamespace.equals(namespaceURI)) { 1614 return prefix; 1615 } 1616 1617 } 1618 } 1619 if (this.hasAttributes()) { 1620 NamedNodeMap map = this.getAttributes(); 1621 int length = map.getLength(); 1622 for (int i=0;i<length;i++) { 1623 Node attr = map.item(i); 1624 String attrPrefix = attr.getPrefix(); 1625 String value = attr.getNodeValue(); 1626 namespace = attr.getNamespaceURI(); 1627 if (namespace !=null && namespace.equals("http://www.w3.org/2000/xmlns/")) { 1628 if (((attr.getNodeName().equals("xmlns")) || 1630 (attrPrefix !=null && attrPrefix.equals("xmlns")) && 1631 value.equals(namespaceURI))) { 1632 1633 String localname= attr.getLocalName(); 1634 String foundNamespace = el.lookupNamespaceURI(localname); 1635 if (foundNamespace !=null && foundNamespace.equals(namespaceURI)) { 1636 return localname; 1637 } 1638 } 1639 1640 1641 } 1642 } 1643 } 1644 NodeImpl ancestor = (NodeImpl)getElementAncestor(this); 1645 1646 if (ancestor != null) { 1647 return ancestor.lookupNamespacePrefix(namespaceURI, el); 1648 } 1649 return null; 1650 } 1651 1652 1694 public boolean isEqualNode(Node arg) { 1695 if (arg == this) { 1696 return true; 1697 } 1698 if (arg.getNodeType() != getNodeType()) { 1699 return false; 1700 } 1701 if (getNodeName() == null) { 1704 if (arg.getNodeName() != null) { 1705 return false; 1706 } 1707 } 1708 else if (!getNodeName().equals(arg.getNodeName())) { 1709 return false; 1710 } 1711 1712 if (getLocalName() == null) { 1713 if (arg.getLocalName() != null) { 1714 return false; 1715 } 1716 } 1717 else if (!getLocalName().equals(arg.getLocalName())) { 1718 return false; 1719 } 1720 1721 if (getNamespaceURI() == null) { 1722 if (arg.getNamespaceURI() != null) { 1723 return false; 1724 } 1725 } 1726 else if (!getNamespaceURI().equals(arg.getNamespaceURI())) { 1727 return false; 1728 } 1729 1730 if (getPrefix() == null) { 1731 if (arg.getPrefix() != null) { 1732 return false; 1733 } 1734 } 1735 else if (!getPrefix().equals(arg.getPrefix())) { 1736 return false; 1737 } 1738 1739 if (getNodeValue() == null) { 1740 if (arg.getNodeValue() != null) { 1741 return false; 1742 } 1743 } 1744 else if (!getNodeValue().equals(arg.getNodeValue())) { 1745 return false; 1746 } 1747 1748 1749 return true; 1750 } 1751 1752 1755 public Object getFeature(String feature, String version) { 1756 return isSupported(feature, version) ? this : null; 1759 } 1760 1761 1774 public Object setUserData(String key, 1775 Object data, 1776 UserDataHandler handler) { 1777 return ownerDocument().setUserData(this, key, data, handler); 1778 } 1779 1780 1789 public Object getUserData(String key) { 1790 return ownerDocument().getUserData(this, key); 1791 } 1792 1793 protected Hashtable getUserDataRecord(){ 1794 return ownerDocument().getUserDataRecord(this); 1795 } 1796 1797 1801 1819 public void setReadOnly(boolean readOnly, boolean deep) { 1820 1821 if (needsSyncData()) { 1822 synchronizeData(); 1823 } 1824 isReadOnly(readOnly); 1825 1826 } 1828 1832 public boolean getReadOnly() { 1833 1834 if (needsSyncData()) { 1835 synchronizeData(); 1836 } 1837 return isReadOnly(); 1838 1839 } 1841 1853 public void setUserData(Object data) { 1854 ownerDocument().setUserData(this, data); 1855 } 1856 1857 1861 public Object getUserData() { 1862 return ownerDocument().getUserData(this); 1863 } 1864 1865 1869 1872 protected void changed() { 1873 ownerDocument().changed(); 1877 } 1878 1879 1882 protected int changes() { 1883 return ownerDocument().changes(); 1887 } 1888 1889 1893 protected void synchronizeData() { 1894 needsSyncData(false); 1896 } 1897 1898 1902 protected Node getContainer() { 1903 return null; 1904 } 1905 1906 1907 1910 1911 final boolean isReadOnly() { 1912 return (flags & READONLY) != 0; 1913 } 1914 1915 final void isReadOnly(boolean value) { 1916 flags = (short) (value ? flags | READONLY : flags & ~READONLY); 1917 } 1918 1919 final boolean needsSyncData() { 1920 return (flags & SYNCDATA) != 0; 1921 } 1922 1923 final void needsSyncData(boolean value) { 1924 flags = (short) (value ? flags | SYNCDATA : flags & ~SYNCDATA); 1925 } 1926 1927 final boolean needsSyncChildren() { 1928 return (flags & SYNCCHILDREN) != 0; 1929 } 1930 1931 public final void needsSyncChildren(boolean value) { 1932 flags = (short) (value ? flags | SYNCCHILDREN : flags & ~SYNCCHILDREN); 1933 } 1934 1935 final boolean isOwned() { 1936 return (flags & OWNED) != 0; 1937 } 1938 1939 final void isOwned(boolean value) { 1940 flags = (short) (value ? flags | OWNED : flags & ~OWNED); 1941 } 1942 1943 final boolean isFirstChild() { 1944 return (flags & FIRSTCHILD) != 0; 1945 } 1946 1947 final void isFirstChild(boolean value) { 1948 flags = (short) (value ? flags | FIRSTCHILD : flags & ~FIRSTCHILD); 1949 } 1950 1951 final boolean isSpecified() { 1952 return (flags & SPECIFIED) != 0; 1953 } 1954 1955 final void isSpecified(boolean value) { 1956 flags = (short) (value ? flags | SPECIFIED : flags & ~SPECIFIED); 1957 } 1958 1959 final boolean internalIsIgnorableWhitespace() { 1961 return (flags & IGNORABLEWS) != 0; 1962 } 1963 1964 final void isIgnorableWhitespace(boolean value) { 1965 flags = (short) (value ? flags | IGNORABLEWS : flags & ~IGNORABLEWS); 1966 } 1967 1968 final boolean hasStringValue() { 1969 return (flags & HASSTRING) != 0; 1970 } 1971 1972 final void hasStringValue(boolean value) { 1973 flags = (short) (value ? flags | HASSTRING : flags & ~HASSTRING); 1974 } 1975 1976 final boolean isNormalized() { 1977 return (flags & NORMALIZED) != 0; 1978 } 1979 1980 final void isNormalized(boolean value) { 1981 if (!value && isNormalized() && ownerNode != null) { 1983 ownerNode.isNormalized(false); 1984 } 1985 flags = (short) (value ? flags | NORMALIZED : flags & ~NORMALIZED); 1986 } 1987 1988 final boolean isIdAttribute() { 1989 return (flags & ID) != 0; 1990 } 1991 1992 final void isIdAttribute(boolean value) { 1993 flags = (short) (value ? flags | ID : flags & ~ID); 1994 } 1995 1996 2000 2001 public String toString() { 2002 return "["+getNodeName()+": "+getNodeValue()+"]"; 2003 } 2004 2005 2009 2010 private void writeObject(ObjectOutputStream out) throws IOException { 2011 2012 if (needsSyncData()) { 2014 synchronizeData(); 2015 } 2016 out.defaultWriteObject(); 2018 2019 } 2021} | Popular Tags |