001 /*
002 * Copyright 2000-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.xml.transform;
027
028 /**
029 * <p>A TransformerFactory instance can be used to create
030 * {@link javax.xml.transform.Transformer} and
031 * {@link javax.xml.transform.Templates} objects.</p>
032 *
033 * <p>The system property that determines which Factory implementation
034 * to create is named <code>"javax.xml.transform.TransformerFactory"</code>.
035 * This property names a concrete subclass of the
036 * <code>TransformerFactory</code> abstract class. If the property is not
037 * defined, a platform default is be used.</p>
038 *
039 * @author <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a>
040 * @author <a href="mailto:Neeraj.Bajaj@sun.com">Neeraj Bajaj</a>
041 *
042 */
043 public abstract class TransformerFactory {
044
045 /**
046 * Default constructor is protected on purpose.
047 */
048 protected TransformerFactory() {
049 }
050
051 /**
052 * <p>Obtain a new instance of a <code>TransformerFactory</code>.
053 * This static method creates a new factory instance
054 * This method uses the following ordered lookup procedure to determine
055 * the <code>TransformerFactory</code> implementation class to
056 * load:</p>
057 * <ul>
058 * <li>
059 * Use the <code>javax.xml.transform.TransformerFactory</code> system
060 * property.
061 * </li>
062 * <li>
063 * Use the properties file "lib/jaxp.properties" in the JRE directory.
064 * This configuration file is in standard <code>java.util.Properties
065 * </code> format and contains the fully qualified name of the
066 * implementation class with the key being the system property defined
067 * above.
068 *
069 * The jaxp.properties file is read only once by the JAXP implementation
070 * and it's values are then cached for future use. If the file does not exist
071 * when the first attempt is made to read from it, no further attempts are
072 * made to check for its existence. It is not possible to change the value
073 * of any property in jaxp.properties after it has been read for the first time.
074 * </li>
075 * <li>
076 * Use the Services API (as detailed in the JAR specification), if
077 * available, to determine the classname. The Services API will look
078 * for a classname in the file
079 * <code>META-INF/services/javax.xml.transform.TransformerFactory</code>
080 * in jars available to the runtime.
081 * </li>
082 * <li>
083 * Platform default <code>TransformerFactory</code> instance.
084 * </li>
085 * </ul>
086 *
087 * <p>Once an application has obtained a reference to a <code>
088 * TransformerFactory</code> it can use the factory to configure
089 * and obtain transformer instances.</p>
090 *
091 * @return new TransformerFactory instance, never null.
092 *
093 * @throws TransformerFactoryConfigurationError Thrown if the implementation
094 * is not available or cannot be instantiated.
095 */
096 public static TransformerFactory newInstance()
097 throws TransformerFactoryConfigurationError {
098 try {
099 return (TransformerFactory) FactoryFinder
100 .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(e
107 .getException(), e.getMessage());
108 }
109 }
110
111 /**
112 * <p>Obtain a new instance of a <code>TransformerFactory</code> from factory class name.
113 * This function is useful when there are multiple providers in the classpath.
114 * It gives more control to the application as it can specify which provider
115 * should be loaded.</p>
116 *
117 * <p>Once an application has obtained a reference to a <code>
118 * TransformerFactory</code> it can use the factory to configure
119 * and obtain transformer instances.</p>
120 *
121 * <h2>Tip for Trouble-shooting</h2>
122 * <p>Setting the <code>jaxp.debug</code> system property will cause
123 * this method to print a lot of debug messages
124 * to <code>System.err</code> about what it is doing and where it is looking at.</p>
125 *
126 * <p> If you have problems try:</p>
127 * <pre>
128 * java -Djaxp.debug=1 YourProgram ....
129 * </pre>
130 *
131 * @param factoryClassName fully qualified factory class name that provides implementation of <code>javax.xml.transform.TransformerFactory</code>.
132 *
133 * @param classLoader <code>ClassLoader</code> used to load the factory class. If <code>null</code>
134 * current <code>Thread</code>'s context classLoader is used to load the factory class.
135 *
136 * @return new TransformerFactory instance, never null.
137 *
138 * @throws TransformerFactoryConfigurationError
139 * if <code>factoryClassName</code> is <code>null</code>, or
140 * the factory class cannot be loaded, instantiated.
141 *
142 * @see #newInstance()
143 *
144 * @since 1.6
145 */
146 public static TransformerFactory newInstance(
147 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(
152 factoryClassName, classLoader, false);
153 } catch (FactoryFinder.ConfigurationError e) {
154 throw new TransformerFactoryConfigurationError(e
155 .getException(), e.getMessage());
156 }
157 }
158
159 /**
160 * <p>Process the <code>Source</code> into a <code>Transformer</code>
161 * <code>Object</code>. The <code>Source</code> is an XSLT document that
162 * conforms to <a href="http://www.w3.org/TR/xslt">
163 * XSL Transformations (XSLT) Version 1.0</a>. Care must
164 * be taken not to use this <code>Transformer</code> in multiple
165 * <code>Thread</code>s running concurrently.
166 * Different <code>TransformerFactories</code> can be used concurrently by
167 * different <code>Thread</code>s.</p>
168 *
169 * @param source <code>Source </code> of XSLT document used to create
170 * <code>Transformer</code>.
171 * Examples of XML <code>Source</code>s include
172 * {@link javax.xml.transform.dom.DOMSource DOMSource},
173 * {@link javax.xml.transform.sax.SAXSource SAXSource}, and
174 * {@link javax.xml.transform.stream.StreamSource StreamSource}.
175 *
176 * @return A <code>Transformer</code> object that may be used to perform
177 * a transformation in a single <code>Thread</code>, never
178 * <code>null</code>.
179 *
180 * @throws TransformerConfigurationException Thrown if there are errors when
181 * parsing the <code>Source</code> or it is not possible to create a
182 * <code>Transformer</code> instance.
183 *
184 * @see <a href="http://www.w3.org/TR/xslt">
185 * XSL Transformations (XSLT) Version 1.0</a>
186 */
187 public abstract Transformer newTransformer(Source source)
188 throws TransformerConfigurationException;
189
190 /**
191 * <p>Create a new <code>Transformer</code> that performs a copy
192 * of the <code>Source</code> to the <code>Result</code>.
193 * i.e. the "<em>identity transform</em>".</p>
194 *
195 * @return A Transformer object that may be used to perform a transformation
196 * in a single thread, never null.
197 *
198 * @throws TransformerConfigurationException When it is not
199 * possible to create a <code>Transformer</code> instance.
200 */
201 public abstract Transformer newTransformer()
202 throws TransformerConfigurationException;
203
204 /**
205 * Process the Source into a Templates object, which is a
206 * a compiled representation of the source. This Templates object
207 * may then be used concurrently across multiple threads. Creating
208 * a Templates object allows the TransformerFactory to do detailed
209 * performance optimization of transformation instructions, without
210 * penalizing runtime transformation.
211 *
212 * @param source An object that holds a URL, input stream, etc.
213 *
214 * @return A Templates object capable of being used for transformation
215 * purposes, never <code>null</code>.
216 *
217 * @throws TransformerConfigurationException When parsing to
218 * construct the Templates object fails.
219 */
220 public abstract Templates newTemplates(Source source)
221 throws TransformerConfigurationException;
222
223 /**
224 * <p>Get the stylesheet specification(s) associated with the
225 * XML <code>Source</code> document via the
226 * <a href="http://www.w3.org/TR/xml-stylesheet/">
227 * xml-stylesheet processing instruction</a> that match the given criteria.
228 * Note that it is possible to return several stylesheets, in which case
229 * they are applied as if they were a list of imports or cascades in a
230 * single stylesheet.</p>
231 *
232 * @param source The XML source document.
233 * @param media The media attribute to be matched. May be null, in which
234 * case the prefered templates will be used (i.e. alternate = no).
235 * @param title The value of the title attribute to match. May be null.
236 * @param charset The value of the charset attribute to match. May be null.
237 *
238 * @return A <code>Source</code> <code>Object</code> suitable for passing
239 * to the <code>TransformerFactory</code>.
240 *
241 * @throws TransformerConfigurationException An <code>Exception</code>
242 * is thrown if an error occurings during parsing of the
243 * <code>source</code>.
244 *
245 * @see <a href="http://www.w3.org/TR/xml-stylesheet/">
246 * Associating Style Sheets with XML documents Version 1.0</a>
247 */
248 public abstract Source getAssociatedStylesheet(Source source,
249 String media, String title, String charset)
250 throws TransformerConfigurationException;
251
252 /**
253 * Set an object that is used by default during the transformation
254 * to resolve URIs used in document(), xsl:import, or xsl:include.
255 *
256 * @param resolver An object that implements the URIResolver interface,
257 * or null.
258 */
259 public abstract void setURIResolver(URIResolver resolver);
260
261 /**
262 * Get the object that is used by default during the transformation
263 * to resolve URIs used in document(), xsl:import, or xsl:include.
264 *
265 * @return The URIResolver that was set with setURIResolver.
266 */
267 public abstract URIResolver getURIResolver();
268
269 //======= CONFIGURATION METHODS =======
270
271 /**
272 * <p>Set a feature for this <code>TransformerFactory</code> and <code>Transformer</code>s
273 * or <code>Template</code>s created by this factory.</p>
274 *
275 * <p>
276 * Feature names are fully qualified {@link java.net.URI}s.
277 * Implementations may define their own features.
278 * An {@link TransformerConfigurationException} is thrown if this <code>TransformerFactory</code> or the
279 * <code>Transformer</code>s or <code>Template</code>s it creates cannot support the feature.
280 * It is possible for an <code>TransformerFactory</code> to expose a feature value but be unable to change its state.
281 * </p>
282 *
283 * <p>All implementations are required to support the {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} feature.
284 * When the feature is:</p>
285 * <ul>
286 * <li>
287 * <code>true</code>: the implementation will limit XML processing to conform to implementation limits
288 * and behave in a secure fashion as defined by the implementation.
289 * Examples include resolving user defined style sheets and functions.
290 * If XML processing is limited for security reasons, it will be reported via a call to the registered
291 * {@link ErrorListener#fatalError(TransformerException exception)}.
292 * See {@link #setErrorListener(ErrorListener listener)}.
293 * </li>
294 * <li>
295 * <code>false</code>: the implementation will processing XML according to the XML specifications without
296 * regard to possible implementation limits.
297 * </li>
298 * </ul>
299 *
300 * @param name Feature name.
301 * @param value Is feature state <code>true</code> or <code>false</code>.
302 *
303 * @throws TransformerConfigurationException if this <code>TransformerFactory</code>
304 * or the <code>Transformer</code>s or <code>Template</code>s it creates cannot support this feature.
305 * @throws NullPointerException If the <code>name</code> parameter is null.
306 */
307 public abstract void setFeature(String name, boolean value)
308 throws TransformerConfigurationException;
309
310 /**
311 * Look up the value of a feature.
312 *
313 * <p>
314 * Feature names are fully qualified {@link java.net.URI}s.
315 * Implementations may define their own features.
316 * <code>false</code> is returned if this <code>TransformerFactory</code> or the
317 * <code>Transformer</code>s or <code>Template</code>s it creates cannot support the feature.
318 * It is possible for an <code>TransformerFactory</code> to expose a feature value but be unable to change its state.
319 * </p>
320 *
321 * @param name Feature name.
322 *
323 * @return The current state of the feature, <code>true</code> or <code>false</code>.
324 *
325 * @throws NullPointerException If the <code>name</code> parameter is null.
326 */
327 public abstract boolean getFeature(String name);
328
329 /**
330 * Allows the user to set specific attributes on the underlying
331 * implementation. An attribute in this context is defined to
332 * be an option that the implementation provides.
333 * An <code>IllegalArgumentException</code> is thrown if the underlying
334 * implementation doesn't recognize the attribute.
335 *
336 * @param name The name of the attribute.
337 * @param value The value of the attribute.
338 *
339 * @throws IllegalArgumentException When implementation does not
340 * recognize the attribute.
341 */
342 public abstract void setAttribute(String name, Object value);
343
344 /**
345 * Allows the user to retrieve specific attributes on the underlying
346 * implementation.
347 * An <code>IllegalArgumentException</code> is thrown if the underlying
348 * implementation doesn't recognize the attribute.
349 *
350 * @param name The name of the attribute.
351 *
352 * @return value The value of the attribute.
353 *
354 * @throws IllegalArgumentException When implementation does not
355 * recognize the attribute.
356 */
357 public abstract Object getAttribute(String name);
358
359 /**
360 * Set the error event listener for the TransformerFactory, which
361 * is used for the processing of transformation instructions,
362 * and not for the transformation itself.
363 * An <code>IllegalArgumentException</code> is thrown if the
364 * <code>ErrorListener</code> listener is <code>null</code>.
365 *
366 * @param listener The new error listener.
367 *
368 * @throws IllegalArgumentException When <code>listener</code> is
369 * <code>null</code>
370 */
371 public abstract void setErrorListener(ErrorListener listener);
372
373 /**
374 * Get the error event handler for the TransformerFactory.
375 *
376 * @return The current error handler, which should never be null.
377 */
378 public abstract ErrorListener getErrorListener();
379
380 }
|