1 package com.icl.saxon.om; 2 import com.icl.saxon.om.Axis; 3 import com.icl.saxon.expr.NodeSetExtent; 4 import com.icl.saxon.expr.XPathException; 5 import com.icl.saxon.pattern.NodeTest; 6 import com.icl.saxon.pattern.NameTest; 7 import com.icl.saxon.pattern.AnyNodeTest; 8 import com.icl.saxon.pattern.NodeTypeTest; 9 import com.icl.saxon.output.Outputter; 10 import com.icl.saxon.tree.DOMExceptionImpl; 11 import com.icl.saxon.sort.LocalOrderComparer; 12 13 import org.w3c.dom.*; 14 15 import javax.xml.transform.SourceLocator ; 16 import javax.xml.transform.dom.DOMLocator ; 17 import javax.xml.transform.TransformerException ; 18 19 20 32 33 public abstract class AbstractNode implements Node, NodeInfo, SourceLocator , DOMLocator { 34 35 39 40 public static final char[] NODE_LETTER = 41 {'x', 'e', 'a', 't', 'x', 'x', 'x', 'p', 'c', 'r', 'x', 'x', 'x', 'n'}; 42 43 48 49 public abstract boolean isSameNode(NodeInfo other); 50 51 55 56 public abstract String generateId(); 57 58 61 62 public abstract String getSystemId(); 63 64 68 69 public abstract String getBaseURI(); 70 71 74 75 public Node getOriginatingNode() { 76 return this; 77 } 78 79 87 88 public abstract int compareOrder(NodeInfo other); 89 90 93 94 public abstract int getNameCode(); 95 96 99 100 public abstract int getFingerprint(); 101 102 108 109 public String getNodeName() { 110 switch (getNodeType()) { 111 case NodeInfo.ROOT: 112 return "#document"; 113 case NodeInfo.ELEMENT: 114 return getDisplayName(); 115 case NodeInfo.ATTRIBUTE: 116 return getDisplayName(); 117 case NodeInfo.TEXT: 118 return "#text"; 119 case NodeInfo.COMMENT: 120 return "#comment"; 121 case NodeInfo.PI: 122 return getLocalName(); 123 case NodeInfo.NAMESPACE: 124 return getLocalName(); 125 default: 126 return "#unknown"; 127 } 128 } 129 130 134 135 public abstract String getPrefix(); 136 137 143 144 public abstract String getURI(); 145 146 152 153 public String getDisplayName() { 154 String localName = getLocalName(); 155 if ("".equals(localName)) { 156 return ""; 157 } 158 String prefix = getPrefix(); 159 if ("".equals(prefix)) { 160 return localName; 161 } 162 return prefix + ":" + localName; 163 } 164 165 170 171 public abstract String getLocalName(); 172 173 178 179 public abstract boolean hasChildNodes(); 180 181 187 188 public abstract boolean hasAttributes(); 189 190 198 199 public abstract String getAttributeValue( String uri, String localName ); 200 201 206 207 public abstract String getAttributeValue(int fingerprint); 208 209 213 214 public int getLineNumber() { 215 return -1; 216 } 217 218 222 223 public int getColumnNumber() { 224 return -1; 225 } 226 227 231 232 public String getPublicId() { 233 return null; 234 } 235 236 244 245 public abstract AxisEnumeration getEnumeration( 246 byte axisNumber, 247 NodeTest nodeTest); 248 249 253 254 public abstract NodeInfo getParent(); 255 256 260 261 public DocumentInfo getDocumentRoot() { 262 NodeInfo parent = this; 263 while (parent.getNodeType() != NodeInfo.ROOT) { 264 parent = parent.getParent(); 265 } 266 return (DocumentInfo)parent; 267 } 268 269 273 274 public Node getParentNode() { 275 return (Node)getParent(); 276 } 277 278 283 284 public Node getPreviousSibling() { 285 AxisEnumeration prev = 286 getEnumeration(Axis.PRECEDING_SIBLING, AnyNodeTest.getInstance()); 287 if (prev.hasMoreElements()) { 288 return (Node)prev.nextElement(); 289 } else { 290 return null; 291 } 292 } 293 294 299 300 public Node getNextSibling() { 301 AxisEnumeration foll = 302 getEnumeration(Axis.FOLLOWING_SIBLING, AnyNodeTest.getInstance()); 303 if (foll.hasMoreElements()) { 304 return (Node)foll.nextElement(); 305 } else { 306 return null; 307 } 308 } 309 310 314 315 public Node getFirstChild() { 316 AxisEnumeration children = 317 getEnumeration(Axis.CHILD, AnyNodeTest.getInstance()); 318 if (children.hasMoreElements()) { 319 return (Node)children.nextElement(); 320 } else { 321 return null; 322 } 323 } 324 325 329 330 public Node getLastChild() { 331 AxisEnumeration children = 332 getEnumeration(Axis.CHILD, AnyNodeTest.getInstance()); 333 NodeInfo last = null; 334 while (children.hasMoreElements()) { 335 last = children.nextElement(); 336 } 337 return (Node)last; 338 } 339 340 341 347 348 public Element getDocumentElement() { 349 NodeInfo root = getDocumentRoot(); 350 AxisEnumeration children = 351 root.getEnumeration(Axis.CHILD, new NodeTypeTest(NodeInfo.ELEMENT)); 352 if (children.hasMoreElements()) { 353 return (Element)children.nextElement(); 354 } else { 355 return null; 356 } 357 358 } 359 360 365 366 public void copyStringValue(Outputter out) throws TransformerException { 367 out.writeContent(getStringValue()); } 369 370 377 378 public void outputNamespaceNodes(Outputter out, boolean includeAncestors) 379 throws TransformerException 380 {} 381 382 383 387 388 public String getNodeValue() { 389 switch (getNodeType()) { 390 case NodeInfo.ROOT: 391 case NodeInfo.ELEMENT: 392 return null; 393 case NodeInfo.ATTRIBUTE: 394 case NodeInfo.TEXT: 395 case NodeInfo.COMMENT: 396 case NodeInfo.PI: 397 case NodeInfo.NAMESPACE: 398 return getStringValue(); 399 default: 400 return null; 401 } 402 } 403 404 407 408 public void setNodeValue(String nodeValue) throws DOMException { 409 disallowUpdate(); 410 } 411 412 417 418 public NodeList getChildNodes() { 419 try { 420 return new NodeSetExtent( 421 getEnumeration(Axis.CHILD, AnyNodeTest.getInstance()), 422 LocalOrderComparer.getInstance()); 423 } catch (XPathException err) { 424 return null; 425 } 427 } 428 429 433 434 public NamedNodeMap getAttributes() { 435 if (getNodeType()==NodeInfo.ELEMENT) { 436 return new AttributeMap(); 437 } else { 438 return null; 439 } 440 } 441 442 445 446 public Document getOwnerDocument() { 447 return (Document)getDocumentRoot(); 448 } 449 450 460 461 public Node insertBefore(Node newChild, 462 Node refChild) 463 throws DOMException { 464 disallowUpdate(); 465 return null; 466 } 467 468 478 479 public Node replaceChild(Node newChild, 480 Node oldChild) 481 throws DOMException{ 482 disallowUpdate(); 483 return null; 484 } 485 486 494 495 public Node removeChild(Node oldChild) throws DOMException { 496 disallowUpdate(); 497 return null; 498 } 499 500 508 509 public Node appendChild(Node newChild) throws DOMException { 510 disallowUpdate(); 511 return null; 512 } 513 514 524 525 public Node cloneNode(boolean deep) { 526 return null; 528 } 529 530 539 540 public void normalize() { 541 } 543 544 558 559 public boolean isSupported(String feature, 560 String version) { 561 return feature.equalsIgnoreCase("xml"); 562 } 563 564 567 568 public boolean supports(String feature, 569 String version) { 570 return isSupported(feature, version); 571 } 572 573 589 590 public String getNamespaceURI() { 591 String uri = getURI(); 592 return (uri.equals("") ? null : uri); 593 } 594 595 598 599 public void setPrefix(String prefix) 600 throws DOMException { 601 disallowUpdate(); 602 } 603 604 607 608 protected void disallowUpdate() throws DOMException { 609 throw new UnsupportedOperationException ("The Saxon DOM cannot be updated"); 610 } 611 612 616 624 625 public DocumentType getDoctype() { 626 return null; 627 } 628 629 634 635 public DOMImplementation getImplementation() { 636 return new DOMImplementationImpl(); 637 } 638 639 643 644 public Element createElement(String tagName) throws DOMException { 645 disallowUpdate(); 646 return null; 647 } 648 649 654 655 public DocumentFragment createDocumentFragment() { 656 return null; 657 } 658 659 665 666 public Text createTextNode(String data) { 667 return null; 668 } 669 670 676 public Comment createComment(String data) { 677 return null; 678 } 679 680 689 690 public CDATASection createCDATASection(String data) throws DOMException { 691 disallowUpdate(); 692 return null; 693 } 694 695 707 708 public ProcessingInstruction createProcessingInstruction(String target, 709 String data) 710 throws DOMException { 711 disallowUpdate(); 712 return null; 713 } 714 715 727 728 public Attr createAttribute(String name) throws DOMException { 729 disallowUpdate(); 730 return null; 731 } 732 733 743 744 public EntityReference createEntityReference(String name) throws DOMException { 745 disallowUpdate(); 746 return null; 747 } 748 749 758 759 public NodeList getElementsByTagName(String tagname) { 760 763 AxisEnumeration allElements = 764 getEnumeration(Axis.DESCENDANT, AnyNodeTest.getInstance()); 765 NodeSetExtent nodes = new NodeSetExtent(LocalOrderComparer.getInstance()); 766 while(allElements.hasMoreElements()) { 767 NodeInfo next = allElements.nextElement(); 768 if (next.getNodeType()==ELEMENT) { 769 if (tagname.equals("*") || tagname.equals(next.getDisplayName())) { 770 nodes.append(next); 771 } 772 } 773 } 774 return nodes; 775 } 776 777 778 784 785 public Node importNode(Node importedNode, boolean deep) throws DOMException { 786 disallowUpdate(); 787 return null; 788 } 789 790 800 801 public Element createElementNS(String namespaceURI, 802 String qualifiedName) 803 throws DOMException 804 { 805 disallowUpdate(); 806 return null; 807 } 808 809 819 820 public Attr createAttributeNS(String namespaceURI, 821 String qualifiedName) 822 throws DOMException { 823 disallowUpdate(); 824 return null; 825 } 826 827 840 841 public NodeList getElementsByTagNameNS(String namespaceURI, String localName) { 842 845 AxisEnumeration allElements = 846 getEnumeration(Axis.DESCENDANT, AnyNodeTest.getInstance()); 847 NodeSetExtent nodes = new NodeSetExtent(LocalOrderComparer.getInstance()); 848 while(allElements.hasMoreElements()) { 849 NodeInfo next = allElements.nextElement(); 850 if (next.getNodeType()==ELEMENT) { 851 if ((namespaceURI.equals("*") || namespaceURI.equals(next.getURI())) && 852 (localName.equals("*") || localName.equals(next.getLocalName()))) { 853 nodes.append(next); 854 } 855 } 856 } 857 return nodes; 858 } 859 860 873 874 public Element getElementById(String elementId) { 875 return (Element)getDocumentRoot().selectID(elementId); 877 } 878 879 883 886 887 public String getTagName() { 888 return getDisplayName(); 889 } 890 891 898 899 public String getAttribute(String name) { 900 AxisEnumeration atts = getEnumeration(Axis.ATTRIBUTE, AnyNodeTest.getInstance()); 901 while (atts.hasMoreElements()) { 902 NodeInfo att = atts.nextElement(); 903 if (att.getDisplayName().equals(name)) { 904 String val = att.getStringValue(); 905 if (val==null) return ""; 906 return val; 907 } 908 } 909 return ""; 910 } 911 912 923 924 public Attr getAttributeNode(String name) { 925 AxisEnumeration atts = getEnumeration(Axis.ATTRIBUTE, AnyNodeTest.getInstance()); 926 while (atts.hasMoreElements()) { 927 NodeInfo att = atts.nextElement(); 928 if (att.getDisplayName().equals(name)) { 929 return (Attr)att; 930 } 931 } 932 return null; 933 } 934 935 940 941 public Attr setAttributeNode(Attr newAttr) throws DOMException { 942 disallowUpdate(); 943 return null; 944 } 945 946 951 952 public void removeAttribute(String oldAttr) throws DOMException { 953 disallowUpdate(); 954 } 955 956 961 962 public Attr removeAttributeNode(Attr oldAttr) throws DOMException { 963 disallowUpdate(); 964 return null; 965 } 966 967 968 977 978 public String getAttributeNS(String namespaceURI, String localName) { 979 String val = getAttributeValue(namespaceURI, localName); 980 if (val==null) return ""; 981 return val; 982 } 983 984 994 995 public void setAttributeNS(String namespaceURI, 996 String qualifiedName, 997 String value) 998 throws DOMException { 999 disallowUpdate(); 1000 } 1001 1002 1008 1009 public void removeAttributeNS(String namespaceURI, 1010 String localName) 1011 throws DOMException{ 1012 disallowUpdate(); 1013 } 1014 1015 1025 1026 public Attr getAttributeNodeNS(String namespaceURI, String localName) { 1027 int fingerprint = getDocumentRoot().getNamePool().getFingerprint(namespaceURI, localName); 1028 if (fingerprint==-1) return null; 1029 NameTest test = new NameTest(ATTRIBUTE, fingerprint); 1030 AxisEnumeration atts = getEnumeration(Axis.ATTRIBUTE, test); 1031 if (atts.hasMoreElements()) return (Attr)atts.nextElement(); 1032 return null; 1033 } 1034 1035 1046 1047 public Attr setAttributeNodeNS(Attr newAttr) 1048 throws DOMException{ 1049 disallowUpdate(); 1050 return null; 1051 } 1052 1053 1064 1065 public boolean hasAttribute(String name) { 1066 AxisEnumeration atts = getEnumeration(Axis.ATTRIBUTE, AnyNodeTest.getInstance()); 1067 while (atts.hasMoreElements()) { 1068 NodeInfo att = atts.nextElement(); 1069 if (att.getDisplayName().equals(name)) { 1070 return true; 1071 } 1072 } 1073 return false; 1074 } 1075 1076 1088 1089 public boolean hasAttributeNS(String namespaceURI, String localName) { 1090 return (getAttributeValue(namespaceURI, localName) != null); 1091 } 1092 1093 1094 1095 1096 1100 1101 1105 1106 public String getData() { 1107 return getStringValue(); 1108 } 1109 1110 1114 1115 public void setData(String data) throws DOMException { 1116 disallowUpdate(); 1117 } 1118 1119 1123 1124 public int getLength() { 1125 return getStringValue().length(); 1126 } 1127 1128 1141 1142 public String substringData(int offset, int count) throws DOMException { 1143 try { 1144 return getStringValue().substring(offset, offset+count); 1145 } catch (IndexOutOfBoundsException err2) { 1146 throw new DOMExceptionImpl(DOMException.INDEX_SIZE_ERR, 1147 "substringData: index out of bounds"); 1148 } 1149 } 1150 1151 1158 1159 public void appendData(String arg) throws DOMException { 1160 disallowUpdate(); 1161 } 1162 1163 1170 1171 public void insertData(int offset, String arg) throws DOMException { 1172 disallowUpdate(); 1173 } 1174 1175 1182 1183 public void deleteData(int offset, int count) throws DOMException { 1184 disallowUpdate(); 1185 } 1186 1187 1197 1198 public void replaceData(int offset, 1199 int count, 1200 String arg) throws DOMException { 1201 disallowUpdate(); 1202 } 1203 1204 1205 1212 1213 public Text splitText(int offset) 1214 throws DOMException { 1215 disallowUpdate(); 1216 return null; 1217 } 1218 1219 1223 1226 1227 public String getName() { 1228 return getDisplayName(); 1229 } 1230 1231 1235 1236 public String getValue() { 1237 return getStringValue(); 1238 } 1239 1240 1246 1247 public boolean getSpecified() { 1248 return true; 1249 } 1250 1251 1255 1256 public void setValue(String value) throws DOMException { 1257 disallowUpdate(); 1258 } 1259 1260 1265 1266 public Element getOwnerElement() { 1267 if (getNodeType()!=ATTRIBUTE) { 1268 throw new UnsupportedOperationException ( 1269 "This method is defined only on attribute nodes"); 1270 } 1271 return (Element)getParent(); 1272 } 1273 1274 1278 1281 1282 private class DOMImplementationImpl implements DOMImplementation { 1283 1284 1291 1292 public boolean hasFeature(String feature, String version) { 1293 return false; 1294 } 1295 1296 1297 1312 1313 public DocumentType createDocumentType(String qualifiedName, 1314 String publicId, 1315 String systemId) 1316 throws DOMException 1317 { 1318 disallowUpdate(); 1319 return null; 1320 } 1321 1322 1334 public Document createDocument(String namespaceURI, 1335 String qualifiedName, 1336 DocumentType doctype) 1337 throws DOMException 1338 { 1339 disallowUpdate(); 1340 return null; 1341 } 1342 1343 } 1345 1349 private class AttributeMap implements NamedNodeMap { 1350 1351 1354 1355 public Node getNamedItem(String name) { 1356 AxisEnumeration atts = 1357 getEnumeration(Axis.ATTRIBUTE, AnyNodeTest.getInstance()); 1358 while (atts.hasMoreElements()) { 1359 NodeInfo att = atts.nextElement(); 1360 if (name.equals(att.getDisplayName())) { 1361 return (Node)att; 1362 } 1363 } 1364 return null; 1365 } 1366 1367 1371 1372 public Node item(int index) { 1373 if (index<0) { 1374 return null; 1375 } 1376 int length = 0; 1377 AxisEnumeration atts = 1378 getEnumeration(Axis.ATTRIBUTE, AnyNodeTest.getInstance()); 1379 while (atts.hasMoreElements()) { 1380 NodeInfo att = atts.nextElement(); 1381 if (length==index) { 1382 return (Node)att; 1383 } 1384 length++; 1385 } 1386 return null; 1387 } 1388 1389 1392 1393 public int getLength() { 1394 int length = 0; 1395 AxisEnumeration atts = 1396 getEnumeration(Axis.ATTRIBUTE, AnyNodeTest.getInstance()); 1397 while (atts.hasMoreElements()) { 1398 atts.nextElement(); 1399 length++; 1400 } 1401 return length; 1402 } 1403 1404 1407 1408 public Node getNamedItemNS(String uri, String localName) { 1409 if (uri==null) uri=""; 1410 AxisEnumeration atts = 1411 getEnumeration(Axis.ATTRIBUTE, AnyNodeTest.getInstance()); 1412 while (atts.hasMoreElements()) { 1413 NodeInfo att = atts.nextElement(); 1414 if (uri.equals(att.getURI()) && localName.equals(att.getLocalName())) { 1415 return (Node)att; 1416 } 1417 } 1418 return null; 1419 } 1420 1421 1424 1425 public Node setNamedItem(Node arg) throws DOMException { 1426 disallowUpdate(); 1427 return null; 1428 } 1429 1430 1433 1434 public Node removeNamedItem(String name) throws DOMException { 1435 disallowUpdate(); 1436 return null; 1437 } 1438 1441 1442 public Node setNamedItemNS(Node arg) throws DOMException { 1443 disallowUpdate(); 1444 return null; 1445 } 1446 1447 1450 1451 public Node removeNamedItemNS(String uri, String localName) throws DOMException { 1452 disallowUpdate(); 1453 return null; 1454 } 1455 1456 } 1458} 1459 1460 | Popular Tags |