1 package net.sf.saxon.om; 2 3 import net.sf.saxon.Configuration; 4 import net.sf.saxon.value.Value; 5 import net.sf.saxon.event.Receiver; 6 import net.sf.saxon.pattern.AnyNodeTest; 7 import net.sf.saxon.pattern.NodeTest; 8 import net.sf.saxon.trans.XPathException; 9 10 15 16 public class VirtualCopy implements NodeInfo { 17 18 protected String baseURI; 19 protected int documentNumber; 20 protected NodeInfo original; 21 protected VirtualCopy parent; 22 protected NodeInfo root; 24 protected VirtualCopy(NodeInfo base) { 25 this.original = base; 26 } 27 28 public static VirtualCopy makeVirtualCopy(NodeInfo original, NodeInfo root) { 29 30 VirtualCopy vc; 31 while (original instanceof VirtualCopy) { 33 original = ((VirtualCopy)original).original; 34 } 35 while (root instanceof VirtualCopy) { 36 root = ((VirtualCopy)root).original; 37 } 38 if (original instanceof DocumentInfo) { 39 vc = new VirtualDocumentCopy((DocumentInfo)original); 40 } else { 41 vc = new VirtualCopy(original); 42 } 43 vc.root = root; 44 return vc; 45 } 46 47 public void setDocumentNumber(int documentNumber) { 48 this.documentNumber = documentNumber; 49 } 50 51 58 59 public int getNodeKind() { 60 return original.getNodeKind(); 61 } 62 63 73 74 public boolean isSameNodeInfo(NodeInfo other) { 75 return other instanceof VirtualCopy && 76 documentNumber == other.getDocumentNumber() && 77 original.isSameNodeInfo(((VirtualCopy)other).original); 78 79 80 } 81 82 90 91 public String getSystemId() { 92 return baseURI; 93 } 94 95 101 102 public String getBaseURI() { 103 return baseURI; 104 } 105 106 112 113 public int getLineNumber() { 114 return original.getLineNumber(); 115 } 116 117 128 129 public int compareOrder(NodeInfo other) { 130 return original.compareOrder(((VirtualCopy)other).original); 131 } 132 133 140 141 public String getStringValue() { 142 return original.getStringValue(); 143 } 144 145 149 150 public CharSequence getStringValueCS() { 151 return original.getStringValueCS(); 152 } 153 154 166 167 public int getNameCode() { 168 return original.getNameCode(); 169 } 170 171 180 181 public int getFingerprint() { 182 return original.getFingerprint(); 183 } 184 185 191 192 public String getLocalPart() { 193 return original.getLocalPart(); 194 } 195 196 204 205 public String getURI() { 206 return original.getURI(); 207 } 208 209 215 216 public String getPrefix() { 217 return original.getPrefix(); 218 } 219 220 227 228 public String getDisplayName() { 229 return original.getDisplayName(); 230 } 231 232 235 236 public Configuration getConfiguration() { 237 return original.getConfiguration(); 238 } 239 240 245 246 public NamePool getNamePool() { 247 return original.getNamePool(); 248 } 249 250 258 259 public int getTypeAnnotation() { 260 return original.getTypeAnnotation(); 261 } 262 263 268 269 public NodeInfo getParent() { 270 if (original.isSameNodeInfo(root)) { 271 return null; 272 } 273 if (parent == null) { 274 NodeInfo basep = original.getParent(); 275 if (basep == null) { 276 return null; 277 } 278 parent = new VirtualCopy(basep); 279 parent.setDocumentNumber(documentNumber); 280 } 281 return parent; 282 } 283 284 295 296 public AxisIterator iterateAxis(byte axisNumber) { 297 return iterateAxis(axisNumber, AnyNodeTest.getInstance()); 298 } 299 300 314 315 public AxisIterator iterateAxis(byte axisNumber, NodeTest nodeTest) { 316 VirtualCopy newParent = null; 317 if (axisNumber == Axis.CHILD || axisNumber == Axis.ATTRIBUTE || axisNumber == Axis.NAMESPACE) { 318 newParent = this; 319 } else if (axisNumber == Axis.SELF || axisNumber == Axis.PRECEDING_SIBLING || axisNumber == Axis.FOLLOWING_SIBLING) { 320 newParent = parent; 321 } 322 NodeInfo root; 323 if (Axis.isSubtreeAxis[axisNumber]) { 324 root = null; 325 } else { 326 root = this.root; 327 } 328 return new VirtualCopier(original.iterateAxis(axisNumber, nodeTest), newParent, root); 329 } 330 331 337 338 public String getAttributeValue(int fingerprint) { 339 return original.getAttributeValue(fingerprint); 340 } 341 342 348 349 public NodeInfo getRoot() { 350 NodeInfo n = this; 351 while (true) { 352 NodeInfo p = n.getParent(); 353 if (p == null) { 354 return n; 355 } 356 n = p; 357 } 358 } 359 360 367 368 public DocumentInfo getDocumentRoot() { 369 NodeInfo root = getRoot(); 370 if (root instanceof DocumentInfo) { 371 return (DocumentInfo)root; 372 } 373 return null; 374 } 375 376 383 384 public boolean hasChildNodes() { 385 return original.hasChildNodes(); 386 } 387 388 397 398 public String generateId() { 399 return "d" + documentNumber + original.generateId(); 400 } 401 402 406 407 public int getDocumentNumber() { 408 return documentNumber; 409 } 410 411 425 426 public void copy(Receiver out, int whichNamespaces, boolean copyAnnotations, int locationId) throws XPathException { 427 original.copy(out, whichNamespaces, copyAnnotations, locationId); 428 } 429 430 437 438 public void sendNamespaceDeclarations(Receiver out, boolean includeAncestors) throws XPathException { 439 original.sendNamespaceDeclarations(out, includeAncestors); 440 } 441 442 457 458 public int[] getDeclaredNamespaces(int[] buffer) { 459 return original.getDeclaredNamespaces(buffer); 460 } 461 462 472 public void setSystemId(String systemId) { 473 baseURI = systemId; 474 } 475 476 484 485 public SequenceIterator getTypedValue() throws XPathException { 486 return original.getTypedValue(); 487 } 488 489 499 500 public Value atomize() throws XPathException { 501 return original.atomize(); 502 } 503 504 511 512 private class VirtualCopier implements AxisIterator, AtomizableIterator { 513 514 private AxisIterator base; 515 private VirtualCopy parent; 516 private NodeInfo subtreeRoot; 517 private Item current; 518 519 public VirtualCopier(AxisIterator base, VirtualCopy parent, NodeInfo subtreeRoot) { 520 this.base = base; 521 this.parent = parent; 522 this.subtreeRoot = subtreeRoot; 523 } 524 525 535 536 public void setIsAtomizing(boolean atomizing) { 537 if (base instanceof AtomizableIterator) { 538 ((AtomizableIterator)base).setIsAtomizing(atomizing); 539 } 540 } 541 542 547 548 public Item next() { 549 Item next = base.next(); 550 551 if (next instanceof NodeInfo) { 552 if (subtreeRoot != null) { 553 if (!isAncestorOrSelf(subtreeRoot, ((NodeInfo)next))) { 557 return null; 558 } 559 } 560 VirtualCopy vc = VirtualCopy.makeVirtualCopy(((NodeInfo)next), root); 561 vc.parent = parent; 562 vc.baseURI = baseURI; 563 vc.documentNumber = documentNumber; 564 next = vc; 565 } 566 current = next; 567 return next; 568 } 569 570 576 577 public Item current() { 578 return current; 579 } 580 581 587 588 public int position() { 589 return base.position(); 590 } 591 592 598 599 public SequenceIterator getAnother() { 600 return new VirtualCopier((AxisIterator)base.getAnother(), parent, subtreeRoot); 601 } 602 603 612 613 public int getProperties() { 614 return 0; 615 } 616 617 620 621 private boolean isAncestorOrSelf(NodeInfo a, NodeInfo d) { 622 while (true) { 623 if (a.isSameNodeInfo(d)) { 624 return true; 625 } 626 d = d.getParent(); 627 if (d == null) { 628 return false; 629 } 630 } 631 } 632 633 } 634 635 } 636 637 638 | Popular Tags |