1 /*
2 * Copyright 2000-2006 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any 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 */
43 public abstract class TransformerFactory {
44
45 /**
46 * Default constructor is protected on purpose.
47 */
48 protected TransformerFactory() { }
49
50
51
52 /**
53 * <p>Obtain a new instance of a <code>TransformerFactory</code>.
54 * This static method creates a new factory instance
55 * This method uses the following ordered lookup procedure to determine
56 * the <code>TransformerFactory</code> implementation class to
57 * load:</p>
58 * <ul>
59 * <li>
60 * Use the <code>javax.xml.transform.TransformerFactory</code> system
61 * property.
62 * </li>
63 * <li>
64 * Use the properties file "lib/jaxp.properties" in the JRE directory.
65 * This configuration file is in standard <code>java.util.Properties
66 * </code> format and contains the fully qualified name of the
67 * implementation class with the key being the system property defined
68 * above.
69 *
70 * The jaxp.properties file is read only once by the JAXP implementation
71 * and it's values are then cached for future use. If the file does not exist
72 * when the first attempt is made to read from it, no further attempts are
73 * made to check for its existence. It is not possible to change the value
74 * of any property in jaxp.properties after it has been read for the first time.
75 * </li>
76 * <li>
77 * Use the Services API (as detailed in the JAR specification), if
78 * available, to determine the classname. The Services API will look
79 * for a classname in the file
80 * <code>META-INF/services/javax.xml.transform.TransformerFactory</code>
81 * in jars available to the runtime.
82 * </li>
83 * <li>
84 * Platform default <code>TransformerFactory</code> instance.
85 * </li>
86 * </ul>
87 *
88 * <p>Once an application has obtained a reference to a <code>
89 * TransformerFactory</code> it can use the factory to configure
90 * and obtain transformer instances.</p>
91 *
92 * @return new TransformerFactory instance, never null.
93 *
94 * @throws TransformerFactoryConfigurationError Thrown if the implementation
95 * is not available or cannot be instantiated.
96 */
97 public static TransformerFactory newInstance()
98 throws TransformerFactoryConfigurationError {
99 try {
100 return (TransformerFactory) FactoryFinder.find(
101 /* The default property name according to the JAXP spec */
102 "javax.xml.transform.TransformerFactory",
103 /* The fallback implementation class name, XSLTC */
104 "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");
105 } catch (FactoryFinder.ConfigurationError e) {
106 throw new TransformerFactoryConfigurationError(
107 e.getException(),
108 e.getMessage());
109 }
110 }
111
112 /**
113 * <p>Obtain a new instance of a <code>TransformerFactory</code> from factory class name.
114 * This function is useful when there are multiple providers in the classpath.
115 * It gives more control to the application as it can specify which provider
116 * should be loaded.</p>
117 *
118 * <p>Once an application has obtained a reference to a <code>
119 * TransformerFactory</code> it can use the factory to configure
120 * and obtain transformer instances.</p>
121 *
122 * <h2>Tip for Trouble-shooting</h2>
123 * <p>Setting the <code>jaxp.debug</code> system property will cause
124 * this method to print a lot of debug messages
125 * to <code>System.err</code> about what it is doing and where it is looking at.</p>
126 *
127 * <p> If you have problems try:</p>
128 * <pre>
129 * java -Djaxp.debug=1 YourProgram ....
130 * </pre>
131 *
132 * @param factoryClassName fully qualified factory class name that provides implementation of <code>javax.xml.transform.TransformerFactory</code>.
133 *
134 * @param classLoader <code>ClassLoader</code> used to load the factory class. If <code>null</code>
135 * current <code>Thread</code>'s context classLoader is used to load the factory class.
136 *
137 * @return new TransformerFactory instance, never null.
138 *
139 * @throws TransformerFactoryConfigurationError
140 * if <code>factoryClassName</code> is <code>null</code>, or
141 * the factory class cannot be loaded, instantiated.
142 *
143 * @see #newInstance()
144 *
145 * @since 1.6
146 */
147 public static TransformerFactory newInstance(String factoryClassName, ClassLoader classLoader)
148 throws TransformerFactoryConfigurationError{
149 try {
150 //do not fallback if given classloader can't find the class, throw exception
151 return (TransformerFactory) FactoryFinder.newInstance(factoryClassName, classLoader, false);
152 } catch (FactoryFinder.ConfigurationError e) {
153 throw new TransformerFactoryConfigurationError(
154 e.getException(),
155 e.getMessage());
156 }
157 }
158 /**
159 * <p>Process the <code>Source</code> into a <code>Transformer</code>
160 * <code>Object</code>. The <code>Source</code> is an XSLT document that
161 * conforms to <a href="http://www.w3.org/TR/xslt">
162 * XSL Transformations (XSLT) Version 1.0</a>. Care must
163 * be taken not to use this <code>Transformer</code> in multiple
164 * <code>Thread</code>s running concurrently.
165 * Different <code>TransformerFactories</code> can be used concurrently by
166 * different <code>Thread</code>s.</p>
167 *
168 * @param source <code>Source </code> of XSLT document used to create
169 * <code>Transformer</code>.
170 * Examples of XML <code>Source</code>s include
171 * {@link javax.xml.transform.dom.DOMSource DOMSource},
172 * {@link javax.xml.transform.sax.SAXSource SAXSource}, and
173 * {@link javax.xml.transform.stream.StreamSource StreamSource}.
174 *
175 * @return A <code>Transformer</code> object that may be used to perform
176 * a transformation in a single <code>Thread</code>, never
177 * <code>null</code>.
178 *
179 * @throws TransformerConfigurationException Thrown if there are errors when
180 * parsing the <code>Source</code> or it is not possible to create a
181 * <code>Transformer</code> instance.
182 *
183 * @see <a href="http://www.w3.org/TR/xslt">
184 * XSL Transformations (XSLT) Version 1.0</a>
185 */
186 public abstract Transformer newTransformer(Source source)
187 throws TransformerConfigurationException;
188
189 /**
190 * <p>Create a new <code>Transformer</code> that performs a copy
191 * of the <code>Source</code> to the <code>Result</code>.
192 * i.e. the "<em>identity transform</em>".</p>
193 *
194 * @return A Transformer object that may be used to perform a transformation
195 * in a single thread, never null.
196 *
197 * @throws TransformerConfigurationException When it is not
198 * possible to create a <code>Transformer</code> instance.
199 */
200 public abstract Transformer newTransformer()
201 throws TransformerConfigurationException;
202
203 /**
204 * Process the Source into a Templates object, which is a
205 * a compiled representation of the source. This Templates object
206 * may then be used concurrently across multiple threads. Creating
207 * a Templates object allows the TransformerFactory to do detailed
208 * performance optimization of transformation instructions, without
209 * penalizing runtime transformation.
210 *
211 * @param source An object that holds a URL, input stream, etc.
212 *
213 * @return A Templates object capable of being used for transformation
214 * purposes, never <code>null</code>.
215 *
216 * @throws TransformerConfigurationException When parsing to
217 * construct the Templates object fails.
218 */
219 public abstract Templates newTemplates(Source source)
220 throws TransformerConfigurationException;
221
222 /**
223 * <p>Get the stylesheet specification(s) associated with the
224 * XML <code>Source</code> document via the
225 * <a href="http://www.w3.org/TR/xml-stylesheet/">
226 * xml-stylesheet processing instruction</a> that match the given criteria.
227 * Note that it is possible to return several stylesheets, in which case
228 * they are applied as if they were a list of imports or cascades in a
229 * single stylesheet.</p>
230 *
231 * @param source The XML source document.
232 * @param media The media attribute to be matched. May be null, in which
233 * case the prefered templates will be used (i.e. alternate = no).
234 * @param title The value of the title attribute to match. May be null.
235 * @param charset The value of the charset attribute to match. May be null.
236 *
237 * @return A <code>Source</code> <code>Object</code> suitable for passing
238 * to the <code>TransformerFactory</code>.
239 *
240 * @throws TransformerConfigurationException An <code>Exception</code>
241 * is thrown if an error occurings during parsing of the
242 * <code>source</code>.
243 *
244 * @see <a href="http://www.w3.org/TR/xml-stylesheet/">
245 * Associating Style Sheets with XML documents Version 1.0</a>
246 */
247 public abstract Source getAssociatedStylesheet(
248 Source source,
249 String media,
250 String title,
251 String charset)
252 throws TransformerConfigurationException;
253
254 /**
255 * Set an object that is used by default during the transformation
256 * to resolve URIs used in document(), xsl:import, or xsl:include.
257 *
258 * @param resolver An object that implements the URIResolver interface,
259 * or null.
260 */
261 public abstract void setURIResolver(URIResolver resolver);
262
263 /**
264 * Get the object that is used by default during the transformation
265 * to resolve URIs used in document(), xsl:import, or xsl:include.
266 *
267 * @return The URIResolver that was set with setURIResolver.
268 */
269 public abstract URIResolver getURIResolver();
270
271 //======= CONFIGURATION METHODS =======
272
273 /**
274 * <p>Set a feature for this <code>TransformerFactory</code> and <code>Transformer</code>s
275 * or <code>Template</code>s created by this factory.</p>
276 *
277 * <p>
278 * Feature names are fully qualified {@link java.net.URI}s.
279 * Implementations may define their own features.
280 * An {@link TransformerConfigurationException} is thrown if this <code>TransformerFactory</code> or the
281 * <code>Transformer</code>s or <code>Template</code>s it creates cannot support the feature.
282 * It is possible for an <code>TransformerFactory</code> to expose a feature value but be unable to change its state.
283 * </p>
284 *
285 * <p>All implementations are required to support the {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} feature.
286 * When the feature is:</p>
287 * <ul>
288 * <li>
289 * <code>true</code>: the implementation will limit XML processing to conform to implementation limits
290 * and behave in a secure fashion as defined by the implementation.
291 * Examples include resolving user defined style sheets and functions.
292 * If XML processing is limited for security reasons, it will be reported via a call to the registered
293 * {@link ErrorListener#fatalError(TransformerException exception)}.
294 * See {@link #setErrorListener(ErrorListener listener)}.
295 * </li>
296 * <li>
297 * <code>false</code>: the implementation will processing XML according to the XML specifications without
298 * regard to possible implementation limits.
299 * </li>
300 * </ul>
301 *
302 * @param name Feature name.
303 * @param value Is feature state <code>true</code> or <code>false</code>.
304 *
305 * @throws TransformerConfigurationException if this <code>TransformerFactory</code>
306 * or the <code>Transformer</code>s or <code>Template</code>s it creates cannot support this feature.
307 * @throws NullPointerException If the <code>name</code> parameter is null.
308 */
309 public abstract void setFeature(String name, boolean value)
310 throws TransformerConfigurationException;
311
312 /**
313 * Look up the value of a feature.
314 *
315 * <p>
316 * Feature names are fully qualified {@link java.net.URI}s.
317 * Implementations may define their own features.
318 * <code>false</code> is returned if this <code>TransformerFactory</code> or the
319 * <code>Transformer</code>s or <code>Template</code>s it creates cannot support the feature.
320 * It is possible for an <code>TransformerFactory</code> to expose a feature value but be unable to change its state.
321 * </p>
322 *
323 * @param name Feature name.
324 *
325 * @return The current state of the feature, <code>true</code> or <code>false</code>.
326 *
327 * @throws NullPointerException If the <code>name</code> parameter is null.
328 */
329 public abstract boolean getFeature(String name);
330
331 /**
332 * Allows the user to set specific attributes on the underlying
333 * implementation. An attribute in this context is defined to
334 * be an option that the implementation provides.
335 * An <code>IllegalArgumentException</code> is thrown if the underlying
336 * implementation doesn't recognize the attribute.
337 *
338 * @param name The name of the attribute.
339 * @param value The value of the attribute.
340 *
341 * @throws IllegalArgumentException When implementation does not
342 * recognize the attribute.
343 */
344 public abstract void setAttribute(String name, Object value);
345
346 /**
347 * Allows the user to retrieve specific attributes on the underlying
348 * implementation.
349 * An <code>IllegalArgumentException</code> is thrown if the underlying
350 * implementation doesn't recognize the attribute.
351 *
352 * @param name The name of the attribute.
353 *
354 * @return value The value of the attribute.
355 *
356 * @throws IllegalArgumentException When implementation does not
357 * recognize the attribute.
358 */
359 public abstract Object getAttribute(String name);
360
361 /**
362 * Set the error event listener for the TransformerFactory, which
363 * is used for the processing of transformation instructions,
364 * and not for the transformation itself.
365 * An <code>IllegalArgumentException</code> is thrown if the
366 * <code>ErrorListener</code> listener is <code>null</code>.
367 *
368 * @param listener The new error listener.
369 *
370 * @throws IllegalArgumentException When <code>listener</code> is
371 * <code>null</code>
372 */
373 public abstract void setErrorListener(ErrorListener listener);
374
375 /**
376 * Get the error event handler for the TransformerFactory.
377 *
378 * @return The current error handler, which should never be null.
379 */
380 public abstract ErrorListener getErrorListener();
381
382 }