TransformerFactory.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* TransformerFactory.java --
  2. Copyright (C) 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.xml.transform;
  32. import java.io.BufferedReader;
  33. import java.io.File;
  34. import java.io.FileInputStream;
  35. import java.io.InputStream;
  36. import java.io.InputStreamReader;
  37. import java.io.IOException;
  38. import java.util.Properties;
  39. /**
  40. * Factory for obtaining transformation contexts.
  41. *
  42. * @author (a href='mailto:dog@gnu.org'>Chris Burdess</a)
  43. */
  44. public abstract class TransformerFactory
  45. {
  46. protected TransformerFactory()
  47. {
  48. }
  49. /**
  50. * Creates a new factory instance.
  51. * The implementation class to load is the first found in the following
  52. * locations:
  53. * <ol>
  54. * <li>the <code>javax.xml.transform.TransformerFactory</code> system
  55. * property</li>
  56. * <li>the above named property value in the
  57. * <code><i>$JAVA_HOME</i>/lib/jaxp.properties</code> file</li>
  58. * <li>the class name specified in the
  59. * <code>META-INF/services/javax.xml.parsers.DocumentBuilderFactory</code>
  60. * system resource</li>
  61. * <li>the default factory class</li>
  62. * </ol>
  63. */
  64. public static TransformerFactory newInstance()
  65. throws TransformerFactoryConfigurationError
  66. {
  67. ClassLoader loader = Thread.currentThread().getContextClassLoader();
  68. if (loader == null)
  69. {
  70. loader = TransformerFactory.class.getClassLoader();
  71. }
  72. String className = null;
  73. int count = 0;
  74. do
  75. {
  76. className = getFactoryClassName(loader, count++);
  77. if (className != null)
  78. {
  79. try
  80. {
  81. Class<?> t = (loader != null) ? loader.loadClass(className) :
  82. Class.forName(className);
  83. return (TransformerFactory) t.newInstance();
  84. }
  85. catch (ClassNotFoundException e)
  86. {
  87. className = null;
  88. }
  89. catch (Exception e)
  90. {
  91. throw new TransformerFactoryConfigurationError(e,
  92. "error instantiating class " + className);
  93. }
  94. }
  95. }
  96. while (className == null && count < 3);
  97. try
  98. {
  99. Class<?> t =
  100. Class.forName("gnu.xml.transform.TransformerFactoryImpl");
  101. return (TransformerFactory) t.newInstance();
  102. }
  103. catch (Exception e)
  104. {
  105. throw new TransformerFactoryConfigurationError(e);
  106. }
  107. }
  108. private static String getFactoryClassName(ClassLoader loader, int attempt)
  109. {
  110. final String propertyName = "javax.xml.transform.TransformerFactory";
  111. switch (attempt)
  112. {
  113. case 0:
  114. return System.getProperty(propertyName);
  115. case 1:
  116. try
  117. {
  118. File file = new File(System.getProperty("java.home"));
  119. file = new File(file, "lib");
  120. file = new File(file, "jaxp.properties");
  121. InputStream in = new FileInputStream(file);
  122. Properties props = new Properties();
  123. props.load(in);
  124. in.close();
  125. return props.getProperty(propertyName);
  126. }
  127. catch (IOException e)
  128. {
  129. return null;
  130. }
  131. case 2:
  132. try
  133. {
  134. String serviceKey = "/META-INF/services/" + propertyName;
  135. InputStream in = (loader != null) ?
  136. loader.getResourceAsStream(serviceKey) :
  137. TransformerFactory.class.getResourceAsStream(serviceKey);
  138. if (in != null)
  139. {
  140. BufferedReader r =
  141. new BufferedReader(new InputStreamReader(in));
  142. String ret = r.readLine();
  143. r.close();
  144. return ret;
  145. }
  146. }
  147. catch (IOException e)
  148. {
  149. }
  150. return null;
  151. default:
  152. return null;
  153. }
  154. }
  155. /**
  156. * Creates a new transformer using the specified stylesheet.
  157. * @param source the source of an <a href='http://www.w3.org/TR/xslt'>XSLT
  158. * stylesheet</a> specifying the transformation to apply
  159. */
  160. public abstract Transformer newTransformer(Source source)
  161. throws TransformerConfigurationException;
  162. /**
  163. * Creates a new transformer that applies the identity transform.
  164. */
  165. public abstract Transformer newTransformer()
  166. throws TransformerConfigurationException;
  167. /**
  168. * Creates a new compiled transformation using the specified stylesheet.
  169. * @param source the source of an <a href='http://www.w3.org/TR/xslt'>XSLT
  170. * stylesheet</a> specifying the transformation to apply
  171. */
  172. public abstract Templates newTemplates(Source source)
  173. throws TransformerConfigurationException;
  174. /**
  175. * Returns a source object representing the XML resource specified by the
  176. * <a href='http://www.w3.org/TR/xml-stylesheet/'>xml-stylesheet</a>
  177. * processing instruction and matching the given criteria.
  178. * Note that if multiple stylesheets are selected, the source represents a
  179. * stylesheet composed of a list of imports.
  180. * @param source the source XML document
  181. * @param media the media attribute to match, or <code>null</code> to match
  182. * the preferred templates
  183. * @param title the title attribute to match, or <code>null</code> to match
  184. * any
  185. * @param charset the charset attribute to match, or <code>null</code> to
  186. * match any
  187. */
  188. public abstract Source getAssociatedStylesheet(Source source,
  189. String media,
  190. String title,
  191. String charset)
  192. throws TransformerConfigurationException;
  193. /**
  194. * Set the resolver callback to be used by transformers obtained from
  195. * this factory.
  196. */
  197. public abstract void setURIResolver(URIResolver resolver);
  198. /**
  199. * Returns the resolver callback to be used by transformers obtained from
  200. * this factory.
  201. */
  202. public abstract URIResolver getURIResolver();
  203. /**
  204. * Sets a feature of transformers and templates obtained from this
  205. * factory.
  206. * Feature names are fully qualified URIs, and may depend on the factory
  207. * implementation.
  208. * @param name the name of the feature
  209. * @param value the feature state
  210. * @exception TransformerConfigurationException if the feature is
  211. * unsupported
  212. */
  213. public abstract void setFeature(String name, boolean value)
  214. throws TransformerConfigurationException;
  215. /**
  216. * Returns the state of a feature in the factory implementation.
  217. * Feature names are fully qualified URIs, and may depend on the factory
  218. * implementation. JAXP also predefines several features, including the
  219. * constants in {@link javax.xml.XMLConstants} and
  220. * <ul>
  221. * <li>{@link javax.xml.transform.dom.DOMSource#FEATURE}</li>
  222. * <li>{@link javax.xml.transform.dom.DOMResult#FEATURE}</li>
  223. * <li>{@link javax.xml.transform.sax.SAXSource#FEATURE}</li>
  224. * <li>{@link javax.xml.transform.sax.SAXResult#FEATURE}</li>
  225. * <li>{@link javax.xml.transform.sax.SAXTransformerFactory#FEATURE}</li>
  226. * <li>{@link javax.xml.transform.sax.SAXTransformerFactory#FEATURE_XMLFILTER}</li>
  227. * <li>{@link javax.xml.transform.stream.StreamSource#FEATURE}</li>
  228. * <li>{@link javax.xml.transform.stream.StreamResult#FEATURE}</li>
  229. * </ul>
  230. * The latter expose various capabilities of the factory implementation.
  231. */
  232. public abstract boolean getFeature(String name);
  233. /**
  234. * Set a named attribute on the underlying implementation.
  235. * @param name the attribute name
  236. * @param value the value to assign
  237. * @exception IllegalArgumentException if the attribute is not supported
  238. */
  239. public abstract void setAttribute(String name, Object value)
  240. throws IllegalArgumentException;
  241. /**
  242. * Retrieve the specified named attribute value.
  243. * @param name the attribute name
  244. * @exception IllegalArgumentException if the attribute is not supported
  245. */
  246. public abstract Object getAttribute(String name)
  247. throws IllegalArgumentException;
  248. /**
  249. * Sets the callback to be used by transformers obtained from this factory
  250. * to report transformation errors.
  251. */
  252. public abstract void setErrorListener(ErrorListener listener)
  253. throws IllegalArgumentException;
  254. /**
  255. * Returns the callback to be used by transformers obtained from this
  256. * factory to report transformation errors.
  257. */
  258. public abstract ErrorListener getErrorListener();
  259. }