| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.trans; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.Controller; |
| 12 |
import net.sf.saxon.PreparedStylesheet; |
| 13 |
import net.sf.saxon.event.*; |
| 14 |
import net.sf.saxon.expr.*; |
| 15 |
import net.sf.saxon.expr.accum.AccumulatorManager; |
| 16 |
import net.sf.saxon.expr.instruct.*; |
| 17 |
import net.sf.saxon.expr.parser.ExplicitLocation; |
| 18 |
import net.sf.saxon.lib.*; |
| 19 |
import net.sf.saxon.om.*; |
| 20 |
import net.sf.saxon.s9api.Destination; |
| 21 |
import net.sf.saxon.serialize.Emitter; |
| 22 |
import net.sf.saxon.serialize.MessageEmitter; |
| 23 |
import net.sf.saxon.serialize.PrincipalOutputGatekeeper; |
| 24 |
import net.sf.saxon.style.StylesheetPackage; |
| 25 |
import net.sf.saxon.tree.iter.EmptyIterator; |
| 26 |
import net.sf.saxon.tree.iter.ManualIterator; |
| 27 |
import net.sf.saxon.tree.iter.SingletonIterator; |
| 28 |
import net.sf.saxon.tree.wrapper.SpaceStrippedDocument; |
| 29 |
import net.sf.saxon.tree.wrapper.SpaceStrippedNode; |
| 30 |
import net.sf.saxon.tree.wrapper.TypeStrippedDocument; |
| 31 |
import org.xml.sax.SAXParseException; |
| 32 |
|
| 33 |
import javax.xml.transform.OutputKeys; |
| 34 |
import javax.xml.transform.Source; |
| 35 |
import javax.xml.transform.sax.SAXSource; |
| 36 |
import javax.xml.transform.stream.StreamSource; |
| 37 |
import java.util.*; |
| 38 |
import java.util.function.Supplier; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public class XsltController extends Controller { |
| 47 |
|
| 48 |
private final Map<StructuredQName, Integer> messageCounters = new HashMap<>(); |
| 49 |
private Receiver explicitMessageReceiver = null; |
| 50 |
private Supplier<Receiver> messageFactory = MessageEmitter::new; |
| 51 |
private boolean assertionsEnabled = true; |
| 52 |
private ResultDocumentResolver resultDocumentResolver; |
| 53 |
private HashSet<DocumentURI> allOutputDestinations; |
| 54 |
private Component.M initialMode = null; |
| 55 |
private Function initialFunction = null; |
| 56 |
private int recoveryPolicy; |
| 57 |
private Map<StructuredQName, Sequence<?>> initialTemplateParams; |
| 58 |
private Map<StructuredQName, Sequence<?>> initialTemplateTunnelParams; |
| 59 |
private Map<Long, Stack<AttributeSet>> attributeSetEvaluationStacks = new HashMap<>(); |
| 60 |
private AccumulatorManager accumulatorManager = new AccumulatorManager(); |
| 61 |
private PrincipalOutputGatekeeper gatekeeper = null; |
| 62 |
private Destination principalDestination; |
| 63 |
|
| 64 |
public XsltController(Configuration config, PreparedStylesheet pss) { |
| 65 |
super(config, pss); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public void reset() { |
| 90 |
super.reset(); |
| 91 |
Configuration config = getConfiguration(); |
| 92 |
|
| 93 |
setErrorListener(config.getErrorListener()); |
| 94 |
recoveryPolicy = config.getRecoveryPolicy(); |
| 95 |
validationMode = config.getSchemaValidationMode(); |
| 96 |
accumulatorManager = new AccumulatorManager(); |
| 97 |
if (errorListener instanceof StandardErrorListener) { |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
Logger ps = ((StandardErrorListener) errorListener).getLogger(); |
| 102 |
errorListener = ((StandardErrorListener) errorListener).makeAnother(executable.getHostLanguage()); |
| 103 |
((StandardErrorListener) errorListener).setLogger(ps); |
| 104 |
((StandardErrorListener) errorListener).setRecoveryPolicy(recoveryPolicy); |
| 105 |
} |
| 106 |
|
| 107 |
traceListener = null; |
| 108 |
TraceListener tracer; |
| 109 |
try { |
| 110 |
tracer = config.makeTraceListener(); |
| 111 |
} catch (XPathException err) { |
| 112 |
throw new IllegalStateException(err.getMessage()); |
| 113 |
} |
| 114 |
if (tracer != null) { |
| 115 |
addTraceListener(tracer); |
| 116 |
} |
| 117 |
|
| 118 |
setModel(config.getParseOptions().getModel()); |
| 119 |
|
| 120 |
globalContextItem = null; |
| 121 |
initialMode = null; |
| 122 |
clearPerTransformationData(); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
protected synchronized void clearPerTransformationData() { |
| 131 |
super.clearPerTransformationData(); |
| 132 |
principalResult = null; |
| 133 |
allOutputDestinations = null; |
| 134 |
if (messageCounters != null) { |
| 135 |
messageCounters.clear(); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public void setInitialMode(StructuredQName expandedModeName) throws XPathException { |
| 159 |
if (expandedModeName == null || expandedModeName.equals(Mode.UNNAMED_MODE_NAME)) { |
| 160 |
Mode initial = ((PreparedStylesheet) executable).getRuleManager().obtainMode(Mode.UNNAMED_MODE_NAME, true); |
| 161 |
initialMode = initial.getDeclaringComponent(); |
| 162 |
} else { |
| 163 |
StylesheetPackage topLevelPackage = (StylesheetPackage) executable.getTopLevelPackage(); |
| 164 |
if (expandedModeName.equals(Mode.DEFAULT_MODE_NAME)) { |
| 165 |
StructuredQName defaultModeName = topLevelPackage.getDefaultMode(); |
| 166 |
if (!expandedModeName.equals(defaultModeName)) { |
| 167 |
setInitialMode(defaultModeName); |
| 168 |
} |
| 169 |
} else { |
| 170 |
|
| 171 |
boolean declaredModes = topLevelPackage.isDeclaredModes(); |
| 172 |
SymbolicName sn = new SymbolicName(StandardNames.XSL_MODE, expandedModeName); |
| 173 |
Component.M c = (Component.M)topLevelPackage.getComponent(sn); |
| 174 |
if (c == null) { |
| 175 |
throw new XPathException("Requested initial mode " + expandedModeName + " is not defined in the stylesheet", "XTDE0045"); |
| 176 |
} |
| 177 |
if (!((PreparedStylesheet)executable).isEligibleInitialMode(c)) { |
| 178 |
|
| 179 |
throw new XPathException("Requested initial mode " + expandedModeName + " is private in the top-level package", "XTDE0045"); |
| 180 |
} |
| 181 |
initialMode = c; |
| 182 |
if (!declaredModes && initialMode.getActor().isEmpty() && !expandedModeName.equals(topLevelPackage.getDefaultMode())) { |
| 183 |
throw new XPathException("Requested initial mode " + expandedModeName + " contains no template rules", "XTDE0045"); |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public StructuredQName getInitialModeName() { |
| 197 |
return initialMode == null ? null : initialMode.getActor().getModeName(); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
public Mode getInitialMode() { |
| 209 |
if (initialMode == null) { |
| 210 |
StylesheetPackage top = (StylesheetPackage) executable.getTopLevelPackage(); |
| 211 |
StructuredQName defaultMode = top.getDefaultMode(); |
| 212 |
if (defaultMode == null) { |
| 213 |
defaultMode = Mode.UNNAMED_MODE_NAME; |
| 214 |
} |
| 215 |
Component.M c = (Component.M) top.getComponent(new SymbolicName(StandardNames.XSL_MODE, defaultMode)); |
| 216 |
initialMode = c; |
| 217 |
return c.getActor(); |
| 218 |
} else { |
| 219 |
return initialMode.getActor(); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
public AccumulatorManager getAccumulatorManager() { |
| 231 |
return accumulatorManager; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
public synchronized boolean checkUniqueOutputDestination( DocumentURI uri) { |
| 244 |
if (uri == null) { |
| 245 |
return true; |
| 246 |
} |
| 247 |
if (allOutputDestinations == null) { |
| 248 |
allOutputDestinations = new HashSet<>(20); |
| 249 |
} |
| 250 |
|
| 251 |
return !allOutputDestinations.contains(uri); |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
public void addUnavailableOutputDestination(DocumentURI uri) { |
| 262 |
if (allOutputDestinations == null) { |
| 263 |
allOutputDestinations = new HashSet<>(20); |
| 264 |
} |
| 265 |
allOutputDestinations.add(uri); |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
public void removeUnavailableOutputDestination(DocumentURI uri) { |
| 276 |
if (allOutputDestinations != null) { |
| 277 |
allOutputDestinations.remove(uri); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
public boolean isUnusedOutputDestination(DocumentURI uri) { |
| 293 |
return allOutputDestinations == null || !allOutputDestinations.contains(uri); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
public void setInitialTemplateParameters(Map<StructuredQName, Sequence<?>> params, boolean tunnel) { |
| 308 |
if (tunnel) { |
| 309 |
this.initialTemplateTunnelParams = params; |
| 310 |
} else { |
| 311 |
this.initialTemplateParams = params; |
| 312 |
} |
| 313 |
|
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
public Map<StructuredQName, Sequence<?>> getInitialTemplateParameters(boolean tunnel) { |
| 326 |
return tunnel ? initialTemplateTunnelParams : initialTemplateParams; |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
public void setMessageFactory(Supplier<Receiver> messageReceiverFactory) { |
| 340 |
this.messageFactory = messageReceiverFactory; |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
public void setMessageReceiverClassName(String name) { |
| 353 |
if (!name.equals(MessageEmitter.class.getName())) { |
| 354 |
this.messageFactory = () -> { |
| 355 |
try { |
| 356 |
Object messageReceiver = getConfiguration().getInstance(name, null); |
| 357 |
if (!(messageReceiver instanceof Receiver)) { |
| 358 |
throw new XPathException(name + " is not a Receiver"); |
| 359 |
} |
| 360 |
return (Receiver) messageReceiver; |
| 361 |
} catch (XPathException e) { |
| 362 |
throw new UncheckedXPathException(e); |
| 363 |
} |
| 364 |
}; |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
public Receiver makeMessageReceiver() throws XPathException { |
| 381 |
return messageFactory.get(); |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
public void setMessageEmitter(Receiver receiver) { |
| 430 |
if (getConfiguration().getBooleanProperty(Feature.ALLOW_MULTITHREADING)) { |
| 431 |
throw new IllegalStateException("XsltController#setMessageEmitter() is not supported for a configuration that allows multi-threading. Use setMessageFactory() instead"); |
| 432 |
} |
| 433 |
final Receiver messageReceiver = explicitMessageReceiver = receiver; |
| 434 |
receiver.setPipelineConfiguration(makePipelineConfiguration()); |
| 435 |
if (receiver instanceof Emitter && ((Emitter) receiver).getOutputProperties() == null) { |
| 436 |
try { |
| 437 |
Properties props = new Properties(); |
| 438 |
props.setProperty(OutputKeys.METHOD, "xml"); |
| 439 |
props.setProperty(OutputKeys.INDENT, "yes"); |
| 440 |
props.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); |
| 441 |
((Emitter) receiver).setOutputProperties(props); |
| 442 |
} catch (XPathException e) { |
| 443 |
|
| 444 |
} |
| 445 |
} |
| 446 |
setMessageFactory(() -> new ProxyReceiver(messageReceiver) { |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
@Override |
| 452 |
public void close() { |
| 453 |
|
| 454 |
} |
| 455 |
}); |
| 456 |
|
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
public Receiver getMessageEmitter() { |
| 471 |
return explicitMessageReceiver; |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
public void incrementMessageCounter(StructuredQName code) { |
| 484 |
synchronized(messageCounters) { |
| 485 |
Integer c = messageCounters.get(code); |
| 486 |
int n = c == null ? 1 : c + 1; |
| 487 |
messageCounters.put(code, n); |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
public Map<StructuredQName, Integer> getMessageCounters() { |
| 499 |
synchronized(messageCounters) { |
| 500 |
return new HashMap<>(messageCounters); |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
public void setRecoveryPolicy(int policy) { |
| 520 |
recoveryPolicy = policy; |
| 521 |
if (errorListener instanceof StandardErrorListener) { |
| 522 |
((StandardErrorListener) errorListener).setRecoveryPolicy(policy); |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
public int getRecoveryPolicy() { |
| 535 |
return recoveryPolicy; |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
@Override |
| 550 |
public void recoverableError(XPathException err) throws XPathException { |
| 551 |
if (recoveryPolicy == Configuration.DO_NOT_RECOVER) { |
| 552 |
throw err; |
| 553 |
} else { |
| 554 |
errorListener.warning(err); |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
public void setOutputURIResolver( OutputURIResolver resolver) { |
| 578 |
OutputURIResolver our = resolver == null ? getConfiguration().getOutputURIResolver() : resolver; |
| 579 |
setResultDocumentResolver(new OutputURIResolverWrapper(our)); |
| 580 |
} |
| 581 |
|
| 582 |
public ResultDocumentResolver getResultDocumentResolver() { |
| 583 |
return resultDocumentResolver; |
| 584 |
} |
| 585 |
|
| 586 |
public void setResultDocumentResolver(ResultDocumentResolver resultDocumentResolver) { |
| 587 |
this.resultDocumentResolver = resultDocumentResolver; |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
public OutputURIResolver getOutputURIResolver() { |
| 602 |
if (resultDocumentResolver instanceof OutputURIResolverWrapper) { |
| 603 |
return ((OutputURIResolverWrapper) resultDocumentResolver).getOutputURIResolver(); |
| 604 |
} else { |
| 605 |
return getConfiguration().getOutputURIResolver(); |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
public void setPrincipalDestination(Destination destination) { |
| 616 |
principalDestination = destination; |
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
public Destination getPrincipalDestination() { |
| 625 |
return principalDestination; |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
public boolean isAssertionsEnabled() { |
| 641 |
return assertionsEnabled; |
| 642 |
} |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
public void setAssertionsEnabled(boolean enabled) { |
| 657 |
this.assertionsEnabled = enabled; |
| 658 |
} |
| 659 |
|
| 660 |
@Override |
| 661 |
public void preEvaluateGlobals(XPathContext context) throws XPathException { |
| 662 |
openMessageEmitter(); |
| 663 |
super.preEvaluateGlobals(context); |
| 664 |
closeMessageEmitter(); |
| 665 |
} |
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
public void applyTemplates(Sequence<?> source, Receiver out) throws XPathException { |
| 685 |
|
| 686 |
checkReadiness(); |
| 687 |
openMessageEmitter(); |
| 688 |
|
| 689 |
try { |
| 690 |
out = prepareOutputReceiver(out); |
| 691 |
|
| 692 |
XPathContextMajor initialContext = newXPathContext(); |
| 693 |
initialContext.createThreadManager(); |
| 694 |
initialContext.setOrigin(this); |
| 695 |
initialContext.setReceiver(out); |
| 696 |
|
| 697 |
boolean close = false; |
| 698 |
|
| 699 |
Mode mode = getInitialMode(); |
| 700 |
if (mode == null) { |
| 701 |
throw new XPathException("Requested initial mode " + |
| 702 |
(initialMode == null ? "#unnamed" : initialMode.getActor().getModeName().getDisplayName()) + |
| 703 |
" does not exist", "XTDE0045"); |
| 704 |
} |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
if (!((PreparedStylesheet)executable).isEligibleInitialMode(initialMode)) { |
| 713 |
throw new XPathException("Requested initial mode " + |
| 714 |
(mode.getModeName().getDisplayName()) + |
| 715 |
" is not public or final", "XTDE0045"); |
| 716 |
} |
| 717 |
|
| 718 |
|
| 719 |
warningIfStreamable(mode); |
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
boolean mustClose = false; |
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
ParameterSet ordinaryParams = null; |
| 730 |
if (initialTemplateParams != null) { |
| 731 |
ordinaryParams = new ParameterSet(initialTemplateParams); |
| 732 |
} |
| 733 |
ParameterSet tunnelParams = null; |
| 734 |
if (initialTemplateTunnelParams != null) { |
| 735 |
tunnelParams = new ParameterSet(initialTemplateTunnelParams); |
| 736 |
} |
| 737 |
|
| 738 |
SequenceIterator<?> iter = source.iterate(); |
| 739 |
|
| 740 |
MappingFunction<Item<?>, Item<?>> preprocessor = getInputPreprocessor(mode); |
| 741 |
iter = new MappingIterator<>(iter, preprocessor); |
| 742 |
|
| 743 |
initialContext.trackFocus(iter); |
| 744 |
initialContext.setCurrentMode(initialMode); |
| 745 |
initialContext.setCurrentComponent(initialMode); |
| 746 |
|
| 747 |
TailCall tc = mode.applyTemplates(ordinaryParams, tunnelParams, initialContext, ExplicitLocation.UNKNOWN_LOCATION); |
| 748 |
while (tc != null) { |
| 749 |
tc = tc.processLeavingTail(); |
| 750 |
} |
| 751 |
|
| 752 |
initialContext.waitForChildThreads(); |
| 753 |
|
| 754 |
out.close(); |
| 755 |
|
| 756 |
} catch (TerminationException err) { |
| 757 |
|
| 758 |
if (!err.hasBeenReported()) { |
| 759 |
reportFatalError(err); |
| 760 |
} |
| 761 |
throw err; |
| 762 |
} catch (XPathException err) { |
| 763 |
Throwable cause = err.getException(); |
| 764 |
if (cause instanceof SAXParseException) { |
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
SAXParseException spe = (SAXParseException) cause; |
| 770 |
cause = spe.getException(); |
| 771 |
if (cause instanceof RuntimeException) { |
| 772 |
reportFatalError(err); |
| 773 |
} |
| 774 |
} else { |
| 775 |
reportFatalError(err); |
| 776 |
} |
| 777 |
throw err; |
| 778 |
} finally { |
| 779 |
inUse = false; |
| 780 |
closeMessageEmitter(); |
| 781 |
if (traceListener != null) { |
| 782 |
traceListener.close(); |
| 783 |
} |
| 784 |
principalResultURI = null; |
| 785 |
} |
| 786 |
} |
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
private Receiver prepareOutputReceiver(Receiver out) throws XPathException { |
| 799 |
principalResult = out; |
| 800 |
if (principalResultURI == null) { |
| 801 |
principalResultURI = out.getSystemId(); |
| 802 |
} |
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
NamespaceReducer nr = new NamespaceReducer(out); |
| 807 |
ComplexContentOutputter cco = new ComplexContentOutputter(nr); |
| 808 |
cco.setSystemId(out.getSystemId()); |
| 809 |
out = cco; |
| 810 |
|
| 811 |
|
| 812 |
if (getExecutable().createsSecondaryResult()) { |
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
out = this.gatekeeper = new PrincipalOutputGatekeeper(this, out); |
| 817 |
} |
| 818 |
|
| 819 |
out.open(); |
| 820 |
return out; |
| 821 |
} |
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
public PrincipalOutputGatekeeper getGatekeeper() { |
| 830 |
return gatekeeper; |
| 831 |
} |
| 832 |
|
| 833 |
private MappingFunction<Item<?>, Item<?>> getInputPreprocessor(Mode finalMode) { |
| 834 |
return item -> { |
| 835 |
if (item instanceof NodeInfo) { |
| 836 |
NodeInfo node = (NodeInfo) item; |
| 837 |
if (node.getConfiguration() == null) { |
| 838 |
|
| 839 |
throw new XPathException("The supplied source document must be associated with a Configuration"); |
| 840 |
} |
| 841 |
|
| 842 |
if (!node.getConfiguration().isCompatible(executable.getConfiguration())) { |
| 843 |
throw new XPathException( |
| 844 |
"Source document and stylesheet must use the same or compatible Configurations", |
| 845 |
SaxonErrorCode.SXXP0004); |
| 846 |
} |
| 847 |
|
| 848 |
if (node.getTreeInfo().isTyped() && !executable.isSchemaAware()) { |
| 849 |
throw new XPathException("Cannot use a schema-validated source document unless the stylesheet is schema-aware"); |
| 850 |
} |
| 851 |
|
| 852 |
if (isStylesheetStrippingTypeAnnotations() && node != globalContextItem) { |
| 853 |
TreeInfo docInfo = node.getTreeInfo(); |
| 854 |
if (docInfo.isTyped()) { |
| 855 |
TypeStrippedDocument strippedDoc = new TypeStrippedDocument(docInfo); |
| 856 |
node = strippedDoc.wrap(node); |
| 857 |
} |
| 858 |
} |
| 859 |
|
| 860 |
SpaceStrippingRule spaceStrippingRule = getSpaceStrippingRule(); |
| 861 |
if (isStylesheetContainingStripSpace() && isStripSourceTree() && !(node instanceof SpaceStrippedNode) |
| 862 |
&& node != globalContextItem && node.getTreeInfo().getSpaceStrippingRule() != spaceStrippingRule) { |
| 863 |
SpaceStrippedDocument strippedDoc = new SpaceStrippedDocument(node.getTreeInfo(), spaceStrippingRule); |
| 864 |
|
| 865 |
if (!SpaceStrippedNode.isPreservedNode(node, strippedDoc, node.getParent())) { |
| 866 |
return EmptyIterator.emptyIterator(); |
| 867 |
} |
| 868 |
node = strippedDoc.wrap(node); |
| 869 |
} |
| 870 |
|
| 871 |
if (getAccumulatorManager() != null) { |
| 872 |
getAccumulatorManager().setApplicableAccumulators(node.getTreeInfo(), finalMode.getAccumulators()); |
| 873 |
} |
| 874 |
return SingletonIterator.makeIterator(node); |
| 875 |
} else { |
| 876 |
return SingletonIterator.makeIterator(item); |
| 877 |
} |
| 878 |
}; |
| 879 |
} |
| 880 |
|
| 881 |
private void warningIfStreamable(Mode mode) { |
| 882 |
if (mode.isDeclaredStreamable()) { |
| 883 |
warning((initialMode == null ? "" : getInitialMode().getModeTitle()) + |
| 884 |
" is streamable, but the input is not supplied as a stream", null, null); |
| 885 |
} |
| 886 |
} |
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
public void callTemplate(StructuredQName initialTemplateName, Receiver out) |
| 907 |
throws XPathException { |
| 908 |
checkReadiness(); |
| 909 |
openMessageEmitter(); |
| 910 |
|
| 911 |
try { |
| 912 |
out = prepareOutputReceiver(out); |
| 913 |
|
| 914 |
XPathContextMajor initialContext = newXPathContext(); |
| 915 |
initialContext.createThreadManager(); |
| 916 |
initialContext.setOrigin(this); |
| 917 |
initialContext.setReceiver(out); |
| 918 |
|
| 919 |
if (globalContextItem != null) { |
| 920 |
initialContext.setCurrentIterator(new ManualIterator<>(globalContextItem)); |
| 921 |
} |
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
ParameterSet ordinaryParams = null; |
| 926 |
if (initialTemplateParams != null) { |
| 927 |
ordinaryParams = new ParameterSet(initialTemplateParams); |
| 928 |
} |
| 929 |
ParameterSet tunnelParams = null; |
| 930 |
if (initialTemplateTunnelParams != null) { |
| 931 |
tunnelParams = new ParameterSet(initialTemplateTunnelParams); |
| 932 |
} |
| 933 |
|
| 934 |
StylesheetPackage pack = (StylesheetPackage) executable.getTopLevelPackage(); |
| 935 |
Component initialComponent = pack.getComponent(new SymbolicName(StandardNames.XSL_TEMPLATE, initialTemplateName)); |
| 936 |
if (initialComponent == null) { |
| 937 |
throw new XPathException("Template " + initialTemplateName.getDisplayName() + " does not exist", "XTDE0040"); |
| 938 |
} |
| 939 |
if (!pack.isImplicitPackage() && !(initialComponent.getVisibility() == Visibility.PUBLIC || initialComponent.getVisibility() == Visibility.FINAL)) { |
| 940 |
throw new XPathException("Template " + initialTemplateName.getDisplayName() + " is " + initialComponent.getVisibility().show(), "XTDE0040"); |
| 941 |
} |
| 942 |
NamedTemplate t = (NamedTemplate) initialComponent.getActor(); |
| 943 |
|
| 944 |
XPathContextMajor c2 = initialContext.newContext(); |
| 945 |
initialContext.setOrigin(this); |
| 946 |
c2.setCurrentComponent(initialComponent); |
| 947 |
c2.openStackFrame(t.getStackFrameMap()); |
| 948 |
c2.setLocalParameters(ordinaryParams); |
| 949 |
c2.setTunnelParameters(tunnelParams); |
| 950 |
|
| 951 |
|
| 952 |
TailCall tc = t.expand(c2); |
| 953 |
while (tc != null) { |
| 954 |
tc = tc.processLeavingTail(); |
| 955 |
} |
| 956 |
|
| 957 |
initialContext.waitForChildThreads(); |
| 958 |
out.close(); |
| 959 |
|
| 960 |
} finally { |
| 961 |
if (traceListener != null) { |
| 962 |
traceListener.close(); |
| 963 |
} |
| 964 |
closeMessageEmitter(); |
| 965 |
inUse = false; |
| 966 |
} |
| 967 |
} |
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
public void applyStreamingTemplates(Source source, Receiver out) throws XPathException { |
| 988 |
checkReadiness(); |
| 989 |
openMessageEmitter(); |
| 990 |
|
| 991 |
out = prepareOutputReceiver(out); |
| 992 |
|
| 993 |
boolean close = false; |
| 994 |
try { |
| 995 |
int validationMode = getSchemaValidationMode(); |
| 996 |
|
| 997 |
Source underSource = source; |
| 998 |
if (source instanceof AugmentedSource) { |
| 999 |
close = ((AugmentedSource) source).isPleaseCloseAfterUse(); |
| 1000 |
int localValidate = ((AugmentedSource) source).getSchemaValidation(); |
| 1001 |
if (localValidate != Validation.DEFAULT) { |
| 1002 |
validationMode = localValidate; |
| 1003 |
} |
| 1004 |
underSource = ((AugmentedSource) source).getContainedSource(); |
| 1005 |
} |
| 1006 |
Configuration config = getConfiguration(); |
| 1007 |
Source s2 = config.getSourceResolver().resolveSource(underSource, config); |
| 1008 |
if (s2 != null) { |
| 1009 |
underSource = s2; |
| 1010 |
} |
| 1011 |
if (!(source instanceof SAXSource || source instanceof StreamSource || source instanceof EventSource)) { |
| 1012 |
throw new IllegalArgumentException("Streaming requires a SAXSource, StreamSource, or EventSource"); |
| 1013 |
} |
| 1014 |
if (!initialMode.getActor().isDeclaredStreamable()) { |
| 1015 |
throw new IllegalArgumentException("Initial mode is not streamable"); |
| 1016 |
} |
| 1017 |
if (source instanceof SAXSource && config.getBooleanProperty(Feature.IGNORE_SAX_SOURCE_PARSER)) { |
| 1018 |
|
| 1019 |
|
| 1020 |
((SAXSource) source).setXMLReader(null); |
| 1021 |
} |
| 1022 |
|
| 1023 |
XPathContextMajor initialContext = newXPathContext(); |
| 1024 |
initialContext.createThreadManager(); |
| 1025 |
initialContext.setOrigin(this); |
| 1026 |
initialContext.setReceiver(out); |
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
ParameterSet ordinaryParams = null; |
| 1031 |
if (initialTemplateParams != null) { |
| 1032 |
ordinaryParams = new ParameterSet(initialTemplateParams); |
| 1033 |
} |
| 1034 |
ParameterSet tunnelParams = null; |
| 1035 |
if (initialTemplateTunnelParams != null) { |
| 1036 |
tunnelParams = new ParameterSet(initialTemplateTunnelParams); |
| 1037 |
} |
| 1038 |
|
| 1039 |
Receiver despatcher = config.makeStreamingTransformer(initialContext, initialMode.getActor(), ordinaryParams, tunnelParams); |
| 1040 |
if (config.isStripsAllWhiteSpace() || isStylesheetContainingStripSpace()) { |
| 1041 |
despatcher = makeStripper(despatcher); |
| 1042 |
} |
| 1043 |
PipelineConfiguration pipe = despatcher.getPipelineConfiguration(); |
| 1044 |
pipe.getParseOptions().setSchemaValidationMode(this.validationMode); |
| 1045 |
boolean verbose = getConfiguration().isTiming(); |
| 1046 |
if (verbose) { |
| 1047 |
getConfiguration().getLogger().info("Streaming " + source.getSystemId()); |
| 1048 |
} |
| 1049 |
try { |
| 1050 |
Sender.send(source, despatcher, null); |
| 1051 |
} catch (QuitParsingException e) { |
| 1052 |
if (verbose) { |
| 1053 |
getConfiguration().getLogger().info("Streaming " + source.getSystemId() + " : early exit"); |
| 1054 |
} |
| 1055 |
} |
| 1056 |
|
| 1057 |
initialContext.waitForChildThreads(); |
| 1058 |
out.close(); |
| 1059 |
|
| 1060 |
|
| 1061 |
} catch (TerminationException err) { |
| 1062 |
|
| 1063 |
if (!err.hasBeenReported()) { |
| 1064 |
reportFatalError(err); |
| 1065 |
} |
| 1066 |
throw err; |
| 1067 |
} catch (XPathException err) { |
| 1068 |
Throwable cause = err.getException(); |
| 1069 |
if (cause instanceof SAXParseException) { |
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
SAXParseException spe = (SAXParseException) cause; |
| 1075 |
cause = spe.getException(); |
| 1076 |
if (cause instanceof RuntimeException) { |
| 1077 |
reportFatalError(err); |
| 1078 |
} |
| 1079 |
} else { |
| 1080 |
reportFatalError(err); |
| 1081 |
} |
| 1082 |
throw err; |
| 1083 |
} finally { |
| 1084 |
inUse = false; |
| 1085 |
if (close && source instanceof AugmentedSource) { |
| 1086 |
((AugmentedSource) source).close(); |
| 1087 |
} |
| 1088 |
if (traceListener != null) { |
| 1089 |
traceListener.close(); |
| 1090 |
} |
| 1091 |
} |
| 1092 |
} |
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
public Receiver getStreamingReceiver(Mode mode, Receiver result) |
| 1106 |
throws XPathException { |
| 1107 |
|
| 1108 |
|
| 1109 |
checkReadiness(); |
| 1110 |
openMessageEmitter(); |
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
result = prepareOutputReceiver(result); |
| 1117 |
|
| 1118 |
final XPathContextMajor initialContext = newXPathContext(); |
| 1119 |
initialContext.setOrigin(this); |
| 1120 |
initialContext.setReceiver(result); |
| 1121 |
|
| 1122 |
globalContextItem = null; |
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
if (!mode.isDeclaredStreamable()) { |
| 1127 |
throw new XPathException("mode supplied to getStreamingReceiver() must be streamable"); |
| 1128 |
} |
| 1129 |
Configuration config = getConfiguration(); |
| 1130 |
Receiver despatcher = config.makeStreamingTransformer(initialContext, mode, null, null); |
| 1131 |
if (despatcher == null) { |
| 1132 |
throw new XPathException("Streaming requires Saxon-EE"); |
| 1133 |
} |
| 1134 |
if (config.isStripsAllWhiteSpace() || isStylesheetContainingStripSpace()) { |
| 1135 |
despatcher = makeStripper(despatcher); |
| 1136 |
} |
| 1137 |
despatcher.setPipelineConfiguration(makePipelineConfiguration()); |
| 1138 |
|
| 1139 |
final Receiver finalResult = result; |
| 1140 |
return new ProxyReceiver(despatcher) { |
| 1141 |
public void close() throws XPathException { |
| 1142 |
if (traceListener != null) { |
| 1143 |
traceListener.close(); |
| 1144 |
} |
| 1145 |
closeMessageEmitter(); |
| 1146 |
finalResult.close(); |
| 1147 |
inUse = false; |
| 1148 |
} |
| 1149 |
}; |
| 1150 |
|
| 1151 |
} |
| 1152 |
|
| 1153 |
private void openMessageEmitter() throws XPathException { |
| 1154 |
if (explicitMessageReceiver != null) { |
| 1155 |
explicitMessageReceiver.open(); |
| 1156 |
if (explicitMessageReceiver instanceof Emitter && ((Emitter) explicitMessageReceiver).getWriter() == null) { |
| 1157 |
((Emitter) explicitMessageReceiver).setStreamResult(getConfiguration().getLogger().asStreamResult()); |
| 1158 |
} |
| 1159 |
} |
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
} |
| 1169 |
|
| 1170 |
private void closeMessageEmitter() throws XPathException { |
| 1171 |
if (explicitMessageReceiver != null) { |
| 1172 |
explicitMessageReceiver.close(); |
| 1173 |
} |
| 1174 |
} |
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
public synchronized Stack<AttributeSet> getAttributeSetEvaluationStack() { |
| 1183 |
long thread = Thread.currentThread().getId(); |
| 1184 |
return attributeSetEvaluationStacks.computeIfAbsent(thread, k -> new Stack<>()); |
| 1185 |
} |
| 1186 |
|
| 1187 |
public synchronized void releaseAttributeSetEvaluationStack() { |
| 1188 |
long thread = Thread.currentThread().getId(); |
| 1189 |
attributeSetEvaluationStacks.remove(thread); |
| 1190 |
} |
| 1191 |
} |
| 1192 |
|