| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr.instruct; |
| 9 |
|
| 10 |
import net.sf.saxon.event.PipelineConfiguration; |
| 11 |
import net.sf.saxon.expr.*; |
| 12 |
import net.sf.saxon.expr.parser.*; |
| 13 |
import net.sf.saxon.om.*; |
| 14 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 15 |
import net.sf.saxon.trans.*; |
| 16 |
import net.sf.saxon.trans.rules.RuleManager; |
| 17 |
import net.sf.saxon.tree.iter.EmptyIterator; |
| 18 |
|
| 19 |
import java.util.ArrayList; |
| 20 |
import java.util.List; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public class ApplyTemplates extends Instruction implements ITemplateCall, ComponentInvocation { |
| 28 |
|
| 29 |
private Operand selectOp; |
| 30 |
private WithParam[] actualParams; |
| 31 |
private WithParam[] tunnelParams; |
| 32 |
|
| 33 |
protected boolean useCurrentMode = false; |
| 34 |
protected boolean useTailRecursion = false; |
| 35 |
protected Mode mode; |
| 36 |
protected boolean implicitSelect; |
| 37 |
protected boolean inStreamableConstruct = false; |
| 38 |
protected RuleManager ruleManager; |
| 39 |
private int bindingSlot = -1; |
| 40 |
|
| 41 |
protected ApplyTemplates() { |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public ApplyTemplates( Expression select, |
| 56 |
boolean useCurrentMode, |
| 57 |
boolean useTailRecursion, |
| 58 |
boolean implicitSelect, |
| 59 |
boolean inStreamableConstruct, |
| 60 |
Mode mode, |
| 61 |
RuleManager ruleManager) { |
| 62 |
|
| 63 |
selectOp = new Operand(this, select, OperandRole.SINGLE_ATOMIC); |
| 64 |
init(select, useCurrentMode, useTailRecursion, mode); |
| 65 |
this.implicitSelect = implicitSelect; |
| 66 |
this.inStreamableConstruct = inStreamableConstruct; |
| 67 |
this.ruleManager = ruleManager; |
| 68 |
} |
| 69 |
|
| 70 |
protected void init(Expression select, |
| 71 |
boolean useCurrentMode, |
| 72 |
boolean useTailRecursion, |
| 73 |
Mode mode) { |
| 74 |
this.setSelect(select); |
| 75 |
this.useCurrentMode = useCurrentMode; |
| 76 |
this.useTailRecursion = useTailRecursion; |
| 77 |
this.mode = mode; |
| 78 |
adoptChildExpression(select); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public void setMode(SimpleMode target) { |
| 88 |
this.mode = target; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public WithParam[] getActualParams() { |
| 99 |
return actualParams; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public WithParam[] getTunnelParams() { |
| 110 |
return tunnelParams; |
| 111 |
} |
| 112 |
|
| 113 |
public void setActualParams(WithParam[] params) { |
| 114 |
actualParams = params; |
| 115 |
} |
| 116 |
|
| 117 |
public void setTunnelParams(WithParam[] params) { |
| 118 |
tunnelParams = params; |
| 119 |
} |
| 120 |
|
| 121 |
@Override |
| 122 |
public Iterable<Operand> operands() { |
| 123 |
List<Operand> operanda = new ArrayList<>(); |
| 124 |
operanda.add(selectOp); |
| 125 |
WithParam.gatherOperands(this, getActualParams(), operanda); |
| 126 |
WithParam.gatherOperands(this, getTunnelParams(), operanda); |
| 127 |
return operanda; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public int getInstructionNameCode() { |
| 135 |
return StandardNames.XSL_APPLY_TEMPLATES; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public int getImplementationMethod() { |
| 144 |
return super.getImplementationMethod() | Expression.WATCH_METHOD; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public Expression simplify() throws XPathException { |
| 160 |
WithParam.simplify(getActualParams()); |
| 161 |
WithParam.simplify(getTunnelParams()); |
| 162 |
setSelect(getSelect().simplify()); |
| 163 |
return this; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 168 |
WithParam.typeCheck(actualParams, visitor, contextInfo); |
| 169 |
WithParam.typeCheck(tunnelParams, visitor, contextInfo); |
| 170 |
try { |
| 171 |
selectOp.typeCheck(visitor, contextInfo); |
| 172 |
} catch (XPathException e) { |
| 173 |
if (implicitSelect) { |
| 174 |
String code = e.getErrorCodeLocalPart(); |
| 175 |
if ("XPTY0020".equals(code) || "XPTY0019".equals(code)) { |
| 176 |
XPathException err = new XPathException("Cannot apply-templates to child nodes when the context item is an atomic value"); |
| 177 |
err.setErrorCode("XTTE0510"); |
| 178 |
err.setIsTypeError(true); |
| 179 |
throw err; |
| 180 |
} else if ("XPDY0002".equals(code)) { |
| 181 |
XPathException err = new XPathException("Cannot apply-templates to child nodes when the context item is absent"); |
| 182 |
err.setErrorCode("XTTE0510"); |
| 183 |
err.setIsTypeError(true); |
| 184 |
throw err; |
| 185 |
} |
| 186 |
} |
| 187 |
throw e; |
| 188 |
} |
| 189 |
adoptChildExpression(getSelect()); |
| 190 |
if (Literal.isEmptySequence(getSelect())) { |
| 191 |
return getSelect(); |
| 192 |
} |
| 193 |
return this; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 198 |
WithParam.optimize(visitor, actualParams, contextInfo); |
| 199 |
WithParam.optimize(visitor, tunnelParams, contextInfo); |
| 200 |
selectOp.typeCheck(visitor, contextInfo); |
| 201 |
selectOp.optimize(visitor, contextInfo); |
| 202 |
if (Literal.isEmptySequence(getSelect())) { |
| 203 |
return getSelect(); |
| 204 |
} |
| 205 |
|
| 206 |
return this; |
| 207 |
} |
| 208 |
|
| 209 |
public int getIntrinsicDependencies() { |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
return super.getIntrinsicDependencies() | (useCurrentMode ? StaticProperty.DEPENDS_ON_CURRENT_ITEM : 0); |
| 214 |
} |
| 215 |
|
| 216 |
public RuleManager getRuleManager() { |
| 217 |
return ruleManager; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
public Expression copy(RebindingMap rebindings) { |
| 231 |
ApplyTemplates a2 = new ApplyTemplates( |
| 232 |
getSelect().copy(rebindings), useCurrentMode, useTailRecursion, implicitSelect, inStreamableConstruct, mode, ruleManager); |
| 233 |
a2.setActualParams(WithParam.copy(a2, getActualParams(), rebindings)); |
| 234 |
a2.setTunnelParams(WithParam.copy(a2, getTunnelParams(), rebindings)); |
| 235 |
ExpressionTool.copyLocationInfo(this, a2); |
| 236 |
a2.ruleManager = ruleManager; |
| 237 |
return a2; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
public final boolean mayCreateNewNodes() { |
| 247 |
return true; |
| 248 |
} |
| 249 |
|
| 250 |
public void process(XPathContext context) throws XPathException { |
| 251 |
apply(context, false); |
| 252 |
} |
| 253 |
|
| 254 |
public TailCall processLeavingTail(XPathContext context) throws XPathException { |
| 255 |
return apply(context, useTailRecursion); |
| 256 |
} |
| 257 |
|
| 258 |
protected TailCall apply(XPathContext context, boolean returnTailCall) throws XPathException { |
| 259 |
|
| 260 |
Component.M targetMode = getTargetMode(context); |
| 261 |
Mode thisMode = targetMode.getActor(); |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
ParameterSet params = assembleParams(context, getActualParams()); |
| 266 |
ParameterSet tunnels = assembleTunnelParams(context, getTunnelParams()); |
| 267 |
|
| 268 |
if (returnTailCall) { |
| 269 |
XPathContextMajor c2 = context.newContext(); |
| 270 |
c2.setOrigin(this); |
| 271 |
return new ApplyTemplatesPackage( |
| 272 |
ExpressionTool.lazyEvaluate(getSelect(), context, false), |
| 273 |
targetMode, params, tunnels, c2, getLocation()); |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
SequenceIterator iter = getSelect().iterate(context); |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
if (iter instanceof EmptyIterator) { |
| 283 |
return null; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
XPathContextMajor c2 = context.newContext(); |
| 289 |
c2.trackFocus(iter); |
| 290 |
c2.setCurrentMode(targetMode); |
| 291 |
c2.setOrigin(this); |
| 292 |
c2.setCurrentComponent(targetMode); |
| 293 |
if (inStreamableConstruct) { |
| 294 |
c2.setCurrentGroupIterator(null); |
| 295 |
} |
| 296 |
PipelineConfiguration pipe = c2.getReceiver().getPipelineConfiguration(); |
| 297 |
pipe.setXPathContext(c2); |
| 298 |
|
| 299 |
try { |
| 300 |
TailCall tc = thisMode.applyTemplates(params, tunnels, c2, getLocation()); |
| 301 |
while (tc != null) { |
| 302 |
tc = tc.processLeavingTail(); |
| 303 |
} |
| 304 |
} catch (StackOverflowError e) { |
| 305 |
XPathException err = new XPathException("Too many nested apply-templates calls. The stylesheet may be looping."); |
| 306 |
err.setErrorCode(SaxonErrorCode.SXLM0001); |
| 307 |
err.setLocation(getLocation()); |
| 308 |
err.setXPathContext(context); |
| 309 |
throw err; |
| 310 |
} |
| 311 |
pipe.setXPathContext(context); |
| 312 |
return null; |
| 313 |
|
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
public Component.M getTargetMode(XPathContext context) { |
| 324 |
Component.M targetMode; |
| 325 |
if (useCurrentMode) { |
| 326 |
targetMode = context.getCurrentMode(); |
| 327 |
} else { |
| 328 |
if (bindingSlot >= 0) { |
| 329 |
targetMode = (Component.M)context.getTargetComponent(bindingSlot); |
| 330 |
if (targetMode.getVisibility() == Visibility.ABSTRACT) { |
| 331 |
throw new AssertionError("Modes cannot be abstract"); |
| 332 |
} |
| 333 |
} else { |
| 334 |
|
| 335 |
targetMode = mode.getDeclaringComponent(); |
| 336 |
} |
| 337 |
} |
| 338 |
return targetMode; |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
public Expression getSelectExpression() { |
| 349 |
return getSelect(); |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
public boolean isImplicitSelect() { |
| 359 |
return implicitSelect; |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
public boolean useTailRecursion() { |
| 369 |
return useTailRecursion; |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
public boolean usesCurrentMode() { |
| 379 |
return useCurrentMode; |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
public Mode getMode() { |
| 389 |
return mode; |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
public Component getFixedTarget() { |
| 399 |
return mode.getDeclaringComponent(); |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
public SymbolicName getSymbolicName() { |
| 408 |
return mode==null ? null : mode.getSymbolicName(); |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
public PathMap.PathMapNodeSet addToPathMap(PathMap pathMap, PathMap.PathMapNodeSet pathMapNodeSet) { |
| 432 |
|
| 433 |
PathMap.PathMapNodeSet result = super.addToPathMap(pathMap, pathMapNodeSet); |
| 434 |
result.setReturnable(false); |
| 435 |
return new PathMap.PathMapNodeSet(pathMap.makeNewRoot(this)); |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
public void export(ExpressionPresenter out) throws XPathException { |
| 447 |
|
| 448 |
out.startElement("applyT", this); |
| 449 |
if (mode != null && !mode.isUnnamedMode()) { |
| 450 |
out.emitAttribute("mode", mode.getModeName()); |
| 451 |
} |
| 452 |
String flags = ""; |
| 453 |
if (useCurrentMode) { |
| 454 |
flags = "c"; |
| 455 |
} |
| 456 |
if (useTailRecursion) { |
| 457 |
flags += "t"; |
| 458 |
} |
| 459 |
if (implicitSelect) { |
| 460 |
flags += "i"; |
| 461 |
} |
| 462 |
if (!flags.isEmpty()) { |
| 463 |
out.emitAttribute("flags", flags); |
| 464 |
} |
| 465 |
out.emitAttribute("bSlot", "" + getBindingSlot()); |
| 466 |
out.setChildRole("select"); |
| 467 |
getSelect().export(out); |
| 468 |
if (getActualParams().length != 0) { |
| 469 |
WithParam.exportParameters(getActualParams(), out, false); |
| 470 |
} |
| 471 |
if (getTunnelParams().length != 0) { |
| 472 |
WithParam.exportParameters(getTunnelParams(), out, true); |
| 473 |
} |
| 474 |
out.endElement(); |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
public Expression getSelect() { |
| 483 |
return selectOp.getChildExpression(); |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
public void setSelect(Expression select) { |
| 492 |
selectOp.setChildExpression(select); |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
public void setBindingSlot(int slot) { |
| 506 |
bindingSlot = slot; |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
public int getBindingSlot() { |
| 519 |
return bindingSlot; |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
protected static class ApplyTemplatesPackage implements TailCall { |
| 531 |
|
| 532 |
private Sequence<?> selectedItems; |
| 533 |
private Component.M targetMode; |
| 534 |
private ParameterSet params; |
| 535 |
private ParameterSet tunnelParams; |
| 536 |
private XPathContextMajor evaluationContext; |
| 537 |
private Location locationId; |
| 538 |
|
| 539 |
ApplyTemplatesPackage(Sequence<?> selectedItems, |
| 540 |
Component.M targetMode, |
| 541 |
ParameterSet params, |
| 542 |
ParameterSet tunnelParams, |
| 543 |
XPathContextMajor context, |
| 544 |
Location locationId) { |
| 545 |
this.selectedItems = selectedItems; |
| 546 |
this.targetMode = targetMode; |
| 547 |
this.params = params; |
| 548 |
this.tunnelParams = tunnelParams; |
| 549 |
evaluationContext = context; |
| 550 |
this.locationId = locationId; |
| 551 |
} |
| 552 |
|
| 553 |
public TailCall processLeavingTail() throws XPathException { |
| 554 |
evaluationContext.trackFocus(selectedItems.iterate()); |
| 555 |
evaluationContext.setCurrentMode(targetMode); |
| 556 |
evaluationContext.setCurrentComponent(targetMode); |
| 557 |
return targetMode.getActor().applyTemplates(params, tunnelParams, evaluationContext, locationId); |
| 558 |
} |
| 559 |
} |
| 560 |
|
| 561 |
@Override |
| 562 |
public String getStreamerName() { |
| 563 |
return "ApplyTemplates"; |
| 564 |
} |
| 565 |
|
| 566 |
} |
| 567 |
|