1 package com.icl.saxon.tree; 2 import com.icl.saxon.om.NodeInfo; 3 import com.icl.saxon.om.DocumentInfo; 4 import com.icl.saxon.om.NamePool; 5 import com.icl.saxon.om.Axis; 6 import com.icl.saxon.pattern.NodeTest; 7 import com.icl.saxon.pattern.NameTest; 8 import com.icl.saxon.expr.NodeSetExtent; 9 import com.icl.saxon.om.AxisEnumeration; 10 import com.icl.saxon.om.SingletonEnumeration; 11 import com.icl.saxon.om.EmptyEnumeration; 12 import com.icl.saxon.om.AbstractNode; 13 import com.icl.saxon.expr.XPathException; 14 import com.icl.saxon.output.Outputter; 15 16 import org.w3c.dom.*; 17 import javax.xml.transform.SourceLocator ; 18 import javax.xml.transform.TransformerException ; 19 20 21 28 29 abstract public class NodeImpl extends AbstractNode { 30 31 protected static NodeInfo[] emptyArray = new NodeInfo[0]; 32 33 protected ParentNodeImpl parent; 34 protected int index; 35 36 41 42 public void setSystemId(String uri) { 43 getParent().setSystemId(uri); 45 } 46 47 52 53 public boolean isSameNode(NodeInfo other) { 54 return this==other; 56 } 57 58 61 62 public int getNameCode() { 63 return -1; 65 } 66 67 71 72 public int getFingerprint() { 73 int nameCode = getNameCode(); 74 if (nameCode==-1) return -1; 75 return nameCode & 0xfffff; 76 } 77 78 83 84 public String generateId() { 85 return "" + NODE_LETTER[getNodeType()] + 86 getSequenceNumber(); 87 } 88 89 92 93 public Node getOriginatingNode() { 94 return this; 95 } 96 97 100 101 public String getSystemId() { 102 return parent.getSystemId(); 103 } 104 105 108 109 public String getBaseURI() { 110 return parent.getBaseURI(); 111 } 112 113 120 121 protected long getSequenceNumber() { 122 NodeImpl prev = this; 123 for (int i=0;; i++) { 124 if (prev instanceof ParentNodeImpl) { 125 return prev.getSequenceNumber() + 0x10000 + i; 126 } 128 prev = prev.getPreviousInDocument(); 129 } 130 131 } 132 133 141 142 public final int compareOrder(NodeInfo other) { 143 long a = getSequenceNumber(); 144 long b = ((NodeImpl)other).getSequenceNumber(); 145 if (a<b) return -1; 146 if (a>b) return +1; 147 return 0; 148 } 149 150 153 154 public NamePool getNamePool() { 155 return getDocumentRoot().getNamePool(); 156 } 157 158 162 163 public String getPrefix() { 164 int nameCode = getNameCode(); 165 if (nameCode == -1) return ""; 166 if ((nameCode>>20 & 0xff) == 0) return ""; 167 return getNamePool().getPrefix(nameCode); 168 } 169 170 176 177 public String getURI() { 178 int nameCode = getNameCode(); 179 if (nameCode == -1) return null; 180 return getNamePool().getURI(nameCode); 181 } 182 183 189 190 195 196 202 203 public String getDisplayName() { 204 int nameCode = getNameCode(); 205 if (nameCode == -1) return ""; 206 return getNamePool().getDisplayName(nameCode); 207 } 208 209 214 215 public String getLocalName() { 216 int nameCode = getNameCode(); 217 if (nameCode == -1) return ""; 218 return getNamePool().getLocalName(nameCode); 219 } 220 221 224 225 public int getLineNumber() { 226 return parent.getLineNumber(); 227 } 228 229 233 234 238 239 243 244 248 252 253 public final NodeInfo getParent() { 254 return parent; 255 } 256 257 261 262 266 271 272 public Node getPreviousSibling() { 273 return parent.getNthChild(index-1); 274 } 275 276 277 282 283 public Node getNextSibling() { 284 return parent.getNthChild(index+1); 285 } 286 287 291 292 public Node getFirstChild() { 293 return null; 294 } 295 296 300 301 public Node getLastChild() { 302 return null; 303 } 304 305 313 314 public AxisEnumeration getEnumeration( 315 byte axisNumber, 316 NodeTest nodeTest) { 317 318 switch (axisNumber) { 319 case Axis.ANCESTOR: 320 return new AncestorEnumeration(this, nodeTest, false); 321 322 case Axis.ANCESTOR_OR_SELF: 323 return new AncestorEnumeration(this, nodeTest, true); 324 325 case Axis.ATTRIBUTE: 326 if (this.getNodeType()!=ELEMENT) return EmptyEnumeration.getInstance(); 327 return new AttributeEnumeration(this, nodeTest); 328 329 case Axis.CHILD: 330 if (this instanceof ParentNodeImpl) { 331 return ((ParentNodeImpl)this).enumerateChildren(nodeTest); 332 } else { 333 return EmptyEnumeration.getInstance(); 334 } 335 336 case Axis.DESCENDANT: 337 if (getNodeType()==ROOT && 338 nodeTest instanceof NameTest && 339 nodeTest.getNodeType()==ELEMENT) { 340 return ((DocumentImpl)this).getAllElements( 341 ((NameTest)nodeTest).getFingerprint()); 342 } else if (hasChildNodes()) { 343 return new DescendantEnumeration(this, nodeTest, false); 344 } else { 345 return EmptyEnumeration.getInstance(); 346 } 347 348 case Axis.DESCENDANT_OR_SELF: 349 return new DescendantEnumeration(this, nodeTest, true); 350 351 case Axis.FOLLOWING: 352 return new FollowingEnumeration(this, nodeTest); 353 354 case Axis.FOLLOWING_SIBLING: 355 return new FollowingSiblingEnumeration(this, nodeTest); 356 357 case Axis.NAMESPACE: 358 if (this.getNodeType()!=ELEMENT) return EmptyEnumeration.getInstance(); 359 return new NamespaceEnumeration(this, nodeTest); 360 361 case Axis.PARENT: 362 NodeInfo parent = (NodeInfo)getParentNode(); 363 if (parent==null) return EmptyEnumeration.getInstance(); 364 if (nodeTest.matches(parent)) return new SingletonEnumeration(parent); 365 return EmptyEnumeration.getInstance(); 366 367 case Axis.PRECEDING: 368 return new PrecedingEnumeration(this, nodeTest); 369 370 case Axis.PRECEDING_SIBLING: 371 return new PrecedingSiblingEnumeration(this, nodeTest); 372 373 case Axis.SELF: 374 if (nodeTest.matches(this)) return new SingletonEnumeration(this); 375 return EmptyEnumeration.getInstance(); 376 377 case Axis.PRECEDING_OR_ANCESTOR: 378 return new PrecedingOrAncestorEnumeration(this, nodeTest); 379 380 default: 381 throw new IllegalArgumentException ("Unknown axis number " + axisNumber); 382 } 383 } 384 385 391 392 public boolean hasAttributes() { 393 return false; 394 } 395 396 404 405 public String getAttributeValue( String uri, String localName ) { 406 return null; 407 } 408 409 417 418 422 427 428 public String getAttributeValue(int fingerprint) { 429 return null; 430 } 431 432 438 439 public Element getDocumentElement() { 440 return ((DocumentImpl)getDocumentRoot()).getDocumentElement(); 441 442 } 443 444 448 449 public DocumentInfo getDocumentRoot() { 450 return getParent().getDocumentRoot(); 451 } 452 453 459 460 public NodeImpl getNextInDocument(NodeImpl anchor) { 461 465 NodeImpl next = (NodeImpl)getFirstChild(); 466 if (next!=null) return next; 467 if (this==anchor) return null; 468 next = (NodeImpl)getNextSibling(); 469 if (next!=null) return next; 470 NodeImpl parent = this; 471 while (true) { 472 parent = (NodeImpl)parent.getParent(); 473 if (parent==null) return null; 474 if (parent==anchor) return null; 475 next = (NodeImpl)parent.getNextSibling(); 476 if (next!=null) return next; 477 } 478 } 479 480 481 485 486 public NodeImpl getPreviousInDocument() { 487 488 493 NodeImpl prev = (NodeImpl)getPreviousSibling(); 494 if (prev!=null) { 495 return ((NodeImpl)prev).getLastDescendantOrSelf(); 496 } 497 return (NodeImpl)getParentNode(); 498 } 499 500 private NodeImpl getLastDescendantOrSelf() { 501 NodeImpl last = (NodeImpl)getLastChild(); 502 if (last==null) return this; 503 return last.getLastDescendantOrSelf(); 504 } 505 506 513 514 public void outputNamespaceNodes(Outputter out, boolean includeAncestors) 515 throws TransformerException 516 {} 517 518 524 525 public void removeNode() { 526 parent.removeChild(index); 527 } 528 529 530 532 535 536 541 546 547 public NodeList getChildNodes() { 548 try { 549 return new NodeSetExtent(EmptyEnumeration.getInstance(), null); 550 } catch (XPathException err) { 551 return null; 552 } 553 } 554 555 559 560 public NamedNodeMap getAttributes() { 561 return null; 563 } 564 565 566 567 572 573 public boolean hasChildNodes() { 574 return getFirstChild() != null; 575 } 576 577 } 578 579 | Popular Tags |