| 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 | package net.sf.saxon.jaxp; |
| 9 | |
| 10 | import net.sf.saxon.s9api.XsltExecutable; |
| 11 | |
| 12 | import javax.xml.transform.Templates; |
| 13 | import javax.xml.transform.Transformer; |
| 14 | import java.util.Properties; |
| 15 | |
| 16 | /** |
| 17 | * Saxon implementation of the JAXP Templates interface. |
| 18 | * <p>Since Saxon 9.6, JAXP interfaces are implemented as a layer above the s9api interface</p> |
| 19 | */ |
| 20 | public class TemplatesImpl implements Templates { |
| 21 | |
| 22 | private XsltExecutable executable; |
| 23 | private boolean forceStreaming; |
| 24 | |
| 25 | public TemplatesImpl(XsltExecutable executable) { |
| 26 | this.executable = executable; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Create a new transformation context for this Templates object. |
| 31 | * |
| 32 | * @return A valid non-null instance of a Transformer. |
| 33 | */ |
| 34 | public Transformer newTransformer() { |
| 35 | if (forceStreaming) { |
| 36 | return new StreamingTransformerImpl(executable, executable.load30()); |
| 37 | } else { |
| 38 | return new TransformerImpl(executable, executable.load()); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Get the properties corresponding to the effective xsl:output element. |
| 44 | * The object returned will |
| 45 | * be a clone of the internal values. Accordingly, it can be mutated |
| 46 | * without mutating the Templates object, and then handed in to |
| 47 | * {@link javax.xml.transform.Transformer#setOutputProperties}. |
| 48 | * <p>The properties returned should contain properties set by the stylesheet, |
| 49 | * and these properties are "defaulted" by default properties specified by |
| 50 | * <a href="http://www.w3.org/TR/xslt#output">section 16 of the |
| 51 | * XSL Transformations (XSLT) W3C Recommendation</a>. The properties that |
| 52 | * were specifically set by the stylesheet should be in the base |
| 53 | * Properties list, while the XSLT default properties that were not |
| 54 | * specifically set should be in the "default" Properties list. Thus, |
| 55 | * getOutputProperties().getProperty(String key) will obtain any |
| 56 | * property in that was set by the stylesheet, <em>or</em> the default |
| 57 | * properties, while |
| 58 | * getOutputProperties().get(String key) will only retrieve properties |
| 59 | * that were explicitly set in the stylesheet.</p> |
| 60 | * <p>For XSLT, |
| 61 | * <a href="http://www.w3.org/TR/xslt#attribute-value-templates">Attribute |
| 62 | * Value Templates</a> attribute values will |
| 63 | * be returned unexpanded (since there is no context at this point). The |
| 64 | * namespace prefixes inside Attribute Value Templates will be unexpanded, |
| 65 | * so that they remain valid XPath values.</p> |
| 66 | * |
| 67 | * @return A Properties object, never null. |
| 68 | */ |
| 69 | public Properties getOutputProperties() { |
| 70 | Properties details = executable.getUnderlyingCompiledStylesheet().getPrimarySerializationProperties().getProperties(); |
| 71 | return new Properties(details); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Ask whether any transformer created from this Templates object should have streaming behaviour. |
| 76 | * Specifically this means that when a SAXSource or StreamSource is supplied as input to the transform() |
| 77 | * method, provided that the initial mode is actually streamable, then the source object is not |
| 78 | * materialized as the global context item, so any reference to the global context item from within |
| 79 | * a global variable will fail with dynamic error XPDY0002. |
| 80 | * |
| 81 | * @return true if streaming behaviour is enforced for any generated Transformer object. |
| 82 | */ |
| 83 | |
| 84 | public boolean isForceStreaming() { |
| 85 | return forceStreaming; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Say that any transformer created from this Templates object should have streaming behaviour. |
| 90 | * Specifically this means that when a SAXSource or StreamSource is supplied as input to the transform() |
| 91 | * method, provided that the initial mode is actually streamable, then the source object is not |
| 92 | * materialized as the global context item, so any reference to the global context item from within |
| 93 | * a global variable will fail with dynamic error XPDY0002. |
| 94 | * |
| 95 | * @param forceStreaming true if streaming behaviour is to be enforced for any generated |
| 96 | * Transformer object. |
| 97 | */ |
| 98 | |
| 99 | public void setForceStreaming(boolean forceStreaming) { |
| 100 | this.forceStreaming = forceStreaming; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Get the underlying implementation object |
| 105 | * @return the underlying S9API {@link XsltExecutable} object |
| 106 | */ |
| 107 | |
| 108 | public XsltExecutable getImplementation() { |
| 109 | return executable; |
| 110 | } |
| 111 | } |