| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr; |
| 9 |
|
| 10 |
import net.sf.saxon.Controller; |
| 11 |
import net.sf.saxon.PreparedStylesheet; |
| 12 |
import net.sf.saxon.expr.instruct.Executable; |
| 13 |
import net.sf.saxon.expr.instruct.ParameterSet; |
| 14 |
import net.sf.saxon.expr.instruct.SlotManager; |
| 15 |
import net.sf.saxon.expr.sort.GroupIterator; |
| 16 |
import net.sf.saxon.lib.DelegatingErrorListener; |
| 17 |
import net.sf.saxon.lib.StandardURIResolver; |
| 18 |
import net.sf.saxon.lib.UnfailingErrorListener; |
| 19 |
import net.sf.saxon.om.*; |
| 20 |
import net.sf.saxon.regex.RegexIterator; |
| 21 |
import net.sf.saxon.trans.XPathException; |
| 22 |
import net.sf.saxon.trans.XsltController; |
| 23 |
import net.sf.saxon.trans.rules.Rule; |
| 24 |
import net.sf.saxon.trans.rules.RuleManager; |
| 25 |
import net.sf.saxon.tree.iter.SingletonIterator; |
| 26 |
import net.sf.saxon.tree.iter.UnfailingIterator; |
| 27 |
|
| 28 |
import javax.xml.transform.ErrorListener; |
| 29 |
import javax.xml.transform.URIResolver; |
| 30 |
import java.util.Arrays; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class XPathContextMajor extends XPathContextMinor { |
| 39 |
|
| 40 |
private ParameterSet localParameters; |
| 41 |
private ParameterSet tunnelParameters; |
| 42 |
private TailCallLoop.TailCallInfo tailCallInfo; |
| 43 |
private Component.M currentMode; |
| 44 |
private Rule currentTemplate; |
| 45 |
private GroupIterator currentGroupIterator; |
| 46 |
private GroupIterator currentMergeGroupIterator; |
| 47 |
private RegexIterator currentRegexIterator; |
| 48 |
private ContextOriginator origin; |
| 49 |
private ThreadManager threadManager = null; |
| 50 |
|
| 51 |
private URIResolver uriResolver; |
| 52 |
private UnfailingErrorListener errorListener; |
| 53 |
private Component currentComponent; |
| 54 |
XPathException currentException; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public XPathContextMajor(Controller controller) { |
| 65 |
this.controller = controller; |
| 66 |
stackFrame = StackFrame.EMPTY; |
| 67 |
origin = controller; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
private XPathContextMajor() { |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public XPathContextMajor(Item<?> item, Executable exec) { |
| 88 |
controller = exec instanceof PreparedStylesheet ? |
| 89 |
new XsltController(exec.getConfiguration(), (PreparedStylesheet)exec) : |
| 90 |
new Controller(exec.getConfiguration(), exec); |
| 91 |
if (item != null) { |
| 92 |
UnfailingIterator<?> iter = SingletonIterator.makeIterator(item); |
| 93 |
currentIterator = new FocusTrackingIterator<>(iter); |
| 94 |
try { |
| 95 |
currentIterator.next(); |
| 96 |
} catch (XPathException e) { |
| 97 |
|
| 98 |
} |
| 99 |
last = new LastValue(1); |
| 100 |
} |
| 101 |
origin = controller; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public XPathContextMajor newContext() { |
| 110 |
XPathContextMajor c = new XPathContextMajor(); |
| 111 |
c.controller = controller; |
| 112 |
c.currentIterator = currentIterator; |
| 113 |
c.stackFrame = stackFrame; |
| 114 |
c.localParameters = localParameters; |
| 115 |
c.tunnelParameters = tunnelParameters; |
| 116 |
c.last = last; |
| 117 |
c.currentReceiver = currentReceiver; |
| 118 |
c.currentDestination = currentDestination; |
| 119 |
c.currentMode = currentMode; |
| 120 |
c.currentTemplate = currentTemplate; |
| 121 |
c.currentRegexIterator = currentRegexIterator; |
| 122 |
c.currentGroupIterator = currentGroupIterator; |
| 123 |
c.currentMergeGroupIterator = currentMergeGroupIterator; |
| 124 |
c.currentException = currentException; |
| 125 |
c.caller = this; |
| 126 |
c.tailCallInfo = null; |
| 127 |
c.temporaryOutputState = temporaryOutputState; |
| 128 |
c.threadManager = threadManager; |
| 129 |
c.currentComponent = currentComponent; |
| 130 |
c.errorListener = errorListener; |
| 131 |
c.uriResolver = uriResolver; |
| 132 |
return c; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public static XPathContextMajor newContext(XPathContextMinor prev) { |
| 143 |
XPathContextMajor c = new XPathContextMajor(); |
| 144 |
XPathContext p = prev; |
| 145 |
while (!(p instanceof XPathContextMajor)) { |
| 146 |
p = p.getCaller(); |
| 147 |
} |
| 148 |
c.controller = p.getController(); |
| 149 |
c.currentIterator = prev.getCurrentIterator(); |
| 150 |
c.stackFrame = prev.getStackFrame(); |
| 151 |
c.localParameters = p.getLocalParameters(); |
| 152 |
c.tunnelParameters = p.getTunnelParameters(); |
| 153 |
c.last = prev.last; |
| 154 |
c.currentReceiver = prev.currentReceiver; |
| 155 |
c.currentDestination = prev.currentDestination; |
| 156 |
c.currentMode = p.getCurrentMode(); |
| 157 |
c.currentTemplate = p.getCurrentTemplateRule(); |
| 158 |
c.currentRegexIterator = p.getCurrentRegexIterator(); |
| 159 |
c.currentGroupIterator = p.getCurrentGroupIterator(); |
| 160 |
c.currentMergeGroupIterator = null; |
| 161 |
c.caller = prev; |
| 162 |
c.tailCallInfo = null; |
| 163 |
c.threadManager = ((XPathContextMajor) p).threadManager; |
| 164 |
c.currentComponent = ((XPathContextMajor) p).currentComponent; |
| 165 |
c.errorListener = ((XPathContextMajor) p).errorListener; |
| 166 |
c.uriResolver = ((XPathContextMajor) p).uriResolver; |
| 167 |
c.temporaryOutputState = prev.temporaryOutputState; |
| 168 |
return c; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public static XPathContextMajor newThreadContext(XPathContextMinor prev) { |
| 180 |
XPathContextMajor c = newContext(prev); |
| 181 |
c.stackFrame = prev.stackFrame.copy(); |
| 182 |
return c; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public abstract static class ThreadManager { |
| 191 |
public abstract void waitForChildThreads() throws XPathException; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
public ThreadManager getThreadManager() { |
| 200 |
return threadManager; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
public void createThreadManager() { |
| 209 |
threadManager = getConfiguration().makeThreadManager(); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
public void waitForChildThreads() throws XPathException { |
| 223 |
if (threadManager != null) { |
| 224 |
threadManager.waitForChildThreads(); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
public ParameterSet getLocalParameters() { |
| 236 |
if (localParameters == null) { |
| 237 |
localParameters = new ParameterSet(); |
| 238 |
} |
| 239 |
return localParameters; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
public void setLocalParameters(ParameterSet localParameters) { |
| 249 |
this.localParameters = localParameters; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
public ParameterSet getTunnelParameters() { |
| 259 |
return tunnelParameters; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
public void setTunnelParameters(ParameterSet tunnelParameters) { |
| 269 |
this.tunnelParameters = tunnelParameters; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
public void setOrigin(ContextOriginator expr) { |
| 280 |
origin = expr; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
public ContextOriginator getOrigin() { |
| 288 |
return origin; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
public void setStackFrame(SlotManager map, Sequence[] variables) { |
| 303 |
stackFrame = new StackFrame(map, variables); |
| 304 |
if (map != null && variables.length != map.getNumberOfVariables()) { |
| 305 |
if (variables.length > map.getNumberOfVariables()) { |
| 306 |
throw new IllegalStateException( |
| 307 |
"Attempting to set more local variables (" + variables.length + |
| 308 |
") than the stackframe can accommodate (" + map.getNumberOfVariables() + ")"); |
| 309 |
} |
| 310 |
stackFrame.slots = (Sequence<?>[])new Sequence[map.getNumberOfVariables()]; |
| 311 |
System.arraycopy(variables, 0, stackFrame.slots, 0, variables.length); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
public void resetStackFrameMap(SlotManager map, int numberOfParams) { |
| 324 |
stackFrame.map = map; |
| 325 |
if (stackFrame.slots.length != map.getNumberOfVariables()) { |
| 326 |
Sequence<?>[] v2 = |
| 327 |
(Sequence<?>[])new Sequence[map.getNumberOfVariables()]; |
| 328 |
System.arraycopy(stackFrame.slots, 0, v2, 0, numberOfParams); |
| 329 |
stackFrame.slots = v2; |
| 330 |
} else { |
| 331 |
|
| 332 |
Arrays.fill(stackFrame.slots, numberOfParams, stackFrame.slots.length, null); |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
public Sequence[] getAllVariableValues() { |
| 343 |
return stackFrame.getStackFrameValues(); |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
public void resetAllVariableValues(Sequence[] values) { |
| 354 |
stackFrame.setStackFrameValues(values); |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
public void resetParameterValues(Sequence[] values) { |
| 365 |
System.arraycopy(values, 0, stackFrame.slots, 0, values.length); |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
public void requestTailCall(TailCallLoop.TailCallInfo targetFn, Sequence<?>[] variables) { |
| 378 |
if (variables != null) { |
| 379 |
if (variables.length > stackFrame.slots.length) { |
| 380 |
stackFrame.slots = Arrays.copyOf(variables, variables.length); |
| 381 |
} else { |
| 382 |
System.arraycopy(variables, 0, stackFrame.slots, 0, variables.length); |
| 383 |
} |
| 384 |
} |
| 385 |
tailCallInfo = targetFn; |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
public TailCallLoop.TailCallInfo getTailCallInfo() { |
| 396 |
TailCallLoop.TailCallInfo fn = tailCallInfo; |
| 397 |
tailCallInfo = null; |
| 398 |
return fn; |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
public void openStackFrame(SlotManager map) { |
| 408 |
int numberOfSlots = map.getNumberOfVariables(); |
| 409 |
if (numberOfSlots == 0) { |
| 410 |
stackFrame = StackFrame.EMPTY; |
| 411 |
} else { |
| 412 |
stackFrame = new StackFrame(map, new Sequence[numberOfSlots]); |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
public void openStackFrame(int numberOfVariables) { |
| 425 |
stackFrame = new StackFrame(new SlotManager(numberOfVariables), |
| 426 |
SequenceTool.makeSequenceArray(numberOfVariables)); |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
public void setCurrentMode(Component.M mode) { |
| 436 |
this.currentMode = mode; |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
public Component.M getCurrentMode() { |
| 446 |
Component.M m = currentMode; |
| 447 |
if (m == null) { |
| 448 |
RuleManager rm = getController().getRuleManager(); |
| 449 |
if (rm != null) { |
| 450 |
return rm.getUnnamedMode().getDeclaringComponent(); |
| 451 |
} else { |
| 452 |
return null; |
| 453 |
} |
| 454 |
} else { |
| 455 |
return m; |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
public void setCurrentTemplateRule( Rule rule) { |
| 468 |
this.currentTemplate = rule; |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
public Rule getCurrentTemplateRule() { |
| 478 |
return currentTemplate; |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
public void setCurrentGroupIterator(GroupIterator iterator) { |
| 489 |
this.currentGroupIterator = iterator; |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
public GroupIterator getCurrentGroupIterator() { |
| 500 |
return currentGroupIterator; |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
public void setCurrentMergeGroupIterator(GroupIterator iterator) { |
| 511 |
this.currentMergeGroupIterator = iterator; |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
public GroupIterator getCurrentMergeGroupIterator() { |
| 522 |
return currentMergeGroupIterator; |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
public void setCurrentRegexIterator(RegexIterator currentRegexIterator) { |
| 533 |
this.currentRegexIterator = currentRegexIterator; |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
public RegexIterator getCurrentRegexIterator() { |
| 544 |
return currentRegexIterator; |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
public int useLocalParameter( |
| 559 |
StructuredQName paramName, int slotNumber, boolean isTunnel) throws XPathException { |
| 560 |
|
| 561 |
ParameterSet params = isTunnel ? getTunnelParameters() : localParameters; |
| 562 |
if (params == null) { |
| 563 |
return ParameterSet.NOT_SUPPLIED; |
| 564 |
} |
| 565 |
int index = params.getIndex(paramName); |
| 566 |
if (index < 0) { |
| 567 |
return ParameterSet.NOT_SUPPLIED; |
| 568 |
} |
| 569 |
Sequence<?> val = params.getValue(index); |
| 570 |
stackFrame.slots[slotNumber] = val; |
| 571 |
boolean checked = params.isTypeChecked(index); |
| 572 |
return checked ? ParameterSet.SUPPLIED_AND_CHECKED : ParameterSet.SUPPLIED; |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
public void setURIResolver(URIResolver resolver) { |
| 588 |
uriResolver = resolver; |
| 589 |
if (resolver instanceof StandardURIResolver) { |
| 590 |
((StandardURIResolver) resolver).setConfiguration(getConfiguration()); |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
public URIResolver getURIResolver() { |
| 604 |
return uriResolver == null ? controller.getURIResolver() : uriResolver; |
| 605 |
} |
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
public void setErrorListener(ErrorListener listener) { |
| 616 |
if (listener instanceof UnfailingErrorListener) { |
| 617 |
errorListener = (UnfailingErrorListener) listener; |
| 618 |
} else { |
| 619 |
errorListener = new DelegatingErrorListener(listener); |
| 620 |
} |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
public UnfailingErrorListener getErrorListener() { |
| 636 |
return errorListener == null ? controller.getErrorListener() : errorListener; |
| 637 |
} |
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
public void setCurrentException(XPathException exception) { |
| 646 |
currentException = exception; |
| 647 |
} |
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
public XPathException getCurrentException() { |
| 656 |
return currentException; |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
public Component getCurrentComponent() { |
| 664 |
return currentComponent; |
| 665 |
} |
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
public void setCurrentComponent(Component component) { |
| 675 |
|
| 676 |
currentComponent = component; |
| 677 |
} |
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
public Component getTargetComponent(int bindingSlot) { |
| 696 |
try { |
| 697 |
ComponentBinding binding = currentComponent.getComponentBindings().get(bindingSlot); |
| 698 |
return binding.getTarget(); |
| 699 |
} catch (NullPointerException e) { |
| 700 |
|
| 701 |
e.printStackTrace(); |
| 702 |
throw e; |
| 703 |
} catch (IndexOutOfBoundsException e) { |
| 704 |
|
| 705 |
|
| 706 |
e.printStackTrace(); |
| 707 |
throw e; |
| 708 |
} |
| 709 |
} |
| 710 |
} |
| 711 |
|