KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > TransformerFactoryImpl


1 package net.sf.saxon;
2
3 import net.sf.saxon.event.PIGrabber;
4 import net.sf.saxon.event.Sender;
5 import net.sf.saxon.om.NamePool;
6 import net.sf.saxon.om.NamespaceConstant;
7 import net.sf.saxon.trace.TraceListener;
8 import net.sf.saxon.trans.XPathException;
9 import net.sf.saxon.sort.CollationURIResolver;
10 import org.xml.sax.InputSource JavaDoc;
11 import org.xml.sax.XMLFilter JavaDoc;
12
13 import javax.xml.transform.*;
14 import javax.xml.transform.dom.DOMResult JavaDoc;
15 import javax.xml.transform.dom.DOMSource JavaDoc;
16 import javax.xml.transform.sax.*;
17 import javax.xml.transform.stream.StreamResult JavaDoc;
18 import javax.xml.transform.stream.StreamSource JavaDoc;
19 import java.io.StringReader JavaDoc;
20 import java.util.List JavaDoc;
21
22
23 /**
24  * A TransformerFactoryImpl instance can be used to create Transformer and Template
25  * objects.
26  *
27  * <p>The system property that determines which Factory implementation
28  * to create is named "javax.xml.transform.TransformerFactory". This
29  * property names a concrete subclass of the TransformerFactory abstract
30  * class. If the property is not defined, a platform default is be used.</p>
31  *
32  * <p>This implementation class implements the abstract methods on both the
33  * javax.xml.transform.TransformerFactory and javax.xml.transform.sax.SAXTransformerFactory
34  * classes.
35  */

