1. /*
  2. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  3. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  4. */
  5. /*
  6. * @(#)TransformerFactory.java 1.19 02/10/15
  7. */
  8. package javax.xml.transform;
  9. import java.io.IOException;
  10. import java.io.File;
  11. import java.io.FileInputStream;
  12. import java.io.InputStream;
  13. import java.io.InputStreamReader;
  14. import java.io.BufferedReader;
  15. import java.util.Properties;
  16. import java.util.Enumeration;
  17. import java.lang.reflect.Method;
  18. import java.lang.reflect.InvocationTargetException;
  19. /**
  20. * A TransformerFactory instance can be used to create
  21. * {@link javax.xml.transform.Transformer} and
  22. * {@link javax.xml.transform.Templates} objects.
  23. *
  24. * <p>The system property that determines which Factory implementation to
  25. * create is named <code>"javax.xml.transform.TransformerFactory"</code>.
  26. * This property names a concrete subclass of the
  27. * <code>TransformerFactory</code> abstract class. If the property is not
  28. * defined, a platform default is be used.</p>
  29. *
  30. * An implementation of the <code>TransformerFactory</code> class is
  31. * <em>NOT</em> guaranteed to be thread safe. It is up to the user application
  32. * to make sure about the use of the <code>TransformerFactory</code> from
  33. * more than one thread. Alternatively the application can have one instance
  34. * of the <code>TransformerFactory</code> per thread.
  35. * An application can use the same instance of the factory to obtain one or
  36. * more instances of a <code>Transformer</code> or <code>Templates</code>
  37. * provided the instance of the factory isn't being used in more than one
  38. * thread at a time.
  39. */
  40. public abstract class TransformerFactory {
  41. /**
  42. * Default constructor is protected on purpose.
  43. */
  44. protected TransformerFactory() {}
  45. /**
  46. * Obtain a new instance of a <code>TransformerFactory</code>.
  47. * This static method creates a new factory instance
  48. * This method uses the following ordered lookup procedure to determine
  49. * the <code>TransformerFactory</code> implementation class to
  50. * load:
  51. * <ul>
  52. * <li>
  53. * Use the <code>javax.xml.transform.TransformerFactory</code> system
  54. * property.
  55. * </li>
  56. * <li>
  57. * Use the properties file "lib/jaxp.properties" in the JRE directory.
  58. * This configuration file is in standard <code>java.util.Properties
  59. * </code> format and contains the fully qualified name of the
  60. * implementation class with the key being the system property defined
  61. * above.
  62. * </li>
  63. * <li>
  64. * Use the Services API (as detailed in the JAR specification), if
  65. * available, to determine the classname. The Services API will look
  66. * for a classname in the file
  67. * <code>META-INF/services/javax.xml.transform.TransformerFactory</code>
  68. * in jars available to the runtime.
  69. * </li>
  70. * <li>
  71. * Platform default <code>TransformerFactory</code> instance.
  72. * </li>
  73. * </ul>
  74. *
  75. * Once an application has obtained a reference to a <code>
  76. * TransformerFactory</code> it can use the factory to configure
  77. * and obtain parser instances.
  78. *
  79. * @return new TransformerFactory instance, never null.
  80. *
  81. * @throws TransformerFactoryConfigurationError
  82. * if the implmentation is not available or cannot be instantiated.
  83. */
  84. public static TransformerFactory newInstance()
  85. throws TransformerFactoryConfigurationError
  86. {
  87. try {
  88. return (TransformerFactory) FactoryFinder.find(
  89. /* The default property name according to the JAXP spec */
  90. "javax.xml.transform.TransformerFactory",
  91. /* The fallback implementation class name */
  92. "org.apache.xalan.processor.TransformerFactoryImpl");
  93. } catch (FactoryFinder.ConfigurationError e) {
  94. throw new TransformerFactoryConfigurationError(e.getException(),
  95. e.getMessage());
  96. }
  97. }
  98. /**
  99. * Process the Source into a Transformer object. Care must
  100. * be given not to use this object in multiple threads running concurrently.
  101. * Different TransformerFactories can be used concurrently by different
  102. * threads.
  103. *
  104. * @param source An object that holds a URI, input stream, etc.
  105. *
  106. * @return A Transformer object that may be used to perform a transformation
  107. * in a single thread, never null.
  108. *
  109. * @exception TransformerConfigurationException May throw this during the parse
  110. * when it is constructing the Templates object and fails.
  111. */
  112. public abstract Transformer newTransformer(Source source)
  113. throws TransformerConfigurationException;
  114. /**
  115. * Create a new Transformer object that performs a copy
  116. * of the source to the result.
  117. *
  118. * @param source An object that holds a URI, input stream, etc.
  119. *
  120. * @return A Transformer object that may be used to perform a transformation
  121. * in a single thread, never null.
  122. *
  123. * @exception TransformerConfigurationException May throw this during
  124. * the parse when it is constructing the
  125. * Templates object and fails.
  126. */
  127. public abstract Transformer newTransformer()
  128. throws TransformerConfigurationException;
  129. /**
  130. * Process the Source into a Templates object, which is a
  131. * a compiled representation of the source. This Templates object
  132. * may then be used concurrently across multiple threads. Creating
  133. * a Templates object allows the TransformerFactory to do detailed
  134. * performance optimization of transformation instructions, without
  135. * penalizing runtime transformation.
  136. *
  137. * @param source An object that holds a URL, input stream, etc.
  138. *
  139. * @return A Templates object capable of being used for transformation purposes,
  140. * never null.
  141. *
  142. * @exception TransformerConfigurationException May throw this during the parse when it
  143. * is constructing the Templates object and fails.
  144. */
  145. public abstract Templates newTemplates(Source source)
  146. throws TransformerConfigurationException;
  147. /**
  148. * Get the stylesheet specification(s) associated
  149. * via the xml-stylesheet processing instruction (see
  150. * http://www.w3.org/TR/xml-stylesheet/) with the document
  151. * document specified in the source parameter, and that match
  152. * the given criteria. Note that it is possible to return several
  153. * stylesheets, in which case they are applied as if they were
  154. * a list of imports or cascades in a single stylesheet.
  155. *
  156. * @param source The XML source document.
  157. * @param media The media attribute to be matched. May be null, in which
  158. * case the prefered templates will be used (i.e. alternate = no).
  159. * @param title The value of the title attribute to match. May be null.
  160. * @param charset The value of the charset attribute to match. May be null.
  161. *
  162. * @return A Source object suitable for passing to the TransformerFactory.
  163. *
  164. * @throws TransformerConfigurationException.
  165. */
  166. public abstract Source getAssociatedStylesheet(
  167. Source source, String media, String title, String charset)
  168. throws TransformerConfigurationException;
  169. /**
  170. * Set an object that is used by default during the transformation
  171. * to resolve URIs used in xsl:import, or xsl:include.
  172. *
  173. * @param resolver An object that implements the URIResolver interface,
  174. * or null.
  175. */
  176. public abstract void setURIResolver(URIResolver resolver);
  177. /**
  178. * Get the object that is used by default during the transformation
  179. * to resolve URIs used in document(), xsl:import, or xsl:include.
  180. *
  181. * @return The URIResolver that was set with setURIResolver.
  182. */
  183. public abstract URIResolver getURIResolver();
  184. //======= CONFIGURATION METHODS =======
  185. /**
  186. * Look up the value of a feature.
  187. *
  188. * <p>The feature name is any absolute URI.</p>
  189. * @param name The feature name, which is an absolute URI.
  190. * @return The current state of the feature (true or false).
  191. */
  192. public abstract boolean getFeature(String name);
  193. /**
  194. * Allows the user to set specific attributes on the underlying
  195. * implementation. An attribute in this context is defined to
  196. * be an option that the implementation provides.
  197. *
  198. * @param name The name of the attribute.
  199. * @param value The value of the attribute.
  200. * @throws IllegalArgumentException thrown if the underlying
  201. * implementation doesn't recognize the attribute.
  202. */
  203. public abstract void setAttribute(String name, Object value)
  204. throws IllegalArgumentException;
  205. /**
  206. * Allows the user to retrieve specific attributes on the underlying
  207. * implementation.
  208. * @param name The name of the attribute.
  209. * @return value The value of the attribute.
  210. * @throws IllegalArgumentException thrown if the underlying
  211. * implementation doesn't recognize the attribute.
  212. */
  213. public abstract Object getAttribute(String name)
  214. throws IllegalArgumentException;
  215. /**
  216. * Set the error event listener for the TransformerFactory, which
  217. * is used for the processing of transformation instructions,
  218. * and not for the transformation itself.
  219. *
  220. * @param listener The new error listener.
  221. * @throws IllegalArgumentException if listener is null.
  222. */
  223. public abstract void setErrorListener(ErrorListener listener)
  224. throws IllegalArgumentException;
  225. /**
  226. * Get the error event handler for the TransformerFactory.
  227. *
  228. * @return The current error handler, which should never be null.
  229. */
  230. public abstract ErrorListener getErrorListener();
  231. }