Debian Codegraph @

net/sf/saxon/jaxp/StreamingTransformerImpl.java

raw symbols SCIP
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.Controller;
11 import net.sf.saxon.event.PipelineConfiguration;
12 import net.sf.saxon.event.Receiver;
13 import net.sf.saxon.event.ReceivingContentHandler;
14 import net.sf.saxon.event.TreeReceiver;
15 import net.sf.saxon.lib.SerializerFactory;
16 import net.sf.saxon.s9api.*;
17 import net.sf.saxon.serialize.SerializationProperties;
18 import net.sf.saxon.trans.XPathException;
19 import net.sf.saxon.trans.XsltController;
20 import org.xml.sax.XMLFilter;
21
22 import javax.xml.transform.*;
23 import javax.xml.transform.sax.TransformerHandler;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 /**
28 * Saxon implementation of the JAXP Transformer interface. This implementation of Transformer
29 * is used when the factory is a {@link com.saxonica.config.StreamingTransformerFactory}, and
30 * the effect is that the {@link #transform(Source, Result)} method accepts a StreamSource
31 * or SAXSource and processes it in streaming mode, assuming that the initial mode is labelled
32 * with <code>streamable="yes"</code>. The global context item in such a transformation will
33 * be absent, so referring to "." within a global variable is an error.
34 */
35 public class StreamingTransformerImpl extends AbstractTransformerImpl {
36
37 private Xslt30Transformer xsltTransformer;
38 private Map<QName, XdmValue> convertedParameters = new HashMap<QName, XdmValue>();
39
40 protected StreamingTransformerImpl(XsltExecutable e, Xslt30Transformer t) {
41 super(e);
42 this.xsltTransformer = t;
43 }
44
45 /**
46 * <p>Transform the XML <code>Source</code> to a <code>Result</code>.
47 * Specific transformation behavior is determined by the settings of the
48 * <code>TransformerFactory</code> in effect when the
49 * <code>Transformer</code> was instantiated and any modifications made to
50 * the <code>Transformer</code> instance.</p>
51 * <p>An empty <code>Source</code> is represented as an empty document
52 * as constructed by {@link javax.xml.parsers.DocumentBuilder#newDocument()}.
53 * The result of transforming an empty <code>Source</code> depends on
54 * the transformation behavior; it is not always an empty
55 * <code>Result</code>.</p>
56 *
57 * @param xmlSource The XML input to transform.
58 * @param outputTarget The <code>Result</code> of transforming the
59 * <code>xmlSource</code>.
60 * @throws javax.xml.transform.TransformerException
61 * If an unrecoverable error occurs
62 * during the course of the transformation.
63 */
64 @Override
65 public void transform(Source xmlSource, final Result outputTarget) throws XPathException {
66 try {
67 xsltTransformer.setStylesheetParameters(convertedParameters);
68 if (outputTarget.getSystemId() != null) { //bug 2214
69 xsltTransformer.setBaseOutputURI(outputTarget.getSystemId());
70 }
71 Destination destination = makeDestination(outputTarget);
72 if (destination == null) {
73 SerializerFactory sf = getConfiguration().getSerializerFactory();
74 Receiver r = sf.getReceiver(outputTarget, getConfiguration().makePipelineConfiguration(), getLocalOutputProperties());
75 transform(xmlSource, r);
76 return;
77 }
78
79 xsltTransformer.applyTemplates(xmlSource, destination);
80
81 } catch (SaxonApiException e) {
82 throw XPathException.makeXPathException(e);
83 }
84 }
85
86 @Override
87 protected void setConvertedParameter(QName name, XdmValue value) {
88 convertedParameters.put(name, value);
89 }
90
91 /**
92 * Clear all parameters set with setParameter.
93 */
94 @Override
95 public void clearParameters() {
96 super.clearParameters();
97 convertedParameters.clear();
98 }
99
100 /**
101 * Set an object that will be used to resolve URIs used in
102 * document().
103 * <p>If the resolver argument is null, the URIResolver value will
104 * be cleared and the transformer will no longer have a resolver.</p>
105 *
106 * @param resolver An object that implements the URIResolver interface,
107 * or null.
108 */
109 @Override
110 public void setURIResolver(URIResolver resolver) {
111 super.setURIResolver(resolver);
112 xsltTransformer.setURIResolver(resolver);
113 }
114
115
116 /**
117 * Set the error event listener in effect for the transformation.
118 *
119 * @param listener The new error listener.
120 * @throws IllegalArgumentException if listener is null.
121 */
122 @Override
123 public void setErrorListener(ErrorListener listener) throws IllegalArgumentException {
124 super.setErrorListener(listener);
125 xsltTransformer.setErrorListener(listener);
126 }
127
128 /**
129 * Supply an initial mode for a transformation.
130 * <p>This is a Saxon extension to the JAXP interface, needed for XSLT 2.0</p>
131 *
132 * @param name the name of the initial mode, in Clark notation (either a local name,
133 * or "{uri}local")
134 * @throws IllegalArgumentException if the argument is invalid, for example if the
135 * format of the name is incorrect or if there is no mode with this name
136 */
137
138 public void setInitialMode(String name) throws IllegalArgumentException {
139 xsltTransformer.setInitialMode(QName.fromClarkName(name));
140 }
141
142 /**
143 * Get the underlying s9api implementation class wrapped by this JAXP Transformer
144 * @return the underlying s9api XsltTransformer
145 */
146
147 public Xslt30Transformer getUnderlyingXsltTransformer() {
148 return xsltTransformer;
149 }
150
151 /**
152 * Get the internal Saxon Controller instance that implements this transformation.
153 * Note that the Controller interface will not necessarily remain stable in future releases
154 * @return the underlying Saxon Controller instance
155 */
156
157 public Controller getUnderlyingController() {
158 return xsltTransformer.getUnderlyingController();
159 }
160
161 /**
162 * Create a JAXP XMLFilter which allows this transformation to be added to a SAX pipeline
163 *
164 * @return the transformation in the form of an XMLFilter
165 */
166
167 public XMLFilter newXMLFilter() {
168 return new StreamingFilterImpl(xsltTransformer);
169 }
170
171 /**
172 * Get a TransformerHandler that can be used to run the transformation by feeding in SAX events
173 */
174
175 public TransformerHandler newTransformerHandler() throws XPathException {
176 XsltController controller = xsltTransformer.getUnderlyingController();
177 return new StreamingTransformerHandler(controller);
178 }
179
180 public class StreamingTransformerHandler extends ReceivingContentHandler implements TransformerHandler {
181
182 private XsltController controller;
183 private String systemId;
184 //private ProxyReceiver proxy;
185
186 public StreamingTransformerHandler(XsltController controller) {
187 this.controller = controller;
188
189 //setReceiver(proxy);
190 //setPipelineConfiguration(proxy.getPipelineConfiguration());
191 }
192
193 @Override
194 public void setResult(Result result) throws IllegalArgumentException {
195 try {
196 PipelineConfiguration pipe = controller.makePipelineConfiguration();
197 setPipelineConfiguration(pipe);
198 Receiver out = controller.getConfiguration().getSerializerFactory().getReceiver(
199 result, new SerializationProperties(), pipe);
200 Receiver in = controller.getStreamingReceiver(controller.getInitialMode(), new TreeReceiver(out));
201 setReceiver(in);
202 } catch (XPathException e) {
203 throw new IllegalArgumentException(e);
204 }
205 }
206
207 @Override
208 public void setSystemId(String systemID) {
209 this.systemId = systemID;
210 }
211
212 @Override
213 public String getSystemId() {
214 return systemId;
215 }
216
217 @Override
218 public Transformer getTransformer() {
219 return StreamingTransformerImpl.this;
220 }
221 }
222
223 }
224