1 package net.sf.saxon.tree; 2 3 import net.sf.saxon.Configuration; 4 import net.sf.saxon.event.Receiver; 5 import net.sf.saxon.om.*; 6 import net.sf.saxon.pattern.AnyNodeTest; 7 import net.sf.saxon.pattern.NameTest; 8 import net.sf.saxon.pattern.NodeTest; 9 import net.sf.saxon.trans.XPathException; 10 import net.sf.saxon.type.Type; 11 import net.sf.saxon.value.UntypedAtomicValue; 12 import net.sf.saxon.value.Value; 13 14 import javax.xml.transform.SourceLocator ; 15 16 17 25 26 public abstract class NodeImpl implements NodeInfo, FingerprintedNode, SourceLocator { 27 28 protected ParentNodeImpl parent; 29 protected int index; 30 34 35 public static final char[] NODE_LETTER = 36 {'x', 'e', 'a', 't', 'x', 'x', 'x', 'p', 'c', 'r', 'x', 'x', 'x', 'n'}; 37 38 42 43 public CharSequence getStringValueCS() { 44 return getStringValue(); 45 } 46 47 50 51 public int getTypeAnnotation() { 52 return -1; 53 } 54 55 59 60 public int getColumnNumber() { 61 return -1; 62 } 63 64 68 69 public String getPublicId() { 70 return null; 71 } 72 73 74 78 79 public int getDocumentNumber() { 80 return getRoot().getDocumentNumber(); 81 } 82 83 88 89 public SequenceIterator getTypedValue() throws XPathException { 90 return SingletonIterator.makeIterator(new UntypedAtomicValue(getStringValue())); 91 } 92 93 103 104 public Value atomize() throws XPathException { 105 return new UntypedAtomicValue(getStringValue()); 106 } 107 108 113 114 public void setSystemId(String uri) { 115 getParent().setSystemId(uri); 117 } 118 119 125 126 public boolean isSameNodeInfo(NodeInfo other) { 127 return this == other; 129 } 130 131 134 135 public int getNameCode() { 136 return -1; 138 } 139 140 144 145 public int getFingerprint() { 146 int nameCode = getNameCode(); 147 if (nameCode == -1) { 148 return -1; 149 } 150 return nameCode & 0xfffff; 151 } 152 153 159 160 public String generateId() { 161 return getDocumentRoot().generateId() + NODE_LETTER[getNodeKind()] + 162 getSequenceNumber(); 163 } 164 165 168 169 public String getSystemId() { 170 return parent.getSystemId(); 171 } 172 173 176 177 public String getBaseURI() { 178 return parent.getBaseURI(); 179 } 180 181 188 189 protected long getSequenceNumber() { 190 NodeImpl prev = this; 191 for (int i = 0; ; i++) { 192 if (prev instanceof ParentNodeImpl) { 193 return prev.getSequenceNumber() + 0x10000 + i; 194 } 196 prev = prev.getPreviousInDocument(); 197 } 198 199 } 200 201 210 211 public final int compareOrder(NodeInfo other) { 212 long a = getSequenceNumber(); 213 long b = ((NodeImpl)other).getSequenceNumber(); 214 if (a < b) { 215 return -1; 216 } 217 if (a > b) { 218 return +1; 219 } 220 return 0; 221 } 222 223 226 227 public Configuration getConfiguration() { 228 return getDocumentRoot().getConfiguration(); 229 } 230 231 234 235 public NamePool getNamePool() { 236 return getDocumentRoot().getNamePool(); 237 } 238 239 244 245 public String getPrefix() { 246 int nameCode = getNameCode(); 247 if (nameCode == -1) { 248 return ""; 249 } 250 if ((nameCode >> 20 & 0xff) == 0) { 251 return ""; 252 } 253 return getNamePool().getPrefix(nameCode); 254 } 255 256 263 264 public String getURI() { 265 int nameCode = getNameCode(); 266 if (nameCode == -1) { 267 return ""; 268 } 269 return getNamePool().getURI(nameCode); 270 } 271 272 279 280 public String getDisplayName() { 281 int nameCode = getNameCode(); 282 if (nameCode == -1) { 283 return ""; 284 } 285 return getNamePool().getDisplayName(nameCode); 286 } 287 288 294 295 public String getLocalPart() { 296 int nameCode = getNameCode(); 297 if (nameCode == -1) { 298 return ""; 299 } 300 return getNamePool().getLocalName(nameCode); 301 } 302 303 306 307 public int getLineNumber() { 308 return parent.getLineNumber(); 309 } 310 311 316 317 public final NodeInfo getParent() { 318 return parent; 319 } 320 321 327 328 public NodeInfo getPreviousSibling() { 329 return parent.getNthChild(index - 1); 330 } 331 332 333 339 340 public NodeInfo getNextSibling() { 341 return parent.getNthChild(index + 1); 342 } 343 344 349 350 public NodeInfo getFirstChild() { 351 return null; 352 } 353 354 359 360 public NodeInfo getLastChild() { 361 return null; 362 } 363 364 370 371 public AxisIterator iterateAxis(byte axisNumber) { 372 if (axisNumber == Axis.CHILD) { 374 if (this instanceof ParentNodeImpl) { 375 return ((ParentNodeImpl)this).enumerateChildren(null); 376 } else { 377 return EmptyIterator.getInstance(); 378 } 379 } else { 380 return iterateAxis(axisNumber, AnyNodeTest.getInstance()); 381 } 382 } 383 384 391 392 public AxisIterator iterateAxis(byte axisNumber, NodeTest nodeTest) { 393 394 switch (axisNumber) { 395 case Axis.ANCESTOR: 396 return new AncestorEnumeration(this, nodeTest, false); 397 398 case Axis.ANCESTOR_OR_SELF: 399 return new AncestorEnumeration(this, nodeTest, true); 400 401 case Axis.ATTRIBUTE: 402 if (this.getNodeKind() != Type.ELEMENT) { 403 return EmptyIterator.getInstance(); 404 } 405 return new AttributeEnumeration(this, nodeTest); 406 407 case Axis.CHILD: 408 if (this instanceof ParentNodeImpl) { 409 return ((ParentNodeImpl)this).enumerateChildren(nodeTest); 410 } else { 411 return EmptyIterator.getInstance(); 412 } 413 414 case Axis.DESCENDANT: 415 if (getNodeKind() == Type.DOCUMENT && 416 nodeTest instanceof NameTest && 417 nodeTest.getPrimitiveType() == Type.ELEMENT) { 418 return ((DocumentImpl)this).getAllElements(nodeTest.getFingerprint()); 419 } else if (hasChildNodes()) { 420 return new DescendantEnumeration(this, nodeTest, false); 421 } else { 422 return EmptyIterator.getInstance(); 423 } 424 425 case Axis.DESCENDANT_OR_SELF: 426 return new DescendantEnumeration(this, nodeTest, true); 427 428 case Axis.FOLLOWING: 429 return new FollowingEnumeration(this, nodeTest); 430 431 case Axis.FOLLOWING_SIBLING: 432 return new FollowingSiblingEnumeration(this, nodeTest); 433 434 case Axis.NAMESPACE: 435 if (this.getNodeKind() != Type.ELEMENT) { 436 return EmptyIterator.getInstance(); 437 } 438 return new NamespaceIterator(this, nodeTest); 439 440 case Axis.PARENT: 441 NodeInfo parent = getParent(); 442 if (parent == null) { 443 return EmptyIterator.getInstance(); 444 } 445 if (nodeTest.matches(parent)) { 446 return SingletonIterator.makeIterator(parent); 447 } 448 return EmptyIterator.getInstance(); 449 450 case Axis.PRECEDING: 451 return new PrecedingEnumeration(this, nodeTest); 452 453 case Axis.PRECEDING_SIBLING: 454 return new PrecedingSiblingEnumeration(this, nodeTest); 455 456 case Axis.SELF: 457 if (nodeTest.matches(this)) { 458 return SingletonIterator.makeIterator(this); 459 } 460 return EmptyIterator.getInstance(); 461 462 case Axis.PRECEDING_OR_ANCESTOR: 463 return new PrecedingOrAncestorEnumeration(this, nodeTest); 464 465 default: 466 throw new IllegalArgumentException ("Unknown axis number " + axisNumber); 467 } 468 } 469 470 478 479 483 491 492 496 502 503 public String getAttributeValue(int fingerprint) { 504 return null; 505 } 506 507 512 513 public NodeInfo getRoot() { 514 return getDocumentRoot(); 515 } 516 517 522 523 public DocumentInfo getDocumentRoot() { 524 return getParent().getDocumentRoot(); 525 } 526 527 534 535 public NodeImpl getNextInDocument(NodeImpl anchor) { 536 540 NodeImpl next = (NodeImpl)getFirstChild(); 541 if (next != null) { 542 return next; 543 } 544 if (this == anchor) { 545 return null; 546 } 547 next = (NodeImpl)getNextSibling(); 548 if (next != null) { 549 return next; 550 } 551 NodeImpl parent = this; 552 while (true) { 553 parent = (NodeImpl)parent.getParent(); 554 if (parent == null) { 555 return null; 556 } 557 if (parent == anchor) { 558 return null; 559 } 560 next = (NodeImpl)parent.getNextSibling(); 561 if (next != null) { 562 return next; 563 } 564 } 565 } 566 567 568 573 574 public NodeImpl getPreviousInDocument() { 575 576 581 NodeImpl prev = (NodeImpl)getPreviousSibling(); 582 if (prev != null) { 583 return prev.getLastDescendantOrSelf(); 584 } 585 return (NodeImpl)getParent(); 586 } 587 588 private NodeImpl getLastDescendantOrSelf() { 589 NodeImpl last = (NodeImpl)getLastChild(); 590 if (last == null) { 591 return this; 592 } 593 return last.getLastDescendantOrSelf(); 594 } 595 596 603 604 public void sendNamespaceDeclarations(Receiver out, boolean includeAncestors) 605 throws XPathException { 606 } 607 608 623 624 public int[] getDeclaredNamespaces(int[] buffer) { 625 return null; 626 } 627 631 632 639 641 647 648 public boolean hasChildNodes() { 649 return getFirstChild() != null; 650 } 651 652 } 653 654 | Popular Tags |