| 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.Query; |
| 13 | import net.sf.saxon.style.Compilation; |
| 14 | import net.sf.saxon.style.PackageVersion; |
| 15 | import net.sf.saxon.style.StylesheetPackage; |
| 16 | import net.sf.saxon.trace.ExpressionPresenter; |
| 17 | import net.sf.saxon.trans.CompilerInfo; |
| 18 | import net.sf.saxon.trans.XPathException; |
| 19 | |
| 20 | import java.io.File; |
| 21 | import java.io.FileOutputStream; |
| 22 | import java.io.IOException; |
| 23 | |
| 24 | /** |
| 25 | * An XsltPackage object represents the result of compiling an XSLT 3.0 package, as |
| 26 | * represented by an XML document containing an <code>xsl:package</code> element. |
| 27 | * |
| 28 | * @since 9.6 |
| 29 | */ |
| 30 | |
| 31 | public class XsltPackage { |
| 32 | |
| 33 | private XsltCompiler compiler; |
| 34 | private StylesheetPackage stylesheetPackage; |
| 35 | |
| 36 | protected XsltPackage(XsltCompiler compiler, StylesheetPackage pp) { |
| 37 | this.compiler = compiler; |
| 38 | this.stylesheetPackage = pp; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Get the processor under which this package was created |
| 43 | * |
| 44 | * @return the corresponding Processor |
| 45 | */ |
| 46 | |
| 47 | public Processor getProcessor() { |
| 48 | return compiler.getProcessor(); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Get the name of the package (the URI appearing as the value of <code>xsl:package/@name</code>) |
| 53 | * |
| 54 | * @return the package name |
| 55 | */ |
| 56 | |
| 57 | public String getName() { |
| 58 | return stylesheetPackage.getPackageName(); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Get the version number of the package (the value of the attribute <code>xsl:package/@package-version</code>. |
| 63 | * Note that this may have had trailing zeroes truncated. |
| 64 | * |
| 65 | * @return the package version number(s)+suffix? |
| 66 | */ |
| 67 | |
| 68 | public String getVersion() { |
| 69 | return stylesheetPackage.getPackageVersion().toString(); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Get the version of the package as a structured object that meets the requirements of |
| 74 | * <a href="http://www.w3.org/TR/xslt-30/#package-versions">http://www.w3.org/TR/xslt-30/#package-versions</a> |
| 75 | * |
| 76 | * @return the package version of the stylesheet |
| 77 | */ |
| 78 | public PackageVersion getPackageVersion() { |
| 79 | return stylesheetPackage.getPackageVersion(); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Get the whitespace stripping policy defined by this stylesheet package, that is, the policy |
| 84 | * defined by the xsl:strip-space and xsl:preserve-space elements in the source XSLT code of the package. |
| 85 | * |
| 86 | * @return a newly constructed WhitespaceStrippingPolicy based on the declarations in |
| 87 | * this stylesheet package. This policy can be used as input to a {@link DocumentBuilder}. |
| 88 | */ |
| 89 | |
| 90 | public WhitespaceStrippingPolicy getWhitespaceStrippingPolicy() { |
| 91 | return new WhitespaceStrippingPolicy(stylesheetPackage); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | /** |
| 96 | * Link this package with the packages it uses, to form an executable stylesheet. This process fixes |
| 97 | * up any cross-package references to files, templates, and other components, and checks to ensure |
| 98 | * that all such references are consistent. |
| 99 | * |
| 100 | * @return the resulting XsltExecutable |
| 101 | * @throws SaxonApiException if any error is found during the linking process, for example |
| 102 | * if the constituent packages containing duplicate component names, or if abstract components |
| 103 | * are not resolved. |
| 104 | */ |
| 105 | |
| 106 | public XsltExecutable link() throws SaxonApiException { |
| 107 | try { |
| 108 | Configuration config = getProcessor().getUnderlyingConfiguration(); |
| 109 | CompilerInfo info = compiler.getUnderlyingCompilerInfo(); |
| 110 | Compilation compilation = new Compilation(config, info); |
| 111 | compilation.setPackageData(stylesheetPackage); |
| 112 | stylesheetPackage.checkForAbstractComponents(); |
| 113 | PreparedStylesheet pss = new PreparedStylesheet(compilation); |
| 114 | stylesheetPackage.updatePreparedStylesheet(pss); |
| 115 | pss.addPackage(stylesheetPackage); |
| 116 | return new XsltExecutable(getProcessor(), pss); |
| 117 | } catch (XPathException e) { |
| 118 | throw new SaxonApiException(e); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Save this compiled package to filestore. |
| 124 | * |
| 125 | * @param file the file to which the compiled package should be saved |
| 126 | * @throws SaxonApiException if the compiled package cannot be saved to the specified |
| 127 | * location, or if the package was compiled with just-in-time |
| 128 | * compilation enabled |
| 129 | * @since 9.7 |
| 130 | */ |
| 131 | |
| 132 | public void save(File file) throws SaxonApiException { |
| 133 | String target = stylesheetPackage.getTargetEdition(); |
| 134 | if (target == null) { |
| 135 | target = getProcessor().getSaxonEdition(); |
| 136 | } |
| 137 | save(file, target); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Save this compiled package to filestore for a particular target environment |
| 142 | * |
| 143 | * @param file the file to which the compiled package should be saved |
| 144 | * @param target the target environment. The only values currently recognized are "JS" and "JS2, |
| 145 | * which export the package for running under Saxon-JS 1.0 or 2.0 respectively. |
| 146 | * @throws SaxonApiException if the compiled package cannot be saved to the specified |
| 147 | * location, or if the package was compiled with just-in-time |
| 148 | * compilation enabled. |
| 149 | * @since 9.7.0.5 |
| 150 | * @deprecated since 9.9.1.3. Use XsltCompiler.setTargetEdition() to define the target environment. |
| 151 | */ |
| 152 | |
| 153 | public void save(File file, String target) throws SaxonApiException { |
| 154 | try { |
| 155 | Query.createFileIfNecessary(file); |
| 156 | ExpressionPresenter presenter = getProcessor().getUnderlyingConfiguration() |
| 157 | .newExpressionExporter(target, new FileOutputStream(file), stylesheetPackage); |
| 158 | presenter.setRelocatable(stylesheetPackage.isRelocatable()); |
| 159 | stylesheetPackage.export(presenter); |
| 160 | } catch (XPathException | IOException e) { |
| 161 | throw new SaxonApiException(e); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Escape-hatch interface to the underlying implementation class. |
| 167 | * @return the underlying StylesheetPackage. The interface to StylesheetPackage |
| 168 | * is not a stable part of the s9api API definition. |
| 169 | */ |
| 170 | |
| 171 | public StylesheetPackage getUnderlyingPreparedPackage() { |
| 172 | return stylesheetPackage; |
| 173 | } |
| 174 | } |
| 175 |