1 57 58 package com.sun.org.apache.xerces.internal.dom; 59 60 import java.io.Serializable ; 61 import java.util.Enumeration ; 62 import java.util.Hashtable ; 63 64 import org.w3c.dom.DOMConfiguration ; 65 import org.w3c.dom.UserDataHandler ; 66 import com.sun.org.apache.xerces.internal.util.XMLChar; 67 import com.sun.org.apache.xerces.internal.util.XML11Char; 68 import com.sun.org.apache.xerces.internal.xni.NamespaceContext; 69 import org.w3c.dom.Attr ; 70 import org.w3c.dom.CDATASection ; 71 import org.w3c.dom.Comment ; 72 import org.w3c.dom.DOMException ; 73 import org.w3c.dom.DOMImplementation ; 74 import org.w3c.dom.Document ; 75 import org.w3c.dom.DocumentFragment ; 76 import org.w3c.dom.DocumentType ; 77 import org.w3c.dom.Element ; 78 import org.w3c.dom.Entity ; 79 import org.w3c.dom.EntityReference ; 80 import org.w3c.dom.NamedNodeMap ; 81 import org.w3c.dom.Node ; 82 import org.w3c.dom.NodeList ; 83 import org.w3c.dom.Notation ; 84 import org.w3c.dom.ProcessingInstruction ; 85 import org.w3c.dom.Text ; 86 import org.w3c.dom.events.Event ; 87 import org.w3c.dom.events.EventListener ; 88 import org.w3c.dom.ls.DOMImplementationLS ; 89 import org.w3c.dom.ls.LSSerializer ; 90 91 116 117 118 public class CoreDocumentImpl 119 extends ParentNode implements Document { 120 121 127 131 132 static final long serialVersionUID = 0; 133 134 138 140 141 protected DocumentTypeImpl docType; 142 143 144 protected ElementImpl docElement; 145 146 147 transient NodeListCache fFreeNLCache; 148 149 150 protected String encoding; 151 152 153 protected String actualEncoding; 154 155 156 protected String version; 157 158 159 protected boolean standalone; 160 161 162 protected String fDocumentURI; 163 164 166 protected Hashtable userData; 167 168 169 170 protected Hashtable identifiers; 171 172 transient DOMNormalizer domNormalizer = null; 174 transient DOMConfigurationImpl fConfiguration= null; 175 176 transient Object fXPathEvaluator = null; 178 179 180 private final static int[] kidOK; 181 182 216 protected int changes = 0; 217 218 220 221 protected boolean allowGrammarAccess; 222 223 224 protected boolean errorChecking = true; 225 226 protected boolean xmlVersionChanged = false ; 229 230 232 private int documentNumber=0; 235 private int nodeCounter = 0; 239 private Hashtable nodeTable; 240 private boolean xml11Version = false; 245 static { 246 247 kidOK = new int[13]; 248 249 kidOK[DOCUMENT_NODE] = 250 1 << ELEMENT_NODE | 1 << PROCESSING_INSTRUCTION_NODE | 251 1 << COMMENT_NODE | 1 << DOCUMENT_TYPE_NODE; 252 253 kidOK[DOCUMENT_FRAGMENT_NODE] = 254 kidOK[ENTITY_NODE] = 255 kidOK[ENTITY_REFERENCE_NODE] = 256 kidOK[ELEMENT_NODE] = 257 1 << ELEMENT_NODE | 1 << PROCESSING_INSTRUCTION_NODE | 258 1 << COMMENT_NODE | 1 << TEXT_NODE | 259 1 << CDATA_SECTION_NODE | 1 << ENTITY_REFERENCE_NODE ; 260 261 262 kidOK[ATTRIBUTE_NODE] = 263 1 << TEXT_NODE | 1 << ENTITY_REFERENCE_NODE; 264 265 kidOK[DOCUMENT_TYPE_NODE] = 266 kidOK[PROCESSING_INSTRUCTION_NODE] = 267 kidOK[COMMENT_NODE] = 268 kidOK[TEXT_NODE] = 269 kidOK[CDATA_SECTION_NODE] = 270 kidOK[NOTATION_NODE] = 271 0; 272 273 } 275 279 283 public CoreDocumentImpl() { 284 this(false); 285 } 286 287 288 public CoreDocumentImpl(boolean grammarAccess) { 289 super(null); 290 ownerDocument = this; 291 allowGrammarAccess = grammarAccess; 292 } 293 294 298 public CoreDocumentImpl(DocumentType doctype) { 299 this(doctype, false); 300 } 301 302 303 public CoreDocumentImpl(DocumentType doctype, boolean grammarAccess) { 304 this(grammarAccess); 305 if (doctype != null) { 306 DocumentTypeImpl doctypeImpl; 307 try { 308 doctypeImpl = (DocumentTypeImpl) doctype; 309 } catch (ClassCastException e) { 310 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null); 311 throw new DOMException (DOMException.WRONG_DOCUMENT_ERR, msg); 312 } 313 doctypeImpl.ownerDocument = this; 314 appendChild(doctype); 315 } 316 } 317 318 322 final public Document getOwnerDocument() { 325 return null; 326 } 327 328 329 public short getNodeType() { 330 return Node.DOCUMENT_NODE; 331 } 332 333 334 public String getNodeName() { 335 return "#document"; 336 } 337 338 347 public Node cloneNode(boolean deep) { 348 349 CoreDocumentImpl newdoc = new CoreDocumentImpl(); 350 callUserDataHandlers(this, newdoc, UserDataHandler.NODE_CLONED); 351 cloneNode(newdoc, deep); 352 353 return newdoc; 354 355 } 357 358 361 protected void cloneNode(CoreDocumentImpl newdoc, boolean deep) { 362 363 if (needsSyncChildren()) { 365 synchronizeChildren(); 366 } 367 368 if (deep) { 369 Hashtable reversedIdentifiers = null; 370 371 if (identifiers != null) { 372 reversedIdentifiers = new Hashtable (); 374 Enumeration elementIds = identifiers.keys(); 375 while (elementIds.hasMoreElements()) { 376 Object elementId = elementIds.nextElement(); 377 reversedIdentifiers.put(identifiers.get(elementId), 378 elementId); 379 } 380 } 381 382 for (ChildNode kid = firstChild; kid != null; 384 kid = kid.nextSibling) { 385 newdoc.appendChild(newdoc.importNode(kid, true, true, 386 reversedIdentifiers)); 387 } 388 } 389 390 newdoc.allowGrammarAccess = allowGrammarAccess; 392 newdoc.errorChecking = errorChecking; 393 394 } 396 410 public Node insertBefore(Node newChild, Node refChild) 411 throws DOMException { 412 413 int type = newChild.getNodeType(); 415 if (errorChecking) { 416 if((type == Node.ELEMENT_NODE && docElement != null) || 417 (type == Node.DOCUMENT_TYPE_NODE && docType != null)) { 418 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null); 419 throw new DOMException (DOMException.HIERARCHY_REQUEST_ERR, msg); 420 } 421 } 422 if (newChild.getOwnerDocument() == null && 424 newChild instanceof DocumentTypeImpl) { 425 ((DocumentTypeImpl) newChild).ownerDocument = this; 426 } 427 super.insertBefore(newChild,refChild); 428 429 if (type == Node.ELEMENT_NODE) { 431 docElement = (ElementImpl)newChild; 432 } 433 else if (type == Node.DOCUMENT_TYPE_NODE) { 434 docType = (DocumentTypeImpl)newChild; 435 } 436 437 return newChild; 438 439 } 441 448 public Node removeChild(Node oldChild) throws DOMException { 449 450 super.removeChild(oldChild); 451 452 int type = oldChild.getNodeType(); 454 if(type == Node.ELEMENT_NODE) { 455 docElement = null; 456 } 457 else if (type == Node.DOCUMENT_TYPE_NODE) { 458 docType = null; 459 } 460 461 return oldChild; 462 463 } 465 472 public Node replaceChild(Node newChild, Node oldChild) 473 throws DOMException { 474 475 if (newChild.getOwnerDocument() == null && 477 newChild instanceof DocumentTypeImpl) { 478 ((DocumentTypeImpl) newChild).ownerDocument = this; 479 } 480 int type = oldChild.getNodeType(); 481 int newType = newChild.getNodeType(); 482 if (errorChecking &&((docType != null && 483 oldChild.getNodeType() != Node.DOCUMENT_TYPE_NODE && 484 newChild.getNodeType() == Node.DOCUMENT_TYPE_NODE) 485 || (docElement != null && 486 oldChild.getNodeType() != Node.ELEMENT_NODE && 487 newChild.getNodeType() == Node.ELEMENT_NODE))) { 488 throw new DOMException ( 489 DOMException.HIERARCHY_REQUEST_ERR, 490 DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null)); 491 } 492 super.replaceChild(newChild, oldChild); 493 494 if(type == Node.ELEMENT_NODE) { 495 docElement = (ElementImpl)newChild; 496 } 497 else if (type == Node.DOCUMENT_TYPE_NODE) { 498 docType = (DocumentTypeImpl)newChild; 499 } 500 return oldChild; 501 } 503 507 public String getTextContent() throws DOMException { 508 return null; 509 } 510 511 515 public void setTextContent(String textContent) 516 throws DOMException { 517 } 519 520 523 public Object getFeature(String feature, String version) { 524 525 boolean anyVersion = version == null || version.length() == 0; 526 527 if ((feature.equalsIgnoreCase("XPath") 528 || feature.equalsIgnoreCase("+XPath")) && 529 (anyVersion || version.equals("3.0"))) { 530 531 try { 532 Class xpathClass = ObjectFactory.findProviderClass( 533 "com.sun.org.apache.xpath.internal.domapi.XPathEvaluatorImpl", 534 ObjectFactory.findClassLoader(), true); 535 fXPathEvaluator = xpathClass.newInstance(); 536 java.lang.reflect.Method setDocument = xpathClass.getMethod("setDoc", new Class []{Document .class}); 537 setDocument.invoke(fXPathEvaluator, new Object []{this}); 538 return fXPathEvaluator; 539 } 540 catch (Exception e){ 541 return null; 542 } 543 } 544 return super.getFeature(feature, version); 545 } 546 547 551 553 563 public Attr createAttribute(String name) 564 throws DOMException { 565 566 if (errorChecking && !isXMLName(name,xml11Version)) { 567 String msg = 568 DOMMessageFormatter.formatMessage( 569 DOMMessageFormatter.DOM_DOMAIN, 570 "INVALID_CHARACTER_ERR", 571 null); 572 throw new DOMException (DOMException.INVALID_CHARACTER_ERR, msg); 573 } 574 return new AttrImpl(this, name); 575 576 } 578 587 public CDATASection createCDATASection(String data) 588 throws DOMException { 589 return new CDATASectionImpl(this, data); 590 } 591 592 597 public Comment createComment(String data) { 598 return new CommentImpl(this, data); 599 } 600 601 605 public DocumentFragment createDocumentFragment() { 606 return new DocumentFragmentImpl(this); 607 } 608 609 621 public Element createElement(String tagName) 622 throws DOMException { 623 624 if (errorChecking && !isXMLName(tagName,xml11Version)) { 625 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_CHARACTER_ERR", null); 626 throw new DOMException (DOMException.INVALID_CHARACTER_ERR, msg); 627 } 628 return new ElementImpl(this, tagName); 629 630 } 632 642 public EntityReference createEntityReference(String name) 643 throws DOMException { 644 645 if (errorChecking && !isXMLName(name,xml11Version)) { 646 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_CHARACTER_ERR", null); 647 throw new DOMException (DOMException.INVALID_CHARACTER_ERR, msg); 648 } 649 return new EntityReferenceImpl(this, name); 650 651 } 653 666 public ProcessingInstruction createProcessingInstruction(String target, 667 String data) 668 throws DOMException { 669 670 if (errorChecking && !isXMLName(target,xml11Version)) { 671 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_CHARACTER_ERR", null); 672 throw new DOMException (DOMException.INVALID_CHARACTER_ERR, msg); 673 } 674 return new ProcessingInstructionImpl(this, target, data); 675 676 } 678 684 public Text createTextNode(String data) { 685 return new TextImpl(this, data); 686 } 687 688 690 695 public DocumentType getDoctype() { 696 if (needsSyncChildren()) { 697 synchronizeChildren(); 698 } 699 return docType; 700 } 701 702 703 712 public Element getDocumentElement() { 713 if (needsSyncChildren()) { 714 synchronizeChildren(); 715 } 716 return docElement; 717 } 718 719 728 public NodeList getElementsByTagName(String tagname) { 729 return new DeepNodeListImpl(this,tagname); 730 } 731 732 738 public DOMImplementation getImplementation() { 739 return CoreDOMImplementationImpl.getDOMImplementation(); 742 } 743 744 748 750 768 769 public void setErrorChecking(boolean check) { 770 errorChecking = check; 771 772 } 773 774 777 public void setStrictErrorChecking(boolean check) { 778 errorChecking = check; 779 } 780 781 784 public boolean getErrorChecking() { 785 return errorChecking; 786 } 787 788 791 public boolean getStrictErrorChecking() { 792 return errorChecking; 793 } 794 795 796 805 public String getInputEncoding() { 806 return actualEncoding; 807 } 808 809 818 public void setInputEncoding(String value) { 819 actualEncoding = value; 820 } 821 822 829 public void setXmlEncoding(String value) { 830 encoding = value; 831 } 832 833 837 public String getXmlEncoding() { 838 return encoding; 839 } 840 841 846 public void setXmlVersion(String value) { 847 if(value.equals("1.0") || value.equals("1.1")){ 848 if(!getXmlVersion().equals(value)){ 851 xmlVersionChanged = true ; 852 isNormalized(false); 854 version = value; 855 } 856 } 857 else{ 858 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_SUPPORTED_ERR", null); 862 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, msg); 863 864 } 865 if((getXmlVersion()).equals("1.1")){ 866 xml11Version = true; 867 } 868 else{ 869 xml11Version = false; 870 } 871 } 872 873 877 public String getXmlVersion() { 878 return (version == null)?"1.0":version; 879 } 880 881 891 public void setXmlStandalone(boolean value) 892 throws DOMException { 893 standalone = value; 894 } 895 896 901 public boolean getXmlStandalone() { 902 return standalone; 903 } 904 905 906 914 public String getDocumentURI(){ 915 return fDocumentURI; 916 } 917 918 919 923 public Node renameNode(Node n,String namespaceURI,String name) 924 throws DOMException { 925 926 if (n.getOwnerDocument() != this && n != this) { 927 String msg = DOMMessageFormatter.formatMessage( 928 DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null); 929 throw new DOMException (DOMException.WRONG_DOCUMENT_ERR, msg); 930 } 931 switch (n.getNodeType()) { 932 case ELEMENT_NODE: { 933 ElementImpl el = (ElementImpl) n; 934 if (el instanceof ElementNSImpl) { 935 ((ElementNSImpl) el).rename(namespaceURI, name); 936 937 callUserDataHandlers(el, null, 939 UserDataHandler.NODE_RENAMED); 940 } 941 else { 942 if (namespaceURI == null) { 943 int colon1 = name.indexOf(':'); 944 if(colon1 != -1){ 945 String msg = 946 DOMMessageFormatter.formatMessage( 947 DOMMessageFormatter.DOM_DOMAIN, 948 "NAMESPACE_ERR", 949 null); 950 throw new DOMException (DOMException.NAMESPACE_ERR, msg); 951 } 952 if (errorChecking && !isXMLName(name,xml11Version)) { 953 String msg = DOMMessageFormatter.formatMessage( 954 DOMMessageFormatter.DOM_DOMAIN, 955 "INVALID_CHARACTER_ERR", null); 956 throw new DOMException (DOMException.INVALID_CHARACTER_ERR, 957 msg); 958 } 959 el.rename(name); 960 961 callUserDataHandlers(el, null, 963 UserDataHandler.NODE_RENAMED); 964 } 965 else { 966 ElementNSImpl nel = 968 new ElementNSImpl(this, namespaceURI, name); 969 970 copyEventListeners(el, nel); 972 973 Hashtable data = removeUserDataTable(el); 975 976 Node parent = el.getParentNode(); 978 Node nextSib = el.getNextSibling(); 979 if (parent != null) { 980 parent.removeChild(el); 981 } 982 Node child = el.getFirstChild(); 984 while (child != null) { 985 el.removeChild(child); 986 nel.appendChild(child); 987 child = el.getFirstChild(); 988 } 989 nel.moveSpecifiedAttributes(el); 991 992 setUserDataTable(nel, data); 994 995 callUserDataHandlers(el, nel, 997 UserDataHandler.NODE_RENAMED); 998 999 if (parent != null) { 1001 parent.insertBefore(nel, nextSib); 1002 } 1003 el = nel; 1004 } 1005 } 1006 renamedElement((Element ) n, el); 1008 return el; 1009 } 1010 case ATTRIBUTE_NODE: { 1011 AttrImpl at = (AttrImpl) n; 1012 1013 Element el = at.getOwnerElement(); 1015 if (el != null) { 1016 el.removeAttributeNode(at); 1017 } 1018 if (n instanceof AttrNSImpl) { 1019 ((AttrNSImpl) at).rename(namespaceURI, name); 1020 if (el != null) { 1022 el.setAttributeNodeNS(at); 1023 } 1024 1025 callUserDataHandlers(at, null, 1027 UserDataHandler.NODE_RENAMED); 1028 } 1029 else { 1030 if (namespaceURI == null) { 1031 at.rename(name); 1032 if (el != null) { 1034 el.setAttributeNode(at); 1035 } 1036 1037 callUserDataHandlers(at, null, 1039 UserDataHandler.NODE_RENAMED); 1040 } 1041 else { 1042 AttrNSImpl nat = 1044 new AttrNSImpl(this, namespaceURI, name); 1045 1046 copyEventListeners(at, nat); 1048 1049 Hashtable data = removeUserDataTable(at); 1051 1052 Node child = at.getFirstChild(); 1054 while (child != null) { 1055 at.removeChild(child); 1056 nat.appendChild(child); 1057 child = at.getFirstChild(); 1058 } 1059 1060 setUserDataTable(nat, data); 1062 1063 callUserDataHandlers(at, nat, 1065 UserDataHandler.NODE_RENAMED); 1066 1067 if (el != null) { 1069 el.setAttributeNode(nat); 1070 } 1071 at = nat; 1072 } 1073 } 1074 renamedAttrNode((Attr ) n, at); 1076 1077 return at; 1078 } 1079 default: { 1080 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_SUPPORTED_ERR", null); 1081 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, msg); 1082 } 1083 } 1084 1085 } 1086 1087 1088 1092 public void normalizeDocument(){ 1093 if (isNormalized() && !isNormalizeDocRequired()) { 1095 return; 1096 } 1097 if (needsSyncChildren()) { 1098 synchronizeChildren(); 1099 } 1100 1101 if (domNormalizer == null) { 1102 domNormalizer = new DOMNormalizer(); 1103 } 1104 1105 if (fConfiguration == null) { 1106 fConfiguration = new DOMConfigurationImpl(); 1107 } 1108 else { 1109 fConfiguration.reset(); 1110 } 1111 1112 domNormalizer.normalizeDocument(this, fConfiguration); 1113 isNormalized(true); 1114 xmlVersionChanged = false ; 1117 } 1118 1119 1120 1127 public DOMConfiguration getDomConfig(){ 1128 if (fConfiguration == null) { 1129 fConfiguration = new DOMConfigurationImpl(); 1130 } 1131 return fConfiguration; 1132 } 1133 1134 1135 1139 public String getBaseURI() { 1140 return fDocumentURI; 1141 } 1142 1143 1146 public void setDocumentURI(String documentURI){ 1147 fDocumentURI = documentURI; 1148 } 1149 1150 1151 1168 public boolean getAsync() { 1169 return false; 1170 } 1171 1172 1186 public void setAsync(boolean async) { 1187 if (async) { 1188 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_SUPPORTED_ERR", null); 1189 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, msg); 1190 } 1191 } 1192 1199 public void abort() { 1200 } 1201 1202 1238 public boolean load(String uri) { 1239 return false; 1240 } 1241 1242 1250 public boolean loadXML(String source) { 1251 return false; 1252 } 1253 1254 1272 public String saveXML(Node node) 1273 throws DOMException { 1274 if ( node != null && 1275 this != node.getOwnerDocument() ) { 1276 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null); 1277 throw new DOMException (DOMException.WRONG_DOCUMENT_ERR, msg); 1278 } 1279 DOMImplementationLS domImplLS = (DOMImplementationLS )DOMImplementationImpl.getDOMImplementation(); 1280 LSSerializer xmlWriter = domImplLS.createLSSerializer(); 1281 if (node == null) { 1282 node = this; 1283 } 1284 return xmlWriter.writeToString(node); 1285 } 1286 1287 1288 1292 void setMutationEvents(boolean set) { 1293 } 1295 1296 1299 boolean getMutationEvents() { 1300 return false; 1302 } 1303 1304 1305 1306 1308 1319 public DocumentType createDocumentType(String qualifiedName, 1320 String publicID, 1321 String systemID) 1322 throws DOMException { 1323 1324 return new DocumentTypeImpl(this, qualifiedName, publicID, systemID); 1325 1326 } 1328 1340 public Entity createEntity(String name) 1341 throws DOMException { 1342 1343 1344 if (errorChecking && !isXMLName(name,xml11Version)) { 1345 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_CHARACTER_ERR", null); 1346 throw new DOMException (DOMException.INVALID_CHARACTER_ERR, msg); 1347 } 1348 return new EntityImpl(this, name); 1349 1350 } 1352 1364 public Notation createNotation(String name) 1365 throws DOMException { 1366 1367 if (errorChecking && !isXMLName(name,xml11Version)) { 1368 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_CHARACTER_ERR", null); 1369 throw new DOMException (DOMException.INVALID_CHARACTER_ERR, msg); 1370 } 1371 return new NotationImpl(this, name); 1372 1373 } 1375 1379 public ElementDefinitionImpl createElementDefinition(String name) 1380 throws DOMException { 1381 1382 if (errorChecking && !isXMLName(name,xml11Version)) { 1383 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_CHARACTER_ERR", null); 1384 throw new DOMException (DOMException.INVALID_CHARACTER_ERR, msg); 1385 } 1386 return new ElementDefinitionImpl(this, name); 1387 1388 } 1390 1392 1395 protected int getNodeNumber() { 1396 if (documentNumber==0) { 1397 1398 CoreDOMImplementationImpl cd = (CoreDOMImplementationImpl)CoreDOMImplementationImpl.getDOMImplementation(); 1399 documentNumber = cd.assignDocumentNumber(); 1400 } 1401 return documentNumber; 1402 } 1403 1404 1405 1409 protected int getNodeNumber(Node node) { 1410 1411 int num; 1416 if (nodeTable == null) { 1417 nodeTable = new Hashtable (); 1418 num = --nodeCounter; 1419 nodeTable.put(node, new Integer (num)); 1420 } 1421 else { 1422 Integer n = (Integer )nodeTable.get(node); 1423 if (n== null) { 1424 num = --nodeCounter; 1425 nodeTable.put(node, new Integer (num)); 1426 } 1427 else 1428 num = n.intValue(); 1429 } 1430 return num; 1431 } 1432 1433 1442 public Node importNode(Node source, boolean deep) 1443 throws DOMException { 1444 return importNode(source, deep, false, null); 1445 } 1447 1459 private Node importNode(Node source, boolean deep, boolean cloningDoc, 1460 Hashtable reversedIdentifiers) 1461 throws DOMException { 1462 Node newnode=null; 1463 Hashtable userData = null; 1464 1465 if(source instanceof NodeImpl) 1477 userData = ((NodeImpl)source).getUserDataRecord(); 1478 int type = source.getNodeType(); 1479 switch (type) { 1480 case ELEMENT_NODE: { 1481 Element newElement; 1482 boolean domLevel20 = source.getOwnerDocument().getImplementation().hasFeature("XML", "2.0"); 1483 if(domLevel20 == false || source.getLocalName() == null) 1485 newElement = createElement(source.getNodeName()); 1486 else 1487 newElement = createElementNS(source.getNamespaceURI(), 1488 source.getNodeName()); 1489 1490 NamedNodeMap sourceAttrs = source.getAttributes(); 1492 if (sourceAttrs != null) { 1493 int length = sourceAttrs.getLength(); 1494 for (int index = 0; index < length; index++) { 1495 Attr attr = (Attr )sourceAttrs.item(index); 1496 1497 if (attr.getSpecified() || cloningDoc) { 1502 Attr newAttr = (Attr )importNode(attr, true, cloningDoc, 1503 reversedIdentifiers); 1504 1505 if (domLevel20 == false || 1508 attr.getLocalName() == null) 1509 newElement.setAttributeNode(newAttr); 1510 else 1511 newElement.setAttributeNodeNS(newAttr); 1512 } 1513 } 1514 } 1515 1516 if (reversedIdentifiers != null) { 1518 Object elementId = reversedIdentifiers.get(source); 1520 if (elementId != null) { 1521 if (identifiers == null) 1522 identifiers = new Hashtable (); 1523 1524 identifiers.put(elementId, newElement); 1525 } 1526 } 1527 1528 newnode = newElement; 1529 break; 1530 } 1531 1532 case ATTRIBUTE_NODE: { 1533 1534 if( source.getOwnerDocument().getImplementation().hasFeature("XML", "2.0") ){ 1535 if (source.getLocalName() == null) { 1536 newnode = createAttribute(source.getNodeName()); 1537 } else { 1538 newnode = createAttributeNS(source.getNamespaceURI(), 1539 source.getNodeName()); 1540 } 1541 } 1542 else { 1543 newnode = createAttribute(source.getNodeName()); 1544 } 1545 if (source instanceof AttrImpl) { 1548 AttrImpl attr = (AttrImpl) source; 1549 if (attr.hasStringValue()) { 1550 AttrImpl newattr = (AttrImpl) newnode; 1551 newattr.setValue(attr.getValue()); 1552 deep = false; 1553 } 1554 else { 1555 deep = true; 1556 } 1557 } 1558 else { 1559 if (source.getFirstChild() == null) { 1565 newnode.setNodeValue(source.getNodeValue()); 1566 deep = false; 1567 } else { 1568 deep = true; 1569 } 1570 } 1571 break; 1572 } 1573 1574 case TEXT_NODE: { 1575 newnode = createTextNode(source.getNodeValue()); 1576 break; 1577 } 1578 1579 case CDATA_SECTION_NODE: { 1580 newnode = createCDATASection(source.getNodeValue()); 1581 break; 1582 } 1583 1584 case ENTITY_REFERENCE_NODE: { 1585 newnode = createEntityReference(source.getNodeName()); 1586 deep = false; 1589 break; 1590 } 1591 1592 case ENTITY_NODE: { 1593 Entity srcentity = (Entity )source; 1594 EntityImpl newentity = 1595 (EntityImpl)createEntity(source.getNodeName()); 1596 newentity.setPublicId(srcentity.getPublicId()); 1597 newentity.setSystemId(srcentity.getSystemId()); 1598 newentity.setNotationName(srcentity.getNotationName()); 1599 newentity.isReadOnly(false); 1602 newnode = newentity; 1603 break; 1604 } 1605 1606 case PROCESSING_INSTRUCTION_NODE: { 1607 newnode = createProcessingInstruction(source.getNodeName(), 1608 source.getNodeValue()); 1609 break; 1610 } 1611 1612 case COMMENT_NODE: { 1613 newnode = createComment(source.getNodeValue()); 1614 break; 1615 } 1616 1617 case DOCUMENT_TYPE_NODE: { 1618 if (!cloningDoc) { 1621 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_SUPPORTED_ERR", null); 1622 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, msg); 1623 } 1624 DocumentType srcdoctype = (DocumentType )source; 1625 DocumentTypeImpl newdoctype = (DocumentTypeImpl) 1626 createDocumentType(srcdoctype.getNodeName(), 1627 srcdoctype.getPublicId(), 1628 srcdoctype.getSystemId()); 1629 NamedNodeMap smap = srcdoctype.getEntities(); 1631 NamedNodeMap tmap = newdoctype.getEntities(); 1632 if(smap != null) { 1633 for(int i = 0; i < smap.getLength(); i++) { 1634 tmap.setNamedItem(importNode(smap.item(i), true, true, 1635 reversedIdentifiers)); 1636 } 1637 } 1638 smap = srcdoctype.getNotations(); 1639 tmap = newdoctype.getNotations(); 1640 if (smap != null) { 1641 for(int i = 0; i < smap.getLength(); i++) { 1642 tmap.setNamedItem(importNode(smap.item(i), true, true, 1643 reversedIdentifiers)); 1644 } 1645 } 1646 1647 newnode = newdoctype; 1653 break; 1654 } 1655 1656 case DOCUMENT_FRAGMENT_NODE: { 1657 newnode = createDocumentFragment(); 1658 break; 1660 } 1661 1662 case NOTATION_NODE: { 1663 Notation srcnotation = (Notation )source; 1664 NotationImpl newnotation = 1665 (NotationImpl)createNotation(source.getNodeName()); 1666 newnotation.setPublicId(srcnotation.getPublicId()); 1667 newnotation.setSystemId(srcnotation.getSystemId()); 1668 newnode = newnotation; 1670 break; 1672 } 1673 case DOCUMENT_NODE : default: { String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_SUPPORTED_ERR", null); 1676 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, msg); 1677 } 1678 } 1679 1680 if(userData != null) 1681 callUserDataHandlers(source, newnode, UserDataHandler.NODE_IMPORTED,userData); 1682 1683 if (deep) { 1685 for (Node srckid = source.getFirstChild(); 1686 srckid != null; 1687 srckid = srckid.getNextSibling()) { 1688 newnode.appendChild(importNode(srckid, true, cloningDoc, 1689 reversedIdentifiers)); 1690 } 1691 } 1692 if (newnode.getNodeType() == Node.ENTITY_NODE) { 1693 ((NodeImpl)newnode).setReadOnly(true, true); 1694 } 1695 return newnode; 1696 1697 } 1699 1706 public Node adoptNode(Node source) { 1707 NodeImpl node; 1708 Hashtable userData = null; 1709 try { 1710 node = (NodeImpl) source; 1711 } catch (ClassCastException e) { 1712 return null; 1714 } 1715 switch (node.getNodeType()) { 1716 case ATTRIBUTE_NODE: { 1717 AttrImpl attr = (AttrImpl) node; 1718 if( attr.getOwnerElement() != null){ 1720 attr.getOwnerElement().removeAttributeNode(attr); 1722 } 1723 attr.isSpecified(true); 1725 1726 userData = node.getUserDataRecord(); 1727 1728 attr.setOwnerDocument(this); 1730 if(userData != null ) 1731 setUserDataTable(node,userData); 1732 break; 1733 } 1734 case ENTITY_NODE: 1737 case NOTATION_NODE:{ 1738 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 1739 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 1740 1741 } 1742 case DOCUMENT_NODE: 1745 case DOCUMENT_TYPE_NODE: { 1746 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_SUPPORTED_ERR", null); 1747 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, msg); 1748 } 1749 case ENTITY_REFERENCE_NODE: { 1750 userData = node.getUserDataRecord(); 1751 Node parent = node.getParentNode(); 1752 if (parent != null) { 1753 parent.removeChild(source); 1754 } 1755 Node child; 1757 while ((child = node.getFirstChild()) != null) { 1758 node.removeChild(child); 1759 } 1760 node.setOwnerDocument(this); 1762 if(userData != null) 1763 setUserDataTable(node,userData); 1764 if (docType == null) { 1766 break; 1767 } 1768 NamedNodeMap entities = docType.getEntities(); 1769 Node entityNode = entities.getNamedItem(node.getNodeName()); 1770 if (entityNode == null) { 1771 break; 1772 } 1773 EntityImpl entity = (EntityImpl) entityNode; 1774 for (child = entityNode.getFirstChild(); 1775 child != null; child = child.getNextSibling()) { 1776 Node childClone = child.cloneNode(true); 1777 node.appendChild(childClone); 1778 } 1779 break; 1780 } 1781 case ELEMENT_NODE: { 1782 userData = node.getUserDataRecord(); 1783 Node parent = node.getParentNode(); 1785 if (parent != null) { 1786 parent.removeChild(source); 1787 } 1788 if(userData != null) 1790 setUserDataTable(node,userData); 1791 node.setOwnerDocument(this); 1792 ((ElementImpl)node).reconcileDefaultAttributes(); 1794 break; 1795 } 1796 default: { 1797 userData = node.getUserDataRecord(); 1799 Node parent = node.getParentNode(); 1800 if (parent != null) { 1801 parent.removeChild(source); 1802 } 1803 if(userData != null) 1804 setUserDataTable(node,userData); 1805 node.setOwnerDocument(this); 1807 if(userData != null) 1808 setUserDataTable(node,userData); 1809 } 1810 } 1811 1812 if(userData != null) 1815 callUserDataHandlers(source, null, UserDataHandler.NODE_ADOPTED,userData); 1816 1817 return node; 1818 } 1819 1820 1833 public Element getElementById(String elementId) { 1834 return getIdentifier(elementId); 1835 } 1836 1837 1840 protected final void clearIdentifiers(){ 1841 if (identifiers != null){ 1842 identifiers.clear(); 1843 } 1844 } 1845 1846 1855 public void putIdentifier(String idName, Element element) { 1856 1857 if (element == null) { 1858 removeIdentifier(idName); 1859 return; 1860 } 1861 1862 if (needsSyncData()) { 1863 synchronizeData(); 1864 } 1865 1866 if (identifiers == null) { 1867 identifiers = new Hashtable (); 1868 } 1869 1870 identifiers.put(idName, element); 1871 1872 } 1874 1881 public Element getIdentifier(String idName) { 1882 1883 if (needsSyncData()) { 1884 synchronizeData(); 1885 } 1886 1887 if (identifiers == null) { 1888 return null; 1889 } 1890 Element elem = (Element ) identifiers.get(idName); 1891 if (elem != null) { 1892 Node parent = elem.getParentNode(); 1894 while (parent != null) { 1895 if (parent == this) { 1896 return elem; 1897 } 1898 parent = parent.getParentNode(); 1899 } 1900 } 1901 return null; 1902 } 1904 1911 public void removeIdentifier(String idName) { 1912 1913 if (needsSyncData()) { 1914 synchronizeData(); 1915 } 1916 1917 if (identifiers == null) { 1918 return; 1919 } 1920 1921 identifiers.remove(idName); 1922 1923 } 1925 1926 public Enumeration getIdentifiers() { 1927 1928 if (needsSyncData()) { 1929 synchronizeData(); 1930 } 1931 1932 if (identifiers == null) { 1933 identifiers = new Hashtable (); 1934 } 1935 1936 return identifiers.keys(); 1937 1938 } 1940 1944 1967 public Element createElementNS(String namespaceURI, String qualifiedName) 1968 throws DOMException { 1969 return new ElementNSImpl(this, namespaceURI, qualifiedName); 1970 } 1971 1972 1986 public Element createElementNS(String namespaceURI, String qualifiedName, 1987 String localpart) 1988 throws DOMException { 1989 return new ElementNSImpl(this, namespaceURI, qualifiedName, localpart); 1990 } 1991 1992 2010 public Attr createAttributeNS(String namespaceURI, String qualifiedName) 2011 throws DOMException { 2012 return new AttrNSImpl(this, namespaceURI, qualifiedName); 2013 } 2014 2015 2030 public Attr createAttributeNS(String namespaceURI, String qualifiedName, 2031 String localpart) 2032 throws DOMException { 2033 return new AttrNSImpl(this, namespaceURI, qualifiedName, localpart); 2034 } 2035 2036 2052 public NodeList getElementsByTagNameNS(String namespaceURI, 2053 String localName) { 2054 return new DeepNodeListImpl(this, namespaceURI, localName); 2055 } 2056 2057 2061 2062 public Object clone() throws CloneNotSupportedException { 2063 CoreDocumentImpl newdoc = (CoreDocumentImpl) super.clone(); 2064 newdoc.docType = null; 2065 newdoc.docElement = null; 2066 return newdoc; 2067 } 2068 2069 2073 2078 2079 public static final boolean isXMLName(String s, boolean xml11Version) { 2080 2081 if (s == null) { 2082 return false; 2083 } 2084 if(!xml11Version) 2085 return XMLChar.isValidName(s); 2086 else 2087 return XML11Char.isXML11ValidName(s); 2088 2089 } 2091 2098 public static final boolean isValidQName(String prefix, String local, boolean xml11Version) { 2099 2100 if (local == null) return false; 2102 boolean validNCName = false; 2103 2104 if (!xml11Version) { 2105 validNCName = (prefix == null || XMLChar.isValidNCName(prefix)) 2106 && XMLChar.isValidNCName(local); 2107 } 2108 else { 2109 validNCName = (prefix == null || XML11Char.isXML11ValidNCName(prefix)) 2110 && XML11Char.isXML11ValidNCName(local); 2111 } 2112 2113 return validNCName; 2114 } 2115 2119 2123 protected boolean isKidOK(Node parent, Node child) { 2124 if (allowGrammarAccess && 2125 parent.getNodeType() == Node.DOCUMENT_TYPE_NODE) { 2126 return child.getNodeType() == Node.ELEMENT_NODE; 2127 } 2128 return 0 != (kidOK[parent.getNodeType()] & 1 << child.getNodeType()); 2129 } 2130 2131 2134 protected void changed() { 2135 changes++; 2136 } 2137 2138 2141 protected int changes() { 2142 return changes; 2143 } 2144 2145 2147 2150 NodeListCache getNodeListCache(ParentNode owner) { 2151 if (fFreeNLCache == null) { 2152 return new NodeListCache(owner); 2153 } 2154 NodeListCache c = fFreeNLCache; 2155 fFreeNLCache = fFreeNLCache.next; 2156 c.fChild = null; 2157 c.fChildIndex = -1; 2158 c.fLength = -1; 2159 if (c.fOwner != null) { 2161 c.fOwner.fNodeListCache = null; 2162 } 2163 c.fOwner = owner; 2164 return c; 2166 } 2167 2168 2172 void freeNodeListCache(NodeListCache c) { 2173 c.next = fFreeNLCache; 2174 fFreeNLCache = c; 2175 } 2176 2177 2178 2179 2195 public Object setUserData(Node n, String key, 2196 Object data, UserDataHandler handler) { 2197 if (data == null) { 2198 if (userData != null) { 2199 Hashtable t = (Hashtable ) userData.get(n); 2200 if (t != null) { 2201 Object o = t.remove(key); 2202 if (o != null) { 2203 UserDataRecord r = (UserDataRecord) o; 2204 return r.fData; 2205 } 2206 } 2207 } 2208 return null; 2209 } 2210 else { 2211 Hashtable t; 2212 if (userData == null) { 2213 userData = new Hashtable (); 2214 t = new Hashtable (); 2215 userData.put(n, t); 2216 } 2217 else { 2218 t = (Hashtable ) userData.get(n); 2219 if (t == null) { 2220 t = new Hashtable (); 2221 userData.put(n, t); 2222 } 2223 } 2224 Object o = t.put(key, new UserDataRecord(data, handler)); 2225 if (o != null) { 2226 UserDataRecord r = (UserDataRecord) o; 2227 return r.fData; 2228 } 2229 return null; 2230 } 2231 } 2232 2233 2234 2244 public Object getUserData(Node n, String key) { 2245 if (userData == null) { 2246 return null; 2247 } 2248 Hashtable t = (Hashtable ) userData.get(n); 2249 if (t == null) { 2250 return null; 2251 } 2252 Object o = t.get(key); 2253 if (o != null) { 2254 UserDataRecord r = (UserDataRecord) o; 2255 return r.fData; 2256 } 2257 return null; 2258 } 2259 2260 protected Hashtable getUserDataRecord(Node n){ 2261 if (userData == null) { 2262 return null; 2263 } 2264 Hashtable t = (Hashtable ) userData.get(n); 2265 if (t == null) { 2266 return null; 2267 } 2268 return t; 2269 } 2270 2271 2276 Hashtable removeUserDataTable(Node n) { 2277 if (userData == null) { 2278 return null; 2279 } 2280 return (Hashtable ) userData.get(n); 2281 } 2282 2283 2288 void setUserDataTable(Node n, Hashtable data) { 2289 if (userData == null) 2290 userData = new Hashtable (); 2291 if (data != null) { 2292 userData.put(n, data); 2293 } 2294 } 2295 2296 2302 void callUserDataHandlers(Node n, Node c, short operation) { 2303 if (userData == null) { 2304 return; 2305 } 2306 if(n instanceof NodeImpl){ 2308 Hashtable t = ((NodeImpl)n).getUserDataRecord(); 2309 if (t == null || t.isEmpty()) { 2310 return; 2311 } 2312 callUserDataHandlers(n, c, operation,t); 2313 } 2314 } 2315 2316 2323 void callUserDataHandlers(Node n, Node c, short operation,Hashtable userData) { 2324 if (userData == null || userData.isEmpty()) { 2325 return; 2326 } 2327 Enumeration keys = userData.keys(); 2328 while (keys.hasMoreElements()) { 2329 String key = (String ) keys.nextElement(); 2330 UserDataRecord r = (UserDataRecord) userData.get(key); 2331 if (r.fHandler != null) { 2332 r.fHandler.handle(operation, key, r.fData, n, c); 2333 } 2334 } 2335 } 2336 2337 2346 2372 2373 protected final void checkNamespaceWF( String qname, int colon1, 2374 int colon2) { 2375 2376 if (!errorChecking) { 2377 return; 2378 } 2379 if (colon1 == 0 || colon1 == qname.length() - 1 || colon2 != colon1) { 2383 String msg = 2384 DOMMessageFormatter.formatMessage( 2385 DOMMessageFormatter.DOM_DOMAIN, 2386 "NAMESPACE_ERR", 2387 null); 2388 throw new DOMException (DOMException.NAMESPACE_ERR, msg); 2389 } 2390 } 2391 protected final void checkDOMNSErr(String prefix, 2392 String namespace) { 2393 if (errorChecking) { 2394 if (namespace == null) { 2395 String msg = 2396 DOMMessageFormatter.formatMessage( 2397 DOMMessageFormatter.DOM_DOMAIN, 2398 "NAMESPACE_ERR", 2399 null); 2400 throw new DOMException (DOMException.NAMESPACE_ERR, msg); 2401 } 2402 else if (prefix.equals("xml") 2403 && !namespace.equals(NamespaceContext.XML_URI)) { 2404 String msg = 2405 DOMMessageFormatter.formatMessage( 2406 DOMMessageFormatter.DOM_DOMAIN, 2407 "NAMESPACE_ERR", 2408 null); 2409 throw new DOMException (DOMException.NAMESPACE_ERR, msg); 2410 } 2411 else if ( 2412 prefix.equals("xmlns") 2413 && !namespace.equals(NamespaceContext.XMLNS_URI) 2414 || (!prefix.equals("xmlns") 2415 && namespace.equals(NamespaceContext.XMLNS_URI))) { 2416 String msg = 2417 DOMMessageFormatter.formatMessage( 2418 DOMMessageFormatter.DOM_DOMAIN, 2419 "NAMESPACE_ERR", 2420 null); 2421 throw new DOMException (DOMException.NAMESPACE_ERR, msg); 2422 } 2423 } 2424 } 2425 2426 2433 protected final void checkQName(String prefix, String local) { 2434 if (!errorChecking) { 2435 return; 2436 } 2437 2438 boolean validNCName = false; 2440 if (!xml11Version) { 2441 validNCName = (prefix == null || XMLChar.isValidNCName(prefix)) 2442 && XMLChar.isValidNCName(local); 2443 } 2444 else { 2445 validNCName = (prefix == null || XML11Char.isXML11ValidNCName(prefix)) 2446 && XML11Char.isXML11ValidNCName(local); 2447 } 2448 2449 if (!validNCName) { 2450 String msg = 2452 DOMMessageFormatter.formatMessage( 2453 DOMMessageFormatter.DOM_DOMAIN, 2454 "INVALID_CHARACTER_ERR", 2455 null); 2456 throw new DOMException (DOMException.INVALID_CHARACTER_ERR, msg); 2457 } 2458 } 2459 2460 2464 boolean isXML11Version(){ 2465 return xml11Version; 2466 } 2467 2468 boolean isNormalizeDocRequired(){ 2469 return true; 2472 } 2473 2474 boolean isXMLVersionChanged(){ 2477 return xmlVersionChanged ; 2478 } 2479 2486 protected void setUserData(NodeImpl n, Object data) { 2487 setUserData(n, "XERCES1DOMUSERDATA", data, null); 2488 } 2489 2490 2494 protected Object getUserData(NodeImpl n) { 2495 return getUserData(n, "XERCES1DOMUSERDATA"); 2496 } 2497 2498 2499 2501 protected void addEventListener(NodeImpl node, String type, 2502 EventListener listener, 2503 boolean useCapture) { 2504 } 2506 2507 protected void removeEventListener(NodeImpl node, String type, 2508 EventListener listener, 2509 boolean useCapture) { 2510 } 2512 2513 protected void copyEventListeners(NodeImpl src, NodeImpl tgt) { 2514 } 2516 2517 protected boolean dispatchEvent(NodeImpl node, Event event) { 2518 return false; 2520 } 2521 2522 2524 2528 void replacedText(NodeImpl node) { 2529 } 2530 2531 2535 void deletedText(NodeImpl node, int offset, int count) { 2536 } 2537 2538 2542 void insertedText(NodeImpl node, int offset, int count) { 2543 } 2544 2545 2548 void modifyingCharacterData(NodeImpl node) { 2549 } 2550 2551 2554 void modifiedCharacterData(NodeImpl node, String oldvalue, String value) { 2555 } 2556 2557 2560 void insertingNode(NodeImpl node, boolean replace) { 2561 } 2562 2563 2566 void insertedNode(NodeImpl node, NodeImpl newInternal, boolean replace) { 2567 } 2568 2569 2572 void removingNode(NodeImpl node, NodeImpl oldChild, boolean replace) { 2573 } 2574 2575 2578 void removedNode(NodeImpl node, boolean replace) { 2579 } 2580 2581 2584 void replacingNode(NodeImpl node) { 2585 } 2586 2587 2590 void replacedNode(NodeImpl node) { 2591 } 2592 2593 2596 void modifiedAttrValue(AttrImpl attr, String oldvalue) { 2597 } 2598 2599 2602 void setAttrNode(AttrImpl attr, AttrImpl previous) { 2603 } 2604 2605 2608 void removedAttrNode(AttrImpl attr, NodeImpl oldOwner, String name) { 2609 } 2610 2611 2614 void renamedAttrNode(Attr oldAt, Attr newAt) { 2615 } 2616 2617 2620 void renamedElement(Element oldEl, Element newEl) { 2621 } 2622 2623} | Popular Tags |