| 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.s9api; |
| 9 | |
| 10 | import net.sf.saxon.Configuration; |
| 11 | import net.sf.saxon.PreparedStylesheet; |
| 12 | import net.sf.saxon.event.Receiver; |
| 13 | import net.sf.saxon.expr.instruct.GlobalParam; |
| 14 | import net.sf.saxon.om.StructuredQName; |
| 15 | import net.sf.saxon.style.StylesheetPackage; |
| 16 | import net.sf.saxon.trace.ExpressionPresenter; |
| 17 | import net.sf.saxon.trans.XPathException; |
| 18 | import net.sf.saxon.value.SequenceType; |
| 19 | |
| 20 | import java.io.IOException; |
| 21 | import java.io.OutputStream; |
| 22 | import java.util.HashMap; |
| 23 | import java.util.Map; |
| 24 | |
| 25 | /** |
| 26 | * An XsltExecutable represents the compiled form of a stylesheet. |
| 27 | * To execute the stylesheet, it must first be loaded to form an {@link XsltTransformer}. |
| 28 | * <p>An XsltExecutable is immutable, and therefore thread-safe. |
| 29 | * It is simplest to load a new XsltTransformer each time the stylesheet is to be run. |
| 30 | * However, the XsltTransformer is serially reusable within a single thread. </p> |
| 31 | * <p>An XsltExecutable is created by using one of the <code>compile</code> methods on the |
| 32 | * {@link XsltCompiler} class.</p> |
| 33 | */ |
| 34 | public class XsltExecutable { |
| 35 | |
| 36 | Processor processor; |
| 37 | PreparedStylesheet preparedStylesheet; |
| 38 | |
| 39 | protected XsltExecutable(Processor processor, PreparedStylesheet preparedStylesheet) { |
| 40 | this.processor = processor; |
| 41 | this.preparedStylesheet = preparedStylesheet; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Get the Processor that was used to create this XsltExecutable |
| 46 | * |
| 47 | * @return the original Processor object |
| 48 | * @since 9.6 |
| 49 | */ |
| 50 | |
| 51 | public Processor getProcessor() { |
| 52 | return processor; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Load the stylesheet to prepare it for execution. This version of the load() method |
| 57 | * creates an <code>XsltTransformer</code> which offers interfaces for stylesheet |
| 58 | * invocation corresponding to those described in the XSLT 2.0 specification. It can be used |
| 59 | * with XSLT 2.0 or XSLT 3.0 stylesheets, but does not offer new XSLT 3.0 functionality such |
| 60 | * as the ability to supply parameters to the initial template, or the ability to invoke |
| 61 | * stylesheet-defined functions, or the ability to return an arbitrary sequence as a result |
| 62 | * without wrapping it in a document node. If such facilities are required, use the |
| 63 | * method {@link #load30()} in preference. |
| 64 | * |
| 65 | * @return An XsltTransformer. The returned XsltTransformer can be used to set up the |
| 66 | * dynamic context for stylesheet evaluation, and to run the stylesheet. |
| 67 | */ |
| 68 | |
| 69 | public XsltTransformer load() { |
| 70 | XsltTransformer xt = new XsltTransformer( |
| 71 | processor, preparedStylesheet.newController(), preparedStylesheet.getCompileTimeParams()); |
| 72 | StructuredQName initialTemplate = preparedStylesheet.getDefaultInitialTemplateName(); |
| 73 | if (initialTemplate != null) { |
| 74 | xt.setInitialTemplate(new QName(initialTemplate)); |
| 75 | } |
| 76 | return xt; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Load the stylesheet to prepare it for execution. This version of the load() method |
| 81 | * creates an <code>Xslt30Transformer</code> which offers interfaces for stylesheet |
| 82 | * invocation corresponding to those described in the XSLT 3.0 specification. It can be used |
| 83 | * with XSLT 2.0 or XSLT 3.0 stylesheets, and in both cases it offers new XSLT 3.0 functionality such |
| 84 | * as the ability to supply parameters to the initial template, or the ability to invoke |
| 85 | * stylesheet-defined functions, or the ability to return an arbitrary sequence as a result |
| 86 | * without wrapping it in a document node. |
| 87 | * |
| 88 | * @return An Xslt30Transformer. The returned Xslt30Transformer can be used to set up the |
| 89 | * dynamic context for stylesheet evaluation, and to run the stylesheet. |
| 90 | */ |
| 91 | |
| 92 | public Xslt30Transformer load30() { |
| 93 | return new Xslt30Transformer(processor, preparedStylesheet.newController(), preparedStylesheet.getCompileTimeParams()); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Produce a diagnostic representation of the compiled stylesheet, in XML form. |
| 98 | * <p><i>The detailed form of this representation is not stable (or even documented).</i></p> |
| 99 | * |
| 100 | * @param destination the destination for the XML document containing the diagnostic representation |
| 101 | * of the compiled stylesheet |
| 102 | * @since 9.1 |
| 103 | */ |
| 104 | |
| 105 | public void explain(Destination destination) throws SaxonApiException { |
| 106 | Configuration config = processor.getUnderlyingConfiguration(); |
| 107 | try { |
| 108 | Receiver out = destination.getReceiver(config.makePipelineConfiguration(), config.obtainDefaultSerializationProperties()); |
| 109 | preparedStylesheet.explain(new ExpressionPresenter(config, out)); |
| 110 | } catch (XPathException e) { |
| 111 | throw new SaxonApiException(e); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Produce a representation of the compiled stylesheet, in XML form, suitable for |
| 117 | * distribution and reloading. If the configuration under which the export takes place |
| 118 | * is suitably licensed, then license information will be included in the export file |
| 119 | * allowing execution of the stylesheet without any additional license. |
| 120 | * <p><i>The detailed form of the output representation is not documented.</i></p> |
| 121 | * |
| 122 | * @param destination the destination for the XML document containing the diagnostic representation |
| 123 | * of the compiled stylesheet. The stream will be closed when writing has finished. |
| 124 | * @throws SaxonApiException if the stylesheet was compiled with just-in-time compilation enabled. |
| 125 | * @since 9.7 |
| 126 | */ |
| 127 | |
| 128 | public void export(OutputStream destination) throws SaxonApiException { |
| 129 | String target = preparedStylesheet.getTopLevelPackage().getTargetEdition(); |
| 130 | if (target == null) { |
| 131 | target = getProcessor().getSaxonEdition(); |
| 132 | } |
| 133 | export(destination, target); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Produce a representation of the compiled stylesheet for a particular target environment, in XML form, suitable for |
| 138 | * distribution and reloading. If the configuration under which the export takes place |
| 139 | * is suitably licensed, then license information will be included in the export file |
| 140 | * allowing execution of the stylesheet without any additional license. |
| 141 | * <p><i>The detailed form of the output representation is not documented.</i></p> |
| 142 | * |
| 143 | * @param destination the destination for the XML document containing the diagnostic representation |
| 144 | * of the compiled stylesheet. The stream will be closed when writing has finished. |
| 145 | * @param target the target environment. The only values currently recognized are "JS" and "JS2, |
| 146 | * which export the package for running under Saxon-JS 1.0 or 2.0 respectively. |
| 147 | * @since 9.7 |
| 148 | */ |
| 149 | |
| 150 | public void export(OutputStream destination, String target) throws SaxonApiException { |
| 151 | Configuration config = processor.getUnderlyingConfiguration(); |
| 152 | try { |
| 153 | StylesheetPackage topLevelPackage = preparedStylesheet.getTopLevelPackage(); |
| 154 | ExpressionPresenter presenter = config.newExpressionExporter(target, destination, topLevelPackage); |
| 155 | presenter.setRelocatable(topLevelPackage.isRelocatable()); |
| 156 | topLevelPackage.export(presenter); |
| 157 | } catch (XPathException e) { |
| 158 | throw new SaxonApiException(e); |
| 159 | } |
| 160 | try { |
| 161 | destination.close(); |
| 162 | } catch (IOException e) { |
| 163 | throw new SaxonApiException(e); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Get the whitespace stripping policy defined by this stylesheet, that is, the policy |
| 169 | * defined by the xsl:strip-space and xsl:preserve-space elements in the top-level package |
| 170 | * of this stylesheet |
| 171 | * |
| 172 | * @return a newly constructed WhitespaceStrippingPolicy based on the declarations in |
| 173 | * the top-level package of this stylesheet. This policy can be used as input to a {@link DocumentBuilder}. |
| 174 | */ |
| 175 | |
| 176 | public WhitespaceStrippingPolicy getWhitespaceStrippingPolicy() { |
| 177 | StylesheetPackage top = preparedStylesheet.getTopLevelPackage(); |
| 178 | if (top.isStripsWhitespace()) { |
| 179 | return new WhitespaceStrippingPolicy(preparedStylesheet.getTopLevelPackage()); |
| 180 | } else { |
| 181 | return WhitespaceStrippingPolicy.UNSPECIFIED; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Get the names of the xsl:param elements defined in this stylesheet, with details |
| 187 | * of each parameter including its required type, and whether it is required or optional |
| 188 | * |
| 189 | * @return a Map whose keys are the names of global parameters in the stylesheet, |
| 190 | * and whose values are {@link ParameterDetails} objects giving information about the |
| 191 | * corresponding parameter. |
| 192 | * @since 9.3 |
| 193 | */ |
| 194 | |
| 195 | /*@NotNull*/ |
| 196 | public HashMap<QName, ParameterDetails> getGlobalParameters() { |
| 197 | Map<StructuredQName, GlobalParam> globals = preparedStylesheet.getGlobalParameters(); |
| 198 | HashMap<QName, ParameterDetails> params = new HashMap<>(); |
| 199 | for (GlobalParam v : globals.values()) { |
| 200 | ParameterDetails details = new ParameterDetails(v.getRequiredType(), v.isRequiredParam()); |
| 201 | params.put(new QName(v.getVariableQName()), details); |
| 202 | } |
| 203 | return params; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Inner class containing information about a global parameter to a compiled stylesheet |
| 208 | * |
| 209 | * @since 9.3 |
| 210 | */ |
| 211 | |
| 212 | public class ParameterDetails { |
| 213 | |
| 214 | private SequenceType type; |
| 215 | private boolean isRequired; |
| 216 | |
| 217 | protected ParameterDetails(SequenceType type, boolean isRequired) { |
| 218 | this.type = type; |
| 219 | this.isRequired = isRequired; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Get the declared item type of the parameter |
| 224 | * |
| 225 | * @return the type defined in the <code>as</code> attribute of the <code>xsl:param</code> element, |
| 226 | * without its occurrence indicator |
| 227 | */ |
| 228 | |
| 229 | public ItemType getDeclaredItemType() { |
| 230 | return new ConstructedItemType(type.getPrimaryType(), processor); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Get the declared cardinality of the parameter |
| 235 | * |
| 236 | * @return the occurrence indicator from the type appearing in the <code>as</code> attribute |
| 237 | * of the <code>xsl:param</code> element |
| 238 | */ |
| 239 | |
| 240 | public OccurrenceIndicator getDeclaredCardinality() { |
| 241 | return OccurrenceIndicator.getOccurrenceIndicator(type.getCardinality()); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * |
| 246 | */ |
| 247 | |
| 248 | public SequenceType getUnderlyingDeclaredType() { |
| 249 | return type; |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Ask whether the parameter is required (mandatory) or optional |
| 254 | * |
| 255 | * @return true if the parameter is mandatory (<code>required="yes"</code>), false |
| 256 | * if it is optional |
| 257 | */ |
| 258 | |
| 259 | public boolean isRequired() { |
| 260 | return this.isRequired; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | |
| 265 | /** |
| 266 | * Get the underlying implementation object representing the compiled stylesheet. This provides |
| 267 | * an escape hatch into lower-level APIs. The object returned by this method may change from release |
| 268 | * to release. |
| 269 | * |
| 270 | * @return the underlying implementation of the compiled stylesheet |
| 271 | */ |
| 272 | |
| 273 | public PreparedStylesheet getUnderlyingCompiledStylesheet() { |
| 274 | return preparedStylesheet; |
| 275 | } |
| 276 | |
| 277 | } |
| 278 |