1 /*
2 * Copyright 2000-2005 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 import java.util.Properties;
29
30 /**
31 * An instance of this abstract class can transform a
32 * source tree into a result tree.
33 *
34 * <p>An instance of this class can be obtained with the
35 * {@link TransformerFactory#newTransformer TransformerFactory.newTransformer}
36 * method. This instance may then be used to process XML from a
37 * variety of sources and write the transformation output to a
38 * variety of sinks.</p>
39 *
40 * <p>An object of this class may not be used in multiple threads
41 * running concurrently. Different Transformers may be used
42 * concurrently by different threads.</p>
43 *
44 * <p>A <code>Transformer</code> may be used multiple times. Parameters and
45 * output properties are preserved across transformations.</p>
46 *
47 * @author <a href="Jeff.Suttor@Sun.com">Jeff Suttor</a>
48 */
49 public abstract class Transformer {
50
51 /**
52 * Default constructor is protected on purpose.
53 */
54 protected Transformer() { }
55
56 /**
57 * <p>Reset this <code>Transformer</code> to its original configuration.</p>
58 *
59 * <p><code>Transformer</code> is reset to the same state as when it was created with
60 * {@link TransformerFactory#newTransformer()},
61 * {@link TransformerFactory#newTransformer(Source source)} or
62 * {@link Templates#newTransformer()}.
63 * <code>reset()</code> is designed to allow the reuse of existing <code>Transformer</code>s
64 * thus saving resources associated with the creation of new <code>Transformer</code>s.</p>
65 *
66 * <p>The reset <code>Transformer</code> is not guaranteed to have the same {@link URIResolver}
67 * or {@link ErrorListener} <code>Object</code>s, e.g. {@link Object#equals(Object obj)}.
68 * It is guaranteed to have a functionally equal <code>URIResolver</code>
69 * and <code>ErrorListener</code>.</p>
70 *
71 * @throws UnsupportedOperationException When implementation does not
72 * override this method.
73 *
74 * @since 1.5
75 */
76 public void reset() {
77
78 // implementors should override this method
79 throw new UnsupportedOperationException(
80 "This Transformer, \"" + this.getClass().getName() + "\", does not support the reset functionality."
81 + " Specification \"" + this.getClass().getPackage().getSpecificationTitle() + "\""
82 + " version \"" + this.getClass().getPackage().getSpecificationVersion() + "\""
83 );
84 }
85
86 /**
87 * <p>Transform the XML <code>Source</code> to a <code>Result</code>.
88 * Specific transformation behavior is determined by the settings of the
89 * <code>TransformerFactory</code> in effect when the
90 * <code>Transformer</code> was instantiated and any modifications made to
91 * the <code>Transformer</code> instance.</p>
92 *
93 * <p>An empty <code>Source</code> is represented as an empty document
94 * as constructed by {@link javax.xml.parsers.DocumentBuilder#newDocument()}.
95 * The result of transforming an empty <code>Source</code> depends on
96 * the transformation behavior; it is not always an empty
97 * <code>Result</code>.</p>
98 *
99 * @param xmlSource The XML input to transform.
100 * @param outputTarget The <code>Result</code> of transforming the
101 * <code>xmlSource</code>.
102 *
103 * @throws TransformerException If an unrecoverable error occurs
104 * during the course of the transformation.
105 */
106 public abstract void transform(Source xmlSource, Result outputTarget)
107 throws TransformerException;
108
109 /**
110 * Add a parameter for the transformation.
111 *
112 * <p>Pass a qualified name as a two-part string, the namespace URI
113 * enclosed in curly braces ({}), followed by the local name. If the
114 * name has a null URL, the String only contain the local name. An
115 * application can safely check for a non-null URI by testing to see if the
116 * first character of the name is a '{' character.</p>
117 * <p>For example, if a URI and local name were obtained from an element
118 * defined with <xyz:foo
119 * xmlns:xyz="http://xyz.foo.com/yada/baz.html"/>,
120 * then the qualified name would be "{http://xyz.foo.com/yada/baz.html}foo".
121 * Note that no prefix is used.</p>
122 *
123 * @param name The name of the parameter, which may begin with a
124 * namespace URI in curly braces ({}).
125 * @param value The value object. This can be any valid Java object. It is
126 * up to the processor to provide the proper object coersion or to simply
127 * pass the object on for use in an extension.
128 *
129 * @throws NullPointerException If value is null.
130 */
131 public abstract void setParameter(String name, Object value);
132
133 /**
134 * Get a parameter that was explicitly set with setParameter.
135 *
136 * <p>This method does not return a default parameter value, which
137 * cannot be determined until the node context is evaluated during
138 * the transformation process.
139 *
140 * @param name of <code>Object</code> to get
141 *
142 * @return A parameter that has been set with setParameter.
143 */
144 public abstract Object getParameter(String name);
145
146 /**
147 * <p>Set a list of parameters.</p>
148 *
149 * <p>Note that the list of parameters is specified as a
150 * <code>Properties</code> <code>Object</code> which limits the parameter
151 * values to <code>String</code>s. Multiple calls to
152 * {@link #setParameter(String name, Object value)} should be used when the
153 * desired values are non-<code>String</code> <code>Object</code>s.
154 * The parameter names should conform as specified in
155 * {@link #setParameter(String name, Object value)}.
156 * An <code>IllegalArgumentException</code> is thrown if any names do not
157 * conform.</p>
158 *
159 * <p>New parameters in the list are added to any existing parameters.
160 * If the name of a new parameter is equal to the name of an existing
161 * parameter as determined by {@link java.lang.Object#equals(Object obj)},
162 * the existing parameter is set to the new value.</p>
163 *
164 * @param params Parameters to set.
165 *
166 * @throws IllegalArgumentException If any parameter names do not conform
167 * to the naming rules.
168 */
169
170 /**
171 * Clear all parameters set with setParameter.
172 */
173 public abstract void clearParameters();
174
175 /**
176 * Set an object that will be used to resolve URIs used in
177 * document().
178 *
179 * <p>If the resolver argument is null, the URIResolver value will
180 * be cleared and the transformer will no longer have a resolver.</p>
181 *
182 * @param resolver An object that implements the URIResolver interface,
183 * or null.
184 */
185 public abstract void setURIResolver(URIResolver resolver);
186
187 /**
188 * Get an object that will be used to resolve URIs used in
189 * document().
190 *
191 * @return An object that implements the URIResolver interface,
192 * or null.
193 */
194 public abstract URIResolver getURIResolver();
195
196 /**
197 * Set the output properties for the transformation. These
198 * properties will override properties set in the Templates
199 * with xsl:output.
200 *
201 * <p>If argument to this function is null, any properties
202 * previously set are removed, and the value will revert to the value
203 * defined in the templates object.</p>
204 *
205 * <p>Pass a qualified property key name as a two-part string, the namespace
206 * URI enclosed in curly braces ({}), followed by the local name. If the
207 * name has a null URL, the String only contain the local name. An
208 * application can safely check for a non-null URI by testing to see if the
209 * first character of the name is a '{' character.</p>
210 * <p>For example, if a URI and local name were obtained from an element
211 * defined with <xyz:foo
212 * xmlns:xyz="http://xyz.foo.com/yada/baz.html"/>,
213 * then the qualified name would be "{http://xyz.foo.com/yada/baz.html}foo".
214 * Note that no prefix is used.</p>
215 * An <code>IllegalArgumentException</code> is thrown if any of the
216 * argument keys are not recognized and are not namespace qualified.
217 *
218 * @param oformat A set of output properties that will be
219 * used to override any of the same properties in affect
220 * for the transformation.
221 *
222 * @throws IllegalArgumentException When keys are not recognized and
223 * are not namespace qualified.
224 *
225 * @see javax.xml.transform.OutputKeys
226 * @see java.util.Properties
227 *
228 */
229 public abstract void setOutputProperties(Properties oformat);
230
231 /**
232 * <p>Get a copy of the output properties for the transformation.</p>
233 *
234 * <p>The properties returned should contain properties set by the user,
235 * and properties set by the stylesheet, and these properties
236 * are "defaulted" by default properties specified by
237 * <a href="http://www.w3.org/TR/xslt#output">section 16 of the
238 * XSL Transformations (XSLT) W3C Recommendation</a>. The properties that
239 * were specifically set by the user or the stylesheet should be in the base
240 * Properties list, while the XSLT default properties that were not
241 * specifically set should be the default Properties list. Thus,
242 * getOutputProperties().getProperty(String key) will obtain any
243 * property in that was set by {@link #setOutputProperty},
244 * {@link #setOutputProperties}, in the stylesheet, <em>or</em> the default
245 * properties, while
246 * getOutputProperties().get(String key) will only retrieve properties
247 * that were explicitly set by {@link #setOutputProperty},
248 * {@link #setOutputProperties}, or in the stylesheet.</p>
249 *
250 * <p>Note that mutation of the Properties object returned will not
251 * effect the properties that the transformer contains.</p>
252 *
253 * <p>If any of the argument keys are not recognized and are not
254 * namespace qualified, the property will be ignored and not returned.
255 * In other words the behaviour is not orthogonal with
256 * {@link #setOutputProperties setOutputProperties}.</p>
257 *
258 * @return A copy of the set of output properties in effect for
259 * the next transformation.
260 *
261 * @see javax.xml.transform.OutputKeys
262 * @see java.util.Properties
263 * @see <a href="http://www.w3.org/TR/xslt#output">
264 * XSL Transformations (XSLT) Version 1.0</a>
265 */
266 public abstract Properties getOutputProperties();
267
268 /**
269 * Set an output property that will be in effect for the
270 * transformation.
271 *
272 * <p>Pass a qualified property name as a two-part string, the namespace URI
273 * enclosed in curly braces ({}), followed by the local name. If the
274 * name has a null URL, the String only contain the local name. An
275 * application can safely check for a non-null URI by testing to see if the
276 * first character of the name is a '{' character.</p>
277 * <p>For example, if a URI and local name were obtained from an element
278 * defined with <xyz:foo
279 * xmlns:xyz="http://xyz.foo.com/yada/baz.html"/>,
280 * then the qualified name would be "{http://xyz.foo.com/yada/baz.html}foo".
281 * Note that no prefix is used.</p>
282 *
283 * <p>The Properties object that was passed to {@link #setOutputProperties}
284 * won't be effected by calling this method.</p>
285 *
286 * @param name A non-null String that specifies an output
287 * property name, which may be namespace qualified.
288 * @param value The non-null string value of the output property.
289 *
290 * @throws IllegalArgumentException If the property is not supported, and is
291 * not qualified with a namespace.
292 *
293 * @see javax.xml.transform.OutputKeys
294 */
295 public abstract void setOutputProperty(String name, String value)
296 throws IllegalArgumentException;
297
298 /**
299 * <p>Get an output property that is in effect for the transformer.</p>
300 *
301 * <p>If a property has been set using {@link #setOutputProperty},
302 * that value will be returned. Otherwise, if a property is explicitly
303 * specified in the stylesheet, that value will be returned. If
304 * the value of the property has been defaulted, that is, if no
305 * value has been set explicitly either with {@link #setOutputProperty} or
306 * in the stylesheet, the result may vary depending on
307 * implementation and input stylesheet.</p>
308 *
309 * @param name A non-null String that specifies an output
310 * property name, which may be namespace qualified.
311 *
312 * @return The string value of the output property, or null
313 * if no property was found.
314 *
315 * @throws IllegalArgumentException If the property is not supported.
316 *
317 * @see javax.xml.transform.OutputKeys
318 */
319 public abstract String getOutputProperty(String name)
320 throws IllegalArgumentException;
321
322 /**
323 * Set the error event listener in effect for the transformation.
324 *
325 * @param listener The new error listener.
326 *
327 * @throws IllegalArgumentException if listener is null.
328 */
329 public abstract void setErrorListener(ErrorListener listener)
330 throws IllegalArgumentException;
331
332 /**
333 * Get the error event handler in effect for the transformation.
334 * Implementations must provide a default error listener.
335 *
336 * @return The current error handler, which should never be null.
337 */
338 public abstract ErrorListener getErrorListener();
339 }