36
37 public class TransformerFactoryImpl extends SAXTransformerFactory {
38
39     private Configuration config;
40
41     /**
42      * Default constructor.
43      */

44     public TransformerFactoryImpl() {
45         config = new Configuration();
46     }
47
48     /**
49      * Construct a TransformerFactory using an existing Configuration.
50      */

51     public TransformerFactoryImpl(Configuration config) {
52         this.config = config;
53     }
54
55     /**
56      * Set the configuration (en bloc)
57      */

58
59     public void setConfiguration(Configuration config) {
60         this.config = config;
61     }
62
63     /**
64      * Get the configuration (en bloc)
65      */

66
67     public Configuration getConfiguration() {
68         return config;
69     }
70
71     /**
72      * Process the Source into a Transformer object. Care must
73      * be given not to use this object in multiple threads running concurrently.
74      * Different TransformerFactories can be used concurrently by different
75      * threads.
76      *
77      * @param source An object that holds a URI, input stream, etc.
78      *
79      * @return A Transformer object that may be used to perform a transformation
80      * in a single thread, never null.
81      *
82      * @exception TransformerConfigurationException May throw this during the parse
83      * when it is constructing the Templates object and fails.
84      */

85
86     public Transformer newTransformer(Source JavaDoc source)
87             throws TransformerConfigurationException {
88         Templates templates = newTemplates(source);
89         Transformer trans = templates.newTransformer();
90         return trans;
91     }
92
93     /**
94      * Create a new Transformer object that performs a copy
95      * of the source to the result.
96      *
97      * @return A Transformer object that may be used to perform a transformation
98      * in a single thread, never null.
99      *
100      * @exception TransformerConfigurationException May throw this during
101      * the parse when it is constructing the
102      * Templates object and fails.
103      */

104
105     public Transformer newTransformer()
106                     throws TransformerConfigurationException {
107
108         return new IdentityTransformer(config);
109     }
110
111
112     /**
113      * Process the Source into a Templates object, which is a
114      * a compiled representation of the source. This Templates object
115      * may then be used concurrently across multiple threads. Creating
116      * a Templates object allows the TransformerFactory to do detailed
117      * performance optimization of transformation instructions, without
118      * penalizing runtime transformation.
119      *
120      * @param source An object that holds a URL, input stream, etc.
121      *
122      * @return A Templates object capable of being used for transformation purposes,
123      * never null.
124      *
125      * @exception TransformerConfigurationException May throw this during the parse when it
126      * is constructing the Templates object and fails.
127      */

128
129     public Templates newTemplates(Source JavaDoc source)
130         throws TransformerConfigurationException {
131
132         PreparedStylesheet pss = new PreparedStylesheet(config);
133         pss.prepare(source);
134         return pss;
135     }
136
137     /**
138      * Get the stylesheet specification(s) associated
139      * via the xml-stylesheet processing instruction (see
140      * http://www.w3.org/TR/xml-stylesheet/) with the document
141      * document specified in the source parameter, and that match
142      * the given criteria. Note that it is possible to return several
143      * stylesheets, in which case they are applied as if they were
144      * a list of imports or cascades.
145      *
146      * @param source The XML source document.
147      * @param media The media attribute to be matched. May be null, in which
148      * case the prefered templates will be used (i.e. alternate = no).
149      * @param title The value of the title attribute to match. May be null.
150      * @param charset The value of the charset attribute to match. May be null.
151      *
152      * @return A Source object suitable for passing to the TransformerFactory.
153      *
154      * @throws TransformerConfigurationException if any problems occur
155      */

156
157     public Source JavaDoc getAssociatedStylesheet(
158         Source JavaDoc source, String JavaDoc media, String JavaDoc title, String JavaDoc charset)
159             throws TransformerConfigurationException {
160
161
162         PIGrabber grabber = new PIGrabber();
163         grabber.setFactory(config);
164         grabber.setCriteria(media, title, charset);
165         grabber.setBaseURI(source.getSystemId());
166         grabber.setURIResolver(config.getURIResolver());
167
168
169         try {
170             new Sender(config.makePipelineConfiguration()).send(source, grabber);
171             // this parse will be aborted when the first start tag is found
172
} catch (XPathException err) {
173             if (grabber.isTerminated()) {
174                 // do nothing
175
} else {
176                 throw new TransformerConfigurationException(
177                         "Failed while looking for xml-stylesheet PI", err);
178             }
179         }
180
181         try {
182             Source JavaDoc[] sources = grabber.getAssociatedStylesheets();
183             if (sources==null) {
184                 throw new TransformerConfigurationException(
185                     "No matching <?xml-stylesheet?> processing instruction found");
186             }
187             return compositeStylesheet(source.getSystemId(), sources);
188         } catch (TransformerException err) {
189             if (err instanceof TransformerConfigurationException) {
190                 throw (TransformerConfigurationException)err;
191             } else {
192                 throw new TransformerConfigurationException(err);
193             }
194         }
195     }
196
197     /**
198     * Process a series of stylesheet inputs, treating them in import or cascade
199     * order. This is mainly for support of the getAssociatedStylesheets
200     * method, but may be useful for other purposes.
201     *
202     * @param sources An array of Source objects representing individual stylesheets.
203     * @return A Source object representing a composite stylesheet.
204     */

205
206     private Source JavaDoc compositeStylesheet(String JavaDoc baseURI, Source JavaDoc[] sources)
207                         throws TransformerConfigurationException {
208
209         if (sources.length == 1) {
210             return sources[0];
211         } else if (sources.length == 0) {
212             throw new TransformerConfigurationException(
213                             "No stylesheets were supplied");
214         }
215
216         // create a new top-level stylesheet that imports all the others
217

218         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(250);
219         sb.append("<xsl:stylesheet version='1.0' ");
220         sb.append(" xmlns:xsl='" + NamespaceConstant.XSLT + "'>");
221         for (int i=0; i<sources.length; i++) {
222             sb.append("<xsl:import HREF='" + sources[i].getSystemId() + "'/>");
223         }
224         sb.append("</xsl:stylesheet>");
225         InputSource JavaDoc composite = new InputSource JavaDoc();
226         composite.setSystemId(baseURI);
227         composite.setCharacterStream(new StringReader JavaDoc(sb.toString()));
228         return new SAXSource(config.getSourceParser(), composite);
229     }
230
231     /**
232      * Set an object that is used by default during the transformation
233      * to resolve URIs used in xsl:import, or xsl:include.
234      *
235      * @param resolver An object that implements the URIResolver interface,
236      * or null.
237      */

238
239     public void setURIResolver(URIResolver resolver) {
240         config.setURIResolver(resolver);
241     }
242
243     /**
244      * Get the object that is used by default during the transformation
245      * to resolve URIs used in document(), xsl:import, or xsl:include.
246      *
247      * @return The URIResolver that was set with setURIResolver.
248      */

249
250     public URIResolver getURIResolver() {
251         return config.getURIResolver();
252     }
253
254     //======= CONFIGURATION METHODS =======
255

256     private static final String JavaDoc FEATURE_SECURE_PROCESSING =
257             //javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING;
258
"http://javax.xml.XMLConstants/feature/secure-processing";
259                     // Avoid reference to this JDK 1.5 constant
260

261
262     /**
263      * Look up the value of a feature.
264      *
265      * <p>The feature name is any absolute URI.</p>
266      * @param name The feature name, which is an absolute URI.
267      * @return The current state of the feature (true or false).
268      */

269
270     public boolean getFeature(String JavaDoc name) {
271         if (name.equals(SAXSource.FEATURE)) return true;
272         if (name.equals(SAXResult.FEATURE)) return true;
273         if (name.equals(DOMSource.FEATURE)) return isDOMAvailable();
274         if (name.equals(DOMResult.FEATURE)) return isDOMAvailable();
275         if (name.equals(StreamSource.FEATURE)) return true;
276         if (name.equals(StreamResult.FEATURE)) return true;
277         if (name.equals(SAXTransformerFactory.FEATURE)) return true;
278         if (name.equals(SAXTransformerFactory.FEATURE_XMLFILTER)) return true;
279         if (name.equals(FEATURE_SECURE_PROCESSING)) {
280             return !config.isAllowExternalFunctions();
281         }
282         throw new IllegalArgumentException JavaDoc("Unknown feature " + name);
283     }
284
285     /**
286      * Test whether DOM processing is available
287      */

288
289     private boolean isDOMAvailable() {
290         List JavaDoc models = config.getExternalObjectModels();
291         for (int i=0; i<models.size(); i++) {
292             if (models.get(i).getClass().getName().equals("net.sf.saxon.dom.DOMObjectModel")) {
293                 return true;
294             }
295         }
296         return false;
297     }
298
299     /**
300      * Allows the user to set specific attributes on the underlying
301      * implementation. An attribute in this context is defined to
302      * be an option that the implementation provides.
303      *
304      * @param name The name of the attribute. This must be one of the constants
305      * defined in class net.sf.saxon.FeatureKeys.
306      * @param value The value of the attribute.
307      * @throws IllegalArgumentException thrown if Saxon
308      * doesn't recognize the attribute.
309      * @see net.sf.saxon.FeatureKeys
310      */

311
312     public void setAttribute(String JavaDoc name, Object JavaDoc value)
313                                     throws IllegalArgumentException JavaDoc {
314         if (name.equals(FeatureKeys.TREE_MODEL)) {
315             if (!(value instanceof Integer JavaDoc)) {
316                 throw new IllegalArgumentException JavaDoc("Tree model must be an Integer");
317             }
318             config.setTreeModel(((Integer JavaDoc)value).intValue());
319
320         } else if (name.equals(FeatureKeys.ALLOW_EXTERNAL_FUNCTIONS)) {
321             if (!(value instanceof Boolean JavaDoc)) {
322                 throw new IllegalArgumentException JavaDoc("allow-external-functions must be a boolean");
323             }
324             config.setAllowExternalFunctions(((Boolean JavaDoc)value).booleanValue());
325
326         } else if (name.equals(FeatureKeys.RECOGNIZE_URI_QUERY_PARAMETERS)) {
327             if (!(value instanceof Boolean JavaDoc)) {
328                 throw new IllegalArgumentException JavaDoc("recognize-uri-query-parameters must be a boolean");
329             }
330             config.getSystemURIResolver().setRecognizeQueryParameters(((Boolean JavaDoc)value).booleanValue());
331
332         } else if (name.equals(FeatureKeys.TRACE_EXTERNAL_FUNCTIONS)) {
333             if (!(value instanceof Boolean JavaDoc)) {
334                 throw new IllegalArgumentException JavaDoc("trace-external-functions must be a boolean");
335             }
336             config.setTraceExternalFunctions(((Boolean JavaDoc)value).booleanValue());
337
338         } else if (name.equals(FeatureKeys.TIMING)) {
339             if (!(value instanceof Boolean JavaDoc)) {
340                 throw new IllegalArgumentException JavaDoc("Timing must be a boolean");
341             }
342             config.setTiming(((Boolean JavaDoc)value).booleanValue());
343
344         } else if (name.equals(FeatureKeys.DTD_VALIDATION)) {
345             if (!(value instanceof Boolean JavaDoc)) {
346                 throw new IllegalArgumentException JavaDoc("Validation must be a boolean");
347             }
348             config.setValidation(((Boolean JavaDoc)value).booleanValue());
349
350         } else if (name.equals(FeatureKeys.SCHEMA_VALIDATION)) {
351             if (!(value instanceof Integer JavaDoc)) {
352                 throw new IllegalArgumentException JavaDoc("Schema validation must be an integer");
353             }
354             config.setSchemaValidationMode(((Integer JavaDoc)value).intValue());
355
356         } else if (name.equals(FeatureKeys.VALIDATION_WARNINGS)) {
357             if (!(value instanceof Boolean JavaDoc)) {
358                 throw new IllegalArgumentException JavaDoc("validation-warnings must be a boolean");
359             }
360             config.setValidationWarnings(((Boolean JavaDoc)value).booleanValue());
361
362         } else if (name.equals(FeatureKeys.VERSION_WARNING)) {
363              if (!(value instanceof Boolean JavaDoc)) {
364                  throw new IllegalArgumentException JavaDoc("version-warning must be a boolean");
365              }
366              config.setVersionWarning(((Boolean JavaDoc)value).booleanValue());
367
368         } else if (name.equals(FeatureKeys.TRACE_LISTENER)) {
369             if (!(value instanceof TraceListener)) {
370                 throw new IllegalArgumentException JavaDoc("Trace listener is of wrong class");
371             }
372             config.setTraceListener((TraceListener)value);
373
374         } else if (name.equals(FeatureKeys.LINE_NUMBERING)) {
375             if (!(value instanceof Boolean JavaDoc)) {
376                 throw new IllegalArgumentException JavaDoc("Line Numbering value must be Boolean");
377             }
378             config.setLineNumbering(((Boolean JavaDoc)value).booleanValue());
379
380         } else if (name.equals(FeatureKeys.RECOVERY_POLICY)) {
381             if (!(value instanceof Integer JavaDoc)) {
382                 throw new IllegalArgumentException JavaDoc("Recovery Policy value must be Integer");
383             }
384             config.setRecoveryPolicy(((Integer JavaDoc)value).intValue());
385
386         } else if (name.equals(FeatureKeys.MESSAGE_EMITTER_CLASS)) {
387             if (!(value instanceof String JavaDoc)) {
388                 throw new IllegalArgumentException JavaDoc("Message Emitter class must be a String");
389             }
390             config.setMessageEmitterClass((String JavaDoc)value);
391
392         } else if (name.equals(FeatureKeys.SOURCE_PARSER_CLASS)) {
393             if (!(value instanceof String JavaDoc)) {
394                 throw new IllegalArgumentException JavaDoc("Source Parser class must be a String");
395             }
396             config.setSourceParserClass((String JavaDoc)value);
397
398         } else if (name.equals(FeatureKeys.STYLE_PARSER_CLASS)) {
399             if (!(value instanceof String JavaDoc)) {
400                 throw new IllegalArgumentException JavaDoc("Style Parser class must be a String");
401             }
402             config.setStyleParserClass((String JavaDoc)value);
403
404         } else if (name.equals(FeatureKeys.OUTPUT_URI_RESOLVER)) {
405             if (!(value instanceof OutputURIResolver)) {
406                 throw new IllegalArgumentException JavaDoc("Output URI resolver value must be an instance of net.sf.saxon.OutputURIResolver");
407             }
408             config.setOutputURIResolver((OutputURIResolver)value);
409
410         } else if (name.equals(FeatureKeys.NAME_POOL)) {
411             if (!(value instanceof NamePool)) {
412                 throw new IllegalArgumentException JavaDoc("NAME_POOL value must be an instance of net.sf.saxon.om.NamePool");
413             }
414             config.setNamePool((NamePool)value);
415
416         } else if (name.equals(FeatureKeys.COLLATION_URI_RESOLVER)) {
417             if (!(value instanceof CollationURIResolver)) {
418                 throw new IllegalArgumentException JavaDoc(
419                         "COLLATION_URI_RESOLVER value must be an instance of net.sf.saxon.sort.CollationURIResolver");
420             }
421             config.setCollationURIResolver((CollationURIResolver)value);
422
423         } else if (name.equals(FeatureKeys.COLLECTION_URI_RESOLVER)) {
424             if (!(value instanceof CollectionURIResolver)) {
425                 throw new IllegalArgumentException JavaDoc(
426                         "COLLECTION_URI_RESOLVER value must be an instance of net.sf.saxon.CollectionURIResolver");
427             }
428             config.setCollectionURIResolver((CollectionURIResolver)value);
429
430         } else {
431             throw new IllegalArgumentException JavaDoc("Unknown attribute " + name);
432         }
433     }
434
435     /**
436      * Allows the user to retrieve specific attributes on the underlying
437      * implementation.
438      * @param name The name of the attribute.
439      * @return value The value of the attribute.
440      * @throws IllegalArgumentException thrown if the underlying
441      * implementation doesn't recognize the attribute.
442      */

443     public Object JavaDoc getAttribute(String JavaDoc name)
444         throws IllegalArgumentException JavaDoc{
445         if (name.equals(FeatureKeys.TREE_MODEL)) {
446             return new Integer JavaDoc(config.getTreeModel());
447
448         } else if (name.equals(FeatureKeys.TIMING)) {
449             return Boolean.valueOf(config.isTiming());
450
451         } else if (name.equals(FeatureKeys.DTD_VALIDATION)) {
452             return Boolean.valueOf(config.isValidation());
453
454         } else if (name.equals(FeatureKeys.SCHEMA_VALIDATION)) {
455             return new Integer JavaDoc(config.getSchemaValidationMode());
456
457         } else if (name.equals(FeatureKeys.VALIDATION_WARNINGS)) {
458             return Boolean.valueOf(config.isValidationWarnings());
459
460         } else if (name.equals(FeatureKeys.ALLOW_EXTERNAL_FUNCTIONS)) {
461             return Boolean.valueOf(config.isAllowExternalFunctions());
462
463         } else if (name.equals(FeatureKeys.RECOGNIZE_URI_QUERY_PARAMETERS)) {
464             return Boolean.valueOf(config.getSystemURIResolver().queryParametersAreRecognized());
465
466         } else if (name.equals(FeatureKeys.TRACE_EXTERNAL_FUNCTIONS)) {
467             return Boolean.valueOf(config.isTraceExternalFunctions());
468
469         } else if (name.equals(FeatureKeys.VERSION_WARNING)) {
470             return Boolean.valueOf(config.isVersionWarning());
471
472         } else if (name.equals(FeatureKeys.TRACE_LISTENER)) {
473             return config.getTraceListener();
474
475         } else if (name.equals(FeatureKeys.LINE_NUMBERING)) {
476             return Boolean.valueOf(config.isLineNumbering());
477
478         } else if (name.equals(FeatureKeys.RECOVERY_POLICY)) {
479             return new Integer JavaDoc(config.getRecoveryPolicy());
480
481         } else if (name.equals(FeatureKeys.MESSAGE_EMITTER_CLASS)) {
482             return config.getMessageEmitterClass();
483
484         } else if (name.equals(FeatureKeys.SOURCE_PARSER_CLASS)) {
485             return config.getSourceParserClass();
486
487         } else if (name.equals(FeatureKeys.STYLE_PARSER_CLASS)) {
488             return config.getStyleParserClass();
489
490         } else if (name.equals(FeatureKeys.OUTPUT_URI_RESOLVER)) {
491             return config.getOutputURIResolver();
492
493         } else if (name.equals(FeatureKeys.NAME_POOL)) {
494             return config.getNamePool();
495
496         } else if (name.equals(FeatureKeys.COLLATION_URI_RESOLVER)) {
497             return config.getCollationURIResolver();
498
499         } else if (name.equals(FeatureKeys.COLLECTION_URI_RESOLVER)) {
500             return config.getCollectionURIResolver();
501
502         } else {
503             throw new IllegalArgumentException JavaDoc("Unknown attribute " + name);
504         }
505     }
506
507     /**
508      * Set the error event listener for the TransformerFactory, which
509      * is used for the processing of transformation instructions,
510      * and not for the transformation itself.
511      *
512      * @param listener The new error listener.
513      * @throws IllegalArgumentException if listener is null.
514      */

515
516     public void setErrorListener(ErrorListener listener)
517             throws IllegalArgumentException JavaDoc {
518         config.setErrorListener(listener);
519     }
520
521     /**
522      * Get the error event handler for the TransformerFactory.
523      * @return The current error listener, which should never be null.
524      */

525     public ErrorListener getErrorListener() {
526         return config.getErrorListener();
527     }
528
529
530
531
532     ///////////////////////////////////////////////////////////////////////////////
533
// Methods defined in class javax.xml.transform.sax.SAXTransformerFactory
534
///////////////////////////////////////////////////////////////////////////////
535

536      /**
537      * Get a TransformerHandler object that can process SAX
538      * ContentHandler events into a Result, based on the transformation
539      * instructions specified by the argument.
540      *
541      * @param src The Source of the transformation instructions.
542      *
543      * @return TransformerHandler ready to transform SAX events.
544      *
545      * @throws TransformerConfigurationException If for some reason the
546      * TransformerHandler can not be created.
547      */

548
549     public TransformerHandler newTransformerHandler(Source JavaDoc src)
550     throws TransformerConfigurationException {
551         Templates tmpl = newTemplates(src);
552         return newTransformerHandler(tmpl);
553     }
554
555     /**
556      * Get a TransformerHandler object that can process SAX
557      * ContentHandler events into a Result, based on the Templates argument.
558      *
559      * @param templates The compiled transformation instructions.
560      *
561      * @return TransformerHandler ready to transform SAX events.
562      *
563      * @throws TransformerConfigurationException If for some reason the
564      * TransformerHandler can not be created.
565      */

566
567     public TransformerHandler newTransformerHandler(Templates templates)
568     throws TransformerConfigurationException {
569         if (!(templates instanceof PreparedStylesheet)) {
570             throw new TransformerConfigurationException("Templates object was not created by Saxon");
571         }
572         Controller controller = (Controller)templates.newTransformer();
573         TransformerHandlerImpl handler = new TransformerHandlerImpl(controller);
574         return handler;
575     }
576
577     /**
578      * Get a TransformerHandler object that can process SAX
579      * ContentHandler events into a Result. The transformation
580      * is defined as an identity (or copy) transformation, for example
581      * to copy a series of SAX parse events into a DOM tree.
582      *
583      * @return A non-null reference to a TransformerHandler, that may
584      * be used as a ContentHandler for SAX parse events.
585      *
586      * @throws TransformerConfigurationException If for some reason the
587      * TransformerHandler cannot be created.
588      */

589
590     public TransformerHandler newTransformerHandler()
591     throws TransformerConfigurationException {
592         Controller controller = new IdentityTransformer(config);
593         return new IdentityTransformerHandler(controller);
594     }
595
596     /**
597      * Get a TemplatesHandler object that can process SAX
598      * ContentHandler events into a Templates object.
599      *
600      * @return A non-null reference to a TransformerHandler, that may
601      * be used as a ContentHandler for SAX parse events.
602      *
603      * @throws TransformerConfigurationException If for some reason the
604      * TemplatesHandler cannot be created.
605      */

606
607     public TemplatesHandler newTemplatesHandler()
608     throws TransformerConfigurationException {
609         return new TemplatesHandlerImpl(config);
610     }
611
612     /**
613      * Create an XMLFilter that uses the given Source as the
614      * transformation instructions.
615      *
616      * @param src The Source of the transformation instructions.
617      *
618      * @return An XMLFilter object, or null if this feature is not supported.
619      *
620      * @throws TransformerConfigurationException If for some reason the
621      * XMLFilter cannot be created.
622      */

623
624     public XMLFilter JavaDoc newXMLFilter(Source JavaDoc src)
625     throws TransformerConfigurationException {
626         Templates tmpl = newTemplates(src);
627         return newXMLFilter(tmpl);
628     }
629
630     /**
631      * Create an XMLFilter, based on the Templates argument..
632      *
633      * @param templates The compiled transformation instructions.
634      *
635      * @return An XMLFilter object, or null if this feature is not supported.
636      *
637      * @throws TransformerConfigurationException If for some reason the
638      * XMLFilter cannot be created.
639      */

640
641     public XMLFilter JavaDoc newXMLFilter(Templates templates)
642     throws TransformerConfigurationException {
643         if (!(templates instanceof PreparedStylesheet)) {
644             throw new TransformerConfigurationException("Supplied Templates object was not created using Saxon");
645         }
646         Controller controller = (Controller)templates.newTransformer();
647         return new Filter(controller);
648     }
649
650     /**
651      * <p>Set a feature for this <code>TransformerFactory</code> and <code>Transformer</code>s
652      * or <code>Template</code>s created by this factory.</p>
653      * <p/>
654      * <p/>
655      * Feature names are fully qualified {@link java.net.URI}s.
656      * Implementations may define their own features.
657      * An {@link javax.xml.transform.TransformerConfigurationException} is thrown if this <code>TransformerFactory</code> or the
658      * <code>Transformer</code>s or <code>Template</code>s it creates cannot support the feature.
659      * It is possible for an <code>TransformerFactory</code> to expose a feature value but be unable to change its state.
660      * </p>
661      * <p/>
662      * <p>All implementations are required to support the FEATURE_SECURE_PROCESSING feature.
663      * When the feature is:</p>
664      * <ul>
665      * <li>
666      * <code>true</code>: the implementation will limit XML processing to conform to implementation limits
667      * and behave in a secure fashion as defined by the implementation.
668      * Examples include resolving user defined style sheets and functions.
669      * If XML processing is limited for security reasons, it will be reported via a call to the registered
670      * {@link javax.xml.transform.ErrorListener#fatalError(javax.xml.transform.TransformerException exception)}.
671      * See {@link #setErrorListener(javax.xml.transform.ErrorListener listener)}. In the Saxon implementation,
672      * this option causes calls on extension functions and extensions instructions to be disabled, and also
673      * disables the use of xsl:result-document to write to secondary output destinations.
674      * </li>
675      * <li>
676      * <code>false</code>: the implementation will processing XML according to the XML specifications without
677      * regard to possible implementation limits.
678      * </li>
679      * </ul>
680      *
681      * @param name Feature name.
682      * @param value Is feature state <code>true</code> or <code>false</code>.
683      * @throws javax.xml.transform.TransformerConfigurationException
684      * if this <code>TransformerFactory</code>
685      * or the <code>Transformer</code>s or <code>Template</code>s it creates cannot support this feature.
686      * @throws NullPointerException If the <code>name</code> parameter is null.
687      */

688
689     public void setFeature(String JavaDoc name, boolean value) throws TransformerConfigurationException {
690         if (name.equals(FEATURE_SECURE_PROCESSING)) {
691             config.setAllowExternalFunctions(!value);
692         } else {
693             throw new TransformerConfigurationException("Unsupported TransformerFactory feature: " + name);
694         }
695     }
696
697 }
698
699 //
700
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
701
// you may not use this file except in compliance with the License. You may obtain a copy of the
702
// License at http://www.mozilla.org/MPL/
703
//
704
// Software distributed under the License is distributed on an "AS IS" basis,
705
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
706
// See the License for the specific language governing rights and limitations under the License.
707
//
708
// The Original Code is: all this file.
709
//
710
// The Initial Developer of the Original Code is Michael H. Kay
711
//
712
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
713
//
714
// Contributor(s): none.
715
//
716
Popular Tags