KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > xml > transform > TransformerFactory


1 // $Id: TransformerFactory.java,v 1.14.14.1 2004/05/05 20:04:52 jsuttor Exp $
2
/*
3  * @(#)TransformerFactory.java 1.36 04/07/26
4  *
5  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
6  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
7  */

8
9 package javax.xml.transform;
10
11 /**
12  * <p>A TransformerFactory instance can be used to create
13  * {@link javax.xml.transform.Transformer} and
14  * {@link javax.xml.transform.Templates} objects.</p>
15  *
16  * <p>The system property that determines which Factory implementation
17  * to create is named <code>"javax.xml.transform.TransformerFactory"</code>.
18  * This property names a concrete subclass of the
19  * <code>TransformerFactory</code> abstract class. If the property is not
20  * defined, a platform default is be used.</p>
21  *
22  * @author <a HREF="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a>
23  */

24 public abstract class TransformerFactory {
25     
26     /**
27      * Default constructor is protected on purpose.
28      */

29     protected TransformerFactory() { }
30
31
32     /**
33      * <p>Get current state of canonicalization.</p>
34      *
35      * @return current state canonicalization control
36      */

37     /*
38     public boolean getCanonicalization() {
39         return canonicalState;
40     }
41     */

42     
43     /**
44      * <p>Set canonicalization control to <code>true</code> or
45      * </code>false</code>.</p>
46      *
47      * @param state of canonicalization
48      */

49     /*
50     public void setCanonicalization(boolean state) {
51         canonicalState = state;
52     }
53     */

54
55     /**
56      * Obtain a new instance of a <code>TransformerFactory</code>.
57      * This static method creates a new factory instance
58      * This method uses the following ordered lookup procedure to determine
59      * the <code>TransformerFactory</code> implementation class to
60      * load:
61      * <ul>
62      * <li>
63      * Use the <code>javax.xml.transform.TransformerFactory</code> system
64      * property.
65      * </li>
66      * <li>
67      * Use the properties file "lib/jaxp.properties" in the JRE directory.
68      * This configuration file is in standard <code>java.util.Properties
69      * </code> format and contains the fully qualified name of the
70      * implementation class with the key being the system property defined
71      * above.
72      *
73      * The jaxp.properties file is read only once by the JAXP implementation
74      * and it's values are then cached for future use. If the file does not exist
75      * when the first attempt is made to read from it, no further attempts are
76      * made to check for its existence. It is not possible to change the value
77      * of any property in jaxp.properties after it has been read for the first time.
78      * </li>
79      * <li>
80      * Use the Services API (as detailed in the JAR specification), if
81      * available, to determine the classname. The Services API will look
82      * for a classname in the file
83      * <code>META-INF/services/javax.xml.transform.TransformerFactory</code>
84      * in jars available to the runtime.
85      * </li>
86      * <li>
87      * Platform default <code>TransformerFactory</code> instance.
88      * </li>
89      * </ul>
90      *
91      * Once an application has obtained a reference to a <code>
92      * TransformerFactory</code> it can use the factory to configure
93      * and obtain parser instances.
94      *
95      * @return new TransformerFactory instance, never null.
96      *
97      * @throws TransformerFactoryConfigurationError Thrown if the implementation
98      * is not available or cannot be instantiated.
99      */

100     public static TransformerFactory JavaDoc newInstance()
101         throws TransformerFactoryConfigurationError JavaDoc {
102         try {
103             return (TransformerFactory JavaDoc) FactoryFinder.find(
104             /* The default property name according to the JAXP spec */
105             "javax.xml.transform.TransformerFactory",
106             /* The fallback implementation class name, XSLTC */
107             "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");
108         } catch (FactoryFinder.ConfigurationError JavaDoc e) {
109             throw new TransformerFactoryConfigurationError JavaDoc(
110                 e.getException(),
111                 e.getMessage());
112         }
113     }
114
115     /**
116      * <p>Process the <code>Source</code> into a <code>Transformer</code>
117      * <code>Object</code>. The <code>Source</code> is an XSLT document that
118      * conforms to <a HREF="http://www.w3.org/TR/xslt">
119      * XSL Transformations (XSLT) Version 1.0</a>. Care must
120      * be taken not to use this <code>Transformer</code> in multiple
121      * <code>Thread</code>s running concurrently.
122      * Different <code>TransformerFactories</code> can be used concurrently by
123      * different <code>Thread</code>s.</p>
124      *
125      * @param source <code>Source </code> of XSLT document used to create
126      * <code>Transformer</code>.
127      * Examples of XML <code>Source</code>s include
128      * {@link javax.xml.transform.dom.DOMSource DOMSource},
129      * {@link javax.xml.transform.sax.SAXSource SAXSource}, and
130      * {@link javax.xml.transform.stream.StreamSource StreamSource}.
131      *
132      * @return A <code>Transformer</code> object that may be used to perform
133      * a transformation in a single <code>Thread</code>, never
134      * <code>null</code>.
135      *
136      * @throws TransformerConfigurationException Thrown if there are errors when
137      * parsing the <code>Source</code> or it is not possible to create a
138      * <code>Transformer</code> instance.
139      *
140      * @see <a HREF="http://www.w3.org/TR/xslt">
141      * XSL Transformations (XSLT) Version 1.0</a>
142      */

143     public abstract Transformer JavaDoc newTransformer(Source JavaDoc source)
144         throws TransformerConfigurationException JavaDoc;
145
146     /**
147      * <p>Create a new <code>Transformer<code> that performs a copy
148      * of the <code>Source</code> to the <code>Result</code>.
149      * i.e. the "<em>identity transform</em>".</p>
150      *
151      * @return A Transformer object that may be used to perform a transformation
152      * in a single thread, never null.
153      *
154      * @exception TransformerConfigurationException Thrown if it is not
155      * possible to create a <code>Transformer</code> instance.
156      */

157     public abstract Transformer JavaDoc newTransformer()
158         throws TransformerConfigurationException JavaDoc;
159
160     /**
161      * Process the Source into a Templates object, which is a
162      * a compiled representation of the source. This Templates object
163      * may then be used concurrently across multiple threads. Creating
164      * a Templates object allows the TransformerFactory to do detailed
165      * performance optimization of transformation instructions, without
166      * penalizing runtime transformation.
167      *
168      * @param source An object that holds a URL, input stream, etc.
169      *
170      * @return A Templates object capable of being used for transformation
171      * purposes, never null.
172      *
173      * @exception TransformerConfigurationException May throw this during the
174      * parse when it is constructing the Templates object and fails.
175      */

176     public abstract Templates JavaDoc newTemplates(Source JavaDoc source)
177         throws TransformerConfigurationException JavaDoc;
178
179     /**
180      * <p>Get the stylesheet specification(s) associated with the
181      * XML <code>Source</code> document via the
182      * <a HREF="http://www.w3.org/TR/xml-stylesheet/">
183      * xml-stylesheet processing instruction</a> that match the given criteria.
184      * Note that it is possible to return several stylesheets, in which case
185      * they are applied as if they were a list of imports or cascades in a
186      * single stylesheet.</p>
187      *
188      * @param source The XML source document.
189      * @param media The media attribute to be matched. May be null, in which
190      * case the prefered templates will be used (i.e. alternate = no).
191      * @param title The value of the title attribute to match. May be null.
192      * @param charset The value of the charset attribute to match. May be null.
193      *
194      * @return A <code>Source</code> <code>Object</code> suitable for passing
195      * to the <code>TransformerFactory</code>.
196      *
197      * @throws TransformerConfigurationException An <code>Exception</code>
198      * is thrown if an error occurings during parsing of the
199      * <code>source</code>.
200      *
201      * @see <a HREF="http://www.w3.org/TR/xml-stylesheet/">
202      * Associating Style Sheets with XML documents Version 1.0</a>
203      */

204     public abstract Source JavaDoc getAssociatedStylesheet(
205         Source JavaDoc source,
206         String JavaDoc media,
207         String JavaDoc title,
208         String JavaDoc charset)
209         throws TransformerConfigurationException JavaDoc;
210
211     /**
212      * Set an object that is used by default during the transformation
213      * to resolve URIs used in document(), xsl:import, or xsl:include.
214      *
215      * @param resolver An object that implements the URIResolver interface,
216      * or null.
217      */

218     public abstract void setURIResolver(URIResolver JavaDoc resolver);
219
220     /**
221      * Get the object that is used by default during the transformation
222      * to resolve URIs used in document(), xsl:import, or xsl:include.
223      *
224      * @return The URIResolver that was set with setURIResolver.
225      */

226     public abstract URIResolver JavaDoc getURIResolver();
227
228     //======= CONFIGURATION METHODS =======
229

230     /**
231      * <p>Set a feature for this <code>TransformerFactory</code> and <code>Transformer</code>s
232      * or <code>Template</code>s created by this factory.</p>
233      *
234      * <p>
235      * Feature names are fully qualified {@link java.net.URI}s.
236      * Implementations may define their own features.
237      * An {@link TransformerConfigurationException} is thrown if this <code>TransformerFactory</code> or the
238      * <code>Transformer</code>s or <code>Template</code>s it creates cannot support the feature.
239      * It is possible for an <code>TransformerFactory</code> to expose a feature value but be unable to change its state.
240      * </p>
241      *
242      * <p>All implementations are required to support the {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} feature.
243      * When the feature is:</p>
244      * <ul>
245      * <li>
246      * <code>true</code>: the implementation will limit XML processing to conform to implementation limits
247      * and behave in a secure fashion as defined by the implementation.
248      * Examples include resolving user defined style sheets and functions.
249      * If XML processing is limited for security reasons, it will be reported via a call to the registered
250      * {@link ErrorListener#fatalError(TransformerException exception)}.
251      * See {@link #setErrorListener(ErrorListener listener)}.
252      * </li>
253      * <li>
254      * <code>false</code>: the implementation will processing XML according to the XML specifications without
255      * regard to possible implementation limits.
256      * </li>
257      * </ul>
258      *
259      * @param name Feature name.
260      * @param value Is feature state <code>true</code> or <code>false</code>.
261      *
262      * @throws TransformerConfigurationException if this <code>TransformerFactory</code>
263      * or the <code>Transformer</code>s or <code>Template</code>s it creates cannot support this feature.
264      * @throws NullPointerException If the <code>name</code> parameter is null.
265      */

266     public abstract void setFeature(String JavaDoc name, boolean value)
267         throws TransformerConfigurationException JavaDoc;
268
269     /**
270      * Look up the value of a feature.
271      *
272      * <p>
273      * Feature names are fully qualified {@link java.net.URI}s.
274      * Implementations may define their own features.
275      * <code>false</code> is returned if this <code>TransformerFactory</code> or the
276      * <code>Transformer</code>s or <code>Template</code>s it creates cannot support the feature.
277      * It is possible for an <code>TransformerFactory</code> to expose a feature value but be unable to change its state.
278      * </p>
279      *
280      * @param name Feature name.
281      *
282      * @return The current state of the feature, <code>true</code> or <code>false</code>.
283      *
284      * @throws NullPointerException If the <code>name</code> parameter is null.
285      */

286     public abstract boolean getFeature(String JavaDoc name);
287
288     /**
289      * Allows the user to set specific attributes on the underlying
290      * implementation. An attribute in this context is defined to
291      * be an option that the implementation provides.
292      * An <code>IllegalArgumentException</code> is thrown if the underlying
293      * implementation doesn't recognize the attribute.
294      *
295      * @param name The name of the attribute.
296      * @param value The value of the attribute.
297      */

298     public abstract void setAttribute(String JavaDoc name, Object JavaDoc value);
299
300     /**
301      * Allows the user to retrieve specific attributes on the underlying
302      * implementation.
303      * An <code>IllegalArgumentException</code> is thrown if the underlying
304      * implementation doesn't recognize the attribute.
305      *
306      * @param name The name of the attribute.
307      * @return value The value of the attribute.
308      */

309     public abstract Object JavaDoc getAttribute(String JavaDoc name);
310
311     /**
312      * Set the error event listener for the TransformerFactory, which
313      * is used for the processing of transformation instructions,
314      * and not for the transformation itself.
315      * An <code>IllegalArgumentException</code> is thrown if the
316      * <code>ErrorListener</code> listener is <code>null</code>.
317      *
318      * @param listener The new error listener.
319      */

320     public abstract void setErrorListener(ErrorListener JavaDoc listener);
321
322     /**
323      * Get the error event handler for the TransformerFactory.
324      *
325      * @return The current error handler, which should never be null.
326      */

327     public abstract ErrorListener JavaDoc getErrorListener();
328
329 }
330
331
Popular Tags