Debian Codegraph @

net/sf/saxon/jaxp/SaxonTransformerFactory.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.Configuration;
11 import net.sf.saxon.lib.DelegatingErrorListener;
12 import net.sf.saxon.lib.Feature;
13 import net.sf.saxon.lib.FeatureKeys;
14 import net.sf.saxon.lib.UnfailingErrorListener;
15 import net.sf.saxon.s9api.Processor;
16 import net.sf.saxon.s9api.SaxonApiException;
17 import net.sf.saxon.s9api.XsltCompiler;
18 import net.sf.saxon.s9api.XsltExecutable;
19 import net.sf.saxon.trans.CompilerInfo;
20 import net.sf.saxon.trans.ConfigurationReader;
21 import net.sf.saxon.trans.XPathException;
22 import org.xml.sax.XMLFilter;
23
24 import javax.xml.transform.*;
25 import javax.xml.transform.dom.DOMResult;
26 import javax.xml.transform.dom.DOMSource;
27 import javax.xml.transform.sax.*;
28 import javax.xml.transform.stream.StreamResult;
29 import javax.xml.transform.stream.StreamSource;
30 import java.io.File;
31 import java.util.Comparator;
32
33
34 /**
35 * A SaxonTransformerFactory instance can be used to create Transformer and Template
36 * objects.
37 * <p>Although the class is public, it is not intended to be used directly by applications;
38 * applications should normally invoke the subclass {@link net.sf.saxon.TransformerFactoryImpl}.</p>
39 * <p>The system property that determines which Factory implementation
40 * to create is named "javax.xml.transform.TransformerFactory". This
41 * property names a concrete subclass of the TransformerFactory abstract
42 * class. If the property is not defined, a platform default is be used.</p>
43 * <p>This implementation class implements the abstract methods on both the
44 * javax.xml.transform.TransformerFactory and javax.xml.transform.sax.SAXTransformerFactory
45 * classes.</p>
46 * <p>Since Saxon 9.6, the JAXP transformation interface is re-implemented as a layer
47 * on top of the s9api interface. This will affect applications that attempt to
48 * down-cast from JAXP interfaces to the underlying implementation classes.</p>
49 */
50
51 public class SaxonTransformerFactory extends SAXTransformerFactory implements Configuration.ApiProvider {
52
53 private Processor processor;
54
55 /**
56 * Default constructor.
57 */
58 public SaxonTransformerFactory() {
59 processor = new Processor(true);
60 }
61
62 /**
63 * Construct a TransformerFactory using an existing Configuration.
64 *
65 * @param config the Saxon configuration
66 */
67 public SaxonTransformerFactory(Configuration config) {
68 processor = new Processor(config);
69 }
70
71 /**
72 * Set the configuration. This can also be done using the JAXP method
73 * setAttribute, with the attribute name {@link net.sf.saxon.lib.FeatureKeys#CONFIGURATION}
74 *
75 * @param config the Saxon configuration
76 */
77
78 public void setConfiguration(Configuration config) {
79 processor.setConfigurationProperty(Feature.CONFIGURATION, config);
80 }
81
82 /**
83 * Get the configuration. This can also be done using the JAXP method
84 * getAttribute, with the attribute name {@link net.sf.saxon.lib.FeatureKeys#CONFIGURATION}
85 * This is a trapdoor method that provides access to underlying implementation details that
86 * may change in subsequent Saxon releases.
87 * @return the Saxon configuration
88 */
89
90 public Configuration getConfiguration() {
91 return processor.getUnderlyingConfiguration();
92 }
93
94 /**
95 * Process the Source into a Transformer object.
96 * The Transformer object must not be used in multiple threads running concurrently.
97 * Different TransformerFactories can be used concurrently by different
98 * threads.
99 *
100 * @param source An object that holds a URI, input stream, etc. of the stylesheet
101 * used to perform the transformation.
102 * @return A Transformer object that may be used to perform a transformation
103 * in a single thread, never null.
104 * @throws javax.xml.transform.TransformerConfigurationException
105 * May throw this during the parse
106 * when it is constructing the Templates object and fails.
107 */
108
109 public Transformer newTransformer(Source source)
110 throws TransformerConfigurationException {
111 Templates templates = newTemplates(source);
112 return templates.newTransformer();
113 }
114
115 /**
116 * Create a new Transformer object that performs a copy
117 * of the source to the result.
118 *
119 * @return A Transformer object that may be used to perform a transformation
120 * in a single thread, never null.
121 */
122
123 public Transformer newTransformer() {
124 return new IdentityTransformer(processor.getUnderlyingConfiguration());
125 }
126
127
128 /**
129 * Process the Source into a Templates object, which is a
130 * a compiled representation of the source. This Templates object
131 * may then be used concurrently across multiple threads. Creating
132 * a Templates object allows the TransformerFactory to do detailed
133 * performance optimization of transformation instructions, without
134 * penalizing runtime transformation.
135 *
136 * @param source An object that holds a URL, input stream, etc.
137 * @return A Templates object capable of being used for transformation purposes,
138 * never null.
139 * @throws javax.xml.transform.TransformerConfigurationException
140 * May throw this during the parse when it
141 * is constructing the Templates object and fails.
142 */
143
144 public synchronized Templates newTemplates(Source source)
145 throws TransformerConfigurationException {
146
147 try {
148 XsltCompiler compiler = processor.newXsltCompiler();
149 XsltExecutable executable = compiler.compile(source);
150 return new TemplatesImpl(executable);
151 } catch (SaxonApiException e) {
152 throw new TransformerConfigurationException(e);
153 }
154 }
155
156 /**
157 * Process the Source into a Templates object, which is a
158 * a compiled representation of the source. This Templates object
159 * may then be used concurrently across multiple threads. Creating
160 * a Templates object allows the TransformerFactory to do detailed
161 * performance optimization of transformation instructions, without
162 * penalizing runtime transformation.
163 *
164 * @param source An object that holds a URL, input stream, etc.
165 * @param info compile-time options for this stylesheet compilation
166 * @return A Templates object capable of being used for transformation purposes,
167 * never null.
168 * @throws javax.xml.transform.TransformerConfigurationException
169 * May throw this during the parse when it
170 * is constructing the Templates object and fails.
171 */
172
173 public synchronized Templates newTemplates(Source source, CompilerInfo info)
174 throws TransformerConfigurationException {
175
176 try {
177 XsltCompiler compiler = processor.newXsltCompiler();
178 compiler.getUnderlyingCompilerInfo().copyFrom(info);
179 return new TemplatesImpl(compiler.compile(source));
180 } catch (SaxonApiException e) {
181 throw new TransformerConfigurationException(e);
182 }
183 }
184
185
186 /**
187 * Get the stylesheet specification(s) associated
188 * via the xml-stylesheet processing instruction (see
189 * http://www.w3.org/TR/xml-stylesheet/) with the document
190 * document specified in the source parameter, and that match
191 * the given criteria. Note that it is possible to return several
192 * stylesheets, in which case they are applied as if they were
193 * a list of imports or cascades.
194 *
195 * @param source The XML source document.
196 * @param media The media attribute to be matched. May be null, in which
197 * case the prefered templates will be used (i.e. alternate = no).
198 * Note that Saxon does not implement the complex CSS3-based syntax for
199 * media queries. By default, the media value is simply ignored. An algorithm for
200 * comparing the requested media with the declared media can be defined using
201 * the method {@link Configuration#setMediaQueryEvaluator(Comparator)}.
202 * @param title The value of the title attribute to match. May be null.
203 * @param charset The value of the charset attribute to match. May be null.
204 * @return A Source object suitable for passing to the TransformerFactory.
205 * @throws javax.xml.transform.TransformerConfigurationException
206 * if any problems occur
207 */
208
209 public Source getAssociatedStylesheet(
210 Source source, String media, String title, String charset)
211 throws TransformerConfigurationException {
212 try {
213 XsltCompiler compiler = processor.newXsltCompiler();
214 return compiler.getAssociatedStylesheet(source, media, title, charset);
215 } catch (SaxonApiException e) {
216 throw new TransformerConfigurationException(e);
217 }
218 }
219
220
221 /**
222 * Set an object that is used by default during the transformation
223 * to resolve URIs used in xsl:import, or xsl:include.
224 *
225 * @param resolver An object that implements the URIResolver interface,
226 * or null.
227 */
228
229 public void setURIResolver(URIResolver resolver) {
230 getConfiguration().setURIResolver(resolver);
231 }
232
233 /**
234 * Get the object that is used by default during the transformation
235 * to resolve URIs used in document(), xsl:import, or xsl:include.
236 *
237 * @return The URIResolver that was set with setURIResolver.
238 */
239
240 public URIResolver getURIResolver() {
241 return getConfiguration().getURIResolver();
242 }
243
244 //======= CONFIGURATION METHODS =======
245
246 private static final String FEATURE_SECURE_PROCESSING =
247 javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING;
248
249 /**
250 * Look up the value of a feature.
251 * <p>The feature name is any absolute URI.</p>
252 *
253 * @param name The feature name, which is an absolute URI.
254 * @return The current state of the feature (true or false).
255 */
256
257 public boolean getFeature(String name) {
258 switch (name) {
259 case SAXSource.FEATURE:
260 case SAXResult.FEATURE:
261 case DOMSource.FEATURE:
262 case DOMResult.FEATURE:
263 case StreamSource.FEATURE:
264 case StreamResult.FEATURE:
265 case SAXTransformerFactory.FEATURE:
266 case SAXTransformerFactory.FEATURE_XMLFILTER:
267 return true;
268 case FEATURE_SECURE_PROCESSING:
269 return !getConfiguration().getBooleanProperty(Feature.ALLOW_EXTERNAL_FUNCTIONS);
270 default:
271 try {
272 Object val = getConfiguration().getConfigurationProperty(name);
273 return val instanceof Boolean && (Boolean) val;
274 } catch (IllegalArgumentException err) {
275 return false;
276 }
277 }
278 }
279
280 /**
281 * Allows the user to set specific attributes on the underlying
282 * implementation. An attribute in this context is defined to
283 * be an option that the implementation provides.
284 *
285 * <p>Note: setting configuration properties using the method
286 * {@link Configuration#setConfigurationProperty(Feature, Object)}
287 * is more efficient, and gives better type safety.</p>
288 *
289 * @param name The name of the attribute. This must be one of the constants
290 * defined in class {@link net.sf.saxon.lib.FeatureKeys}.
291 * @param value The value of the attribute.
292 * @throws IllegalArgumentException thrown if Saxon
293 * doesn't recognize the attribute.
294 * @see net.sf.saxon.lib.FeatureKeys
295 */
296
297 public void setAttribute(String name, Object value) throws IllegalArgumentException {
298 switch (name) {
299 case FeatureKeys.CONFIGURATION:
300 setConfiguration((Configuration) value);
301 break;
302 case FeatureKeys.CONFIGURATION_FILE:
303 ConfigurationReader reader = new ConfigurationReader();
304 try {
305 setConfiguration(reader.makeConfiguration(new StreamSource(new File((String) value))));
306 } catch (XPathException err) {
307 throw new IllegalArgumentException(err);
308 }
309 break;
310 default:
311 processor.getUnderlyingConfiguration().setConfigurationProperty(name, value);
312 break;
313 }
314 }
315
316 /**
317 * Allows the user to retrieve specific attributes on the underlying
318 * implementation.
319 *
320 * @param name The name of the attribute. This must be one of the constants
321 * defined in class {@link net.sf.saxon.lib.FeatureKeys}.
322 * @return value The value of the attribute.
323 * @throws IllegalArgumentException thrown if the underlying
324 * implementation doesn't recognize the attribute.
325 */
326 /*@Nullable*/
327 public Object getAttribute(String name) throws IllegalArgumentException {
328 return getConfiguration().getConfigurationProperty(name);
329 }
330
331 /**
332 * Set the error event listener for the TransformerFactory, which
333 * is used for the processing of transformation instructions,
334 * and not for the transformation itself.
335 * <p>This method is defined in JAXP but its use with Saxon is deprecated,
336 * because the errorListener will be shared by all stylesheet compilations
337 * running under this factory, which may be operating concurrently
338 * in different threads.</p>
339 *
340 * @param listener The new error listener.
341 * @throws IllegalArgumentException if listener is null.
342 */
343
344 public void setErrorListener(ErrorListener listener)
345 throws IllegalArgumentException {
346 getConfiguration().setErrorListener(listener);
347 }
348
349 /**
350 * Get the error event handler for the TransformerFactory.
351 *
352 * @return The current error listener, which should never be null.
353 */
354 public ErrorListener getErrorListener() {
355 UnfailingErrorListener uel = getConfiguration().getErrorListener();
356 if (uel instanceof DelegatingErrorListener) {
357 return ((DelegatingErrorListener) uel).getBaseErrorListener();
358 } else {
359 return uel;
360 }
361 }
362
363
364 ///////////////////////////////////////////////////////////////////////////////
365 // Methods defined in class javax.xml.transform.sax.SAXTransformerFactory
366 ///////////////////////////////////////////////////////////////////////////////
367
368 /**
369 * Get a TransformerHandler object that can process SAX
370 * ContentHandler events into a Result, based on the transformation
371 * instructions specified by the argument.
372 *
373 * @param src The Source of the transformation instructions.
374 * @return TransformerHandler ready to transform SAX events.
375 * @throws javax.xml.transform.TransformerConfigurationException
376 * If for some reason the
377 * TransformerHandler can not be created.
378 */
379
380 public TransformerHandler newTransformerHandler(Source src)
381 throws TransformerConfigurationException {
382 Templates tmpl = newTemplates(src);
383 return newTransformerHandler(tmpl);
384 }
385
386 /**
387 * Get a TransformerHandler object that can process SAX
388 * ContentHandler events into a Result, based on the Templates argument.
389 *
390 * @param templates The compiled transformation instructions.
391 * @return TransformerHandler ready to transform SAX events.
392 * @throws javax.xml.transform.TransformerConfigurationException
393 * If for some reason the
394 * TransformerHandler can not be created.
395 */
396
397 public TransformerHandler newTransformerHandler(Templates templates)
398 throws TransformerConfigurationException {
399 if (!(templates instanceof TemplatesImpl)) {
400 throw new TransformerConfigurationException("Templates object was not created by Saxon");
401 }
402 TransformerImpl transformer = (TransformerImpl) templates.newTransformer();
403 return transformer.newTransformerHandler();
404 }
405
406 /**
407 * Get a TransformerHandler object that can process SAX
408 * ContentHandler events into a Result. The transformation
409 * is defined as an identity (or copy) transformation, for example
410 * to copy a series of SAX parse events into a DOM tree.
411 *
412 * @return A non-null reference to a TransformerHandler, that may
413 * be used as a ContentHandler for SAX parse events.
414 */
415
416 public TransformerHandler newTransformerHandler() {
417 IdentityTransformer transformer = new IdentityTransformer(getConfiguration());
418 return new IdentityTransformerHandler(transformer);
419 }
420
421 /**
422 * Get a TemplatesHandler object that can process SAX
423 * ContentHandler events into a Templates object.
424 *
425 * @return A non-null reference to a TransformerHandler, that may
426 * be used as a ContentHandler for SAX parse events.
427 */
428
429 public TemplatesHandler newTemplatesHandler() {
430 return new TemplatesHandlerImpl(processor);
431 }
432
433 /**
434 * Create an XMLFilter that uses the given Source as the
435 * transformation instructions.
436 *
437 * @param src The Source of the transformation instructions.
438 * @return An XMLFilter object, or null if this feature is not supported.
439 * @throws javax.xml.transform.TransformerConfigurationException
440 * If for some reason the
441 * XMLFilter cannot be created.
442 */
443
444 public XMLFilter newXMLFilter(Source src)
445 throws TransformerConfigurationException {
446 Templates tmpl = newTemplates(src);
447 return newXMLFilter(tmpl);
448 }
449
450 /**
451 * Create an XMLFilter, based on the Templates argument..
452 *
453 * @param templates The compiled transformation instructions.
454 * @return An XMLFilter object.
455 * @throws javax.xml.transform.TransformerConfigurationException
456 * if (for example) the <code>templates</code> object was not created by Saxon.
457 */
458
459 public XMLFilter newXMLFilter(Templates templates)
460 throws TransformerConfigurationException {
461 if (!(templates instanceof TemplatesImpl)) {
462 throw new TransformerConfigurationException("Supplied Templates object was not created using Saxon");
463 }
464 AbstractTransformerImpl transformer = (AbstractTransformerImpl) templates.newTransformer();
465 return transformer.newXMLFilter();
466 }
467
468 /**
469 * <p>Set a feature for this <code>TransformerFactory</code> and <code>Transformer</code>s
470 * or <code>Template</code>s created by this factory.</p>
471 * <p>Feature names are fully qualified {@link java.net.URI}s.
472 * Implementations may define their own features.
473 * An {@link javax.xml.transform.TransformerConfigurationException} is thrown if this <code>TransformerFactory</code> or the
474 * <code>Transformer</code>s or <code>Template</code>s it creates cannot support the feature.
475 * It is possible for an <code>TransformerFactory</code> to expose a feature value but be unable to change its state.</p>
476 * <p>All implementations are required to support the FEATURE_SECURE_PROCESSING feature.
477 * When the feature is:</p>
478 * <ul>
479 * <li>
480 * <code>true</code>: the implementation will limit XML processing to conform to implementation limits
481 * and behave in a secure fashion as defined by the implementation.
482 * Examples include resolving user defined style sheets and functions.
483 * If XML processing is limited for security reasons, it will be reported via a call to the registered
484 * {@link javax.xml.transform.ErrorListener#fatalError(javax.xml.transform.TransformerException exception)}.
485 * See {@link #setErrorListener(javax.xml.transform.ErrorListener listener)}. In the Saxon implementation,
486 * this option causes calls on extension functions and extensions instructions to be disabled, and also
487 * disables the use of xsl:result-document to write to secondary output destinations.
488 * </li>
489 * <li>
490 * <code>false</code>: the implementation will processing XML according to the XML specifications without
491 * regard to possible implementation limits.
492 * </li>
493 * </ul>
494 *
495 * @param name Feature name.
496 * @param value Is feature state <code>true</code> or <code>false</code>.
497 * @throws javax.xml.transform.TransformerConfigurationException
498 * if this <code>TransformerFactory</code>
499 * or the <code>Transformer</code>s or <code>Template</code>s it creates cannot support this feature.
500 * @throws NullPointerException If the <code>name</code> parameter is null.
501 */
502
503 public void setFeature(String name, boolean value) throws TransformerConfigurationException {
504 if (name.equals(FEATURE_SECURE_PROCESSING)) {
505 getConfiguration().setBooleanProperty(Feature.ALLOW_EXTERNAL_FUNCTIONS, !value);
506 } else {
507 try {
508 getConfiguration().setBooleanProperty(name, value);
509 } catch (IllegalArgumentException err) {
510 throw new TransformerConfigurationException("Unsupported TransformerFactory feature: " + name);
511 }
512 }
513 }
514
515 /**
516 * Get the underlying s9api Processor. (Trapdoor method providing access to underlying
517 * implementation details which may change in subsequent releases)
518 *
519 * @return the processor
520 * @since 9.8.0.5
521 */
522
523 public Processor getProcessor() {
524 return processor;
525 }
526
527 }
528