| 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.event.Receiver; |
| 11 | import net.sf.saxon.lib.SerializerFactory; |
| 12 | import net.sf.saxon.s9api.*; |
| 13 | import net.sf.saxon.trans.XPathException; |
| 14 | import net.sf.saxon.trans.XsltController; |
| 15 | import org.xml.sax.XMLFilter; |
| 16 | |
| 17 | import javax.xml.transform.ErrorListener; |
| 18 | import javax.xml.transform.Result; |
| 19 | import javax.xml.transform.Source; |
| 20 | import javax.xml.transform.URIResolver; |
| 21 | import javax.xml.transform.sax.TransformerHandler; |
| 22 | |
| 23 | /** |
| 24 | * Saxon implementation of the JAXP Transformer interface. |
| 25 | * <p>Since Saxon 9.6, JAXP interfaces are implemented as a layer above the s9api interface</p> |
| 26 | */ |
| 27 | public class TransformerImpl extends AbstractTransformerImpl { |
| 28 | |
| 29 | private XsltTransformer xsltTransformer; |
| 30 | |
| 31 | protected TransformerImpl(XsltExecutable e, XsltTransformer t) { |
| 32 | super(e); |
| 33 | this.xsltTransformer = t; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * <p>Transform the XML <code>Source</code> to a <code>Result</code>. |
| 38 | * Specific transformation behavior is determined by the settings of the |
| 39 | * <code>TransformerFactory</code> in effect when the |
| 40 | * <code>Transformer</code> was instantiated and any modifications made to |
| 41 | * the <code>Transformer</code> instance.</p> |
| 42 | * <p>An empty <code>Source</code> is represented as an empty document |
| 43 | * as constructed by {@link javax.xml.parsers.DocumentBuilder#newDocument()}. |
| 44 | * The result of transforming an empty <code>Source</code> depends on |
| 45 | * the transformation behavior; it is not always an empty |
| 46 | * <code>Result</code>.</p> |
| 47 | * |
| 48 | * @param xmlSource The XML input to transform. |
| 49 | * @param outputTarget The <code>Result</code> of transforming the |
| 50 | * <code>xmlSource</code>. |
| 51 | * @throws XPathException |
| 52 | * If an unrecoverable error occurs |
| 53 | * during the course of the transformation. |
| 54 | */ |
| 55 | @Override |
| 56 | public void transform(Source xmlSource, final Result outputTarget) throws XPathException { |
| 57 | try { |
| 58 | xsltTransformer.setSource(xmlSource); |
| 59 | if (outputTarget.getSystemId() != null) { //bug 2214 |
| 60 | xsltTransformer.setBaseOutputURI(outputTarget.getSystemId()); |
| 61 | } |
| 62 | Destination destination = makeDestination(outputTarget); |
| 63 | if (destination == null) { |
| 64 | SerializerFactory sf = getConfiguration().getSerializerFactory(); |
| 65 | Receiver r = sf.getReceiver(outputTarget, getConfiguration().makePipelineConfiguration(), getLocalOutputProperties()); |
| 66 | transform(xmlSource, r); |
| 67 | return; |
| 68 | } |
| 69 | xsltTransformer.setDestination(destination); |
| 70 | |
| 71 | xsltTransformer.transform(); |
| 72 | if (destination instanceof Serializer && ((Serializer)destination).isMustCloseAfterUse()) { |
| 73 | destination.close(); |
| 74 | } |
| 75 | } catch (SaxonApiException e) { |
| 76 | throw XPathException.makeXPathException(e); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | protected void setConvertedParameter(QName name, XdmValue value) { |
| 82 | xsltTransformer.setParameter(name, value); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Clear all parameters set with setParameter. |
| 87 | */ |
| 88 | @Override |
| 89 | public void clearParameters() { |
| 90 | super.clearParameters(); |
| 91 | xsltTransformer.clearParameters(); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Set an object that will be used to resolve URIs used in |
| 96 | * document(). |
| 97 | * <p>If the resolver argument is null, the URIResolver value will |
| 98 | * be cleared and the transformer will no longer have a resolver.</p> |
| 99 | * |
| 100 | * @param resolver An object that implements the URIResolver interface, |
| 101 | * or null. |
| 102 | */ |
| 103 | @Override |
| 104 | public void setURIResolver(URIResolver resolver) { |
| 105 | super.setURIResolver(resolver); |
| 106 | xsltTransformer.setURIResolver(resolver); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | /** |
| 111 | * Set the error event listener in effect for the transformation. |
| 112 | * |
| 113 | * @param listener The new error listener. |
| 114 | * @throws IllegalArgumentException if listener is null. |
| 115 | */ |
| 116 | @Override |
| 117 | public void setErrorListener(ErrorListener listener) throws IllegalArgumentException { |
| 118 | super.setErrorListener(listener); |
| 119 | xsltTransformer.setErrorListener(listener); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Supply an initial template for a transformation. |
| 124 | * <p>This is a Saxon extension to the JAXP interface, needed for XSLT 2.0</p> |
| 125 | * <p>Changed in 9.9 so it no longer validates the supplied name; an incorrect name will |
| 126 | * lead to an error later.</p> |
| 127 | * |
| 128 | * @param name the name of the initial template, in Clark notation (either a local name, |
| 129 | * or "{uri}local") |
| 130 | */ |
| 131 | |
| 132 | public void setInitialTemplate(String name) { |
| 133 | xsltTransformer.setInitialTemplate(QName.fromClarkName(name)); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Supply an initial mode for a transformation. |
| 138 | * <p>This is a Saxon extension to the JAXP interface, needed for XSLT 2.0</p> |
| 139 | * |
| 140 | * @param name the name of the initial mode, in Clark notation (either a local name, |
| 141 | * or "{uri}local") |
| 142 | * @throws IllegalArgumentException if the argument is invalid, for example if the |
| 143 | * format of the name is incorrect or if there is no mode with this name |
| 144 | */ |
| 145 | |
| 146 | public void setInitialMode(String name) throws IllegalArgumentException { |
| 147 | xsltTransformer.setInitialMode(QName.fromClarkName(name)); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Get the underlying s9api implementation class wrapped by this JAXP Transformer |
| 152 | * @return the underlying s9api XsltTransformer |
| 153 | */ |
| 154 | |
| 155 | public XsltTransformer getUnderlyingXsltTransformer() { |
| 156 | return xsltTransformer; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Get the internal Saxon Controller instance that implements this transformation. |
| 161 | * Note that the Controller interface will not necessarily remain stable in future releases |
| 162 | * @return the underlying Saxon Controller instance |
| 163 | */ |
| 164 | |
| 165 | public XsltController getUnderlyingController() { |
| 166 | return xsltTransformer.getUnderlyingController(); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Create a JAXP TransformerHandler to perform the transformation |
| 171 | * @return a JAXP TransformerHandler, which allows the transformation to be performed |
| 172 | * in "push" mode on a SAX pipeline. |
| 173 | */ |
| 174 | |
| 175 | public TransformerHandler newTransformerHandler() { |
| 176 | return new TransformerHandlerImpl(this); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Create a JAXP XMLFilter which allows this transformation to be added to a SAX pipeline |
| 181 | * @return the transformation in the form of an XMLFilter |
| 182 | */ |
| 183 | |
| 184 | public XMLFilter newXMLFilter() { |
| 185 | return new FilterImpl(this); |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public void reset() { |
| 190 | super.reset(); |
| 191 | getUnderlyingController().reset(); |
| 192 | } |
| 193 | |
| 194 | } |
| 195 |