001 /*
002 * Copyright 2000-2005 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 import java.util.Properties;
029
030 /**
031 * An instance of this abstract class can transform a
032 * source tree into a result tree.
033 *
034 * <p>An instance of this class can be obtained with the
035 * {@link TransformerFactory#newTransformer TransformerFactory.newTransformer}
036 * method. This instance may then be used to process XML from a
037 * variety of sources and write the transformation output to a
038 * variety of sinks.</p>
039 *
040 * <p>An object of this class may not be used in multiple threads
041 * running concurrently. Different Transformers may be used
042 * concurrently by different threads.</p>
043 *
044 * <p>A <code>Transformer</code> may be used multiple times. Parameters and
045 * output properties are preserved across transformations.</p>
046 *
047 * @author <a href="Jeff.Suttor@Sun.com">Jeff Suttor</a>
048 * @version $Revision: 1.3 $, $Date: 2005/10/12 17:14:20 $
049 */
050 public abstract class Transformer {
051
052 /**
053 * Default constructor is protected on purpose.
054 */
055 protected Transformer() {
056 }
057
058 /**
059 * <p>Reset this <code>Transformer</code> to its original configuration.</p>
060 *
061 * <p><code>Transformer</code> is reset to the same state as when it was created with
062 * {@link TransformerFactory#newTransformer()},
063 * {@link TransformerFactory#newTransformer(Source source)} or
064 * {@link Templates#newTransformer()}.
065 * <code>reset()</code> is designed to allow the reuse of existing <code>Transformer</code>s
066 * thus saving resources associated with the creation of new <code>Transformer</code>s.</p>
067 *
068 * <p>The reset <code>Transformer</code> is not guaranteed to have the same {@link URIResolver}
069 * or {@link ErrorListener} <code>Object</code>s, e.g. {@link Object#equals(Object obj)}.
070 * It is guaranteed to have a functionally equal <code>URIResolver</code>
071 * and <code>ErrorListener</code>.</p>
072 *
073 * @throws UnsupportedOperationException When implementation does not
074 * override this method.
075 *
076 * @since 1.5
077 */
078 public void reset() {
079
080 // implementors should override this method
081 throw new UnsupportedOperationException("This Transformer, \""
082 + this .getClass().getName()
083 + "\", does not support the reset functionality."
084 + " Specification \""
085 + this .getClass().getPackage().getSpecificationTitle()
086 + "\""
087 + " version \""
088 + this .getClass().getPackage()
089 .getSpecificationVersion() + "\"");
090 }
091
092 /**
093 * <p>Transform the XML <code>Source</code> to a <code>Result</code>.
094 * Specific transformation behavior is determined by the settings of the
095 * <code>TransformerFactory</code> in effect when the
096 * <code>Transformer</code> was instantiated and any modifications made to
097 * the <code>Transformer</code> instance.</p>
098 *
099 * <p>An empty <code>Source</code> is represented as an empty document
100 * as constructed by {@link javax.xml.parsers.DocumentBuilder#newDocument()}.
101 * The result of transforming an empty <code>Source</code> depends on
102 * the transformation behavior; it is not always an empty
103 * <code>Result</code>.</p>
104 *
105 * @param xmlSource The XML input to transform.
106 * @param outputTarget The <code>Result</code> of transforming the
107 * <code>xmlSource</code>.
108 *
109 * @throws TransformerException If an unrecoverable error occurs
110 * during the course of the transformation.
111 */
112 public abstract void transform(Source xmlSource, Result outputTarget)
113 throws TransformerException;
114
115 /**
116 * Add a parameter for the transformation.
117 *
118 * <p>Pass a qualified name as a two-part string, the namespace URI
119 * enclosed in curly braces ({}), followed by the local name. If the
120 * name has a null URL, the String only contain the local name. An
121 * application can safely check for a non-null URI by testing to see if the
122 * first character of the name is a '{' character.</p>
123 * <p>For example, if a URI and local name were obtained from an element
124 * defined with <xyz:foo
125 * xmlns:xyz="http://xyz.foo.com/yada/baz.html"/>,
126 * then the qualified name would be "{http://xyz.foo.com/yada/baz.html}foo".
127 * Note that no prefix is used.</p>
128 *
129 * @param name The name of the parameter, which may begin with a
130 * namespace URI in curly braces ({}).
131 * @param value The value object. This can be any valid Java object. It is
132 * up to the processor to provide the proper object coersion or to simply
133 * pass the object on for use in an extension.
134 *
135 * @throws NullPointerException If value is null.
136 */
137 public abstract void setParameter(String name, Object value);
138
139 /**
140 * Get a parameter that was explicitly set with setParameter.
141 *
142 * <p>This method does not return a default parameter value, which
143 * cannot be determined until the node context is evaluated during
144 * the transformation process.
145 *
146 * @param name of <code>Object</code> to get
147 *
148 * @return A parameter that has been set with setParameter.
149 */
150 public abstract Object getParameter(String name);
151
152 /**
153 * <p>Set a list of parameters.</p>
154 *
155 * <p>Note that the list of parameters is specified as a
156 * <code>Properties</code> <code>Object</code> which limits the parameter
157 * values to <code>String</code>s. Multiple calls to
158 * {@link #setParameter(String name, Object value)} should be used when the
159 * desired values are non-<code>String</code> <code>Object</code>s.
160 * The parameter names should conform as specified in
161 * {@link #setParameter(String name, Object value)}.
162 * An <code>IllegalArgumentException</code> is thrown if any names do not
163 * conform.</p>
164 *
165 * <p>New parameters in the list are added to any existing parameters.
166 * If the name of a new parameter is equal to the name of an existing
167 * parameter as determined by {@link java.lang.Object#equals(Object obj)},
168 * the existing parameter is set to the new value.</p>
169 *
170 * @param params Parameters to set.
171 *
172 * @throws IllegalArgumentException If any parameter names do not conform
173 * to the naming rules.
174 */
175
176 /**
177 * Clear all parameters set with setParameter.
178 */
179 public abstract void clearParameters();
180
181 /**
182 * Set an object that will be used to resolve URIs used in
183 * document().
184 *
185 * <p>If the resolver argument is null, the URIResolver value will
186 * be cleared and the transformer will no longer have a resolver.</p>
187 *
188 * @param resolver An object that implements the URIResolver interface,
189 * or null.
190 */
191 public abstract void setURIResolver(URIResolver resolver);
192
193 /**
194 * Get an object that will be used to resolve URIs used in
195 * document().
196 *
197 * @return An object that implements the URIResolver interface,
198 * or null.
199 */
200 public abstract URIResolver getURIResolver();
201
202 /**
203 * Set the output properties for the transformation. These
204 * properties will override properties set in the Templates
205 * with xsl:output.
206 *
207 * <p>If argument to this function is null, any properties
208 * previously set are removed, and the value will revert to the value
209 * defined in the templates object.</p>
210 *
211 * <p>Pass a qualified property key name as a two-part string, the namespace
212 * URI enclosed in curly braces ({}), followed by the local name. If the
213 * name has a null URL, the String only contain the local name. An
214 * application can safely check for a non-null URI by testing to see if the
215 * first character of the name is a '{' character.</p>
216 * <p>For example, if a URI and local name were obtained from an element
217 * defined with <xyz:foo
218 * xmlns:xyz="http://xyz.foo.com/yada/baz.html"/>,
219 * then the qualified name would be "{http://xyz.foo.com/yada/baz.html}foo".
220 * Note that no prefix is used.</p>
221 * An <code>IllegalArgumentException</code> is thrown if any of the
222 * argument keys are not recognized and are not namespace qualified.
223 *
224 * @param oformat A set of output properties that will be
225 * used to override any of the same properties in affect
226 * for the transformation.
227 *
228 * @throws IllegalArgumentException When keys are not recognized and
229 * are not namespace qualified.
230 *
231 * @see javax.xml.transform.OutputKeys
232 * @see java.util.Properties
233 *
234 */
235 public abstract void setOutputProperties(Properties oformat);
236
237 /**
238 * <p>Get a copy of the output properties for the transformation.</p>
239 *
240 * <p>The properties returned should contain properties set by the user,
241 * and properties set by the stylesheet, and these properties
242 * are "defaulted" by default properties specified by
243 * <a href="http://www.w3.org/TR/xslt#output">section 16 of the
244 * XSL Transformations (XSLT) W3C Recommendation</a>. The properties that
245 * were specifically set by the user or the stylesheet should be in the base
246 * Properties list, while the XSLT default properties that were not
247 * specifically set should be the default Properties list. Thus,
248 * getOutputProperties().getProperty(String key) will obtain any
249 * property in that was set by {@link #setOutputProperty},
250 * {@link #setOutputProperties}, in the stylesheet, <em>or</em> the default
251 * properties, while
252 * getOutputProperties().get(String key) will only retrieve properties
253 * that were explicitly set by {@link #setOutputProperty},
254 * {@link #setOutputProperties}, or in the stylesheet.</p>
255 *
256 * <p>Note that mutation of the Properties object returned will not
257 * effect the properties that the transformer contains.</p>
258 *
259 * <p>If any of the argument keys are not recognized and are not
260 * namespace qualified, the property will be ignored and not returned.
261 * In other words the behaviour is not orthogonal with
262 * {@link #setOutputProperties setOutputProperties}.</p>
263 *
264 * @return A copy of the set of output properties in effect for
265 * the next transformation.
266 *
267 * @see javax.xml.transform.OutputKeys
268 * @see java.util.Properties
269 * @see <a href="http://www.w3.org/TR/xslt#output">
270 * XSL Transformations (XSLT) Version 1.0</a>
271 */
272 public abstract Properties getOutputProperties();
273
274 /**
275 * Set an output property that will be in effect for the
276 * transformation.
277 *
278 * <p>Pass a qualified property name as a two-part string, the namespace URI
279 * enclosed in curly braces ({}), followed by the local name. If the
280 * name has a null URL, the String only contain the local name. An
281 * application can safely check for a non-null URI by testing to see if the
282 * first character of the name is a '{' character.</p>
283 * <p>For example, if a URI and local name were obtained from an element
284 * defined with <xyz:foo
285 * xmlns:xyz="http://xyz.foo.com/yada/baz.html"/>,
286 * then the qualified name would be "{http://xyz.foo.com/yada/baz.html}foo".
287 * Note that no prefix is used.</p>
288 *
289 * <p>The Properties object that was passed to {@link #setOutputProperties}
290 * won't be effected by calling this method.</p>
291 *
292 * @param name A non-null String that specifies an output
293 * property name, which may be namespace qualified.
294 * @param value The non-null string value of the output property.
295 *
296 * @throws IllegalArgumentException If the property is not supported, and is
297 * not qualified with a namespace.
298 *
299 * @see javax.xml.transform.OutputKeys
300 */
301 public abstract void setOutputProperty(String name, String value)
302 throws IllegalArgumentException;
303
304 /**
305 * <p>Get an output property that is in effect for the transformer.</p>
306 *
307 * <p>If a property has been set using {@link #setOutputProperty},
308 * that value will be returned. Otherwise, if a property is explicitly
309 * specified in the stylesheet, that value will be returned. If
310 * the value of the property has been defaulted, that is, if no
311 * value has been set explicitly either with {@link #setOutputProperty} or
312 * in the stylesheet, the result may vary depending on
313 * implementation and input stylesheet.</p>
314 *
315 * @param name A non-null String that specifies an output
316 * property name, which may be namespace qualified.
317 *
318 * @return The string value of the output property, or null
319 * if no property was found.
320 *
321 * @throws IllegalArgumentException If the property is not supported.
322 *
323 * @see javax.xml.transform.OutputKeys
324 */
325 public abstract String getOutputProperty(String name)
326 throws IllegalArgumentException;
327
328 /**
329 * Set the error event listener in effect for the transformation.
330 *
331 * @param listener The new error listener.
332 *
333 * @throws IllegalArgumentException if listener is null.
334 */
335 public abstract void setErrorListener(ErrorListener listener)
336 throws IllegalArgumentException;
337
338 /**
339 * Get the error event handler in effect for the transformation.
340 * Implementations must provide a default error listener.
341 *
342 * @return The current error handler, which should never be null.
343 */
344 public abstract ErrorListener getErrorListener();
345 }
|