View Javadoc
1   /*
2    * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package javax.xml.transform;
27  
28  /**
29   * <p>A TransformerFactory instance can be used to create
30   * {@link javax.xml.transform.Transformer} and
31   * {@link javax.xml.transform.Templates} objects.</p>
32   *
33   * <p>The system property that determines which Factory implementation
34   * to create is named <code>"javax.xml.transform.TransformerFactory"</code>.
35   * This property names a concrete subclass of the
36   * <code>TransformerFactory</code> abstract class. If the property is not
37   * defined, a platform default is be used.</p>
38   *
39   * @author <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a>
40   * @author <a href="mailto:Neeraj.Bajaj@sun.com">Neeraj Bajaj</a>
41   *
42   * @since 1.5
43   */
44  public abstract class TransformerFactory {
45  
46      /**
47       * Default constructor is protected on purpose.
48       */
49      protected TransformerFactory() { }
50  
51  
52  
53      /**
54       * <p>Obtain a new instance of a <code>TransformerFactory</code>.
55       * This static method creates a new factory instance.</p>
56       * <p>This method uses the following ordered lookup procedure to determine
57       * the <code>TransformerFactory</code> implementation class to
58       * load:</p>
59       * <ul>
60       * <li>
61       * Use the <code>javax.xml.transform.TransformerFactory</code> system
62       * property.
63       * </li>
64       * <li>
65       * Use the properties file "lib/jaxp.properties" in the JRE directory.
66       * This configuration file is in standard <code>java.util.Properties
67       * </code> format and contains the fully qualified name of the
68       * implementation class with the key being the system property defined
69       * above.
70       * <br>
71       * The jaxp.properties file is read only once by the JAXP implementation
72       * and it's values are then cached for future use.  If the file does not exist
73       * when the first attempt is made to read from it, no further attempts are
74       * made to check for its existence.  It is not possible to change the value
75       * of any property in jaxp.properties after it has been read for the first time.
76       * </li>
77       * <li>
78       *   Use the service-provider loading facilities, defined by the
79       *   {@link java.util.ServiceLoader} class, to attempt to locate and load an
80       *   implementation of the service using the {@linkplain
81       *   java.util.ServiceLoader#load(java.lang.Class) default loading mechanism}:
82       *   the service-provider loading facility will use the {@linkplain
83       *   java.lang.Thread#getContextClassLoader() current thread's context class loader}
84       *   to attempt to load the service. If the context class
85       *   loader is null, the {@linkplain
86       *   ClassLoader#getSystemClassLoader() system class loader} will be used.
87       * </li>
88       * <li>
89       *   Otherwise, the system-default implementation is returned.
90       * </li>
91       * </ul>
92       *
93       * <p>Once an application has obtained a reference to a <code>
94       * TransformerFactory</code> it can use the factory to configure
95       * and obtain transformer instances.</p>
96       *
97       * @return new TransformerFactory instance, never null.
98       *
99       * @throws TransformerFactoryConfigurationError Thrown in case of {@linkplain
100      * java.util.ServiceConfigurationError service configuration error} or if
101      * the implementation is not available or cannot be instantiated.
102      */
103     public static TransformerFactory newInstance()
104         throws TransformerFactoryConfigurationError {
105 
106         return FactoryFinder.find(
107             /* The default property name according to the JAXP spec */
108             TransformerFactory.class,
109             /* The fallback implementation class name, XSLTC */
110             "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");
111     }
112 
113     /**
114      * <p>Obtain a new instance of a <code>TransformerFactory</code> from factory class name.
115      * This function is useful when there are multiple providers in the classpath.
116      * It gives more control to the application as it can specify which provider
117      * should be loaded.</p>
118      *
119      * <p>Once an application has obtained a reference to a <code>
120      * TransformerFactory</code> it can use the factory to configure
121      * and obtain transformer instances.</p>
122      *
123      * <h2>Tip for Trouble-shooting</h2>
124      * <p>Setting the <code>jaxp.debug</code> system property will cause
125      * this method to print a lot of debug messages
126      * to <code>System.err</code> about what it is doing and where it is looking at.</p>
127      *
128      * <p> If you have problems try:</p>
129      * <pre>
130      * java -Djaxp.debug=1 YourProgram ....
131      * </pre>
132      *
133      * @param factoryClassName fully qualified factory class name that provides implementation of <code>javax.xml.transform.TransformerFactory</code>.
134      *
135      * @param classLoader <code>ClassLoader</code> used to load the factory class. If <code>null</code>
136      *                     current <code>Thread</code>'s context classLoader is used to load the factory class.
137      *
138      * @return new TransformerFactory instance, never null.
139      *
140      * @throws TransformerFactoryConfigurationError
141      *                    if <code>factoryClassName</code> is <code>null</code>, or
142      *                   the factory class cannot be loaded, instantiated.
143      *
144      * @see #newInstance()
145      *
146      * @since 1.6
147      */
148     public static TransformerFactory newInstance(String factoryClassName, ClassLoader classLoader)
149         throws TransformerFactoryConfigurationError{
150 
151         //do not fallback if given classloader can't find the class, throw exception
152         return  FactoryFinder.newInstance(TransformerFactory.class,
153                     factoryClassName, classLoader, false, false);
154     }
155     /**
156      * <p>Process the <code>Source</code> into a <code>Transformer</code>
157      * <code>Object</code>.  The <code>Source</code> is an XSLT document that
158      * conforms to <a href="http://www.w3.org/TR/xslt">
159      * XSL Transformations (XSLT) Version 1.0</a>.  Care must
160      * be taken not to use this <code>Transformer</code> in multiple
161      * <code>Thread</code>s running concurrently.
162      * Different <code>TransformerFactories</code> can be used concurrently by
163      * different <code>Thread</code>s.</p>
164      *
165      * @param source <code>Source </code> of XSLT document used to create
166      *   <code>Transformer</code>.
167      *   Examples of XML <code>Source</code>s include
168      *   {@link javax.xml.transform.dom.DOMSource DOMSource},
169      *   {@link javax.xml.transform.sax.SAXSource SAXSource}, and
170      *   {@link javax.xml.transform.stream.StreamSource StreamSource}.
171      *
172      * @return A <code>Transformer</code> object that may be used to perform
173      *   a transformation in a single <code>Thread</code>, never
174      *   <code>null</code>.
175      *
176      * @throws TransformerConfigurationException Thrown if there are errors when
177      *    parsing the <code>Source</code> or it is not possible to create a
178      *   <code>Transformer</code> instance.
179      *
180      * @see <a href="http://www.w3.org/TR/xslt">
181      *   XSL Transformations (XSLT) Version 1.0</a>
182      */
183     public abstract Transformer newTransformer(Source source)
184         throws TransformerConfigurationException;
185 
186     /**
187      * <p>Create a new <code>Transformer</code> that performs a copy
188      * of the <code>Source</code> to the <code>Result</code>.
189      * i.e. the "<em>identity transform</em>".</p>
190      *
191      * @return A Transformer object that may be used to perform a transformation
192      * in a single thread, never null.
193      *
194      * @throws TransformerConfigurationException When it is not
195      *   possible to create a <code>Transformer</code> instance.
196      */
197     public abstract Transformer newTransformer()
198         throws TransformerConfigurationException;
199 
200     /**
201      * Process the Source into a Templates object, which is a
202      * a compiled representation of the source. This Templates object
203      * may then be used concurrently across multiple threads.  Creating
204      * a Templates object allows the TransformerFactory to do detailed
205      * performance optimization of transformation instructions, without
206      * penalizing runtime transformation.
207      *
208      * @param source An object that holds a URL, input stream, etc.
209      *
210      * @return A Templates object capable of being used for transformation
211      *   purposes, never <code>null</code>.
212      *
213      * @throws TransformerConfigurationException When parsing to
214      *   construct the Templates object fails.
215      */
216     public abstract Templates newTemplates(Source source)
217         throws TransformerConfigurationException;
218 
219     /**
220      * <p>Get the stylesheet specification(s) associated with the
221      * XML <code>Source</code> document via the
222      * <a href="http://www.w3.org/TR/xml-stylesheet/">
223      * xml-stylesheet processing instruction</a> that match the given criteria.
224      * Note that it is possible to return several stylesheets, in which case
225      * they are applied as if they were a list of imports or cascades in a
226      * single stylesheet.</p>
227      *
228      * @param source The XML source document.
229      * @param media The media attribute to be matched.  May be null, in which
230      *      case the prefered templates will be used (i.e. alternate = no).
231      * @param title The value of the title attribute to match.  May be null.
232      * @param charset The value of the charset attribute to match.  May be null.
233      *
234      * @return A <code>Source</code> <code>Object</code> suitable for passing
235      *   to the <code>TransformerFactory</code>.
236      *
237      * @throws TransformerConfigurationException An <code>Exception</code>
238      *   is thrown if an error occurings during parsing of the
239      *   <code>source</code>.
240      *
241      * @see <a href="http://www.w3.org/TR/xml-stylesheet/">
242      *   Associating Style Sheets with XML documents Version 1.0</a>
243      */
244     public abstract Source getAssociatedStylesheet(
245         Source source,
246         String media,
247         String title,
248         String charset)
249         throws TransformerConfigurationException;
250 
251     /**
252      * Set an object that is used by default during the transformation
253      * to resolve URIs used in document(), xsl:import, or xsl:include.
254      *
255      * @param resolver An object that implements the URIResolver interface,
256      * or null.
257      */
258     public abstract void setURIResolver(URIResolver resolver);
259 
260     /**
261      * Get the object that is used by default during the transformation
262      * to resolve URIs used in document(), xsl:import, or xsl:include.
263      *
264      * @return The URIResolver that was set with setURIResolver.
265      */
266     public abstract URIResolver getURIResolver();
267 
268     //======= CONFIGURATION METHODS =======
269 
270         /**
271          * <p>Set a feature for this <code>TransformerFactory</code> and <code>Transformer</code>s
272          * or <code>Template</code>s created by this factory.</p>
273          *
274          * <p>
275          * Feature names are fully qualified {@link java.net.URI}s.
276          * Implementations may define their own features.
277          * An {@link TransformerConfigurationException} is thrown if this <code>TransformerFactory</code> or the
278          * <code>Transformer</code>s or <code>Template</code>s it creates cannot support the feature.
279          * It is possible for an <code>TransformerFactory</code> to expose a feature value but be unable to change its state.
280          * </p>
281          *
282          * <p>All implementations are required to support the {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} feature.
283          * When the feature is:</p>
284          * <ul>
285          *   <li>
286          *     <code>true</code>: the implementation will limit XML processing to conform to implementation limits
287          *     and behave in a secure fashion as defined by the implementation.
288          *     Examples include resolving user defined style sheets and functions.
289          *     If XML processing is limited for security reasons, it will be reported via a call to the registered
290          *     {@link ErrorListener#fatalError(TransformerException exception)}.
291          *     See {@link  #setErrorListener(ErrorListener listener)}.
292          *   </li>
293          *   <li>
294          *     <code>false</code>: the implementation will processing XML according to the XML specifications without
295          *     regard to possible implementation limits.
296          *   </li>
297          * </ul>
298          *
299          * @param name Feature name.
300          * @param value Is feature state <code>true</code> or <code>false</code>.
301          *
302          * @throws TransformerConfigurationException if this <code>TransformerFactory</code>
303          *   or the <code>Transformer</code>s or <code>Template</code>s it creates cannot support this feature.
304      * @throws NullPointerException If the <code>name</code> parameter is null.
305          */
306         public abstract void setFeature(String name, boolean value)
307                 throws TransformerConfigurationException;
308 
309     /**
310      * Look up the value of a feature.
311      *
312          * <p>
313          * Feature names are fully qualified {@link java.net.URI}s.
314          * Implementations may define their own features.
315          * <code>false</code> is returned if this <code>TransformerFactory</code> or the
316          * <code>Transformer</code>s or <code>Template</code>s it creates cannot support the feature.
317          * It is possible for an <code>TransformerFactory</code> to expose a feature value but be unable to change its state.
318          * </p>
319          *
320          * @param name Feature name.
321          *
322      * @return The current state of the feature, <code>true</code> or <code>false</code>.
323      *
324      * @throws NullPointerException If the <code>name</code> parameter is null.
325      */
326     public abstract boolean getFeature(String name);
327 
328     /**
329      * Allows the user to set specific attributes on the underlying
330      * implementation.  An attribute in this context is defined to
331      * be an option that the implementation provides.
332      * An <code>IllegalArgumentException</code> is thrown if the underlying
333      * implementation doesn't recognize the attribute.
334      * <p>
335      * All implementations that implement JAXP 1.5 or newer are required to
336      * support the {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_DTD}  and
337      * {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_STYLESHEET} properties.
338      * </p>
339      * <ul>
340      *   <li>
341      *      <p>
342      *      Access to external DTDs in the source file is restricted to the protocols
343      *      specified by the {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_DTD} property.
344      *      If access is denied during transformation due to the restriction of this property,
345      *      {@link javax.xml.transform.TransformerException} will be thrown by
346      *      {@link javax.xml.transform.Transformer#transform(Source, Result)}.
347      *      </p>
348      *      <p>
349      *      Access to external DTDs in the stylesheet is restricted to the protocols
350      *      specified by the {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_DTD} property.
351      *      If access is denied during the creation of a new transformer due to the
352      *      restriction of this property,
353      *      {@link javax.xml.transform.TransformerConfigurationException} will be thrown
354      *      by the {@link #newTransformer(Source)} method.
355      *      </p>
356      *      <p>
357      *      Access to external reference set by the stylesheet processing instruction,
358      *      Import and Include element is restricted to the protocols specified by the
359      *      {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_STYLESHEET} property.
360      *      If access is denied during the creation of a new transformer due to the
361      *      restriction of this property,
362      *      {@link javax.xml.transform.TransformerConfigurationException} will be thrown
363      *      by the {@link #newTransformer(Source)} method.
364      *      </p>
365      *      <p>
366      *      Access to external document through XSLT document function is restricted
367      *      to the protocols specified by the property. If access is denied during
368      *      the transformation due to the restriction of this property,
369      *      {@link javax.xml.transform.TransformerException} will be thrown by the
370      *      {@link javax.xml.transform.Transformer#transform(Source, Result)} method.
371      *      </p>
372      *   </li>
373      * </ul>
374      *
375      * @param name The name of the attribute.
376      * @param value The value of the attribute.
377      *
378      * @throws IllegalArgumentException When implementation does not
379      *   recognize the attribute.
380      */
381     public abstract void setAttribute(String name, Object value);
382 
383     /**
384      * Allows the user to retrieve specific attributes on the underlying
385      * implementation.
386      * An <code>IllegalArgumentException</code> is thrown if the underlying
387      * implementation doesn't recognize the attribute.
388      *
389      * @param name The name of the attribute.
390      *
391      * @return value The value of the attribute.
392      *
393      * @throws IllegalArgumentException When implementation does not
394      *   recognize the attribute.
395      */
396     public abstract Object getAttribute(String name);
397 
398     /**
399      * Set the error event listener for the TransformerFactory, which
400      * is used for the processing of transformation instructions,
401      * and not for the transformation itself.
402      * An <code>IllegalArgumentException</code> is thrown if the
403      * <code>ErrorListener</code> listener is <code>null</code>.
404      *
405      * @param listener The new error listener.
406      *
407      * @throws IllegalArgumentException When <code>listener</code> is
408      *   <code>null</code>
409      */
410     public abstract void setErrorListener(ErrorListener listener);
411 
412     /**
413      * Get the error event handler for the TransformerFactory.
414      *
415      * @return The current error handler, which should never be null.
416      */
417     public abstract ErrorListener getErrorListener();
418 
419 }