1 57 58 package com.sun.org.apache.xerces.internal.dom; 59 60 import java.io.IOException ; 61 import java.io.ObjectOutputStream ; 62 import java.io.Serializable ; 63 import java.util.Hashtable ; 64 65 import org.w3c.dom.UserDataHandler ; 66 import org.w3c.dom.DOMException ; 67 import org.w3c.dom.Document ; 68 import org.w3c.dom.DocumentType ; 69 import org.w3c.dom.NamedNodeMap ; 70 import org.w3c.dom.Node ; 71 import org.w3c.dom.NodeList ; 72 import org.w3c.dom.events.Event ; 73 import org.w3c.dom.events.EventListener ; 74 import org.w3c.dom.events.EventTarget ; 75 76 77 120 public abstract class NodeImpl 121 implements Node , NodeList , EventTarget , Cloneable , Serializable { 122 123 127 128 133 public static final short TREE_POSITION_PRECEDING = 0x01; 134 137 public static final short TREE_POSITION_FOLLOWING = 0x02; 138 141 public static final short TREE_POSITION_ANCESTOR = 0x04; 142 145 public static final short TREE_POSITION_DESCENDANT = 0x08; 146 151 public static final short TREE_POSITION_EQUIVALENT = 0x10; 152 156 public static final short TREE_POSITION_SAME_NODE = 0x20; 157 161 public static final short TREE_POSITION_DISCONNECTED = 0x00; 162 163 164 public static final short DOCUMENT_POSITION_DISCONNECTED = 0x01; 166 public static final short DOCUMENT_POSITION_PRECEDING = 0x02; 167 public static final short DOCUMENT_POSITION_FOLLOWING = 0x04; 168 public static final short DOCUMENT_POSITION_CONTAINS = 0x08; 169 public static final short DOCUMENT_POSITION_IS_CONTAINED = 0x10; 170 public static final short DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC = 0x20; 171 172 173 static final long serialVersionUID = -6316591992167219696L; 174 175 177 178 public static final short ELEMENT_DEFINITION_NODE = 21; 179 180 184 186 protected NodeImpl ownerNode; 188 190 protected short flags; 191 192 protected final static short READONLY = 0x1<<0; 193 protected final static short SYNCDATA = 0x1<<1; 194 protected final static short SYNCCHILDREN = 0x1<<2; 195 protected final static short OWNED = 0x1<<3; 196 protected final static short FIRSTCHILD = 0x1<<4; 197 protected final static short SPECIFIED = 0x1<<5; 198 protected final static short IGNORABLEWS = 0x1<<6; 199 protected final static short HASSTRING = 0x1<<7; 200 protected final static short NORMALIZED = 0x1<<8; 201 protected final static short ID = 0x1<<9; 202 203 207 213 protected NodeImpl(CoreDocumentImpl ownerDocument) { 214 ownerNode = ownerDocument; 216 } 218 219 public NodeImpl() {} 220 221 225 229 public abstract short getNodeType(); 230 231 234 public abstract String getNodeName(); 235 236 240 public String getNodeValue() 241 throws DOMException { 242 return null; } 244 245 249 public void setNodeValue(String x) 250 throws DOMException { 251 } 253 254 274 public Node appendChild(Node newChild) throws DOMException { 275 return insertBefore(newChild, null); 276 } 277 278 301 public Node cloneNode(boolean deep) { 302 303 if (needsSyncData()) { 304 synchronizeData(); 305 } 306 307 NodeImpl newnode; 308 try { 309 newnode = (NodeImpl)clone(); 310 } 311 catch (CloneNotSupportedException e) { 312 throw new RuntimeException ("**Internal Error**" + e); 315 } 316 317 newnode.ownerNode = ownerDocument(); 319 newnode.isOwned(false); 320 321 newnode.isReadOnly(false); 324 325 ownerDocument().callUserDataHandlers(this, newnode, 326 UserDataHandler.NODE_CLONED); 327 328 return newnode; 329 330 } 332 337 public Document getOwnerDocument() { 338 if (isOwned()) { 341 return ownerNode.ownerDocument(); 342 } else { 343 return (Document ) ownerNode; 344 } 345 } 346 347 351 CoreDocumentImpl ownerDocument() { 352 if (isOwned()) { 355 return ownerNode.ownerDocument(); 356 } else { 357 return (CoreDocumentImpl) ownerNode; 358 } 359 } 360 361 365 void setOwnerDocument(CoreDocumentImpl doc) { 366 if (needsSyncData()) { 367 synchronizeData(); 368 } 369 if (!isOwned()) { 372 ownerNode = doc; 373 } 374 } 375 376 379 protected int getNodeNumber() { 380 int nodeNumber; 381 CoreDocumentImpl cd = (CoreDocumentImpl)(this.getOwnerDocument()); 382 nodeNumber = cd.getNodeNumber(this); 383 return nodeNumber; 384 } 385 386 392 public Node getParentNode() { 393 return null; } 395 396 399 NodeImpl parentNode() { 400 return null; 401 } 402 403 404 public Node getNextSibling() { 405 return null; } 407 408 409 public Node getPreviousSibling() { 410 return null; } 412 413 ChildNode previousSibling() { 414 return null; } 416 417 424 public NamedNodeMap getAttributes() { 425 return null; } 427 428 435 public boolean hasAttributes() { 436 return false; } 438 439 446 public boolean hasChildNodes() { 447 return false; 448 } 449 450 463 public NodeList getChildNodes() { 464 return this; 465 } 466 467 472 public Node getFirstChild() { 473 return null; 474 } 475 476 481 public Node getLastChild() { 482 return null; 483 } 484 485 516 public Node insertBefore(Node newChild, Node refChild) 517 throws DOMException { 518 throw new DOMException (DOMException.HIERARCHY_REQUEST_ERR, 519 DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, 520 "HIERARCHY_REQUEST_ERR", null)); 521 } 522 523 538 public Node removeChild(Node oldChild) 539 throws DOMException { 540 throw new DOMException (DOMException.NOT_FOUND_ERR, 541 DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, 542 "NOT_FOUND_ERR", null)); 543 } 544 545 569 public Node replaceChild(Node newChild, Node oldChild) 570 throws DOMException { 571 throw new DOMException (DOMException.HIERARCHY_REQUEST_ERR, 572 DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, 573 "HIERARCHY_REQUEST_ERR", null)); 574 } 575 576 580 588 public int getLength() { 589 return 0; 590 } 591 592 602 public Node item(int index) { 603 return null; 604 } 605 606 610 628 public void normalize() { 629 631 } 632 633 648 public boolean isSupported(String feature, String version) 649 { 650 return ownerDocument().getImplementation().hasFeature(feature, 651 version); 652 } 653 654 671 public String getNamespaceURI() 672 { 673 return null; 674 } 675 676 690 public String getPrefix() 691 { 692 return null; 693 } 694 695 716 public void setPrefix(String prefix) 717 throws DOMException 718 { 719 throw new DOMException (DOMException.NAMESPACE_ERR, 720 DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, 721 "NAMESPACE_ERR", null)); 722 } 723 724 736 public String getLocalName() 737 { 738 return null; 739 } 740 741 745 public void addEventListener(String type, EventListener listener, 746 boolean useCapture) { 747 ownerDocument().addEventListener(this, type, listener, useCapture); 749 } 750 751 public void removeEventListener(String type, EventListener listener, 752 boolean useCapture) { 753 ownerDocument().removeEventListener(this, type, listener, useCapture); 755 } 756 757 public boolean dispatchEvent(Event event) { 758 return ownerDocument().dispatchEvent(this, event); 760 } 761 762 766 790 public String getBaseURI() { 791 return null; 792 } 793 794 804 public short compareTreePosition(Node other) { 805 840 841 if (this==other) 843 return (TREE_POSITION_SAME_NODE | TREE_POSITION_EQUIVALENT); 844 845 short thisType = this.getNodeType(); 847 short otherType = other.getNodeType(); 848 849 if (thisType == Node.ENTITY_NODE || 851 thisType == Node.NOTATION_NODE || 852 otherType == Node.ENTITY_NODE || 853 otherType == Node.NOTATION_NODE ) { 854 return TREE_POSITION_DISCONNECTED; 855 } 856 857 864 Node node; 865 Node thisAncestor = this; 866 Node otherAncestor = other; 867 int thisDepth=0; 868 int otherDepth=0; 869 for (node=this; node != null; node = node.getParentNode()) { 870 thisDepth +=1; 871 if (node == other) 872 return (TREE_POSITION_ANCESTOR | TREE_POSITION_PRECEDING); 874 thisAncestor = node; 875 } 876 877 for (node=other; node!=null; node=node.getParentNode()) { 878 otherDepth +=1; 879 if (node == this) 880 return (TREE_POSITION_DESCENDANT | TREE_POSITION_FOLLOWING); 882 otherAncestor = node; 883 } 884 885 886 Node thisNode = this; 887 Node otherNode = other; 888 889 int thisAncestorType = thisAncestor.getNodeType(); 890 int otherAncestorType = otherAncestor.getNodeType(); 891 892 895 if (thisAncestorType == Node.ATTRIBUTE_NODE) { 896 thisNode = ((AttrImpl)thisAncestor).getOwnerElement(); 897 } 898 if (otherAncestorType == Node.ATTRIBUTE_NODE) { 899 otherNode = ((AttrImpl)otherAncestor).getOwnerElement(); 900 } 901 902 if (thisAncestorType == Node.ATTRIBUTE_NODE && 905 otherAncestorType == Node.ATTRIBUTE_NODE && 906 thisNode==otherNode) 907 return TREE_POSITION_EQUIVALENT; 908 909 912 if (thisAncestorType == Node.ATTRIBUTE_NODE) { 915 thisDepth=0; 916 for (node=thisNode; node != null; node=node.getParentNode()) { 917 thisDepth +=1; 918 if (node == otherNode) 919 { 921 return TREE_POSITION_PRECEDING; 922 } 923 thisAncestor = node; 924 } 925 } 926 927 if (otherAncestorType == Node.ATTRIBUTE_NODE) { 930 otherDepth=0; 931 for (node=otherNode; node != null; node=node.getParentNode()) { 932 otherDepth +=1; 933 if (node == thisNode) 934 return TREE_POSITION_FOLLOWING; 937 otherAncestor = node; 938 } 939 } 940 941 if (thisAncestor != otherAncestor) 944 return TREE_POSITION_DISCONNECTED; 945 946 947 950 if (thisDepth > otherDepth) { 951 for (int i=0; i<thisDepth - otherDepth; i++) 952 thisNode = thisNode.getParentNode(); 953 if (thisNode == otherNode) 957 return TREE_POSITION_PRECEDING; 958 } 959 960 else { 961 for (int i=0; i<otherDepth - thisDepth; i++) 962 otherNode = otherNode.getParentNode(); 963 if (otherNode == thisNode) 967 return TREE_POSITION_FOLLOWING; 968 } 969 970 Node thisNodeP, otherNodeP; 973 for (thisNodeP=thisNode.getParentNode(), 974 otherNodeP=otherNode.getParentNode(); 975 thisNodeP!=otherNodeP;) { 976 thisNode = thisNodeP; 977 otherNode = otherNodeP; 978 thisNodeP = thisNodeP.getParentNode(); 979 otherNodeP = otherNodeP.getParentNode(); 980 } 981 982 986 for (Node current=thisNodeP.getFirstChild(); 987 current!=null; 988 current=current.getNextSibling()) { 989 if (current==otherNode) { 990 return TREE_POSITION_PRECEDING; 991 } 992 else if (current==thisNode) { 993 return TREE_POSITION_FOLLOWING; 994 } 995 } 996 return 0; 999 1000 } 1001 1009 public short compareDocumentPosition(Node other) throws DOMException { 1010 1011 if (this==other) 1013 return 0; 1014 1015 try { 1017 NodeImpl node = (NodeImpl) other; 1018 } catch (ClassCastException e) { 1019 String msg = DOMMessageFormatter.formatMessage( 1021 DOMMessageFormatter.DOM_DOMAIN, "NOT_SUPPORTED_ERR", null); 1022 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, msg); 1023 } 1024 1025 Document thisOwnerDoc, otherOwnerDoc; 1026 if (this.getNodeType() == Node.DOCUMENT_NODE) 1028 thisOwnerDoc = (Document )this; 1029 else 1030 thisOwnerDoc = this.getOwnerDocument(); 1031 if (other.getNodeType() == Node.DOCUMENT_NODE) 1032 otherOwnerDoc = (Document )other; 1033 else 1034 otherOwnerDoc = other.getOwnerDocument(); 1035 1036 if (thisOwnerDoc != otherOwnerDoc && 1039 thisOwnerDoc !=null && 1040 otherOwnerDoc !=null) 1041 { 1042 int otherDocNum = ((CoreDocumentImpl)otherOwnerDoc).getNodeNumber(); 1043 int thisDocNum = ((CoreDocumentImpl)thisOwnerDoc).getNodeNumber(); 1044 if (otherDocNum > thisDocNum) 1045 return DOCUMENT_POSITION_DISCONNECTED | 1046 DOCUMENT_POSITION_FOLLOWING | 1047 DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC; 1048 else 1049 return DOCUMENT_POSITION_DISCONNECTED | 1050 DOCUMENT_POSITION_PRECEDING | 1051 DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC; 1052 1053 } 1054 1055 1062 Node node; 1063 Node thisAncestor = this; 1064 Node otherAncestor = other; 1065 1066 int thisDepth=0; 1067 int otherDepth=0; 1068 for (node=this; node != null; node = node.getParentNode()) { 1069 thisDepth +=1; 1070 if (node == other) 1071 return (DOCUMENT_POSITION_CONTAINS | 1073 DOCUMENT_POSITION_PRECEDING); 1074 thisAncestor = node; 1075 } 1076 1077 for (node=other; node!=null; node=node.getParentNode()) { 1078 otherDepth +=1; 1079 if (node == this) 1080 return (DOCUMENT_POSITION_IS_CONTAINED | 1082 DOCUMENT_POSITION_FOLLOWING); 1083 otherAncestor = node; 1084 } 1085 1086 1087 1088 int thisAncestorType = thisAncestor.getNodeType(); 1089 int otherAncestorType = otherAncestor.getNodeType(); 1090 Node thisNode = this; 1091 Node otherNode = other; 1092 1093 switch (thisAncestorType) { 1096 case Node.NOTATION_NODE: 1097 case Node.ENTITY_NODE: { 1098 DocumentType container = thisOwnerDoc.getDoctype(); 1099 if (container == otherAncestor) return 1100 (DOCUMENT_POSITION_CONTAINS | DOCUMENT_POSITION_PRECEDING); 1101 switch (otherAncestorType) { 1102 case Node.NOTATION_NODE: 1103 case Node.ENTITY_NODE: { 1104 if (thisAncestorType != otherAncestorType) 1105 return ((thisAncestorType>otherAncestorType) ? 1107 DOCUMENT_POSITION_PRECEDING:DOCUMENT_POSITION_FOLLOWING); 1108 else { 1109 if (thisAncestorType == Node.NOTATION_NODE) 1111 1112 if (((NamedNodeMapImpl)container.getNotations()).precedes(otherAncestor,thisAncestor)) 1113 return (DOCUMENT_POSITION_PRECEDING | 1114 DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC); 1115 else 1116 return (DOCUMENT_POSITION_FOLLOWING | 1117 DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC); 1118 else 1119 if (((NamedNodeMapImpl)container.getEntities()).precedes(otherAncestor,thisAncestor)) 1120 return (DOCUMENT_POSITION_PRECEDING | 1121 DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC); 1122 else 1123 return (DOCUMENT_POSITION_FOLLOWING | 1124 DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC); 1125 } 1126 } 1127 } 1128 thisNode = thisAncestor = thisOwnerDoc; 1129 break; 1130 } 1131 case Node.DOCUMENT_TYPE_NODE: { 1132 if (otherNode == thisOwnerDoc) 1133 return (DOCUMENT_POSITION_PRECEDING | 1134 DOCUMENT_POSITION_CONTAINS); 1135 else if (thisOwnerDoc!=null && thisOwnerDoc==otherOwnerDoc) 1136 return (DOCUMENT_POSITION_FOLLOWING); 1137 break; 1138 } 1139 case Node.ATTRIBUTE_NODE: { 1140 thisNode = ((AttrImpl)thisAncestor).getOwnerElement(); 1141 if (otherAncestorType==Node.ATTRIBUTE_NODE) { 1142 otherNode = ((AttrImpl)otherAncestor).getOwnerElement(); 1143 if (otherNode == thisNode) { 1144 if (((NamedNodeMapImpl)thisNode.getAttributes()).precedes(other,this)) 1145 return (DOCUMENT_POSITION_PRECEDING | 1146 DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC); 1147 else 1148 return (DOCUMENT_POSITION_FOLLOWING | 1149 DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC); 1150 } 1151 } 1152 1153 thisDepth=0; 1155 for (node=thisNode; node != null; node=node.getParentNode()) { 1156 thisDepth +=1; 1157 if (node == otherNode) 1158 { 1159 return (DOCUMENT_POSITION_CONTAINS | 1161 DOCUMENT_POSITION_PRECEDING); 1162 } 1163 thisAncestor = node; 1164 } 1165 } 1166 } 1167 switch (otherAncestorType) { 1168 case Node.NOTATION_NODE: 1169 case Node.ENTITY_NODE: { 1170 DocumentType container = thisOwnerDoc.getDoctype(); 1171 if (container == this) return (DOCUMENT_POSITION_IS_CONTAINED | 1172 DOCUMENT_POSITION_FOLLOWING); 1173 otherNode = otherAncestor = thisOwnerDoc; 1174 break; 1175 } 1176 case Node.DOCUMENT_TYPE_NODE: { 1177 if (thisNode == otherOwnerDoc) 1178 return (DOCUMENT_POSITION_FOLLOWING | 1179 DOCUMENT_POSITION_IS_CONTAINED); 1180 else if (otherOwnerDoc!=null && thisOwnerDoc==otherOwnerDoc) 1181 return (DOCUMENT_POSITION_PRECEDING); 1182 break; 1183 } 1184 case Node.ATTRIBUTE_NODE: { 1185 otherDepth=0; 1186 otherNode = ((AttrImpl)otherAncestor).getOwnerElement(); 1187 for (node=otherNode; node != null; node=node.getParentNode()) { 1188 otherDepth +=1; 1189 if (node == thisNode) 1190 return DOCUMENT_POSITION_FOLLOWING | 1193 DOCUMENT_POSITION_IS_CONTAINED; 1194 otherAncestor = node; 1195 } 1196 1197 } 1198 } 1199 1200 if (thisAncestor != otherAncestor) { 1203 int thisAncestorNum, otherAncestorNum; 1204 thisAncestorNum = ((NodeImpl)thisAncestor).getNodeNumber(); 1205 otherAncestorNum = ((NodeImpl)otherAncestor).getNodeNumber(); 1206 1207 if (thisAncestorNum > otherAncestorNum) 1208 return DOCUMENT_POSITION_DISCONNECTED | 1209 DOCUMENT_POSITION_FOLLOWING | 1210 DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC; 1211 else 1212 return DOCUMENT_POSITION_DISCONNECTED | 1213 DOCUMENT_POSITION_PRECEDING | 1214 DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC; 1215 } 1216 1217 1218 1221 if (thisDepth > otherDepth) { 1222 for (int i=0; i<thisDepth - otherDepth; i++) 1223 thisNode = thisNode.getParentNode(); 1224 if (thisNode == otherNode) 1228{ 1229 return DOCUMENT_POSITION_PRECEDING; 1230 } 1231 } 1232 1233 else { 1234 for (int i=0; i<otherDepth - thisDepth; i++) 1235 otherNode = otherNode.getParentNode(); 1236 if (otherNode == thisNode) 1240 return DOCUMENT_POSITION_FOLLOWING; 1241 } 1242 1243 Node thisNodeP, otherNodeP; 1246 for (thisNodeP=thisNode.getParentNode(), 1247 otherNodeP=otherNode.getParentNode(); 1248 thisNodeP!=otherNodeP;) { 1249 thisNode = thisNodeP; 1250 otherNode = otherNodeP; 1251 thisNodeP = thisNodeP.getParentNode(); 1252 otherNodeP = otherNodeP.getParentNode(); 1253 } 1254 1255 1259 for (Node current=thisNodeP.getFirstChild(); 1260 current!=null; 1261 current=current.getNextSibling()) { 1262 if (current==otherNode) { 1263 return DOCUMENT_POSITION_PRECEDING; 1264 } 1265 else if (current==thisNode) { 1266 return DOCUMENT_POSITION_FOLLOWING; 1267 } 1268 } 1269 return 0; 1272 1273 } 1274 1275 1338 public String getTextContent() throws DOMException { 1339 return getNodeValue(); } 1341 1342 void getTextContent(StringBuffer buf) throws DOMException { 1344 String content = getNodeValue(); 1345 if (content != null) { 1346 buf.append(content); 1347 } 1348 } 1349 1350 1395 public void setTextContent(String textContent) 1396 throws DOMException { 1397 setNodeValue(textContent); 1398 } 1399 1400 1414 public boolean isSameNode(Node other) { 1415 return this == other; 1417 } 1418 1419 1420 1421 1422 1431 public boolean isDefaultNamespace(String namespaceURI){ 1432 short type = this.getNodeType(); 1434 switch (type) { 1435 case Node.ELEMENT_NODE: { 1436 String namespace = this.getNamespaceURI(); 1437 String prefix = this.getPrefix(); 1438 1439 if (prefix == null || prefix.length() == 0) { 1441 if (namespaceURI == null) { 1442 return (namespace == namespaceURI); 1443 } 1444 return namespaceURI.equals(namespace); 1445 } 1446 if (this.hasAttributes()) { 1447 ElementImpl elem = (ElementImpl)this; 1448 NodeImpl attr = (NodeImpl)elem.getAttributeNodeNS("http://www.w3.org/2000/xmlns/", "xmlns"); 1449 if (attr != null) { 1450 String value = attr.getNodeValue(); 1451 if (namespaceURI == null) { 1452 return (namespace == value); 1453 } 1454 return namespaceURI.equals(value); 1455 } 1456 } 1457 1458 NodeImpl ancestor = (NodeImpl)getElementAncestor(this); 1459 if (ancestor != null) { 1460 return ancestor.isDefaultNamespace(namespaceURI); 1461 } 1462 return false; 1463 } 1464 case Node.DOCUMENT_NODE:{ 1465 return((NodeImpl)((Document )this).getDocumentElement()).isDefaultNamespace(namespaceURI); 1466 } 1467 1468 case Node.ENTITY_NODE : 1469 case Node.NOTATION_NODE: 1470 case Node.DOCUMENT_FRAGMENT_NODE: 1471 case Node.DOCUMENT_TYPE_NODE: 1472 return false; 1474 case Node.ATTRIBUTE_NODE:{ 1475 if (this.ownerNode.getNodeType() == Node.ELEMENT_NODE) { 1476 return ownerNode.isDefaultNamespace(namespaceURI); 1477 1478 } 1479 return false; 1480 } 1481 default:{ 1482 NodeImpl ancestor = (NodeImpl)getElementAncestor(this); 1483 if (ancestor != null) { 1484 return ancestor.isDefaultNamespace(namespaceURI); 1485 } 1486 return false; 1487 } 1488 1489 } 1490 1491 1492 } 1493 1494 1495 1503 public String lookupPrefix(String namespaceURI){ 1504 1505 if (namespaceURI == null) { 1508 return null; 1509 } 1510 1511 short type = this.getNodeType(); 1512 1513 switch (type) { 1514 case Node.ELEMENT_NODE: { 1515 1516 String namespace = this.getNamespaceURI(); return lookupNamespacePrefix(namespaceURI, (ElementImpl)this); 1518 } 1519 case Node.DOCUMENT_NODE:{ 1520 return((NodeImpl)((Document )this).getDocumentElement()).lookupPrefix(namespaceURI); 1521 } 1522 1523 case Node.ENTITY_NODE : 1524 case Node.NOTATION_NODE: 1525 case Node.DOCUMENT_FRAGMENT_NODE: 1526 case Node.DOCUMENT_TYPE_NODE: 1527 return null; 1529 case Node.ATTRIBUTE_NODE:{ 1530 if (this.ownerNode.getNodeType() == Node.ELEMENT_NODE) { 1531 return ownerNode.lookupPrefix(namespaceURI); 1532 1533 } 1534 return null; 1535 } 1536 default:{ 1537 NodeImpl ancestor = (NodeImpl)getElementAncestor(this); 1538 if (ancestor != null) { 1539 return ancestor.lookupPrefix(namespaceURI); 1540 } 1541 return null; 1542 } 1543 1544 } 1545 } 1546 1555 public String lookupNamespaceURI(String specifiedPrefix) { 1556 short type = this.getNodeType(); 1557 switch (type) { 1558 case Node.ELEMENT_NODE : { 1559 1560 String namespace = this.getNamespaceURI(); 1561 String prefix = this.getPrefix(); 1562 if (namespace !=null) { 1563 if (specifiedPrefix== null && prefix==specifiedPrefix) { 1565 return namespace; 1567 } else if (prefix != null && prefix.equals(specifiedPrefix)) { 1568 return namespace; 1570 } 1571 } 1572 if (this.hasAttributes()) { 1573 NamedNodeMap map = this.getAttributes(); 1574 int length = map.getLength(); 1575 for (int i=0;i<length;i++) { 1576 Node attr = map.item(i); 1577 String attrPrefix = attr.getPrefix(); 1578 String value = attr.getNodeValue(); 1579 namespace = attr.getNamespaceURI(); 1580 if (namespace !=null && namespace.equals("http://www.w3.org/2000/xmlns/")) { 1581 if (specifiedPrefix == null && 1583 attr.getNodeName().equals("xmlns")) { 1584 return value; 1586 } else if (attrPrefix !=null && 1587 attrPrefix.equals("xmlns") && 1588 attr.getLocalName().equals(specifiedPrefix)) { 1589 return value; 1591 } 1592 } 1593 } 1594 } 1595 NodeImpl ancestor = (NodeImpl)getElementAncestor(this); 1596 if (ancestor != null) { 1597 return ancestor.lookupNamespaceURI(specifiedPrefix); 1598 } 1599 1600 return null; 1601 1602 1603 } 1604 case Node.DOCUMENT_NODE : { 1605 return((NodeImpl)((Document )this).getDocumentElement()).lookupNamespaceURI(specifiedPrefix); 1606 } 1607 case Node.ENTITY_NODE : 1608 case Node.NOTATION_NODE: 1609 case Node.DOCUMENT_FRAGMENT_NODE: 1610 case Node.DOCUMENT_TYPE_NODE: 1611 return null; 1613 case Node.ATTRIBUTE_NODE:{ 1614 if (this.ownerNode.getNodeType() == Node.ELEMENT_NODE) { 1615 return ownerNode.lookupNamespaceURI(specifiedPrefix); 1616 1617 } 1618 return null; 1619 } 1620 default:{ 1621 NodeImpl ancestor = (NodeImpl)getElementAncestor(this); 1622 if (ancestor != null) { 1623 return ancestor.lookupNamespaceURI(specifiedPrefix); 1624 } 1625 return null; 1626 } 1627 1628 } 1629 } 1630 1631 1632 Node getElementAncestor (Node currentNode){ 1633 Node parent = currentNode.getParentNode(); 1634 if (parent != null) { 1635 short type = parent.getNodeType(); 1636 if (type == Node.ELEMENT_NODE) { 1637 return parent; 1638 } 1639 return getElementAncestor(parent); 1640 } 1641 return null; 1642 } 1643 1644 String lookupNamespacePrefix(String namespaceURI, ElementImpl el){ 1645 String namespace = this.getNamespaceURI(); 1646 String prefix = this.getPrefix(); 1649 1650 if (namespace!=null && namespace.equals(namespaceURI)) { 1651 if (prefix != null) { 1652 String foundNamespace = el.lookupNamespaceURI(prefix); 1653 if (foundNamespace !=null && foundNamespace.equals(namespaceURI)) { 1654 return prefix; 1655 } 1656 1657 } 1658 } 1659 if (this.hasAttributes()) { 1660 NamedNodeMap map = this.getAttributes(); 1661 int length = map.getLength(); 1662 for (int i=0;i<length;i++) { 1663 Node attr = map.item(i); 1664 String attrPrefix = attr.getPrefix(); 1665 String value = attr.getNodeValue(); 1666 namespace = attr.getNamespaceURI(); 1667 if (namespace !=null && namespace.equals("http://www.w3.org/2000/xmlns/")) { 1668 if (((attr.getNodeName().equals("xmlns")) || 1670 (attrPrefix !=null && attrPrefix.equals("xmlns")) && 1671 value.equals(namespaceURI))) { 1672 1673 String localname= attr.getLocalName(); 1674 String foundNamespace = el.lookupNamespaceURI(localname); 1675 if (foundNamespace !=null && foundNamespace.equals(namespaceURI)) { 1676 return localname; 1677 } 1678 } 1679 1680 1681 } 1682 } 1683 } 1684 NodeImpl ancestor = (NodeImpl)getElementAncestor(this); 1685 1686 if (ancestor != null) { 1687 return ancestor.lookupNamespacePrefix(namespaceURI, el); 1688 } 1689 return null; 1690 } 1691 1692 1734 public boolean isEqualNode(Node arg) { 1735 if (arg == this) { 1736 return true; 1737 } 1738 if (arg.getNodeType() != getNodeType()) { 1739 return false; 1740 } 1741 if (getNodeName() == null) { 1744 if (arg.getNodeName() != null) { 1745 return false; 1746 } 1747 } 1748 else if (!getNodeName().equals(arg.getNodeName())) { 1749 return false; 1750 } 1751 1752 if (getLocalName() == null) { 1753 if (arg.getLocalName() != null) { 1754 return false; 1755 } 1756 } 1757 else if (!getLocalName().equals(arg.getLocalName())) { 1758 return false; 1759 } 1760 1761 if (getNamespaceURI() == null) { 1762 if (arg.getNamespaceURI() != null) { 1763 return false; 1764 } 1765 } 1766 else if (!getNamespaceURI().equals(arg.getNamespaceURI())) { 1767 return false; 1768 } 1769 1770 if (getPrefix() == null) { 1771 if (arg.getPrefix() != null) { 1772 return false; 1773 } 1774 } 1775 else if (!getPrefix().equals(arg.getPrefix())) { 1776 return false; 1777 } 1778 1779 if (getNodeValue() == null) { 1780 if (arg.getNodeValue() != null) { 1781 return false; 1782 } 1783 } 1784 else if (!getNodeValue().equals(arg.getNodeValue())) { 1785 return false; 1786 } 1787 1788 1789 return true; 1790 } 1791 1792 1795 public Object getFeature(String feature, String version) { 1796 return isSupported(feature, version) ? this : null; 1799 } 1800 1801 1814 public Object setUserData(String key, 1815 Object data, 1816 UserDataHandler handler) { 1817 return ownerDocument().setUserData(this, key, data, handler); 1818 } 1819 1820 1829 public Object getUserData(String key) { 1830 return ownerDocument().getUserData(this, key); 1831 } 1832 1833 1835 protected Hashtable getUserDataRecord(){ 1836 return ownerDocument().getUserDataRecord(this); 1837 } 1838 1839 1843 1861 public void setReadOnly(boolean readOnly, boolean deep) { 1862 1863 if (needsSyncData()) { 1864 synchronizeData(); 1865 } 1866 isReadOnly(readOnly); 1867 1868 } 1870 1874 public boolean getReadOnly() { 1875 1876 if (needsSyncData()) { 1877 synchronizeData(); 1878 } 1879 return isReadOnly(); 1880 1881 } 1883 1895 public void setUserData(Object data) { 1896 ownerDocument().setUserData(this, data); 1897 } 1898 1899 1903 public Object getUserData() { 1904 return ownerDocument().getUserData(this); 1905 } 1906 1907 1911 1914 protected void changed() { 1915 ownerDocument().changed(); 1919 } 1920 1921 1924 protected int changes() { 1925 return ownerDocument().changes(); 1929 } 1930 1931 1935 protected void synchronizeData() { 1936 needsSyncData(false); 1938 } 1939 1940 1944 protected Node getContainer() { 1945 return null; 1946 } 1947 1948 1949 1952 1953 final boolean isReadOnly() { 1954 return (flags & READONLY) != 0; 1955 } 1956 1957 final void isReadOnly(boolean value) { 1958 flags = (short) (value ? flags | READONLY : flags & ~READONLY); 1959 } 1960 1961 final boolean needsSyncData() { 1962 return (flags & SYNCDATA) != 0; 1963 } 1964 1965 final void needsSyncData(boolean value) { 1966 flags = (short) (value ? flags | SYNCDATA : flags & ~SYNCDATA); 1967 } 1968 1969 final boolean needsSyncChildren() { 1970 return (flags & SYNCCHILDREN) != 0; 1971 } 1972 1973 public final void needsSyncChildren(boolean value) { 1974 flags = (short) (value ? flags | SYNCCHILDREN : flags & ~SYNCCHILDREN); 1975 } 1976 1977 final boolean isOwned() { 1978 return (flags & OWNED) != 0; 1979 } 1980 1981 final void isOwned(boolean value) { 1982 flags = (short) (value ? flags | OWNED : flags & ~OWNED); 1983 } 1984 1985 final boolean isFirstChild() { 1986 return (flags & FIRSTCHILD) != 0; 1987 } 1988 1989 final void isFirstChild(boolean value) { 1990 flags = (short) (value ? flags | FIRSTCHILD : flags & ~FIRSTCHILD); 1991 } 1992 1993 final boolean isSpecified() { 1994 return (flags & SPECIFIED) != 0; 1995 } 1996 1997 final void isSpecified(boolean value) { 1998 flags = (short) (value ? flags | SPECIFIED : flags & ~SPECIFIED); 1999 } 2000 2001 final boolean internalIsIgnorableWhitespace() { 2003 return (flags & IGNORABLEWS) != 0; 2004 } 2005 2006 final void isIgnorableWhitespace(boolean value) { 2007 flags = (short) (value ? flags | IGNORABLEWS : flags & ~IGNORABLEWS); 2008 } 2009 2010 final boolean hasStringValue() { 2011 return (flags & HASSTRING) != 0; 2012 } 2013 2014 final void hasStringValue(boolean value) { 2015 flags = (short) (value ? flags | HASSTRING : flags & ~HASSTRING); 2016 } 2017 2018 final boolean isNormalized() { 2019 return (flags & NORMALIZED) != 0; 2020 } 2021 2022 final void isNormalized(boolean value) { 2023 if (!value && isNormalized() && ownerNode != null) { 2025 ownerNode.isNormalized(false); 2026 } 2027 flags = (short) (value ? flags | NORMALIZED : flags & ~NORMALIZED); 2028 } 2029 2030 final boolean isIdAttribute() { 2031 return (flags & ID) != 0; 2032 } 2033 2034 final void isIdAttribute(boolean value) { 2035 flags = (short) (value ? flags | ID : flags & ~ID); 2036 } 2037 2038 2042 2043 public String toString() { 2044 return "["+getNodeName()+": "+getNodeValue()+"]"; 2045 } 2046 2047 2051 2052 private void writeObject(ObjectOutputStream out) throws IOException { 2053 2054 if (needsSyncData()) { 2056 synchronizeData(); 2057 } 2058 out.defaultWriteObject(); 2060 2061 } 2063} | Popular Tags |