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