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