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. package javax.xml.transform;
  9. /**
  10. * <p>A TransformerFactory instance can be used to create
  11. * {@link javax.xml.transform.Transformer} and
  12. * {@link javax.xml.transform.Templates} objects.</p>
  13. *
  14. * <p>The system property that determines which Factory implementation
  15. * to create is named <code>"javax.xml.transform.TransformerFactory"</code>.
  16. * This property names a concrete subclass of the
  17. * <code>TransformerFactory</code> abstract class. If the property is not
  18. * defined, a platform default is be used.</p>
  19. *
  20. * @author <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a>
  21. */
  22. public abstract class TransformerFactory {
  23. /**
  24. * Default constructor is protected on purpose.
  25. */
  26. protected TransformerFactory() { }
  27. /**
  28. * <p>Get current state of canonicalization.</p>
  29. *
  30. * @return current state canonicalization control
  31. */
  32. /*
  33. public boolean getCanonicalization() {
  34. return canonicalState;
  35. }
  36. */
  37. /**
  38. * <p>Set canonicalization control to <code>true</code> or
  39. * </code>false</code>.</p>
  40. *
  41. * @param state of canonicalization
  42. */
  43. /*
  44. public void setCanonicalization(boolean state) {
  45. canonicalState = state;
  46. }
  47. */
  48. /**
  49. * Obtain a new instance of a <code>TransformerFactory</code>.
  50. * This static method creates a new factory instance
  51. * This method uses the following ordered lookup procedure to determine
  52. * the <code>TransformerFactory</code> implementation class to
  53. * load:
  54. * <ul>
  55. * <li>
  56. * Use the <code>javax.xml.transform.TransformerFactory</code> system
  57. * property.
  58. * </li>
  59. * <li>
  60. * Use the properties file "lib/jaxp.properties" in the JRE directory.
  61. * This configuration file is in standard <code>java.util.Properties
  62. * </code> format and contains the fully qualified name of the
  63. * implementation class with the key being the system property defined
  64. * above.
  65. *
  66. * The jaxp.properties file is read only once by the JAXP implementation
  67. * and it's values are then cached for future use. If the file does not exist
  68. * when the first attempt is made to read from it, no further attempts are
  69. * made to check for its existence. It is not possible to change the value
  70. * of any property in jaxp.properties after it has been read for the first time.
  71. * </li>
  72. * <li>
  73. * Use the Services API (as detailed in the JAR specification), if
  74. * available, to determine the classname. The Services API will look
  75. * for a classname in the file
  76. * <code>META-INF/services/javax.xml.transform.TransformerFactory</code>
  77. * in jars available to the runtime.
  78. * </li>
  79. * <li>
  80. * Platform default <code>TransformerFactory</code> instance.
  81. * </li>
  82. * </ul>
  83. *
  84. * Once an application has obtained a reference to a <code>
  85. * TransformerFactory</code> it can use the factory to configure
  86. * and obtain parser instances.
  87. *
  88. * @return new TransformerFactory instance, never null.
  89. *
  90. * @throws TransformerFactoryConfigurationError Thrown if the implementation
  91. * is not available or cannot be instantiated.
  92. */
  93. public static TransformerFactory newInstance()
  94. throws TransformerFactoryConfigurationError {
  95. try {
  96. return (TransformerFactory) FactoryFinder.find(
  97. /* The default property name according to the JAXP spec */
  98. "javax.xml.transform.TransformerFactory",
  99. /* The fallback implementation class name, XSLTC */
  100. "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");
  101. } catch (FactoryFinder.ConfigurationError e) {
  102. throw new TransformerFactoryConfigurationError(
  103. e.getException(),
  104. e.getMessage());
  105. }
  106. }
  107. /**
  108. * <p>Process the <code>Source</code> into a <code>Transformer</code>
  109. * <code>Object</code>. The <code>Source</code> is an XSLT document that
  110. * conforms to <a href="http://www.w3.org/TR/xslt">
  111. * XSL Transformations (XSLT) Version 1.0</a>. Care must
  112. * be taken not to use this <code>Transformer</code> in multiple
  113. * <code>Thread</code>s running concurrently.
  114. * Different <code>TransformerFactories</code> can be used concurrently by
  115. * different <code>Thread</code>s.</p>
  116. *
  117. * @param source <code>Source </code> of XSLT document used to create
  118. * <code>Transformer</code>.
  119. * Examples of XML <code>Source</code>s include
  120. * {@link javax.xml.transform.dom.DOMSource DOMSource},
  121. * {@link javax.xml.transform.sax.SAXSource SAXSource}, and
  122. * {@link javax.xml.transform.stream.StreamSource StreamSource}.
  123. *
  124. * @return A <code>Transformer</code> object that may be used to perform
  125. * a transformation in a single <code>Thread</code>, never
  126. * <code>null</code>.
  127. *
  128. * @throws TransformerConfigurationException Thrown if there are errors when
  129. * parsing the <code>Source</code> or it is not possible to create a
  130. * <code>Transformer</code> instance.
  131. *
  132. * @see <a href="http://www.w3.org/TR/xslt">
  133. * XSL Transformations (XSLT) Version 1.0</a>
  134. */
  135. public abstract Transformer newTransformer(Source source)
  136. throws TransformerConfigurationException;
  137. /**
  138. * <p>Create a new <code>Transformer<code> that performs a copy
  139. * of the <code>Source</code> to the <code>Result</code>.
  140. * i.e. the "<em>identity transform</em>".</p>
  141. *
  142. * @return A Transformer object that may be used to perform a transformation
  143. * in a single thread, never null.
  144. *
  145. * @exception TransformerConfigurationException Thrown if it is not
  146. * possible to create a <code>Transformer</code> instance.
  147. */
  148. public abstract Transformer newTransformer()
  149. throws TransformerConfigurationException;
  150. /**
  151. * Process the Source into a Templates object, which is a
  152. * a compiled representation of the source. This Templates object
  153. * may then be used concurrently across multiple threads. Creating
  154. * a Templates object allows the TransformerFactory to do detailed
  155. * performance optimization of transformation instructions, without
  156. * penalizing runtime transformation.
  157. *
  158. * @param source An object that holds a URL, input stream, etc.
  159. *
  160. * @return A Templates object capable of being used for transformation
  161. * purposes, never null.
  162. *
  163. * @exception TransformerConfigurationException May throw this during the
  164. * parse when it is constructing the Templates object and fails.
  165. */
  166. public abstract Templates newTemplates(Source source)
  167. throws TransformerConfigurationException;
  168. /**
  169. * <p>Get the stylesheet specification(s) associated with the
  170. * XML <code>Source</code> document via the
  171. * <a href="http://www.w3.org/TR/xml-stylesheet/">
  172. * xml-stylesheet processing instruction</a> that match the given criteria.
  173. * Note that it is possible to return several stylesheets, in which case
  174. * they are applied as if they were a list of imports or cascades in a
  175. * single stylesheet.</p>
  176. *
  177. * @param source The XML source document.
  178. * @param media The media attribute to be matched. May be null, in which
  179. * case the prefered templates will be used (i.e. alternate = no).
  180. * @param title The value of the title attribute to match. May be null.
  181. * @param charset The value of the charset attribute to match. May be null.
  182. *
  183. * @return A <code>Source</code> <code>Object</code> suitable for passing
  184. * to the <code>TransformerFactory</code>.
  185. *
  186. * @throws TransformerConfigurationException An <code>Exception</code>
  187. * is thrown if an error occurings during parsing of the
  188. * <code>source</code>.
  189. *
  190. * @see <a href="http://www.w3.org/TR/xml-stylesheet/">
  191. * Associating Style Sheets with XML documents Version 1.0</a>
  192. */
  193. public abstract Source getAssociatedStylesheet(
  194. Source source,
  195. String media,
  196. String title,
  197. String charset)
  198. throws TransformerConfigurationException;
  199. /**
  200. * Set an object that is used by default during the transformation
  201. * to resolve URIs used in document(), xsl:import, or xsl:include.
  202. *
  203. * @param resolver An object that implements the URIResolver interface,
  204. * or null.
  205. */
  206. public abstract void setURIResolver(URIResolver resolver);
  207. /**
  208. * Get the object that is used by default during the transformation
  209. * to resolve URIs used in document(), xsl:import, or xsl:include.
  210. *
  211. * @return The URIResolver that was set with setURIResolver.
  212. */
  213. public abstract URIResolver getURIResolver();
  214. //======= CONFIGURATION METHODS =======
  215. /**
  216. * <p>Set a feature for this <code>TransformerFactory</code> and <code>Transformer</code>s
  217. * or <code>Template</code>s created by this factory.</p>
  218. *
  219. * <p>
  220. * Feature names are fully qualified {@link java.net.URI}s.
  221. * Implementations may define their own features.
  222. * An {@link TransformerConfigurationException} is thrown if this <code>TransformerFactory</code> or the
  223. * <code>Transformer</code>s or <code>Template</code>s it creates cannot support the feature.
  224. * It is possible for an <code>TransformerFactory</code> to expose a feature value but be unable to change its state.
  225. * </p>
  226. *
  227. * <p>All implementations are required to support the {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} feature.
  228. * When the feature is:</p>
  229. * <ul>
  230. * <li>
  231. * <code>true</code>: the implementation will limit XML processing to conform to implementation limits
  232. * and behave in a secure fashion as defined by the implementation.
  233. * Examples include resolving user defined style sheets and functions.
  234. * If XML processing is limited for security reasons, it will be reported via a call to the registered
  235. * {@link ErrorListener#fatalError(TransformerException exception)}.
  236. * See {@link #setErrorListener(ErrorListener listener)}.
  237. * </li>
  238. * <li>
  239. * <code>false</code>: the implementation will processing XML according to the XML specifications without
  240. * regard to possible implementation limits.
  241. * </li>
  242. * </ul>
  243. *
  244. * @param name Feature name.
  245. * @param value Is feature state <code>true</code> or <code>false</code>.
  246. *
  247. * @throws TransformerConfigurationException if this <code>TransformerFactory</code>
  248. * or the <code>Transformer</code>s or <code>Template</code>s it creates cannot support this feature.
  249. * @throws NullPointerException If the <code>name</code> parameter is null.
  250. */
  251. public abstract void setFeature(String name, boolean value)
  252. throws TransformerConfigurationException;
  253. /**
  254. * Look up the value of a feature.
  255. *
  256. * <p>
  257. * Feature names are fully qualified {@link java.net.URI}s.
  258. * Implementations may define their own features.
  259. * <code>false</code> is returned if this <code>TransformerFactory</code> or the
  260. * <code>Transformer</code>s or <code>Template</code>s it creates cannot support the feature.
  261. * It is possible for an <code>TransformerFactory</code> to expose a feature value but be unable to change its state.
  262. * </p>
  263. *
  264. * @param name Feature name.
  265. *
  266. * @return The current state of the feature, <code>true</code> or <code>false</code>.
  267. *
  268. * @throws NullPointerException If the <code>name</code> parameter is null.
  269. */
  270. public abstract boolean getFeature(String name);
  271. /**
  272. * Allows the user to set specific attributes on the underlying
  273. * implementation. An attribute in this context is defined to
  274. * be an option that the implementation provides.
  275. * An <code>IllegalArgumentException</code> is thrown if the underlying
  276. * implementation doesn't recognize the attribute.
  277. *
  278. * @param name The name of the attribute.
  279. * @param value The value of the attribute.
  280. */
  281. public abstract void setAttribute(String name, Object value);
  282. /**
  283. * Allows the user to retrieve specific attributes on the underlying
  284. * implementation.
  285. * An <code>IllegalArgumentException</code> is thrown if the underlying
  286. * implementation doesn't recognize the attribute.
  287. *
  288. * @param name The name of the attribute.
  289. * @return value The value of the attribute.
  290. */
  291. public abstract Object getAttribute(String name);
  292. /**
  293. * Set the error event listener for the TransformerFactory, which
  294. * is used for the processing of transformation instructions,
  295. * and not for the transformation itself.
  296. * An <code>IllegalArgumentException</code> is thrown if the
  297. * <code>ErrorListener</code> listener is <code>null</code>.
  298. *
  299. * @param listener The new error listener.
  300. */
  301. public abstract void setErrorListener(ErrorListener listener);
  302. /**
  303. * Get the error event handler for the TransformerFactory.
  304. *
  305. * @return The current error handler, which should never be null.
  306. */
  307. public abstract ErrorListener getErrorListener();
  308. }