1 package net.sf.saxon.pull; 2 3 import net.sf.saxon.Configuration; 4 import net.sf.saxon.value.Value; 5 import net.sf.saxon.event.PipelineConfiguration; 6 import net.sf.saxon.event.Receiver; 7 import net.sf.saxon.event.SequenceOutputter; 8 import net.sf.saxon.expr.StackFrame; 9 import net.sf.saxon.expr.StaticProperty; 10 import net.sf.saxon.expr.XPathContext; 11 import net.sf.saxon.expr.XPathContextMajor; 12 import net.sf.saxon.instruct.ParentNodeConstructor; 13 import net.sf.saxon.om.*; 14 import net.sf.saxon.pattern.NodeTest; 15 import net.sf.saxon.tinytree.TinyBuilder; 16 import net.sf.saxon.trace.Location; 17 import net.sf.saxon.trans.XPathException; 18 import net.sf.saxon.trans.UncheckedXPathException; 19 20 30 public abstract class UnconstructedParent implements NodeInfo { 31 32 private ParentNodeConstructor instruction; 33 private XPathContextMajor savedXPathContext; 34 NodeInfo node = null; 35 36 public UnconstructedParent(ParentNodeConstructor instruction, XPathContext context) { 37 this.instruction = instruction; 38 savedXPathContext = context.newContext(); 39 savedXPathContext.setOriginatingConstructType(Location.LAZY_EVALUATION); 40 41 45 49 if ((instruction.getDependencies() & StaticProperty.DEPENDS_ON_LOCAL_VARIABLES) != 0) { 50 StackFrame localStackFrame = context.getStackFrame(); 51 ValueRepresentation[] local = localStackFrame.getStackFrameValues(); 52 if (local != null) { 53 ValueRepresentation[] savedStackFrame = new ValueRepresentation[local.length]; 54 System.arraycopy(local, 0, savedStackFrame, 0, local.length); 55 savedXPathContext.setStackFrame(localStackFrame.getStackFrameMap(), savedStackFrame); 56 } 57 } 58 59 SequenceIterator currentIterator = context.getCurrentIterator(); 61 if (currentIterator != null) { 62 Item contextItem = currentIterator.current(); 63 AxisIterator single = SingletonIterator.makeIterator(contextItem); 64 single.next(); 65 savedXPathContext.setCurrentIterator(single); 66 } 70 savedXPathContext.setReceiver(new SequenceOutputter()); 71 } 72 73 public XPathContext getXPathContext() { 74 return savedXPathContext; 75 } 76 77 public ParentNodeConstructor getInstruction() { 78 return instruction; 79 } 80 81 public PullProvider getPuller() { 82 if (node == null) { 83 VirtualTreeWalker walker = new VirtualTreeWalker(instruction, savedXPathContext); 84 walker.setPipelineConfiguration(savedXPathContext.getController().makePipelineConfiguration()); 85 walker.setNameCode(getNameCode()); 86 return walker; 87 } else { 88 return TreeWalker.makeTreeWalker(node); 89 } 90 } 91 92 97 void construct() throws XPathException { 98 PipelineConfiguration pipe = savedXPathContext.getController().makePipelineConfiguration(); 99 PullProvider puller = getPuller(); 100 puller.setPipelineConfiguration(pipe); 101 TinyBuilder builder = new TinyBuilder(); 102 builder.setPipelineConfiguration(pipe); 103 104 105 builder.open(); 106 new PullPushCopier(puller, builder).copy(); 107 builder.close(); 108 109 node = builder.getCurrentRoot(); 110 } 111 112 121 122 void tryToConstruct() { 123 try { 124 construct(); 125 } catch (XPathException err) { 126 throw new UncheckedXPathException(err); 127 } 128 } 129 130 140 141 public boolean isSameNodeInfo(NodeInfo other) { 142 if (this == other) { 143 return true; 144 } 145 if (other instanceof UnconstructedParent) { 146 return false; 147 } 148 if (node != null) { 149 return node.isSameNodeInfo(other); 150 } 151 return false; 152 } 153 154 162 163 public String getSystemId() { 164 if (node == null) { 165 tryToConstruct(); 166 } 167 return node.getSystemId(); 168 } 169 170 176 177 public String getBaseURI() { 178 if (node == null) { 179 tryToConstruct(); 180 } 181 return node.getBaseURI(); 182 } 183 184 190 191 public int getLineNumber() { 192 return -1; 193 } 194 195 206 207 public int compareOrder(NodeInfo other) { 208 if (node == null) { 209 tryToConstruct(); 210 } 211 return node.compareOrder(other); 212 } 213 214 221 222 public String getStringValue() { 223 return getStringValueCS().toString(); 224 } 225 226 235 236 public int getFingerprint() { 237 int nc = getNameCode(); 238 if (nc == -1) { 239 return -1; 240 } 241 return nc & NamePool.FP_MASK; 242 } 243 244 250 251 public String getLocalPart() { 252 return getNamePool().getLocalName(getNameCode()); 253 } 254 255 263 264 public String getURI() { 265 return getNamePool().getURI(getNameCode()); 266 } 267 268 275 276 public String getDisplayName() { 277 return getNamePool().getDisplayName(getNameCode()); 278 } 279 280 286 287 public String getPrefix() { 288 return getNamePool().getPrefix(getNameCode()); 289 } 290 291 294 295 public Configuration getConfiguration() { 296 return savedXPathContext.getController().getConfiguration(); 297 } 298 299 304 305 public NamePool getNamePool() { 306 return getConfiguration().getNamePool(); 307 } 308 309 317 318 public int getTypeAnnotation() { 319 return -1; 320 } 321 322 327 328 public NodeInfo getParent() { 329 return null; 330 } 331 332 343 344 public AxisIterator iterateAxis(byte axisNumber) { 345 if (node == null) { 346 tryToConstruct(); 347 } 348 return node.iterateAxis(axisNumber); 349 } 350 351 365 366 public AxisIterator iterateAxis(byte axisNumber, NodeTest nodeTest) { 367 if (node == null) { 368 tryToConstruct(); 369 } 370 return node.iterateAxis(axisNumber, nodeTest); 371 } 372 373 379 380 public String getAttributeValue(int fingerprint) { 381 if (node == null) { 382 tryToConstruct(); 383 } 384 return node.getAttributeValue(fingerprint); 385 } 386 387 393 394 public NodeInfo getRoot() { 395 return this; 396 } 397 398 405 406 public DocumentInfo getDocumentRoot() { 407 return null; 408 } 409 410 417 418 public boolean hasChildNodes() { 419 if (node == null) { 420 tryToConstruct(); 421 } 422 return node.hasChildNodes(); 423 } 424 425 434 435 public String generateId() { 436 if (node == null) { 437 tryToConstruct(); 438 } 439 return node.generateId(); 440 } 441 442 446 447 public int getDocumentNumber() { 448 if (node == null) { 449 tryToConstruct(); 450 } 451 return node.getDocumentNumber(); 452 } 453 454 470 471 public void copy(Receiver out, int whichNamespaces, boolean copyAnnotations, int locationId) throws XPathException { 472 if (node == null) { 473 if (whichNamespaces == NodeInfo.ALL_NAMESPACES && copyAnnotations) { 474 PullProvider pull = new VirtualTreeWalker(instruction, savedXPathContext); 475 PullPushCopier copier = new PullPushCopier(pull, out); 476 copier.copy(); 477 return; 478 } else { 479 construct(); 480 } 481 } 482 node.copy(out, whichNamespaces, copyAnnotations, locationId); 483 } 484 485 493 494 public void sendNamespaceDeclarations(Receiver out, boolean includeAncestors) throws XPathException { 495 if (node == null) { 496 try { 497 construct(); 498 } catch (UncheckedXPathException e) { 499 throw e.getXPathException(); 500 } 501 } 502 node.sendNamespaceDeclarations(out, includeAncestors); 503 } 504 505 520 521 public int[] getDeclaredNamespaces(int[] buffer) { 522 if (node == null) { 523 tryToConstruct(); 524 } 525 return node.getDeclaredNamespaces(buffer); 526 } 527 528 538 public void setSystemId(String systemId) { 539 } 541 542 546 547 public CharSequence getStringValueCS() { 548 if (node == null) { 549 try { 551 PullProvider puller = getPuller(); 552 puller.next(); return puller.getStringValue(); 554 } catch (XPathException e) { 555 throw new UncheckedXPathException(e); 556 } 557 } 558 return node.getStringValueCS(); 559 } 560 561 569 570 public SequenceIterator getTypedValue() throws XPathException { 571 if (node == null) { 572 construct(); 573 } 574 return node.getTypedValue(); 575 } 576 577 587 588 public Value atomize() throws XPathException { 589 if (node == null) { 590 construct(); 591 } 592 return node.atomize(); 593 } 594 595 596 } 597 598 599 | Popular Tags |