| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | |
| 9 | package net.sf.saxon.s9api; |
| 10 | |
| 11 | import net.sf.saxon.Configuration; |
| 12 | import net.sf.saxon.Controller; |
| 13 | import net.sf.saxon.event.*; |
| 14 | import net.sf.saxon.expr.XPathContext; |
| 15 | import net.sf.saxon.expr.instruct.GlobalParameterSet; |
| 16 | import net.sf.saxon.functions.ResolveURI; |
| 17 | import net.sf.saxon.lib.*; |
| 18 | import net.sf.saxon.om.NodeInfo; |
| 19 | import net.sf.saxon.om.StructuredQName; |
| 20 | import net.sf.saxon.serialize.SerializationProperties; |
| 21 | import net.sf.saxon.trans.SaxonErrorCode; |
| 22 | import net.sf.saxon.trans.XPathException; |
| 23 | import net.sf.saxon.trans.XsltController; |
| 24 | import net.sf.saxon.tree.tiny.TinyBuilder; |
| 25 | |
| 26 | import javax.xml.transform.ErrorListener; |
| 27 | import javax.xml.transform.Source; |
| 28 | import javax.xml.transform.TransformerException; |
| 29 | import javax.xml.transform.URIResolver; |
| 30 | import javax.xml.transform.sax.SAXSource; |
| 31 | import javax.xml.transform.stream.StreamSource; |
| 32 | import java.io.OutputStream; |
| 33 | import java.io.Writer; |
| 34 | import java.net.URI; |
| 35 | import java.net.URISyntaxException; |
| 36 | import java.util.Objects; |
| 37 | |
| 38 | /** |
| 39 | * A class that exists to contain common code shared between XsltTransformer and Xslt30Transformer |
| 40 | */ |
| 41 | |
| 42 | abstract class AbstractXsltTransformer { |
| 43 | protected Processor processor; |
| 44 | protected XsltController controller; |
| 45 | protected boolean baseOutputUriWasSet = false; |
| 46 | private ResultDocumentResolver resultDocumentHandler = null; |
| 47 | private MessageListener messageListener; |
| 48 | private MessageListener2 messageListener2; |
| 49 | |
| 50 | |
| 51 | AbstractXsltTransformer(Processor processor, XsltController controller) { |
| 52 | this.processor = processor; |
| 53 | this.controller = controller; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Set the base output URI. |
| 58 | * <p>This defaults to the base URI of the {@link Destination} for the principal output |
| 59 | * of the transformation if a destination is supplied and its base URI is known.</p> |
| 60 | * <p>If a base output URI is supplied using this method then it takes precedence |
| 61 | * over any base URI defined in the supplied {@code Destination} object, and |
| 62 | * it may cause the base URI of the {@code Destination} object to be modified in situ.</p> |
| 63 | * <p> The base output URI is used for resolving relative URIs in the <code>href</code> attribute |
| 64 | * of the <code>xsl:result-document</code> instruction; it is accessible to XSLT stylesheet |
| 65 | * code using the XPath {@code current-output-uri()} function</p> |
| 66 | * |
| 67 | * @param uri the base output URI |
| 68 | */ |
| 69 | |
| 70 | public void setBaseOutputURI(String uri) { |
| 71 | controller.setBaseOutputURI(uri); |
| 72 | baseOutputUriWasSet = uri != null; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Get the base output URI. |
| 77 | * <p>This returns the value set using the {@link #setBaseOutputURI} method. If no value has been set |
| 78 | * explicitly, then the method returns null if called before the transformation, or the computed |
| 79 | * default base output URI if called after the transformation.</p> |
| 80 | * <p>The base output URI is used for resolving relative URIs in the <code>href</code> attribute |
| 81 | * of the <code>xsl:result-document</code> instruction.</p> |
| 82 | * |
| 83 | * @return the base output URI |
| 84 | */ |
| 85 | |
| 86 | public String getBaseOutputURI() { |
| 87 | return controller.getBaseOutputURI(); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Set an object that will be used to resolve URIs used in |
| 92 | * fn:doc() and related functions. |
| 93 | * |
| 94 | * @param resolver An object that implements the URIResolver interface, or |
| 95 | * null. |
| 96 | */ |
| 97 | |
| 98 | public void setURIResolver(URIResolver resolver) { |
| 99 | controller.setURIResolver(resolver); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Get the URI resolver. |
| 104 | * |
| 105 | * @return the user-supplied URI resolver if there is one, or null otherwise |
| 106 | */ |
| 107 | |
| 108 | public URIResolver getURIResolver() { |
| 109 | return controller.getURIResolver(); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Set the ErrorListener to be used during this transformation |
| 114 | * |
| 115 | * @param listener The error listener to be used. This is notified of all dynamic errors detected during the |
| 116 | * transformation. |
| 117 | */ |
| 118 | |
| 119 | public void setErrorListener(ErrorListener listener) { |
| 120 | controller.setErrorListener(listener); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Get the ErrorListener being used during this compilation episode |
| 125 | * |
| 126 | * @return listener The error listener in use. This is notified of all dynamic errors detected during the |
| 127 | * transformation. If no user-supplied ErrorListener has been set the method will return a system-supplied |
| 128 | * ErrorListener. If an explicit ErrorListener has been set using {@link #setErrorListener(ErrorListener)}, |
| 129 | * then that ErrorListener will generally be returned, unless the internal ErrorListener has been changed |
| 130 | * by some other mechanism. |
| 131 | */ |
| 132 | |
| 133 | public ErrorListener getErrorListener() { |
| 134 | UnfailingErrorListener uel = controller.getErrorListener(); |
| 135 | if (uel instanceof DelegatingErrorListener) { |
| 136 | return ((DelegatingErrorListener) uel).getBaseErrorListener(); |
| 137 | } else { |
| 138 | return uel; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Set a callback function that will be used when {@code xsl:result-document} is evaluated. The argument |
| 144 | * is a function that takes a URI as input (specifically, the value of the {@code href} argument |
| 145 | * to {@code xsl:result-document}, resolved against the base output URI of the transformation), |
| 146 | * and returns a {@link Destination}, which will be used as the destination for the result document. |
| 147 | * <p>If the {@code href} argument of the {@code xsl:result-document} instruction is absent or if |
| 148 | * it is set to a zero length string, then the callback function is not normally called; instead |
| 149 | * a {@code Receiver} for the secondary output is obtained by making a second call on {@link Destination#getReceiver(PipelineConfiguration, SerializationProperties)} |
| 150 | * for the principal destination of the transformation. In that situation, this result document handler |
| 151 | * is invoked only if the call on {@link Destination#getReceiver(PipelineConfiguration, SerializationProperties)} |
| 152 | * returns null. </p> |
| 153 | * <p>If the base output URI is absent (perhaps because the principal output destination for the |
| 154 | * transformation was supplied as a {@link OutputStream} or {@link Writer} with no associated |
| 155 | * URI or systemId), then the value of the {@code href} attribute is used <i>as is</i> if it |
| 156 | * is an absolute URI; if it is a relative URI (including the case where it is absent or zero-length) |
| 157 | * then the callback function is not called; instead a dynamic error is raised (code |
| 158 | * {@link SaxonErrorCode#SXRD0002}).</p> |
| 159 | * <p>If the callback function throws a {@link SaxonApiUncheckedException}, this will result |
| 160 | * in the {@code xsl:result-document} instruction failing with a dynamic error, which can be caught |
| 161 | * using {@code xsl:try/xsl:catch}. The error code, by default, will be "err:SXRD0001".</p> |
| 162 | * <p>The application can request to be notified when the {@code Destination} is closed by setting |
| 163 | * a {@link Destination#onClose(Action)} callback on the {@code Destination} object.</p> |
| 164 | * |
| 165 | * @param handler the callback function to be invoked whenever an {@code xsl:result-document} |
| 166 | * instruction is evaluated. |
| 167 | */ |
| 168 | |
| 169 | public void setResultDocumentHandler(java.util.function.Function<URI, Destination> handler) { |
| 170 | controller.setResultDocumentResolver(new ResultDocumentResolver() { |
| 171 | @Override |
| 172 | public Receiver resolve( |
| 173 | XPathContext context, String href, String baseUri, SerializationProperties properties) |
| 174 | throws XPathException { |
| 175 | try { |
| 176 | URI abs = ResolveURI.makeAbsolute(href, baseUri); |
| 177 | Destination destination; |
| 178 | try { |
| 179 | destination = handler.apply(abs); |
| 180 | } catch (SaxonApiUncheckedException e) { |
| 181 | XPathException xe = XPathException.makeXPathException(e); |
| 182 | xe.maybeSetErrorCode("SXRD0001"); |
| 183 | throw xe; |
| 184 | } |
| 185 | try { |
| 186 | return destination.getReceiver(context.getReceiver().getPipelineConfiguration(), properties); |
| 187 | } catch (SaxonApiException e) { |
| 188 | throw XPathException.makeXPathException(e); |
| 189 | } |
| 190 | } catch (URISyntaxException e) { |
| 191 | throw XPathException.makeXPathException(e); |
| 192 | } |
| 193 | } |
| 194 | }); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Set the MessageListener to be notified whenever the stylesheet evaluates an |
| 199 | * <code>xsl:message</code> instruction. If no MessageListener is nominated, |
| 200 | * the output of <code>xsl:message</code> instructions will be serialized and sent |
| 201 | * to the standard error stream. |
| 202 | * |
| 203 | * @param listener the MessageListener to be used |
| 204 | */ |
| 205 | |
| 206 | public void setMessageListener(MessageListener listener) { |
| 207 | this.messageListener = listener; |
| 208 | controller.setMessageFactory(() -> new MessageListenerProxy(listener, controller.makePipelineConfiguration())); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Set the MessageListener to be notified whenever the stylesheet evaluates an |
| 213 | * <code>xsl:message</code> instruction. If no MessageListener is nominated, |
| 214 | * the output of <code>xsl:message</code> instructions will be serialized and sent |
| 215 | * to the standard error stream. |
| 216 | * <p> |
| 217 | * <p>The <code>MessageListener2</code> interface differs from <code>MessageListener</code> |
| 218 | * in allowing the error code supplied to xsl:message to be notified.</p> |
| 219 | * |
| 220 | * @param listener the MessageListener to be used |
| 221 | */ |
| 222 | |
| 223 | public void setMessageListener(MessageListener2 listener) { |
| 224 | this.messageListener2 = listener; |
| 225 | controller.setMessageFactory(() -> new MessageListener2Proxy(listener, controller.makePipelineConfiguration())); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Get the MessageListener to be notified whenever the stylesheet evaluates an |
| 230 | * <code>xsl:message</code> instruction. If no MessageListener has been nominated, |
| 231 | * return null |
| 232 | * |
| 233 | * @return the user-supplied MessageListener, or null if none has been supplied |
| 234 | */ |
| 235 | |
| 236 | public MessageListener getMessageListener() { |
| 237 | return messageListener; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Get the MessageListener2 to be notified whenever the stylesheet evaluates an |
| 242 | * <code>xsl:message</code> instruction. If no MessageListener2 has been nominated, |
| 243 | * return null |
| 244 | * |
| 245 | * @return the user-supplied MessageListener2, or null if none has been supplied |
| 246 | */ |
| 247 | |
| 248 | public MessageListener2 getMessageListener2() { |
| 249 | return messageListener2; |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Say whether assertions (xsl:assert instructions) should be enabled at run time. By default |
| 254 | * they are disabled at compile time. If assertions are enabled at compile time, then by |
| 255 | * default they will also be enabled at run time; but they can be disabled at run time by |
| 256 | * specific request. At compile time, assertions can be enabled for some packages and |
| 257 | * disabled for others; at run-time, they can only be enabled or disabled globally. |
| 258 | * |
| 259 | * @param enabled true if assertions are to be enabled at run time; this has no effect |
| 260 | * if assertions were disabled (for a particular package) at compile time |
| 261 | * @since 9.7 |
| 262 | */ |
| 263 | |
| 264 | public void setAssertionsEnabled(boolean enabled) { |
| 265 | controller.setAssertionsEnabled(enabled); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Ask whether assertions (xsl:assert instructions) have been enabled at run time. By default |
| 270 | * they are disabled at compile time. If assertions are enabled at compile time, then by |
| 271 | * default they will also be enabled at run time; but they can be disabled at run time by |
| 272 | * specific request. At compile time, assertions can be enabled for some packages and |
| 273 | * disabled for others; at run-time, they can only be enabled or disabled globally. |
| 274 | * |
| 275 | * @return true if assertions are enabled at run time |
| 276 | * @since 9.7 |
| 277 | */ |
| 278 | |
| 279 | public boolean isAssertionsEnabled() { |
| 280 | return controller.isAssertionsEnabled(); |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Set a TraceListener to be notified of all events occurring during the transformation. |
| 285 | * This will only be effective if the stylesheet was compiled with trace code enabled |
| 286 | * (see {@link XsltCompiler#setCompileWithTracing(boolean)}) |
| 287 | * |
| 288 | * @param listener the TraceListener to be used. Note that the TraceListener has access to |
| 289 | * interal Saxon interfaces which may vary from one release to the next. It is also possible that |
| 290 | * the TraceListener interface itself may be changed in future releases. |
| 291 | */ |
| 292 | |
| 293 | public void setTraceListener(TraceListener listener) { |
| 294 | controller.setTraceListener(listener); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Get the TraceListener to be notified of all events occurring during the transformation. |
| 299 | * If no TraceListener has been nominated, return null |
| 300 | * |
| 301 | * @return the user-supplied TraceListener, or null if none has been supplied |
| 302 | */ |
| 303 | |
| 304 | public TraceListener getTraceListener() { |
| 305 | return controller.getTraceListener(); |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Set the destination for output from the fn:trace() function. |
| 310 | * By default, the destination is System.err. If a TraceListener is in use, |
| 311 | * this is ignored, and the trace() output is sent to the TraceListener. |
| 312 | * |
| 313 | * @param stream the PrintStream to which trace output will be sent. If set to |
| 314 | * null, trace output is suppressed entirely. It is the caller's responsibility |
| 315 | * to close the stream after use. |
| 316 | */ |
| 317 | |
| 318 | public void setTraceFunctionDestination(Logger stream) { |
| 319 | controller.setTraceFunctionDestination(stream); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Get the destination for output from the fn:trace() function. |
| 324 | * |
| 325 | * @return the Logger to which trace output will be sent. If no explicitly |
| 326 | * destination has been set, returns System.err. If the destination has been set |
| 327 | * to null to suppress trace output, returns null. |
| 328 | */ |
| 329 | |
| 330 | public Logger getTraceFunctionDestination() { |
| 331 | return controller.getTraceFunctionDestination(); |
| 332 | } |
| 333 | |
| 334 | |
| 335 | protected void applyTemplatesToSource(Source source, Receiver out) throws XPathException { |
| 336 | Objects.requireNonNull(source); |
| 337 | Objects.requireNonNull(out); |
| 338 | if (controller.getInitialMode().isDeclaredStreamable() && isStreamableSource(source)) { |
| 339 | controller.applyStreamingTemplates(source, out); |
| 340 | } else { |
| 341 | NodeInfo node; |
| 342 | if (source instanceof NodeInfo) { |
| 343 | node = (NodeInfo)source; |
| 344 | } else { |
| 345 | node = controller.makeSourceTree(source, controller.getSchemaValidationMode()); |
| 346 | } |
| 347 | controller.applyTemplates(node, out); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | protected boolean isStreamableSource(Source source) { |
| 352 | if (source instanceof AugmentedSource) { |
| 353 | return isStreamableSource (((AugmentedSource) source).getContainedSource()); |
| 354 | } |
| 355 | Configuration config = controller.getConfiguration(); |
| 356 | try { |
| 357 | source = config.getSourceResolver().resolveSource(source, config); |
| 358 | } catch (XPathException e) { |
| 359 | return false; |
| 360 | } |
| 361 | return source instanceof SAXSource || source instanceof StreamSource || source instanceof EventSource; |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Set the schema validation mode for the transformation. This indicates how source documents |
| 366 | * loaded specifically for this transformation will be handled. This applies to the |
| 367 | * principal source document if supplied as a SAXSource or StreamSource, and to all |
| 368 | * documents loaded during the transformation using the <code>doc()</code>, <code>document()</code>, |
| 369 | * or <code>collection()</code> functions. |
| 370 | * |
| 371 | * @param mode the validation mode. Passing null causes no change to the existing value. |
| 372 | * Passing {@link ValidationMode#DEFAULT} resets to the initial value, which determines |
| 373 | * the validation requirements from the Saxon Configuration. |
| 374 | */ |
| 375 | |
| 376 | public void setSchemaValidationMode(ValidationMode mode) { |
| 377 | if (mode != null) { |
| 378 | controller.setSchemaValidationMode(mode.getNumber()); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Get the schema validation mode for the transformation. This indicates how source documents |
| 384 | * loaded specifically for this transformation will be handled. This applies to the |
| 385 | * principal source document if supplied as a SAXSource or StreamSource, and to all |
| 386 | * documents loaded during the transformation using the <code>doc()</code>, <code>document()</code>, |
| 387 | * or <code>collection()</code> functions. |
| 388 | * |
| 389 | * @return the validation mode. |
| 390 | */ |
| 391 | |
| 392 | public ValidationMode getSchemaValidationMode() { |
| 393 | return ValidationMode.get(controller.getSchemaValidationMode()); |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Set the initial mode for the transformation |
| 398 | * |
| 399 | * @param modeName the name of the initial mode. Two special values are recognized, in the |
| 400 | * reserved XSLT namespace: |
| 401 | * xsl:unnamed to indicate the mode with no name, and xsl:default to indicate the |
| 402 | * mode defined in the stylesheet header as the default mode. |
| 403 | * The value null also indicates the default mode (which defaults to the unnamed |
| 404 | * mode, but can be set differently in an XSLT 3.0 stylesheet). |
| 405 | * @throws IllegalArgumentException if the requested mode is not defined in the stylesheet |
| 406 | * @since changed in 9.6 to throw an exception if the mode is not defined in the stylesheet. |
| 407 | * Chaned in 9.7 so that null means the default mode, not necessarily the unnamed mode. |
| 408 | */ |
| 409 | |
| 410 | public void setInitialMode(QName modeName) throws IllegalArgumentException { |
| 411 | try { |
| 412 | controller.setInitialMode(modeName == null ? null : modeName.getStructuredQName()); |
| 413 | } catch (XPathException e) { |
| 414 | throw new IllegalArgumentException(e); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * Get the name of the initial mode for the transformation, if one has been set. |
| 420 | * |
| 421 | * @return the initial mode for the transformation. Returns null if no mode has been set, |
| 422 | * or if the mode was set to null to represent the default (unnamed) mode |
| 423 | */ |
| 424 | |
| 425 | public QName getInitialMode() { |
| 426 | StructuredQName mode = controller.getInitialModeName(); |
| 427 | if (mode == null) { |
| 428 | return null; |
| 429 | } else { |
| 430 | return new QName(mode); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * Get the underlying Controller used to implement this XsltTransformer. This provides access |
| 436 | * to lower-level methods not otherwise available in the s9api interface. Note that classes |
| 437 | * and methods obtained by this route cannot be guaranteed stable from release to release. |
| 438 | * |
| 439 | * @return the underlying {@link Controller} |
| 440 | */ |
| 441 | |
| 442 | public XsltController getUnderlyingController() { |
| 443 | return controller; |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * Get a Receiver corresponding to the chosen Destination for the transformation |
| 448 | * @param destination the destination for the results of this transformation |
| 449 | * @return a receiver that sends the results to this destination |
| 450 | * @throws SaxonApiException if anything goes wrong |
| 451 | */ |
| 452 | |
| 453 | public Receiver getDestinationReceiver(XsltController controller, Destination destination) throws SaxonApiException { |
| 454 | Receiver receiver; |
| 455 | controller.setPrincipalDestination(destination); |
| 456 | PipelineConfiguration pipe = controller.makePipelineConfiguration(); |
| 457 | SerializationProperties params = controller.getExecutable().getPrimarySerializationProperties(); |
| 458 | receiver = destination.getReceiver(pipe, params); |
| 459 | if (Configuration.isAssertionsEnabled()) { |
| 460 | receiver = new RegularSequenceChecker(receiver); |
| 461 | } |
| 462 | receiver.getPipelineConfiguration().setController(controller); |
| 463 | |
| 464 | if (baseOutputUriWasSet) { |
| 465 | try { |
| 466 | if (destination.getDestinationBaseURI() == null) { |
| 467 | destination.setDestinationBaseURI(new URI(controller.getBaseOutputURI())); |
| 468 | } |
| 469 | } catch (URISyntaxException e) { |
| 470 | // no action |
| 471 | } |
| 472 | } else if (destination.getDestinationBaseURI() != null) { |
| 473 | controller.setBaseOutputURI(destination.getDestinationBaseURI().toASCIIString()); |
| 474 | } |
| 475 | receiver.setSystemId(controller.getBaseOutputURI()); |
| 476 | return receiver; |
| 477 | |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * Return a Receiver which can be used to supply the principal source document for the transformation. |
| 482 | * This method is intended primarily for internal use, though it can also |
| 483 | * be called by a user application that wishes to feed events into the transformation engine. |
| 484 | * <p>Saxon calls this method to obtain a Receiver, to which it then sends |
| 485 | * a sequence of events representing the content of an XML document. This method is provided so that |
| 486 | * <code>XsltTransformer</code> implements <code>Destination</code>, allowing one transformation |
| 487 | * to receive the results of another in a pipeline.</p> |
| 488 | * |
| 489 | * @return the Receiver to which events are to be sent. |
| 490 | * @throws SaxonApiException if the Receiver cannot be created |
| 491 | * @throws IllegalStateException if no Destination has been supplied |
| 492 | */ |
| 493 | |
| 494 | protected Receiver getReceivingTransformer(XsltController controller, GlobalParameterSet parameters, Destination finalDestination) throws SaxonApiException { |
| 495 | Configuration config = controller.getConfiguration(); |
| 496 | // try { |
| 497 | // controller.initializeController(parameters); |
| 498 | // } catch (XPathException e) { |
| 499 | // throw new SaxonApiException(e); |
| 500 | // } |
| 501 | if (controller.getInitialMode().isDeclaredStreamable()) { |
| 502 | Receiver sOut = getDestinationReceiver(controller, finalDestination); |
| 503 | try { |
| 504 | //sOut.open(); |
| 505 | controller.initializeController(parameters); |
| 506 | return controller.getStreamingReceiver(controller.getInitialMode(), sOut); |
| 507 | } catch (TransformerException e) { |
| 508 | throw new SaxonApiException(e); |
| 509 | } |
| 510 | } else { |
| 511 | final Builder sourceTreeBuilder = controller.makeBuilder(); |
| 512 | if (sourceTreeBuilder instanceof TinyBuilder) { |
| 513 | ((TinyBuilder) sourceTreeBuilder).setStatistics(config.getTreeStatistics().SOURCE_DOCUMENT_STATISTICS); |
| 514 | } |
| 515 | Receiver stripper = controller.makeStripper(sourceTreeBuilder); |
| 516 | if (controller.isStylesheetStrippingTypeAnnotations()) { |
| 517 | stripper = controller.getConfiguration().getAnnotationStripper(stripper); |
| 518 | } |
| 519 | return new TreeReceiver(stripper) { |
| 520 | boolean closed = false; |
| 521 | public void close() throws XPathException { |
| 522 | if (!closed) { |
| 523 | try { |
| 524 | NodeInfo doc = sourceTreeBuilder.getCurrentRoot(); |
| 525 | if (doc != null) { |
| 526 | doc.getTreeInfo().setSpaceStrippingRule(controller.getSpaceStrippingRule()); |
| 527 | Receiver result = getDestinationReceiver(controller, finalDestination); |
| 528 | try { |
| 529 | controller.setGlobalContextItem(doc); |
| 530 | controller.initializeController(parameters); |
| 531 | controller.applyTemplates(doc, result); |
| 532 | } catch (TransformerException e) { |
| 533 | throw new SaxonApiException(e); |
| 534 | } |
| 535 | } |
| 536 | } catch (SaxonApiException e) { |
| 537 | throw XPathException.makeXPathException(e); |
| 538 | } |
| 539 | closed = true; |
| 540 | } |
| 541 | } |
| 542 | }; |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | } |
| 547 |